List of usage examples for org.objectweb.asm.commons GeneratorAdapter loadLocal
public void loadLocal(final int local)
From source file:co.cask.cdap.internal.io.DatumWriterGenerator.java
License:Apache License
/** * Generates method body for encoding Collection value. The logic is like this: * * <pre>//from w w w .jav a 2s .c om * {@code * * encoder.writeInt(collection.size()); * for (T element : collection) { * encodeElement(element, encoder, elementSchema, seenRefs); * } * if (collection.size() > 0) { * encoder.writeInt(0); * } * } * </pre> * @param mg * @param componentType * @param componentSchema * @param value * @param encoder */ private void encodeCollection(GeneratorAdapter mg, TypeToken<?> componentType, Schema componentSchema, int value, int encoder, int schemaLocal, int seenRefs) { // Encode and store the collection length locally mg.loadArg(value); mg.invokeInterface(Type.getType(Collection.class), getMethod(int.class, "size")); int length = mg.newLocal(Type.INT_TYPE); mg.storeLocal(length); mg.loadArg(encoder); mg.loadLocal(length); mg.invokeInterface(Type.getType(Encoder.class), getMethod(Encoder.class, "writeInt", int.class)); mg.pop(); // Store the component schema mg.loadArg(schemaLocal); mg.invokeVirtual(Type.getType(Schema.class), getMethod(Schema.class, "getComponentSchema")); int componentSchemaLocal = mg.newLocal(Type.getType(Schema.class)); mg.storeLocal(componentSchemaLocal); // Store the iterator int iterator = mg.newLocal(Type.getType(Iterator.class)); mg.loadArg(value); mg.invokeInterface(Type.getType(Collection.class), getMethod(Iterator.class, "iterator")); mg.storeLocal(iterator); // For loop with iterator. Encode each component Label beginFor = mg.mark(); Label endFor = mg.newLabel(); mg.loadLocal(iterator); mg.invokeInterface(Type.getType(Iterator.class), getMethod(boolean.class, "hasNext")); mg.ifZCmp(GeneratorAdapter.EQ, endFor); // Call the encode method for encoding the element. mg.loadThis(); mg.loadLocal(iterator); mg.invokeInterface(Type.getType(Iterator.class), getMethod(Object.class, "next")); doCast(mg, componentType, componentSchema); mg.loadArg(encoder); mg.loadLocal(componentSchemaLocal); mg.loadArg(seenRefs); mg.invokeVirtual(classType, getEncodeMethod(componentType, componentSchema)); mg.goTo(beginFor); mg.mark(endFor); // if length > 0, write out 0 at the end of array. Label zeroLength = mg.newLabel(); mg.loadLocal(length); mg.ifZCmp(GeneratorAdapter.LE, zeroLength); encodeInt(mg, 0, encoder); mg.mark(zeroLength); }
From source file:co.cask.cdap.internal.io.DatumWriterGenerator.java
License:Apache License
/** * Generates method body for encoding array value. The logic is similar to the one in * {@link #encodeCollection}, with collection size replaced with array length. * * @param mg/*from ww w .ja v a2 s . com*/ * @param componentType * @param componentSchema * @param value * @param encoder * @param schemaLocal * @param seenRefs */ private void encodeArray(GeneratorAdapter mg, TypeToken<?> componentType, Schema componentSchema, int value, int encoder, int schemaLocal, int seenRefs) { // Encode and store the array length locally mg.loadArg(value); mg.arrayLength(); int length = mg.newLocal(Type.INT_TYPE); mg.storeLocal(length); mg.loadArg(encoder); mg.loadLocal(length); mg.invokeInterface(Type.getType(Encoder.class), getMethod(Encoder.class, "writeInt", int.class)); mg.pop(); // Store the component schema mg.loadArg(schemaLocal); mg.invokeVirtual(Type.getType(Schema.class), getMethod(Schema.class, "getComponentSchema")); int componentSchemaLocal = mg.newLocal(Type.getType(Schema.class)); mg.storeLocal(componentSchemaLocal); // for (int idx = 0; idx < array.length; idx++) mg.push(0); int idx = mg.newLocal(Type.INT_TYPE); mg.storeLocal(idx); Label beginFor = mg.mark(); Label endFor = mg.newLabel(); mg.loadLocal(idx); mg.loadLocal(length); mg.ifICmp(GeneratorAdapter.GE, endFor); // Call encode method to encode array[idx] mg.loadThis(); mg.loadArg(value); mg.loadLocal(idx); TypeToken<?> callTypeToken = getCallTypeToken(componentType, componentSchema); mg.arrayLoad(Type.getType(callTypeToken.getRawType())); mg.loadArg(encoder); mg.loadLocal(componentSchemaLocal); mg.loadArg(seenRefs); mg.invokeVirtual(classType, getEncodeMethod(componentType, componentSchema)); mg.iinc(idx, 1); mg.goTo(beginFor); mg.mark(endFor); // if length > 0, write out 0 at the end of array. Label zeroLength = mg.newLabel(); mg.loadLocal(length); mg.ifZCmp(GeneratorAdapter.LE, zeroLength); encodeInt(mg, 0, encoder); mg.mark(zeroLength); }
From source file:co.cask.cdap.internal.io.DatumWriterGenerator.java
License:Apache License
/** * Generates method body for encoding map value. The logic is like this: * * <pre>/* w w w . j a v a 2 s . com*/ * {@code * * encoder.writeInt(map.size(); * * for (Map.Entry<Key, Value> entry : map.entrySet()) { * encodeKey(entry.getKey(), encoder, keySchema, seenRefs); * encodeValue(entry.getValue(), encoder, valueSchema, seenRefs); * } * * if (map.size() > 0) { * encoder.writeInt(0); * } * } * </pre> * * @param mg * @param keyType * @param valueType * @param keySchema * @param valueSchema * @param value * @param encoder * @param schemaLocal * @param seenRefs */ private void encodeMap(GeneratorAdapter mg, TypeToken<?> keyType, TypeToken<?> valueType, Schema keySchema, Schema valueSchema, int value, int encoder, int schemaLocal, int seenRefs) { // Encode and store the map length locally mg.loadArg(value); mg.invokeInterface(Type.getType(Map.class), getMethod(int.class, "size")); int length = mg.newLocal(Type.INT_TYPE); mg.storeLocal(length); mg.loadArg(encoder); mg.loadLocal(length); mg.invokeInterface(Type.getType(Encoder.class), getMethod(Encoder.class, "writeInt", int.class)); mg.pop(); // Stores the key and value schema mg.loadArg(schemaLocal); mg.invokeVirtual(Type.getType(Schema.class), getMethod(Map.Entry.class, "getMapSchema")); mg.dup(); int keySchemaLocal = mg.newLocal(Type.getType(Schema.class)); mg.invokeInterface(Type.getType(Map.Entry.class), getMethod(Object.class, "getKey")); mg.checkCast(Type.getType(Schema.class)); mg.storeLocal(keySchemaLocal); int valueSchemaLocal = mg.newLocal(Type.getType(Schema.class)); mg.invokeInterface(Type.getType(Map.Entry.class), getMethod(Object.class, "getValue")); mg.checkCast(Type.getType(Schema.class)); mg.storeLocal(valueSchemaLocal); // Store the entry set iterator int iterator = mg.newLocal(Type.getType(Iterator.class)); mg.loadArg(value); mg.invokeInterface(Type.getType(Map.class), getMethod(Set.class, "entrySet")); mg.invokeInterface(Type.getType(Set.class), getMethod(Iterator.class, "iterator")); mg.storeLocal(iterator); // For loop the entry set iterator, encode each key-value pairs Label beginFor = mg.mark(); Label endFor = mg.newLabel(); mg.loadLocal(iterator); mg.invokeInterface(Type.getType(Iterator.class), getMethod(boolean.class, "hasNext")); mg.ifZCmp(GeneratorAdapter.EQ, endFor); int entry = mg.newLocal(Type.getType(Map.Entry.class)); mg.loadLocal(iterator); mg.invokeInterface(Type.getType(Iterator.class), getMethod(Object.class, "next")); mg.checkCast(Type.getType(Map.Entry.class)); mg.storeLocal(entry); // encode key mg.loadThis(); mg.loadLocal(entry); mg.invokeInterface(Type.getType(Map.Entry.class), getMethod(Object.class, "getKey")); doCast(mg, keyType, keySchema); mg.loadArg(encoder); mg.loadLocal(keySchemaLocal); mg.loadArg(seenRefs); mg.invokeVirtual(classType, getEncodeMethod(keyType, keySchema)); // encode value mg.loadThis(); mg.loadLocal(entry); mg.invokeInterface(Type.getType(Map.Entry.class), getMethod(Object.class, "getValue")); doCast(mg, valueType, valueSchema); mg.loadArg(encoder); mg.loadLocal(valueSchemaLocal); mg.loadArg(seenRefs); mg.invokeVirtual(classType, getEncodeMethod(valueType, valueSchema)); mg.goTo(beginFor); mg.mark(endFor); // if length > 0, write out 0 at the end of map Label zeroLength = mg.newLabel(); mg.loadLocal(length); mg.ifZCmp(GeneratorAdapter.LE, zeroLength); encodeInt(mg, 0, encoder); mg.mark(zeroLength); }
From source file:co.cask.cdap.internal.io.DatumWriterGenerator.java
License:Apache License
/** * Generates method body for encoding java class. If the class given is an interface, * getter method will be used to access the field values, otherwise, it will assumes * fields are public.//from w ww . jav a2 s .c om * * @param mg * @param schema * @param outputType * @param value * @param encoder * @param schemaLocal * @param seenRefs */ private void encodeRecord(GeneratorAdapter mg, Schema schema, TypeToken<?> outputType, int value, int encoder, int schemaLocal, int seenRefs) { try { Class<?> rawType = outputType.getRawType(); // Record type might be defined by the user, hence need to preserve class loading of it preservedClasses.add(rawType); boolean isInterface = rawType.isInterface(); /* Check for circular reference if (value != null && !seenRefs.add(value)) { throw new IOException(...); } */ Label notSeen = mg.newLabel(); mg.loadArg(value); mg.ifNull(notSeen); mg.loadArg(seenRefs); mg.loadArg(value); mg.invokeInterface(Type.getType(Set.class), getMethod(boolean.class, "add", Object.class)); mg.ifZCmp(GeneratorAdapter.NE, notSeen); mg.throwException(Type.getType(IOException.class), "Circular reference not supported."); mg.mark(notSeen); // Store the list of schema fields. mg.loadArg(schemaLocal); mg.invokeVirtual(Type.getType(Schema.class), getMethod(List.class, "getFields")); int fieldSchemas = mg.newLocal(Type.getType(List.class)); mg.storeLocal(fieldSchemas); // For each field, call the encode method for the field List<Schema.Field> fields = schema.getFields(); for (int i = 0; i < fields.size(); i++) { Schema.Field field = fields.get(i); TypeToken<?> fieldType; // this.encodeFieldMethod(value.fieldName, encoder, fieldSchemas.get(i).getSchema()); if (isInterface) { mg.loadThis(); mg.loadArg(value); Method getter = getGetter(outputType, field.getName()); fieldType = outputType.resolveType(rawType.getMethod(getter.getName()).getGenericReturnType()); mg.invokeInterface(Type.getType(rawType), getter); } else { fieldType = outputType .resolveType(Fields.findField(outputType.getType(), field.getName()).getGenericType()); fieldAccessorRequests.put(outputType, field.getName()); mg.loadThis(); mg.dup(); mg.getField(classType, getFieldAccessorName(outputType, field.getName()), Type.getType(FieldAccessor.class)); mg.loadArg(value); mg.invokeInterface(Type.getType(FieldAccessor.class), getAccessorMethod(fieldType)); if (!fieldType.getRawType().isPrimitive()) { doCast(mg, fieldType, field.getSchema()); } } mg.loadArg(encoder); mg.loadLocal(fieldSchemas); mg.push(i); mg.invokeInterface(Type.getType(List.class), getMethod(Object.class, "get", int.class)); mg.checkCast(Type.getType(Schema.Field.class)); mg.invokeVirtual(Type.getType(Schema.Field.class), getMethod(Schema.class, "getSchema")); mg.loadArg(seenRefs); mg.invokeVirtual(classType, getEncodeMethod(fieldType, field.getSchema())); } } catch (Exception e) { throw Throwables.propagate(e); } }
From source file:co.cask.cdap.internal.io.FieldAccessorGenerator.java
License:Apache License
private void initializeReflectionField(GeneratorAdapter mg, Field field) { /*//from ww w. j a v a 2s . com Save the reflected Field object for accessing private field. try { Field field = Fields.findField(classType, "fieldName"); field.setAccessible(true); this.field = field; } catch (Exception e) { throw Throwables.propagate(e); } */ Label beginTry = mg.newLabel(); Label endTry = mg.newLabel(); Label catchHandle = mg.newLabel(); mg.visitTryCatchBlock(beginTry, endTry, catchHandle, Type.getInternalName(Exception.class)); mg.mark(beginTry); // Field field = Fields.findField(classType, "fieldName") mg.loadArg(0); mg.push(field.getName()); mg.invokeStatic(Type.getType(Fields.class), getMethod(Field.class, "findField", java.lang.reflect.Type.class, String.class)); mg.dup(); // field.setAccessible(true); mg.push(true); mg.invokeVirtual(Type.getType(Field.class), getMethod(void.class, "setAccessible", boolean.class)); // this.field = field; // need to swap the this reference and the one in top stack (from dup() ). mg.loadThis(); mg.swap(); mg.putField(Type.getObjectType(className), "field", Type.getType(Field.class)); mg.mark(endTry); Label endCatch = mg.newLabel(); mg.goTo(endCatch); mg.mark(catchHandle); int exception = mg.newLocal(Type.getType(IllegalAccessException.class)); mg.storeLocal(exception); mg.loadLocal(exception); mg.invokeStatic(Type.getType(Throwables.class), getMethod(RuntimeException.class, "propagate", Throwable.class)); mg.throwException(); mg.mark(endCatch); }
From source file:co.cask.cdap.internal.io.FieldAccessorGenerator.java
License:Apache License
/** * Generates the try-catch block that wrap around the given reflection method call. * @param method The method to be called within the try-catch block. *//*from www.j a v a 2 s . c o m*/ private void invokeReflection(Method method, String signature) { GeneratorAdapter mg = new GeneratorAdapter(Opcodes.ACC_PUBLIC, method, signature, new Type[0], classWriter); /** * try { * // Call method * } catch (IllegalAccessException e) { * throw Throwables.propagate(e); * } */ Label beginTry = mg.newLabel(); Label endTry = mg.newLabel(); Label catchHandle = mg.newLabel(); mg.visitTryCatchBlock(beginTry, endTry, catchHandle, Type.getInternalName(IllegalAccessException.class)); mg.mark(beginTry); mg.loadThis(); mg.getField(Type.getObjectType(className), "field", Type.getType(Field.class)); mg.loadArgs(); mg.invokeVirtual(Type.getType(Field.class), method); mg.mark(endTry); mg.returnValue(); mg.mark(catchHandle); int exception = mg.newLocal(Type.getType(IllegalAccessException.class)); mg.storeLocal(exception); mg.loadLocal(exception); mg.invokeStatic(Type.getType(Throwables.class), getMethod(RuntimeException.class, "propagate", Throwable.class)); mg.throwException(); mg.endMethod(); }
From source file:co.cask.common.internal.io.DatumWriterGenerator.java
License:Apache License
/** * Generates method body for encoding java class. If the class given is an interface, * getter method will be used to access the field values, otherwise, it will assumes * fields are public.//from w ww . j av a 2 s .co m * * @param mg * @param schema * @param outputType * @param value * @param encoder * @param schemaLocal * @param seenRefs */ private void encodeRecord(GeneratorAdapter mg, Schema schema, TypeToken<?> outputType, int value, int encoder, int schemaLocal, int seenRefs) { try { Class<?> rawType = outputType.getRawType(); // Record type might be defined by the user, hence need to preserve class loading of it preservedClasses.add(rawType); boolean isInterface = rawType.isInterface(); /* Check for circular reference if (value != null && !seenRefs.add(value)) { throw new IOException(...); } */ Label notSeen = mg.newLabel(); mg.loadArg(value); mg.ifNull(notSeen); mg.loadArg(seenRefs); mg.loadArg(value); mg.invokeInterface(Type.getType(Set.class), getMethod(boolean.class, "add", Object.class)); mg.ifZCmp(GeneratorAdapter.NE, notSeen); mg.throwException(Type.getType(IOException.class), "Circular reference not supported."); mg.mark(notSeen); // Store the list of schema fields. mg.loadArg(schemaLocal); mg.invokeVirtual(Type.getType(Schema.class), getMethod(List.class, "getFields")); int fieldSchemas = mg.newLocal(Type.getType(List.class)); mg.storeLocal(fieldSchemas); // For each field, call the encode method for the field List<Schema.Field> fields = schema.getFields(); for (int i = 0; i < fields.size(); i++) { Schema.Field field = fields.get(i); TypeToken<?> fieldType; // this.encodeFieldMethod(value.fieldName, encoder, fieldSchemas.get(i).getSchema()); if (isInterface) { mg.loadThis(); mg.loadArg(value); Method getter = getGetter(outputType, field.getName()); fieldType = outputType.resolveType(rawType.getMethod(getter.getName()).getGenericReturnType()); mg.invokeInterface(Type.getType(rawType), getter); } else { fieldType = outputType .resolveType(Fields.findField(outputType, field.getName()).getGenericType()); fieldAccessorRequests.put(outputType, field.getName()); mg.loadThis(); mg.dup(); mg.getField(classType, getFieldAccessorName(outputType, field.getName()), Type.getType(FieldAccessor.class)); mg.loadArg(value); mg.invokeInterface(Type.getType(FieldAccessor.class), getAccessorMethod(fieldType)); if (!fieldType.getRawType().isPrimitive()) { doCast(mg, fieldType, field.getSchema()); } } mg.loadArg(encoder); mg.loadLocal(fieldSchemas); mg.push(i); mg.invokeInterface(Type.getType(List.class), getMethod(Object.class, "get", int.class)); mg.checkCast(Type.getType(Schema.Field.class)); mg.invokeVirtual(Type.getType(Schema.Field.class), getMethod(Schema.class, "getSchema")); mg.loadArg(seenRefs); mg.invokeVirtual(classType, getEncodeMethod(fieldType, field.getSchema())); } } catch (Exception e) { throw Throwables.propagate(e); } }
From source file:co.cask.common.internal.io.FieldAccessorGenerator.java
License:Apache License
private void initializeReflectionField(GeneratorAdapter mg, Field field) { /*//from w w w . ja v a2s .co m Save the reflected Field object for accessing private field. try { Field field = Fields.findField(classType, "fieldName"); field.setAccessible(true); this.field = field; } catch (Exception e) { throw Throwables.propagate(e); } */ Label beginTry = mg.newLabel(); Label endTry = mg.newLabel(); Label catchHandle = mg.newLabel(); mg.visitTryCatchBlock(beginTry, endTry, catchHandle, Type.getInternalName(Exception.class)); mg.mark(beginTry); // Field field = findField(classType, "fieldName") mg.loadArg(0); mg.push(field.getName()); mg.invokeStatic(Type.getType(Fields.class), getMethod(Field.class, "findField", TypeToken.class, String.class)); mg.dup(); // field.setAccessible(true); mg.push(true); mg.invokeVirtual(Type.getType(Field.class), getMethod(void.class, "setAccessible", boolean.class)); // this.field = field; // need to swap the this reference and the one in top stack (from dup() ). mg.loadThis(); mg.swap(); mg.putField(Type.getObjectType(className), "field", Type.getType(Field.class)); mg.mark(endTry); Label endCatch = mg.newLabel(); mg.goTo(endCatch); mg.mark(catchHandle); int exception = mg.newLocal(Type.getType(IllegalAccessException.class)); mg.storeLocal(exception); mg.loadLocal(exception); mg.invokeStatic(Type.getType(Throwables.class), getMethod(RuntimeException.class, "propagate", Throwable.class)); mg.throwException(); mg.mark(endCatch); }
From source file:co.cask.tigon.internal.io.DatumWriterGenerator.java
License:Apache License
/** * Generates method body for encoding java class. If the class given is an interface, * getter method will be used to access the field values, otherwise, it will assumes * fields are public.//from www. ja v a 2s. c o m * * @param mg * @param schema * @param outputType * @param value * @param encoder * @param schemaLocal * @param seenRefs */ private void encodeRecord(GeneratorAdapter mg, Schema schema, TypeToken<?> outputType, int value, int encoder, int schemaLocal, int seenRefs) { try { Class<?> rawType = outputType.getRawType(); boolean isInterface = rawType.isInterface(); /* Check for circular reference if (value != null && !seenRefs.add(value)) { throw new IOException(...); } */ Label notSeen = mg.newLabel(); mg.loadArg(value); mg.ifNull(notSeen); mg.loadArg(seenRefs); mg.loadArg(value); mg.invokeInterface(Type.getType(Set.class), getMethod(boolean.class, "add", Object.class)); mg.ifZCmp(GeneratorAdapter.NE, notSeen); mg.throwException(Type.getType(IOException.class), "Circular reference not supported."); mg.mark(notSeen); // Store the list of schema fields. mg.loadArg(schemaLocal); mg.invokeVirtual(Type.getType(Schema.class), getMethod(List.class, "getFields")); int fieldSchemas = mg.newLocal(Type.getType(List.class)); mg.storeLocal(fieldSchemas); // For each field, call the encode method for the field List<Schema.Field> fields = schema.getFields(); for (int i = 0; i < fields.size(); i++) { Schema.Field field = fields.get(i); TypeToken<?> fieldType; // this.encodeFieldMethod(value.fieldName, encoder, fieldSchemas.get(i).getSchema()); if (isInterface) { mg.loadThis(); mg.loadArg(value); Method getter = getGetter(outputType, field.getName()); fieldType = outputType.resolveType(rawType.getMethod(getter.getName()).getGenericReturnType()); mg.invokeInterface(Type.getType(rawType), getter); } else { fieldType = outputType .resolveType(Fields.findField(outputType, field.getName()).getGenericType()); fieldAccessorRequests.put(outputType, field.getName()); mg.loadThis(); mg.dup(); mg.getField(classType, getFieldAccessorName(outputType, field.getName()), Type.getType(FieldAccessor.class)); mg.loadArg(value); mg.invokeInterface(Type.getType(FieldAccessor.class), getAccessorMethod(fieldType)); if (!fieldType.getRawType().isPrimitive()) { doCast(mg, fieldType, field.getSchema()); } } mg.loadArg(encoder); mg.loadLocal(fieldSchemas); mg.push(i); mg.invokeInterface(Type.getType(List.class), getMethod(Object.class, "get", int.class)); mg.checkCast(Type.getType(Schema.Field.class)); mg.invokeVirtual(Type.getType(Schema.Field.class), getMethod(Schema.class, "getSchema")); mg.loadArg(seenRefs); mg.invokeVirtual(classType, getEncodeMethod(fieldType, field.getSchema())); } } catch (Exception e) { throw Throwables.propagate(e); } }
From source file:com.android.build.gradle.internal.incremental.ConstructorArgsRedirection.java
License:Apache License
@Override protected void redirectLocal(GeneratorAdapter mv, int stackIndex, Type arg) { // If the stack index is 0, we do not send the local variable 0 (this) as it // cannot escape the constructor. Instead, we use this argument position to send // a reference to the locals array where the redirected method will return their // values./*w w w . j a va2s .c o m*/ if (stackIndex == 0) { mv.loadLocal(locals); } else { super.redirectLocal(mv, stackIndex, arg); } }