@Override public void generateMethods(Object classVisitor) { assert classVisitor instanceof ClassVisitor; ClassVisitor cv = (ClassVisitor) classVisitor; // generated by compiling the class: // class foo { public ClassLoader getFrameworkClassLoader() { return // getClass().getClassLoader(); } } // and then running ASMifier on it: // java -classpath asm-debug-all-5.0.2.jar:. org.objectweb.asm.util.ASMifier foo MethodVisitor mv = cv.visitMethod( ACC_PUBLIC, "getFrameworkClassLoader", "()Ljava/lang/ClassLoader;", null, null); mv.visitCode(); mv.visitVarInsn(ALOAD, 0); mv.visitMethodInsn( INVOKEVIRTUAL, "java/lang/Object", "getClass", "()Ljava/lang/Class;", false); mv.visitMethodInsn( INVOKEVIRTUAL, "java/lang/Class", "getClassLoader", "()Ljava/lang/ClassLoader;", false); mv.visitInsn(ARETURN); mv.visitMaxs(1, 1); mv.visitEnd(); // generated code ends. }
@Test public void testInstrumentationModernClassFile() throws Exception { ClassVisitor classVisitor = TypeConstantAdjustment.INSTANCE.wrap( mock(TypeDescription.class), this.classVisitor, mock(Implementation.Context.class), mock(TypePool.class), new FieldList.Empty<FieldDescription.InDefinedShape>(), new MethodList.Empty<MethodDescription>(), IGNORED, IGNORED); classVisitor.visit( ClassFileVersion.JAVA_V5.getMinorMajorVersion(), FOOBAR, FOO, BAR, QUX, new String[] {BAZ}); assertThat( classVisitor.visitMethod(FOOBAR, FOO, BAR, QUX, new String[] {BAZ}), is(methodVisitor)); verify(this.classVisitor) .visit( ClassFileVersion.JAVA_V5.getMinorMajorVersion(), FOOBAR, FOO, BAR, QUX, new String[] {BAZ}); verify(this.classVisitor).visitMethod(FOOBAR, FOO, BAR, QUX, new String[] {BAZ}); verifyNoMoreInteractions(this.classVisitor); verifyZeroInteractions(methodVisitor); }
private MethodVisitor declareMethod( ClassVisitor visitor, String methodName, String methodDescriptor, String methodSignature, int access) { MethodVisitor methodVisitor = visitor.visitMethod(access, methodName, methodDescriptor, methodSignature, NO_EXCEPTIONS); methodVisitor.visitCode(); return methodVisitor; }
public SkinnyMethodAdapter( ClassVisitor cv, int flags, String name, String signature, String something, String[] exceptions) { super(flags); setMethodVisitor(cv.visitMethod(flags, name, signature, something, exceptions)); this.cv = cv; this.name = name; }
public void visitConstructor(ConstructorNode node) { visitParameters(node, node.getParameters()); String methodType = BytecodeHelper.getMethodDescriptor(ClassHelper.VOID_TYPE, node.getParameters()); mv = cv.visitMethod(node.getModifiers(), "<init>", methodType, null, null); mv.visitTypeInsn(NEW, "java/lang/RuntimeException"); mv.visitInsn(DUP); mv.visitLdcInsn("not intended for execution"); mv.visitMethodInsn( INVOKESPECIAL, "java/lang/RuntimeException", "<init>", "(Ljava/lang/String;)V", false); mv.visitInsn(ATHROW); mv.visitMaxs(0, 0); }
public void test() throws Exception { ClassWriter cw = new ClassWriter(true); ClassVisitor cv = new StaticInitMerger("$clinit$", cw); cv.visit(V1_1, ACC_PUBLIC, "A", null, "java/lang/Object", null); cv.visitField(ACC_PUBLIC + ACC_STATIC, "counter", "I", null, null); for (int i = 0; i < 5; ++i) { MethodVisitor mv = cv.visitMethod(ACC_PUBLIC, "<clinit>", "()V", null, null); mv.visitFieldInsn(GETSTATIC, "A", "counter", "I"); mv.visitInsn(ICONST_1); mv.visitInsn(IADD); mv.visitFieldInsn(PUTSTATIC, "A", "counter", "I"); mv.visitInsn(RETURN); mv.visitMaxs(0, 0); } MethodVisitor mv = cv.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null); mv.visitVarInsn(ALOAD, 0); mv.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V"); mv.visitInsn(RETURN); mv.visitMaxs(0, 0); cv.visitEnd(); Class c = LOADER.defineClass("A", cw.toByteArray()); assertEquals(c.getField("counter").getInt(c.newInstance()), 5); }
@Test public void testInstrumentationLegacyClassFileArrayType() throws Exception { ClassVisitor classVisitor = TypeConstantAdjustment.INSTANCE.wrap( mock(TypeDescription.class), this.classVisitor, mock(Implementation.Context.class), mock(TypePool.class), new FieldList.Empty<FieldDescription.InDefinedShape>(), new MethodList.Empty<MethodDescription>(), IGNORED, IGNORED); classVisitor.visit( ClassFileVersion.JAVA_V4.getMinorMajorVersion(), FOOBAR, FOO, BAR, QUX, new String[] {BAZ}); MethodVisitor methodVisitor = classVisitor.visitMethod(FOOBAR, FOO, BAR, QUX, new String[] {BAZ}); assertThat(methodVisitor, not(this.methodVisitor)); methodVisitor.visitLdcInsn(Type.getType(Object[].class)); verify(this.classVisitor) .visit( ClassFileVersion.JAVA_V4.getMinorMajorVersion(), FOOBAR, FOO, BAR, QUX, new String[] {BAZ}); verify(this.classVisitor).visitMethod(FOOBAR, FOO, BAR, QUX, new String[] {BAZ}); verifyNoMoreInteractions(this.classVisitor); verify(this.methodVisitor) .visitLdcInsn(Type.getType(Object[].class).getInternalName().replace('/', '.')); verify(this.methodVisitor) .visitMethodInsn( Opcodes.INVOKESTATIC, Type.getType(Class.class).getInternalName(), "forName", Type.getType(Class.class.getDeclaredMethod("forName", String.class)).getDescriptor(), false); verifyNoMoreInteractions(this.methodVisitor); }
private void writeEqualsMethod(ClassVisitor cw, Type generatedType) { MethodVisitor methodVisitor = cw.visitMethod(Opcodes.ACC_PUBLIC, "equals", EQUALS_METHOD_DESCRIPTOR, null, null); methodVisitor.visitCode(); methodVisitor.visitVarInsn(ALOAD, 0); methodVisitor.visitVarInsn(ALOAD, 1); Label notSameLabel = new Label(); methodVisitor.visitJumpInsn(IF_ACMPNE, notSameLabel); methodVisitor.visitInsn(ICONST_1); methodVisitor.visitInsn(IRETURN); methodVisitor.visitLabel(notSameLabel); methodVisitor.visitVarInsn(ALOAD, 1); methodVisitor.visitTypeInsn(INSTANCEOF, MANAGED_INSTANCE_TYPE); Label notManagedInstanceLabel = new Label(); methodVisitor.visitJumpInsn(IFNE, notManagedInstanceLabel); methodVisitor.visitInsn(ICONST_0); methodVisitor.visitInsn(IRETURN); methodVisitor.visitLabel(notManagedInstanceLabel); methodVisitor.visitVarInsn(ALOAD, 0); methodVisitor.visitMethodInsn( INVOKEVIRTUAL, generatedType.getInternalName(), "getBackingNode", GET_BACKING_NODE_METHOD_DESCRIPTOR, false); methodVisitor.visitVarInsn(ALOAD, 1); methodVisitor.visitTypeInsn(CHECKCAST, MANAGED_INSTANCE_TYPE); methodVisitor.visitMethodInsn( INVOKEINTERFACE, MANAGED_INSTANCE_TYPE, "getBackingNode", GET_BACKING_NODE_METHOD_DESCRIPTOR, true); methodVisitor.visitMethodInsn( INVOKEINTERFACE, MUTABLE_MODEL_NODE_TYPE, "equals", EQUALS_METHOD_DESCRIPTOR, true); finishVisitingMethod(methodVisitor, Opcodes.IRETURN); }
public void accept(ClassVisitor classVisitor) { String factoryName = Util.classNameToInternalName(info.getFactoryDelegateInterfaceName()); // TODO: the reactor currently has an issue with interfaces classVisitor.visit( Opcodes.V1_5, Opcodes.ACC_PUBLIC | Opcodes.ACC_ABSTRACT | Opcodes.ACC_INTERFACE, factoryName, null, "java/lang/Object", new String[0]); for (ConstructorInfo constructor : info.getConstructors()) { MethodVisitor mv = classVisitor.visitMethod( Opcodes.ACC_PUBLIC | Opcodes.ACC_ABSTRACT, "create", constructor.getFactoryDelegateMethodDescriptor(), constructor.getSignature(), constructor.getExceptions()); if (mv != null) { mv.visitEnd(); } } classVisitor.visitEnd(); }
RootGenerator( AstNode node, CompilationUnit<?> owner, ClassVisitor type_builder, String class_name) throws NoSuchMethodException { super(node, owner, type_builder, class_name, class_name, new HashSet<String>()); MethodVisitor ctor_builder = type_builder.visitMethod( Opcodes.ACC_PUBLIC, "<init>", makeSignature(null, TaskMaster.class), null, null); ctor_builder.visitCode(); ctor_builder.visitVarInsn(Opcodes.ALOAD, 0); ctor_builder.visitMethodInsn( Opcodes.INVOKESPECIAL, getInternalName(Computation.class), "<init>", "()V"); ctor_builder.visitVarInsn(Opcodes.ALOAD, 0); ctor_builder.visitVarInsn(Opcodes.ALOAD, 1); task_master.store(ctor_builder); ctor_builder.visitVarInsn(Opcodes.ALOAD, 1); ctor_builder.visitVarInsn(Opcodes.ALOAD, 0); ctor_builder.visitInsn(Opcodes.ICONST_0); ctor_builder.visitFieldInsn(Opcodes.PUTFIELD, class_name, "state", getDescriptor(int.class)); createInterlock(ctor_builder); ctor_builder.visitInsn(Opcodes.RETURN); ctor_builder.visitMaxs(0, 0); ctor_builder.visitEnd(); }
public static byte[] generate(EventClassModel model) { ClassWriter cwriter = new ClassWriter(ClassWriter.COMPUTE_MAXS); ClassVisitor cw; if (TRACE) { cw = new TraceClassVisitor(cwriter, new ASMifier(), new PrintWriter(System.out)); cw = new CheckClassAdapter(cw); } else { cw = cwriter; } FieldVisitor fv; MethodVisitor mv; String concreteClassName = model.getRootPackageInternalPrefix() + "event/" + model.technicalNameCapitalized + "Event"; String className = model.isCustomized ? model.getRootPackageInternalPrefix() + "event/Abstract" + model.technicalNameCapitalized + "Event" : concreteClassName; cw.visit( V1_7, ACC_PUBLIC + ACC_SUPER + (model.isCustomized ? ACC_ABSTRACT : 0), className, null, "org/instantlogic/interaction/flow/impl/SimpleFlowEvent", null); // public static final HomeEvent { fv = cw.visitField( ACC_PUBLIC + ACC_FINAL + ACC_STATIC, "INSTANCE", "L" + concreteClassName + ";", null, null); fv.visitEnd(); } { mv = cw.visitMethod(ACC_STATIC, "<clinit>", "()V", null, null); mv.visitCode(); // INSTANCE = new HomeEvent(); mv.visitTypeInsn(NEW, concreteClassName); mv.visitInsn(DUP); mv.visitMethodInsn(INVOKESPECIAL, concreteClassName, "<init>", "()V"); mv.visitFieldInsn(PUTSTATIC, className, "INSTANCE", "L" + concreteClassName + ";"); mv.visitInsn(RETURN); mv.visitMaxs(9, 99); mv.visitEnd(); } // Default synthetic constructor { mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null); mv.visitCode(); mv.visitVarInsn(ALOAD, 0); mv.visitLdcInsn(model.name); mv.visitIntInsn(BIPUSH, model.parameters.size()); mv.visitTypeInsn(ANEWARRAY, "org/instantlogic/fabric/model/Entity"); int i = 0; for (String parameter : model.parameters) { mv.visitInsn(DUP); mv.visitIntInsn(BIPUSH, i); mv.visitFieldInsn( GETSTATIC, model.getRootPackageInternalPrefix() + "entity/" + parameter + "Entity", "INSTANCE", "L" + model.getRootPackageInternalPrefix() + "entity/" + parameter + "Entity;"); mv.visitInsn(AASTORE); i++; } mv.visitMethodInsn( INVOKESPECIAL, "org/instantlogic/interaction/flow/impl/SimpleFlowEvent", "<init>", "(Ljava/lang/String;[Lorg/instantlogic/fabric/model/Entity;)V"); mv.visitInsn(RETURN); mv.visitMaxs(9, 99); mv.visitEnd(); } cw.visitEnd(); return cwriter.toByteArray(); }
@Before public void setUp() throws Exception { when(classVisitor.visitMethod( anyInt(), any(String.class), any(String.class), any(String.class), any(String[].class))) .thenReturn(methodVisitor); }