/** Reallocate a buffer. */ public static <T> T[] reallocateBuffer(Class<T> klass, T[] oldBuffer, int oldCapacity, int newCapacity) { assert (newCapacity > oldCapacity); @SuppressWarnings("unchecked") T[] newBuffer = (T[]) ArrayReflection.newInstance(klass, newCapacity); if (oldBuffer != null) { System.arraycopy(oldBuffer, 0, newBuffer, 0, oldCapacity); } for (int i = oldCapacity; i < newCapacity; i++) { try { newBuffer[i] = ClassReflection.newInstance(klass); } catch (Exception e) { throw new RuntimeException(e); } } return newBuffer; }
@Override public <T> T[] getAll(Class<T> clazz) { Object[] got = gotAll.get(clazz); if (got != null) { return (T[]) got; } Array<ISystem> matching = cache.getNext(); matching.clear(); for (int i = 0; i < systems.size; i++) { ISystem system = systems.items[i]; if (clazz.isAssignableFrom(system.getClass())) { matching.add(system); } } T[] array = (T[]) ArrayReflection.newInstance(clazz, matching.size); System.arraycopy(matching.items, 0, array, 0, array.length); gotAll.put(clazz, array); return array; }
/** Creates a new backing array with the specified size containing the current items. */ protected T[] resize (int newSize) { T[] items = this.items; T[] newItems = (T[])ArrayReflection.newInstance(items.getClass().getComponentType(), newSize); System.arraycopy(items, 0, newItems, 0, Math.min(size, newItems.length)); this.items = newItems; return newItems; }
/** Creates a new backing array with the specified capacity containing the current items. * @param newCapacity the new capacity */ protected void resize (int newCapacity) { @SuppressWarnings("unchecked") T[] newItems = (T[])ArrayReflection.newInstance(items.getClass().getComponentType(), newCapacity); if (tail > head) { System.arraycopy(items, head, newItems, 0, size); } else if (size > 0) { // NOTE: when head == tail the buffer can be empty or full System.arraycopy(items, head, newItems, 0, items.length - head); System.arraycopy(items, 0, newItems, items.length - head, tail); } head = 0; tail = size; items = newItems; }
protected void resize (int newSize) { K[] newKeys = (K[])ArrayReflection.newInstance(keys.getClass().getComponentType(), newSize); System.arraycopy(keys, 0, newKeys, 0, Math.min(size, newKeys.length)); this.keys = newKeys; V[] newValues = (V[])ArrayReflection.newInstance(values.getClass().getComponentType(), newSize); System.arraycopy(values, 0, newValues, 0, Math.min(size, newValues.length)); this.values = newValues; }
@SuppressWarnings("unchecked") <T> T[] requestParticleBuffer(Class<T> klass, T[] buffer) { if (buffer == null) { buffer = (T[]) ArrayReflection.newInstance(klass, m_internalAllocatedCapacity); for (int i = 0; i < m_internalAllocatedCapacity; i++) { try { buffer[i] = ClassReflection.newInstance(klass); } catch (Exception e) { throw new RuntimeException(e); } } } return buffer; }
/** * Creates a new backing array with the specified size containing the current items. * * @param newSize * @return */ protected T[] resize(int newSize) { T[] items = this.items; T[] newItems = (T[]) ArrayReflection.newInstance(items.getClass().getComponentType(), newSize); System.arraycopy(items, 0, newItems, 0, Math.min(size, newItems.length)); this.items = newItems; return newItems; }
protected void resize(int newSize) { K[] newKeys = (K[]) ArrayReflection.newInstance(keys.getClass().getComponentType(), newSize); System.arraycopy(keys, 0, newKeys, 0, Math.min(size, newKeys.length)); this.keys = newKeys; V[] newValues = (V[]) ArrayReflection.newInstance(values.getClass().getComponentType(), newSize); System.arraycopy(values, 0, newValues, 0, Math.min(size, newValues.length)); this.values = newValues; }
/** * Creates a new backing array with the specified size containing the * current items. */ protected T[] resize(int newSize) { T[] items = this.items; T[] newItems = (T[]) ArrayReflection.newInstance(items.getClass().getComponentType(), newSize); System.arraycopy(items, 0, newItems, 0, Math.min(size, newItems.length)); this.items = newItems; return newItems; }
@Override public Object evaluate(VarsContext context) throws ExpressionEvaluationException { // First arg should be the container object // First argument should always be a collection or similar Object arg1 = first().evaluate(context); if (arg1.getClass().isArray()) { return ArrayReflection.getLength(arg1); } else if (arg1 instanceof Array) { Array array = (Array) arg1; return array.size; } else if (arg1 instanceof Map) { Map map = (Map) arg1; return map.size(); } else if (arg1 instanceof Collection) { Collection collection = (Collection) arg1; return collection.size(); } else if (arg1 instanceof Iterable) { Iterable iterable = (Iterable) arg1; int i = 0; for (Object object : iterable) { i++; } return i; } else { throw new ExpressionEvaluationException( "Could not evaluate " + getName() + ". Revise the first argument is a collection, array, iterable or map.", this); } }
/** Creates a new array with {@link #items} of the specified type. * @param ordered If false, methods that remove elements may change the order of other elements in the array, which avoids a * memory copy. * @param capacity Any elements added beyond this will cause the backing array to be grown. */ public Array (boolean ordered, int capacity, Class arrayType) { this.ordered = ordered; items = (T[])ArrayReflection.newInstance(arrayType, capacity); }
public <V> V[] toArray (Class type) { V[] result = (V[])ArrayReflection.newInstance(type, size); System.arraycopy(items, 0, result, 0, size); return result; }
@Override @SuppressWarnings("unchecked") public Type[] getArray(final int size) { return (Type[]) ArrayReflection.newInstance(arrayType, size); }
public ObjectChannel (int id, int strideSize, int size, Class<T> type) { super(id, ArrayReflection.newInstance(type, size*strideSize), strideSize); componentType = type; this.data = (T[]) super.data; }
@Override public void setCapacity (int requiredCapacity) { T[] newData = (T[]) ArrayReflection.newInstance(componentType, strideSize * requiredCapacity); System.arraycopy(data, 0, newData, 0, Math.min(data.length, newData.length)); super.data = data = newData; }
/** Creates a new map with {@link #keys} and {@link #values} of the specified type. * @param ordered If false, methods that remove elements may change the order of other elements in the arrays, which avoids a * memory copy. * @param capacity Any elements added beyond this will cause the backing arrays to be grown. */ public ArrayMap (boolean ordered, int capacity, Class keyArrayType, Class valueArrayType) { this.ordered = ordered; keys = (K[])ArrayReflection.newInstance(keyArrayType, capacity); values = (V[])ArrayReflection.newInstance(valueArrayType, capacity); }
@Override public void create () { font = new BitmapFont(); batch = new SpriteBatch(); try { Vector2 fromDefaultConstructor = ClassReflection.newInstance(Vector2.class); println("From default constructor: " + fromDefaultConstructor); Method mSet = ClassReflection.getMethod(Vector2.class, "set", float.class, float.class); mSet.invoke(fromDefaultConstructor, 10, 11); println("Set to 10/11: " + fromDefaultConstructor); Constructor copyConstroctor = ClassReflection.getConstructor(Vector2.class, Vector2.class); Vector2 fromCopyConstructor = (Vector2)copyConstroctor.newInstance(fromDefaultConstructor); println("From copy constructor: " + fromCopyConstructor); Method mMul = ClassReflection.getMethod(Vector2.class, "scl", float.class); println("Multiplied by 2; " + mMul.invoke(fromCopyConstructor, 2)); Method mNor = ClassReflection.getMethod(Vector2.class, "nor"); println("Normalized: " + mNor.invoke(fromCopyConstructor)); Vector2 fieldCopy = new Vector2(); Field fx = ClassReflection.getField(Vector2.class, "x"); Field fy = ClassReflection.getField(Vector2.class, "y"); fx.set(fieldCopy, fx.get(fromCopyConstructor)); fy.set(fieldCopy, fy.get(fromCopyConstructor)); println("Copied field by field: " + fieldCopy); Json json = new Json(); String jsonString = json.toJson(fromCopyConstructor); Vector2 fromJson = json.fromJson(Vector2.class, jsonString); println("JSON serialized: " + jsonString); println("JSON deserialized: " + fromJson); fromJson.x += 1; fromJson.y += 1; println("JSON deserialized + 1/1: " + fromJson); Object array = ArrayReflection.newInstance(int.class, 5); ArrayReflection.set(array, 0, 42); println("Array int: length=" + ArrayReflection.getLength(array) + ", access=" + ArrayReflection.get(array, 0)); array = ArrayReflection.newInstance(String.class, 5); ArrayReflection.set(array, 0, "test string"); println("Array String: length=" + ArrayReflection.getLength(array) + ", access=" + ArrayReflection.get(array, 0)); } catch (Exception e) { message = "FAILED: " + e.getMessage() + "\n"; message += e.getClass(); } }
public <V> V[] toArray(Class type) { V[] result = (V[]) ArrayReflection.newInstance(type, size); System.arraycopy(items, 0, result, 0, size); return result; }
public ArrayIterable(Object anArray) { this.arrayLength = ArrayReflection.getLength(anArray); this.innerArray = anArray; }
@Override public Expression next() { currentElement.value = ArrayReflection.get(innerArray, arrayPosition++); return currentElement; }
@Override public Object evaluate(VarsContext context) throws ExpressionEvaluationException { // Second argument, if present, should be the index of the element to // retrieve. // If not present, the first (0) element is returned int index = 0; Object indexObject = null; if (children.size() > 1) { Object arg2 = second().evaluate(context); if (arg2 instanceof Number) { index = ((Number) arg2).intValue(); } else { indexObject = arg2; } } // First argument should always be a collection or similar Object arg1 = first().evaluate(context); try { if (arg1.getClass().isArray()) { return ArrayReflection.get(arg1, index); } else if (arg1 instanceof Array) { Array array = (Array) arg1; return array.get(index); } else if (arg1 instanceof List) { List list = (List) arg1; return list.get(index); } else if (arg1 instanceof Map) { Map map = (Map) arg1; return map.get(indexObject); } else if (arg1 instanceof Collection) { Collection collection = (Collection) arg1; return collection.toArray()[index]; } else if (arg1 instanceof Iterable) { Iterable iterable = (Iterable) arg1; int i = 0; for (Object object : iterable) { if (index == i++) { return object; } } } else { throw new ExpressionEvaluationException( "Could not evaluate " + getName() + ". Revise the first argument is a collection, array, iterable or map, and the second argument is a valid element position (for collections, arrays, iterables) or key (for maps)", this); } } catch (Exception e) { throw new ExpressionEvaluationException("Problem accessing index " + index + " + in " + arg1, this, e); } return null; }
/** * Creates a new array with {@link #items} of the specified type. * * @param ordered If false, methods that remove elements may change the order of other elements in the array, which * avoids a memory copy. * @param capacity Any elements added beyond this will cause the backing array to be grown. * @param arrayType */ public Array(boolean ordered, int capacity, Class arrayType) { this.ordered = ordered; items = (T[]) ArrayReflection.newInstance(arrayType, capacity); }
/** * Creates a new map with {@link #keys} and {@link #values} of the specified * type. * * @param ordered * If false, methods that remove elements may change the order of * other elements in the arrays, which avoids a memory copy. * @param capacity * Any elements added beyond this will cause the backing arrays * to be grown. */ public ArrayMap(boolean ordered, int capacity, Class keyArrayType, Class valueArrayType) { this.ordered = ordered; keys = (K[]) ArrayReflection.newInstance(keyArrayType, capacity); values = (V[]) ArrayReflection.newInstance(valueArrayType, capacity); }
/** * Creates a new array with {@link #items} of the specified type. * * @param ordered * If false, methods that remove elements may change the order of * other elements in the array, which avoids a memory copy. * @param capacity * Any elements added beyond this will cause the backing array to * be grown. */ public Array(boolean ordered, int capacity, Class arrayType) { this.ordered = ordered; items = (T[]) ArrayReflection.newInstance(arrayType, capacity); }