List of usage examples for org.objectweb.asm ClassVisitor visitField
public FieldVisitor visitField(final int access, final String name, final String descriptor, final String signature, final Object value)
From source file:Java6to2.java
License:Open Source License
private static byte[] transform(InputStream classfile) throws IOException { ClassReader cr = new ClassReader(classfile); ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS); ClassVisitor cv = new ClassVisitor(ASM7, cw) { String internalName;//from ww w .j av a 2 s . c om boolean classLookupMethodGenerated; Set fieldsGenerated = new HashSet(); public void visit(int version, int access, String name, String signature, String superName, String[] interfaces) { /* Change class file version to 1.2 */ cv.visit(V1_2, access, name, signature, superName, interfaces); this.internalName = name; } /** * Generates the synthetic "class$" method, used to lookup classes via Class.forName(). This uses the exact same code as does JDK8 javac for target 1.2. */ void generateSyntheticClassLookupMethod() { MethodVisitor mv = cv.visitMethod(ACC_PRIVATE | ACC_STATIC | ACC_SYNTHETIC, "class$", "(Ljava/lang/String;)Ljava/lang/Class;", null, null); { Label start = new Label(); Label end = new Label(); Label handler = new Label(); mv.visitTryCatchBlock(start, end, handler, "java/lang/ClassNotFoundException"); mv.visitLabel(start); mv.visitVarInsn(ALOAD, 0); mv.visitMethodInsn(INVOKESTATIC, "java/lang/Class", "forName", "(Ljava/lang/String;)Ljava/lang/Class;", false); mv.visitLabel(end); mv.visitInsn(ARETURN); mv.visitLabel(handler); mv.visitVarInsn(ASTORE, 1); mv.visitTypeInsn(NEW, "java/lang/NoClassDefFoundError"); mv.visitInsn(DUP); mv.visitMethodInsn(INVOKESPECIAL, "java/lang/NoClassDefFoundError", "<init>", "()V", false); mv.visitVarInsn(ALOAD, 1); mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/NoClassDefFoundError", "initCause", "(Ljava/lang/Throwable;)Ljava/lang/Throwable;", false); mv.visitInsn(ATHROW); } mv.visitMaxs(2, 2); mv.visitEnd(); } public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions) { MethodVisitor mv = super.visitMethod(access, name, desc, signature, exceptions); final ClassVisitor cv = this.cv; return new MethodVisitor(ASM7, mv) { /** * Intercepts class instantiations to see whether they instantiate a StringBuilder. Those instructions were generated by javac for string concatenations. But * StringBuilder is not available on JRE1.2, so we just replace it with StringBuffer. */ public void visitTypeInsn(int opcode, String type) { if (opcode == NEW && "java/lang/StringBuilder".equals(type)) { mv.visitTypeInsn(opcode, "java/lang/StringBuffer"); } else { mv.visitTypeInsn(opcode, type); } } /** * Intercepts method invocations to see whether they do something with StringBuilder. Those instructions were generated by javac for string concatenations. But * StringBuilder is not available on JRE1.2, so we just replace it with StringBuffer. */ public void visitMethodInsn(int opcode, String owner, String name, String desc, boolean itf) { if ("java/lang/StringBuilder".equals(owner)) { mv.visitMethodInsn(opcode, "java/lang/StringBuffer", name, desc.replace("java/lang/StringBuilder", "java/lang/StringBuffer"), itf); } else { mv.visitMethodInsn(opcode, owner, name, desc, itf); } } /** * Intercepts LDC instructions and check whether they are used to load a class. This is not supported on Java 1.2, so we convert it to the same code used by the * JDK8 javac: * <ul> * <li>create synthetic fields holding the resolved class objects * <li>create a synthetic method called "class$" which does Class.forName * </ul> */ public void visitLdcInsn(Object cst) { if (cst instanceof Type) { Type t = (Type) cst; String syntheticField = "class$" + t.getInternalName().replace('/', '$').replace("[", ""); if (!classLookupMethodGenerated) { /* Emit the synthetic "class$" method, used to lookup classes via Class.forName() */ generateSyntheticClassLookupMethod(); classLookupMethodGenerated = true; } if (!fieldsGenerated.contains(syntheticField)) { /* Generate a synthetic field holding the resolved Class object */ cv.visitField(ACC_PRIVATE | ACC_STATIC | ACC_SYNTHETIC, syntheticField, "Ljava/lang/Class;", null, null); fieldsGenerated.add(syntheticField); } mv.visitFieldInsn(GETSTATIC, internalName, syntheticField, "Ljava/lang/Class;"); Label nonNull = new Label(); mv.visitJumpInsn(IFNONNULL, nonNull); mv.visitLdcInsn(t.getInternalName().replace('/', '.')); mv.visitMethodInsn(INVOKESTATIC, internalName, "class$", "(Ljava/lang/String;)Ljava/lang/Class;", false); mv.visitInsn(DUP); mv.visitFieldInsn(PUTSTATIC, internalName, syntheticField, "Ljava/lang/Class;"); Label cnt = new Label(); mv.visitJumpInsn(GOTO, cnt); mv.visitLabel(nonNull); mv.visitFieldInsn(GETSTATIC, internalName, syntheticField, "Ljava/lang/Class;"); mv.visitLabel(cnt); } else { mv.visitLdcInsn(cst); } } }; } }; cr.accept(cv, ClassReader.SKIP_FRAMES); // <- Frames are not used in Java 1.2, so skip them return cw.toByteArray(); }
From source file:com.asakusafw.dag.compiler.codegen.AsmUtil.java
License:Apache License
/** * Defines a new field./*from w ww . java 2 s . c om*/ * @param writer the current class visitor * @param target the declaring type * @param name the field name * @param type the field type * @return the defined field ref */ public static FieldRef defineField(ClassVisitor writer, ClassDescription target, String name, Type type) { writer.visitField(Opcodes.ACC_PRIVATE | Opcodes.ACC_FINAL, name, type.getDescriptor(), null, null); return new FieldRef(target, name, type); }
From source file:com.axway.jmb.builders.Fields.java
License:Open Source License
public static void addFieldToClass(int access, ClassField field, AdviceAdapter constructor, ClassVisitor clazz, String classFullyQualifiedName) { if (field.isArray()) { clazz.visitField(access, field.getName(), field.getType().getArrayJvmType(field.getArrayDimension()).getDescriptor(), null, null); constructor.push(field.getArrayDimension()); constructor.newArray(field.getType().getArrayJvmType(field.getArrayDimension())); constructor.putField(Type.getType(classFullyQualifiedName), field.getName(), field.getType().getArrayJvmType(field.getArrayDimension())); } else {// ww w . j av a 2 s . co m if (field.getType() == JMBVariableType.RECORD) { clazz.visitField(access, field.getName(), field.getRecordType().getDescriptor(), null, null); constructor.newInstance(field.getRecordType()); constructor.dup(); constructor.invokeConstructor(field.getRecordType(), Method.getMethod("void <init>()")); constructor.putField(Type.getType(classFullyQualifiedName), field.getName(), field.getRecordType()); } else { clazz.visitField(access, field.getName(), field.getType().getJvmType().getDescriptor(), null, null); } if (field.isInitializationAvailable()) { constructor.putField(Type.getType(classFullyQualifiedName), field.getName(), field.getType().getJvmType()); } } }
From source file:com.cinnober.msgcodec.blink.BaseByteCodeGenerator.java
License:Open Source License
protected void generateConstructorAndFields(Schema schema, ClassVisitor cv, String genClassInternalName, boolean javaClassCodec) { MethodVisitor ctormv;/*from w w w.ja v a 2 s . co m*/ ctormv = cv.visitMethod(ACC_PUBLIC, "<init>", "(L" + blinkCodecIName + ";Lcom/cinnober/msgcodec/Schema;)V", null, null); int nextCtorVar = 3; ctormv.visitCode(); ctormv.visitVarInsn(ALOAD, 0); ctormv.visitVarInsn(ALOAD, 1); ctormv.visitMethodInsn(INVOKESPECIAL, baseclassIName, "<init>", "(L" + blinkCodecIName + ";)V", false); // schema field FieldVisitor fv = cv.visitField(ACC_PRIVATE + ACC_FINAL, "schema", "Lcom/cinnober/msgcodec/Schema;", null, null); fv.visitEnd(); ctormv.visitVarInsn(ALOAD, 0); // this ctormv.visitVarInsn(ALOAD, 2); // schema ctormv.visitFieldInsn(PUTFIELD, genClassInternalName, "schema", "Lcom/cinnober/msgcodec/Schema;"); if (!javaClassCodec) { // store the group type accessor // field fv = cv.visitField(ACC_PRIVATE + ACC_FINAL, "groupTypeAccessor", "Lcom/cinnober/msgcodec/GroupTypeAccessor;", null, null); fv.visitEnd(); // ctor, init field ctormv.visitVarInsn(ALOAD, 0); // this ctormv.visitVarInsn(ALOAD, 2); // schema ctormv.visitMethodInsn(INVOKEVIRTUAL, "com/cinnober/msgcodec/Schema", "getBinding", "()Lcom/cinnober/msgcodec/SchemaBinding;", false); ctormv.visitMethodInsn(INVOKEVIRTUAL, "com/cinnober/msgcodec/SchemaBinding", "getGroupTypeAccessor", "()Lcom/cinnober/msgcodec/GroupTypeAccessor;", false); ctormv.visitFieldInsn(PUTFIELD, genClassInternalName, "groupTypeAccessor", "Lcom/cinnober/msgcodec/GroupTypeAccessor;"); } for (GroupDef group : schema.getGroups()) { if (!javaClassCodec) { // store the group type // field fv = cv.visitField(ACC_PRIVATE + ACC_FINAL, "groupType_" + group.getName(), "Ljava/lang/Object;", null, null); fv.visitEnd(); // ctor, init field ctormv.visitVarInsn(ALOAD, 0); // this ctormv.visitVarInsn(ALOAD, 2); // schema ctormv.visitLdcInsn(group.getName()); ctormv.visitMethodInsn(INVOKEVIRTUAL, "com/cinnober/msgcodec/Schema", "getGroup", "(Ljava/lang/String;)Lcom/cinnober/msgcodec/GroupDef;", false); ctormv.visitMethodInsn(INVOKEVIRTUAL, "com/cinnober/msgcodec/GroupDef", "getGroupType", "()Ljava/lang/Object;", false); ctormv.visitFieldInsn(PUTFIELD, genClassInternalName, "groupType_" + group.getName(), "Ljava/lang/Object;"); } Factory<?> factory = group.getFactory(); if (isPublicConstructorFactory(factory)) { // no factory is needed } else { // field fv = cv.visitField(ACC_PRIVATE + ACC_FINAL, "factory_" + group.getName(), "Lcom/cinnober/msgcodec/Factory;", null, null); fv.visitEnd(); // ctor, init field ctormv.visitVarInsn(ALOAD, 0); // this ctormv.visitVarInsn(ALOAD, 2); // schema ctormv.visitLdcInsn(group.getName()); ctormv.visitMethodInsn(INVOKEVIRTUAL, "com/cinnober/msgcodec/Schema", "getGroup", "(Ljava/lang/String;)Lcom/cinnober/msgcodec/GroupDef;", false); ctormv.visitMethodInsn(INVOKEVIRTUAL, "com/cinnober/msgcodec/GroupDef", "getFactory", "()Lcom/cinnober/msgcodec/Factory;", false); ctormv.visitFieldInsn(PUTFIELD, genClassInternalName, "factory_" + group.getName(), "Lcom/cinnober/msgcodec/Factory;"); } for (FieldDef field : group.getFields()) { Accessor<?, ?> accessor = field.getAccessor(); SymbolMapping<?> symbolMapping = field.getBinding().getSymbolMapping(); TypeDef type = schema.resolveToType(field.getType(), true); TypeDef componentType = null; if (type.getType() == TypeDef.Type.SEQUENCE) { componentType = schema.resolveToType(((TypeDef.Sequence) type).getComponentType(), false); } if (isPublicFieldAccessor(accessor)) { // no accessor needed } else { // field fv = cv.visitField(ACC_PRIVATE + ACC_FINAL, "accessor_" + group.getName() + "_" + field.getName(), "Lcom/cinnober/msgcodec/Accessor;", null, null); fv.visitEnd(); // ctor, init field ctormv.visitVarInsn(ALOAD, 0); // this ctormv.visitVarInsn(ALOAD, 2); // schema ctormv.visitLdcInsn(group.getName()); ctormv.visitMethodInsn(INVOKEVIRTUAL, "com/cinnober/msgcodec/Schema", "getGroup", "(Ljava/lang/String;)Lcom/cinnober/msgcodec/GroupDef;", false); ctormv.visitLdcInsn(field.getName()); ctormv.visitMethodInsn(INVOKEVIRTUAL, "com/cinnober/msgcodec/GroupDef", "getField", "(Ljava/lang/String;)Lcom/cinnober/msgcodec/FieldDef;", false); ctormv.visitMethodInsn(INVOKEVIRTUAL, "com/cinnober/msgcodec/FieldDef", "getAccessor", "()Lcom/cinnober/msgcodec/Accessor;", false); ctormv.visitFieldInsn(PUTFIELD, genClassInternalName, "accessor_" + group.getName() + "_" + field.getName(), "Lcom/cinnober/msgcodec/Accessor;"); } if (accessor.getClass() == CreateAccessor.class) { // No symbol map needed } else if ((type != null && type.getType() == TypeDef.Type.ENUM) || (componentType != null && componentType.getType() == TypeDef.Type.ENUM)) { // If there is an enum and we need the symbol map Objects.requireNonNull(symbolMapping); // Create field String symbolMappingFieldName = "symbolMapping_" + group.getName() + "_" + field.getName(); fv = cv.visitField(ACC_PRIVATE + ACC_FINAL, symbolMappingFieldName, "Lcom/cinnober/msgcodec/SymbolMapping;", null, null); fv.visitEnd(); // Init field in the constructor ctormv.visitVarInsn(ALOAD, 0); // this ctormv.visitVarInsn(ALOAD, 2); // schema ctormv.visitLdcInsn(group.getName()); ctormv.visitMethodInsn(INVOKEVIRTUAL, "com/cinnober/msgcodec/Schema", "getGroup", "(Ljava/lang/String;)Lcom/cinnober/msgcodec/GroupDef;", false); ctormv.visitLdcInsn(field.getName()); ctormv.visitMethodInsn(INVOKEVIRTUAL, "com/cinnober/msgcodec/GroupDef", "getField", "(Ljava/lang/String;)Lcom/cinnober/msgcodec/FieldDef;", false); ctormv.visitMethodInsn(INVOKEVIRTUAL, "com/cinnober/msgcodec/FieldDef", "getBinding", "()Lcom/cinnober/msgcodec/FieldBinding;", false); ctormv.visitMethodInsn(INVOKEVIRTUAL, "com/cinnober/msgcodec/FieldBinding", "getSymbolMapping", "()Lcom/cinnober/msgcodec/SymbolMapping;", false); ctormv.visitFieldInsn(PUTFIELD, genClassInternalName, symbolMappingFieldName, "Lcom/cinnober/msgcodec/SymbolMapping;"); } } } ctormv.visitInsn(RETURN); ctormv.visitMaxs(3, nextCtorVar); ctormv.visitEnd(); }
From source file:com.facebook.presto.byteCode.FieldDefinition.java
License:Apache License
public void visit(ClassVisitor visitor) { FieldVisitor fieldVisitor = visitor.visitField(toAccessModifier(access), name, type.getType(), type.getGenericSignature(), null); if (fieldVisitor == null) { return;/* www. j a v a 2 s .com*/ } for (AnnotationDefinition annotation : annotations) { annotation.visitFieldAnnotation(fieldVisitor); } fieldVisitor.visitEnd(); }
From source file:com.gargoylesoftware.js.nashorn.internal.tools.nasgen.ClassGenerator.java
License:Open Source License
static void addMapField(final ClassVisitor cv) { // add a PropertyMap static field final FieldVisitor fv = cv.visitField(ACC_PRIVATE | ACC_STATIC | ACC_FINAL, PROPERTYMAP_FIELD_NAME, PROPERTYMAP_DESC, null, null); if (fv != null) { fv.visitEnd();/*from w w w. j a v a2 s. c o m*/ } }
From source file:com.gargoylesoftware.js.nashorn.internal.tools.nasgen.ClassGenerator.java
License:Open Source License
static void addField(final ClassVisitor cv, final String name, final String desc) { final FieldVisitor fv = cv.visitField(ACC_PRIVATE, name, desc, null, null); if (fv != null) { fv.visitEnd();/*from ww w . j ava2s . c om*/ } }
From source file:com.gargoylesoftware.js.nashorn.internal.tools.nasgen.ClassJavaGenerator.java
License:Open Source License
static void addField(final ClassVisitor cv, final String name, final String desc) { final FieldVisitor fv = cv.visitField(ACC_PRIVATE, name, desc, null, null); String descJava = descToJava(desc); builder.append(" private " + descJava.substring(0, descJava.length() - ".class".length()) + " " + name + ";" + System.lineSeparator()); if (fv != null) { fv.visitEnd();//from ww w .j a v a 2s.c o m } }
From source file:com.github.antag99.retinazer.weaver.MapperGenerator.java
License:Open Source License
@Override public void accept(ClassVisitor visitor) { visitor.visit(V1_7, ACC_PUBLIC + ACC_FINAL + ACC_SYNTHETIC, metadata.getMapperName(), "Lcom/github/antag99/retinazer/PackedMapper<L" + metadata.getMapperName() + ";>;", "com/github/antag99/retinazer/PackedMapper", null); for (ComponentProperty property : metadata.properties) { FieldVisitor propertyField = visitor.visitField(ACC_PRIVATE + ACC_FINAL, property.getMetadataName(), property.getMetadataDesc(), null, null); if (propertyField != null) propertyField.visitEnd();//from ww w .j av a2 s . c om } FieldVisitor flyweightField = visitor.visitField(ACC_PRIVATE + ACC_FINAL, "flyweight", "L" + metadata.internalName + ";", null, null); if (flyweightField != null) flyweightField.visitEnd(); // @off: Formatter mangles the elegant method syntax new MethodVisitor(ASM5, visitor.visitMethod(ACC_PUBLIC, "<init>", "(Lcom/github/antag99/retinazer/Engine;I)V", null, null)) { { visitCode(); visitVarInsn(ALOAD, 0); visitVarInsn(ALOAD, 1); visitLdcInsn(Type.getObjectType(metadata.internalName)); visitVarInsn(ILOAD, 2); visitMethodInsn(Opcodes.INVOKESPECIAL, "com/github/antag99/retinazer/PackedMapper", "<init>", "(Lcom/github/antag99/retinazer/Engine;Ljava/lang/Class;I)V", false); for (ComponentProperty property : metadata.properties) { visitVarInsn(ALOAD, 0); visitTypeInsn(NEW, WeaverConstants.getMetadataName(property.type)); visitInsn(Opcodes.DUP); visitLdcInsn(property.name); if (property.type.getSort() == Type.OBJECT) { visitLdcInsn(property.type); visitMethodInsn(INVOKESPECIAL, WeaverConstants.getMetadataName(property.type), "<init>", "(Ljava/lang/String;Ljava/lang/Class;)V", false); } else { visitMethodInsn(INVOKESPECIAL, WeaverConstants.getMetadataName(property.type), "<init>", "(Ljava/lang/String;)V", false); } visitFieldInsn(PUTFIELD, metadata.getMapperName(), property.getMetadataName(), property.getMetadataDesc()); } visitVarInsn(ALOAD, 0); visitInsn(DUP); visitMethodInsn(INVOKEVIRTUAL, metadata.getMapperName(), "createFlyweight", "()L" + metadata.internalName + ";", false); visitFieldInsn(PUTFIELD, metadata.getMapperName(), "flyweight", "L" + metadata.internalName + ";"); visitInsn(RETURN); visitMaxs(0, 0); visitEnd(); } }; new MethodVisitor(ASM5, visitor.visitMethod(ACC_PUBLIC, "createFlyweight", "()L" + metadata.internalName + ";", null, null)) { { visitCode(); visitTypeInsn(NEW, metadata.internalName); visitInsn(DUP); visitMethodInsn(INVOKESPECIAL, metadata.internalName, "<init>", "()V", false); for (ComponentProperty property : metadata.properties) { visitInsn(DUP); visitVarInsn(ALOAD, 0); visitFieldInsn(GETFIELD, metadata.getMapperName(), property.getMetadataName(), property.getMetadataDesc()); visitMethodInsn(INVOKEVIRTUAL, WeaverConstants.getMetadataName(property.type), "getBag", "()" + property.getBagDesc(), false); visitFieldInsn(PUTFIELD, metadata.internalName, property.getBagName(), property.getBagDesc()); } visitInsn(ARETURN); visitMaxs(0, 0); visitEnd(); } }; new MethodVisitor(ASM5, visitor.visitMethod(ACC_PUBLIC, "create", "(I)L" + metadata.internalName + ";", null, null)) { { visitCode(); visitVarInsn(ALOAD, 0); visitVarInsn(ILOAD, 1); visitMethodInsn(INVOKEVIRTUAL, "com/github/antag99/retinazer/Mapper", "checkCreate", "(I)V", false); visitVarInsn(ALOAD, 0); visitVarInsn(ILOAD, 1); visitMethodInsn(INVOKEVIRTUAL, metadata.getMapperName(), "get", "(I)L" + metadata.internalName + ";", false); visitInsn(DUP); visitMethodInsn(INVOKEVIRTUAL, metadata.internalName, WeaverConstants.RESET_METHOD_NAME, WeaverConstants.RESET_METHOD_DESC, false); visitInsn(ARETURN); visitMaxs(0, 0); visitEnd(); } }; new MethodVisitor(ASM5, visitor.visitMethod(ACC_PUBLIC + ACC_SYNTHETIC + ACC_BRIDGE, "create", "(I)Lcom/github/antag99/retinazer/Component;", null, null)) { { visitCode(); visitVarInsn(ALOAD, 0); visitVarInsn(ILOAD, 1); visitMethodInsn(INVOKEVIRTUAL, metadata.getMapperName(), "create", "(I)L" + metadata.internalName + ";", false); visitInsn(ARETURN); visitMaxs(0, 0); visitEnd(); } }; new MethodVisitor(ASM5, visitor.visitMethod(ACC_PUBLIC, "get", "(I)L" + metadata.internalName + ";", null, null)) { { visitCode(); visitVarInsn(ALOAD, 0); visitFieldInsn(GETFIELD, metadata.getMapperName(), "flyweight", "L" + metadata.internalName + ";"); visitInsn(DUP); visitVarInsn(ILOAD, 1); visitFieldInsn(PUTFIELD, metadata.internalName, WeaverConstants.INDEX_FIELD_NAME, WeaverConstants.INDEX_FIELD_DESC); visitInsn(ARETURN); visitMaxs(0, 0); visitEnd(); } }; new MethodVisitor(ASM5, visitor.visitMethod(ACC_PUBLIC + ACC_BRIDGE + ACC_SYNTHETIC, "get", "(I)Lcom/github/antag99/retinazer/Component;", null, null)) { { visitCode(); visitVarInsn(ALOAD, 0); visitVarInsn(ILOAD, 1); visitMethodInsn(INVOKEVIRTUAL, metadata.getMapperName(), "get", "(I)L" + metadata.internalName + ";", false); visitInsn(ARETURN); visitMaxs(0, 0); visitEnd(); } }; new MethodVisitor(ASM5, visitor.visitMethod(ACC_PUBLIC, "getProperties", "()[Lcom/github/antag99/retinazer/util/Property;", null, null)) { { visitCode(); visitLdcInsn(metadata.properties.size()); visitTypeInsn(ANEWARRAY, "com/github/antag99/retinazer/util/Property"); List<ComponentProperty> properties = metadata.properties; for (int i = 0, n = properties.size(); i < n; i++) { ComponentProperty property = properties.get(i); visitInsn(DUP); visitLdcInsn(i); visitVarInsn(ALOAD, 0); visitFieldInsn(GETFIELD, metadata.getMapperName(), property.getMetadataName(), property.getMetadataDesc()); visitInsn(AASTORE); } visitInsn(ARETURN); visitMaxs(0, 0); visitEnd(); } }; new MethodVisitor(ASM5, visitor.visitMethod(ACC_PUBLIC, "applyComponentChanges", "()V", null, null)) { { visitCode(); visitVarInsn(ALOAD, 0); visitFieldInsn(GETFIELD, "com/github/antag99/retinazer/Mapper", "componentsMask", "Lcom/github/antag99/retinazer/util/Mask;"); visitVarInsn(ASTORE, 1); visitVarInsn(ALOAD, 0); visitFieldInsn(GETFIELD, "com/github/antag99/retinazer/Mapper", "removeMask", "Lcom/github/antag99/retinazer/util/Mask;"); visitVarInsn(ASTORE, 2); for (ComponentProperty property : metadata.properties) { visitVarInsn(ALOAD, 0); visitFieldInsn(GETFIELD, metadata.getMapperName(), property.getMetadataName(), property.getMetadataDesc()); visitMethodInsn(INVOKEVIRTUAL, WeaverConstants.getMetadataName(property.type), "getBag", "()" + property.getBagDesc(), false); visitVarInsn(ALOAD, 2); visitMethodInsn(INVOKEVIRTUAL, WeaverConstants.getBagName(property.type), "clear", "(Lcom/github/antag99/retinazer/util/Mask;)V", false); } visitVarInsn(ALOAD, 1); visitVarInsn(ALOAD, 2); visitMethodInsn(INVOKEVIRTUAL, "com/github/antag99/retinazer/util/Mask", "andNot", "(Lcom/github/antag99/retinazer/util/Mask;)V", false); visitInsn(RETURN); visitMaxs(0, 0); visitEnd(); } }; visitor.visitEnd(); }
From source file:com.gmail.socraticphoenix.nebula.event.wrappers.BytecodeEventListenerGeneration.java
License:Open Source License
public static String listener(EventListener eventListener, Class eventClass, Object listener, Method method, ClassVisitor visitor) { Type listenerType = Type.getType(listener.getClass()); Type eventType = Type.getType(eventClass); String internalImplName = listenerType.getInternalName() + "$listener$" + method.getName() + "$" + BytecodeEventListenerGeneration.index++; visitor.visit(V1_8, ACC_PUBLIC | ACC_SYNTHETIC | ACC_FINAL, internalImplName, null, BytecodeEventListenerGeneration.objectType.getInternalName(), new String[] { BytecodeEventListenerGeneration.eventWrapperType.getInternalName() }); visitor.visitField(ACC_PRIVATE | ACC_FINAL, "listener", listenerType.getDescriptor(), null, null) .visitEnd();/*from www . j a v a 2 s .co m*/ MethodVisitor constructor = visitor.visitMethod(ACC_PUBLIC, "<init>", Type.getMethodDescriptor(Type.VOID_TYPE, listenerType), null, null); constructor.visitParameter("listener", ACC_FINAL); constructor.visitCode(); constructor.visitVarInsn(ALOAD, 0); constructor.visitMethodInsn(INVOKESPECIAL, BytecodeEventListenerGeneration.objectType.getInternalName(), "<init>", Type.getMethodDescriptor(Type.VOID_TYPE), false); constructor.visitVarInsn(ALOAD, 0); constructor.visitVarInsn(ALOAD, 1); constructor.visitFieldInsn(PUTFIELD, internalImplName, "listener", listenerType.getDescriptor()); constructor.visitInsn(RETURN); constructor.visitMaxs(0, 0); constructor.visitEnd(); MethodVisitor priority = visitor.visitMethod(ACC_PUBLIC | ACC_FINAL, "priority", BytecodeEventListenerGeneration.priorityMethod.getDescriptor(), null, null); priority.visitCode(); priority.visitLdcInsn(eventListener.value()); priority.visitInsn(IRETURN); priority.visitMaxs(0, 0); priority.visitEnd(); MethodVisitor getListener = visitor.visitMethod(ACC_PUBLIC | ACC_FINAL, "listener", BytecodeEventListenerGeneration.listenerMethod.getDescriptor(), null, null); getListener.visitVarInsn(ALOAD, 0); getListener.visitFieldInsn(GETFIELD, internalImplName, "listener", listenerType.getDescriptor()); getListener.visitInsn(ARETURN); getListener.visitMaxs(0, 0); getListener.visitEnd(); MethodVisitor event = visitor.visitMethod(ACC_PUBLIC | ACC_FINAL, "mainEvent", BytecodeEventListenerGeneration.mainEventMethod.getDescriptor(), null, null); event.visitCode(); event.visitLdcInsn(eventType); event.visitInsn(ARETURN); event.visitMaxs(0, 0); event.visitEnd(); return internalImplName; }