diff -r f2352e0b713e -r eaf4e8387065 javap/src/main/java/org/apidesign/javap/TypeArray.java --- a/javap/src/main/java/org/apidesign/javap/TypeArray.java Fri Dec 07 15:02:35 2012 +0100 +++ b/javap/src/main/java/org/apidesign/javap/TypeArray.java Wed Dec 12 11:04:02 2012 +0100 @@ -25,38 +25,85 @@ package org.apidesign.javap; -public class TypeArray { +import static org.apidesign.javap.RuntimeConstants.ITEM_Bogus; +import static org.apidesign.javap.RuntimeConstants.ITEM_Integer; +import static org.apidesign.javap.RuntimeConstants.ITEM_Float; +import static org.apidesign.javap.RuntimeConstants.ITEM_Double; +import static org.apidesign.javap.RuntimeConstants.ITEM_Long; +import static org.apidesign.javap.RuntimeConstants.ITEM_Null; +import static org.apidesign.javap.RuntimeConstants.ITEM_InitObject; +import static org.apidesign.javap.RuntimeConstants.ITEM_Object; +import static org.apidesign.javap.RuntimeConstants.ITEM_NewObject; + +public final class TypeArray { private static final int CAPACITY_INCREMENT = 16; private int[] types; private int size; + public TypeArray() { + } + + public TypeArray(final TypeArray initialTypes) { + setAll(initialTypes); + } + public void add(final int newType) { ensureCapacity(size + 1); types[size++] = newType; } + public void addAll(final TypeArray newTypes) { + addAll(newTypes.types, 0, newTypes.size); + } + public void addAll(final int[] newTypes) { - if (newTypes.length > 0) { - ensureCapacity(size + newTypes.length); - arraycopy(newTypes, 0, types, size, newTypes.length); - size += newTypes.length; + addAll(newTypes, 0, newTypes.length); + } + + public void addAll(final int[] newTypes, + final int offset, + final int count) { + if (count > 0) { + ensureCapacity(size + count); + arraycopy(newTypes, offset, types, size, count); + size += count; } } + public void set(final int index, final int newType) { + types[index] = newType; + } + + public void setAll(final TypeArray newTypes) { + setAll(newTypes.types, 0, newTypes.size); + } + public void setAll(final int[] newTypes) { - if (newTypes.length > 0) { - ensureCapacity(newTypes.length); - arraycopy(newTypes, 0, types, 0, newTypes.length); - size = newTypes.length; + setAll(newTypes, 0, newTypes.length); + } + + public void setAll(final int[] newTypes, + final int offset, + final int count) { + if (count > 0) { + ensureCapacity(count); + arraycopy(newTypes, offset, types, 0, count); + size = count; } else { clear(); } } public void setSize(final int newSize) { - ensureCapacity(newSize); - size = newSize; + if (size != newSize) { + ensureCapacity(newSize); + + for (int i = size; i < newSize; ++i) { + types[i] = 0; + } + size = newSize; + } } public void clear() { @@ -71,6 +118,45 @@ return types[index]; } + public static String typeString(final int type) { + switch (type & 0xff) { + case ITEM_Bogus: + return "_top_"; + case ITEM_Integer: + return "_int_"; + case ITEM_Float: + return "_float_"; + case ITEM_Double: + return "_double_"; + case ITEM_Long: + return "_long_"; + case ITEM_Null: + return "_null_"; + case ITEM_InitObject: // UninitializedThis + return "_init_"; + case ITEM_Object: + return "_object_"; + case ITEM_NewObject: // Uninitialized + return "_new_"; + default: + throw new IllegalArgumentException("Unknown type"); + } + } + + @Override + public String toString() { + final StringBuilder sb = new StringBuilder("["); + if (size > 0) { + sb.append(typeString(types[0])); + for (int i = 1; i < size; ++i) { + sb.append(", "); + sb.append(typeString(types[i])); + } + } + + return sb.append(']').toString(); + } + private void ensureCapacity(final int minCapacity) { if ((minCapacity == 0) || (types != null) && (minCapacity <= types.length)) {