lubomir@319: /* lubomir@319: * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved. lubomir@319: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. lubomir@319: * lubomir@319: * This code is free software; you can redistribute it and/or modify it lubomir@319: * under the terms of the GNU General Public License version 2 only, as lubomir@319: * published by the Free Software Foundation. Oracle designates this lubomir@319: * particular file as subject to the "Classpath" exception as provided lubomir@319: * by Oracle in the LICENSE file that accompanied this code. lubomir@319: * lubomir@319: * This code is distributed in the hope that it will be useful, but WITHOUT lubomir@319: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or lubomir@319: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License lubomir@319: * version 2 for more details (a copy is included in the LICENSE file that lubomir@319: * accompanied this code). lubomir@319: * lubomir@319: * You should have received a copy of the GNU General Public License version lubomir@319: * 2 along with this work; if not, write to the Free Software Foundation, lubomir@319: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. lubomir@319: * lubomir@319: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA lubomir@319: * or visit www.oracle.com if you need additional information or have any lubomir@319: * questions. lubomir@319: */ lubomir@319: lubomir@319: package org.apidesign.javap; lubomir@319: lubomir@319: import static org.apidesign.javap.RuntimeConstants.ITEM_Bogus; lubomir@319: import static org.apidesign.javap.RuntimeConstants.ITEM_Integer; lubomir@319: import static org.apidesign.javap.RuntimeConstants.ITEM_Float; lubomir@319: import static org.apidesign.javap.RuntimeConstants.ITEM_Double; lubomir@319: import static org.apidesign.javap.RuntimeConstants.ITEM_Long; lubomir@319: import static org.apidesign.javap.RuntimeConstants.ITEM_Null; lubomir@319: import static org.apidesign.javap.RuntimeConstants.ITEM_InitObject; lubomir@319: import static org.apidesign.javap.RuntimeConstants.ITEM_Object; lubomir@319: import static org.apidesign.javap.RuntimeConstants.ITEM_NewObject; lubomir@319: lubomir@319: public final class TypeArray { lubomir@319: private static final int CAPACITY_INCREMENT = 16; lubomir@319: lubomir@319: private int[] types; lubomir@319: private int size; lubomir@319: lubomir@319: public TypeArray() { lubomir@319: } lubomir@319: lubomir@319: public TypeArray(final TypeArray initialTypes) { lubomir@319: setAll(initialTypes); lubomir@319: } lubomir@319: lubomir@319: public void add(final int newType) { lubomir@319: ensureCapacity(size + 1); lubomir@319: types[size++] = newType; lubomir@319: } lubomir@319: lubomir@319: public void addAll(final TypeArray newTypes) { lubomir@319: addAll(newTypes.types, 0, newTypes.size); lubomir@319: } lubomir@319: lubomir@319: public void addAll(final int[] newTypes) { lubomir@319: addAll(newTypes, 0, newTypes.length); lubomir@319: } lubomir@319: lubomir@319: public void addAll(final int[] newTypes, lubomir@319: final int offset, lubomir@319: final int count) { lubomir@319: if (count > 0) { lubomir@319: ensureCapacity(size + count); lubomir@319: arraycopy(newTypes, offset, types, size, count); lubomir@319: size += count; lubomir@319: } lubomir@319: } lubomir@319: lubomir@319: public void set(final int index, final int newType) { lubomir@319: types[index] = newType; lubomir@319: } lubomir@319: lubomir@319: public void setAll(final TypeArray newTypes) { lubomir@319: setAll(newTypes.types, 0, newTypes.size); lubomir@319: } lubomir@319: lubomir@319: public void setAll(final int[] newTypes) { lubomir@319: setAll(newTypes, 0, newTypes.length); lubomir@319: } lubomir@319: lubomir@319: public void setAll(final int[] newTypes, lubomir@319: final int offset, lubomir@319: final int count) { lubomir@319: if (count > 0) { lubomir@319: ensureCapacity(count); lubomir@319: arraycopy(newTypes, offset, types, 0, count); lubomir@319: size = count; lubomir@319: } else { lubomir@319: clear(); lubomir@319: } lubomir@319: } lubomir@319: lubomir@319: public void setSize(final int newSize) { lubomir@319: if (size != newSize) { lubomir@319: ensureCapacity(newSize); lubomir@319: lubomir@319: for (int i = size; i < newSize; ++i) { lubomir@319: types[i] = 0; lubomir@319: } lubomir@319: size = newSize; lubomir@319: } lubomir@319: } lubomir@319: lubomir@319: public void clear() { lubomir@319: size = 0; lubomir@319: } lubomir@319: lubomir@319: public int getSize() { lubomir@319: return size; lubomir@319: } lubomir@319: lubomir@319: public int get(final int index) { lubomir@319: return types[index]; lubomir@319: } lubomir@319: lubomir@319: public static String typeString(final int type) { lubomir@319: switch (type & 0xff) { lubomir@319: case ITEM_Bogus: lubomir@319: return "_top_"; lubomir@319: case ITEM_Integer: lubomir@319: return "_int_"; lubomir@319: case ITEM_Float: lubomir@319: return "_float_"; lubomir@319: case ITEM_Double: lubomir@319: return "_double_"; lubomir@319: case ITEM_Long: lubomir@319: return "_long_"; lubomir@319: case ITEM_Null: lubomir@319: return "_null_"; lubomir@319: case ITEM_InitObject: // UninitializedThis lubomir@319: return "_init_"; lubomir@319: case ITEM_Object: lubomir@319: return "_object_"; lubomir@319: case ITEM_NewObject: // Uninitialized lubomir@319: return "_new_"; lubomir@319: default: lubomir@319: throw new IllegalArgumentException("Unknown type"); lubomir@319: } lubomir@319: } lubomir@319: lubomir@319: @Override lubomir@319: public String toString() { lubomir@319: final StringBuilder sb = new StringBuilder("["); lubomir@319: if (size > 0) { lubomir@319: sb.append(typeString(types[0])); lubomir@319: for (int i = 1; i < size; ++i) { lubomir@319: sb.append(", "); lubomir@319: sb.append(typeString(types[i])); lubomir@319: } lubomir@319: } lubomir@319: lubomir@319: return sb.append(']').toString(); lubomir@319: } lubomir@319: lubomir@319: private void ensureCapacity(final int minCapacity) { lubomir@319: if ((minCapacity == 0) lubomir@319: || (types != null) && (minCapacity <= types.length)) { lubomir@319: return; lubomir@319: } lubomir@319: lubomir@319: final int newCapacity = lubomir@319: ((minCapacity + CAPACITY_INCREMENT - 1) / CAPACITY_INCREMENT) lubomir@319: * CAPACITY_INCREMENT; lubomir@319: final int[] newTypes = new int[newCapacity]; lubomir@319: lubomir@319: if (size > 0) { lubomir@319: arraycopy(types, 0, newTypes, 0, size); lubomir@319: } lubomir@319: lubomir@319: types = newTypes; lubomir@319: } lubomir@319: lubomir@319: // no System.arraycopy lubomir@319: private void arraycopy(final int[] src, final int srcPos, lubomir@319: final int[] dest, final int destPos, lubomir@319: final int length) { lubomir@319: for (int i = 0; i < length; ++i) { lubomir@319: dest[destPos + i] = src[srcPos + i]; lubomir@319: } lubomir@319: } lubomir@319: }