Search in sources :

Example 36 with FieldVisitor

use of org.objectweb.asm.FieldVisitor in project spring-loaded by spring-projects.

the class SystemClassReflectionGenerator method generateJLCGDF.

public static void generateJLCGDF(ClassWriter cw, String classname, String fieldname, String methodname) {
    FieldVisitor fv = cw.visitField(ACC_PUBLIC_STATIC, fieldname, "Ljava/lang/reflect/Method;", null, null);
    fv.visitEnd();
    MethodVisitor mv = cw.visitMethod(ACC_PRIVATE + ACC_STATIC, fieldname, "(Ljava/lang/Class;Ljava/lang/String;)Ljava/lang/reflect/Field;", null, new String[] { "java/lang/NoSuchFieldException" });
    mv.visitCode();
    Label l0 = new Label();
    Label l1 = new Label();
    Label l2 = new Label();
    mv.visitTryCatchBlock(l0, l1, l2, "java/lang/reflect/InvocationTargetException");
    Label l3 = new Label();
    mv.visitTryCatchBlock(l0, l1, l3, "java/lang/Exception");
    Label l4 = new Label();
    mv.visitLabel(l4);
    mv.visitFieldInsn(GETSTATIC, classname, fieldname, "Ljava/lang/reflect/Method;");
    mv.visitJumpInsn(IFNONNULL, l0);
    Label l5 = new Label();
    mv.visitLabel(l5);
    mv.visitVarInsn(ALOAD, 0);
    mv.visitVarInsn(ALOAD, 1);
    mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Class", methodname, "(Ljava/lang/String;)Ljava/lang/reflect/Field;", false);
    mv.visitInsn(ARETURN);
    mv.visitLabel(l0);
    mv.visitFieldInsn(GETSTATIC, classname, fieldname, "Ljava/lang/reflect/Method;");
    mv.visitInsn(ACONST_NULL);
    mv.visitInsn(ICONST_2);
    mv.visitTypeInsn(ANEWARRAY, "java/lang/Object");
    mv.visitInsn(DUP);
    mv.visitInsn(ICONST_0);
    mv.visitVarInsn(ALOAD, 0);
    mv.visitInsn(AASTORE);
    mv.visitInsn(DUP);
    mv.visitInsn(ICONST_1);
    mv.visitVarInsn(ALOAD, 1);
    mv.visitInsn(AASTORE);
    mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/reflect/Method", "invoke", "(Ljava/lang/Object;[Ljava/lang/Object;)Ljava/lang/Object;", false);
    mv.visitTypeInsn(CHECKCAST, "java/lang/reflect/Field");
    mv.visitLabel(l1);
    mv.visitInsn(ARETURN);
    mv.visitLabel(l2);
    mv.visitVarInsn(ASTORE, 2);
    Label l6 = new Label();
    mv.visitLabel(l6);
    mv.visitVarInsn(ALOAD, 2);
    mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/reflect/InvocationTargetException", "getCause", "()Ljava/lang/Throwable;", false);
    mv.visitTypeInsn(INSTANCEOF, "java/lang/NoSuchFieldException");
    Label l7 = new Label();
    mv.visitJumpInsn(IFEQ, l7);
    Label l8 = new Label();
    mv.visitLabel(l8);
    mv.visitVarInsn(ALOAD, 2);
    mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/reflect/InvocationTargetException", "getCause", "()Ljava/lang/Throwable;", false);
    mv.visitTypeInsn(CHECKCAST, "java/lang/NoSuchFieldException");
    mv.visitInsn(ATHROW);
    mv.visitLabel(l3);
    mv.visitVarInsn(ASTORE, 2);
    mv.visitLabel(l7);
    mv.visitInsn(ACONST_NULL);
    mv.visitInsn(ARETURN);
    Label l9 = new Label();
    mv.visitLabel(l9);
    // mv.visitLocalVariable("clazz", "Ljava/lang/Class;", "Ljava/lang/Class<*>;", l4, l9, 0);
    // mv.visitLocalVariable("fieldname", "Ljava/lang/String;", null, l4, l9, 1);
    // mv.visitLocalVariable("ite", "Ljava/lang/reflect/InvocationTargetException;", null, l6, l3, 2);
    mv.visitMaxs(6, 3);
    mv.visitEnd();
}
Also used : Label(org.objectweb.asm.Label) FieldVisitor(org.objectweb.asm.FieldVisitor) MethodVisitor(org.objectweb.asm.MethodVisitor)

Example 37 with FieldVisitor

use of org.objectweb.asm.FieldVisitor in project maple-ir by LLVM-but-worse.

the class TraceClassVisitor method visitField.

@Override
public FieldVisitor visitField(final int access, final String name, final String desc, final String signature, final Object value) {
    Printer p = this.p.visitField(access, name, desc, signature, value);
    FieldVisitor fv = cv == null ? null : cv.visitField(access, name, desc, signature, value);
    return new TraceFieldVisitor(fv, p);
}
Also used : FieldVisitor(org.objectweb.asm.FieldVisitor)

Example 38 with FieldVisitor

use of org.objectweb.asm.FieldVisitor in project gradle by gradle.

the class ApiMemberWriter method writeClass.

public void writeClass(ClassMember classMember, Set<MethodMember> methods, Set<FieldMember> fields, Set<InnerClassMember> innerClasses) {
    apiMemberAdapter.visit(classMember.getVersion(), classMember.getAccess(), classMember.getName(), classMember.getSignature(), classMember.getSuperName(), classMember.getInterfaces());
    writeClassAnnotations(classMember.getAnnotations());
    for (MethodMember method : methods) {
        writeMethod(method);
    }
    for (FieldMember field : fields) {
        FieldVisitor fieldVisitor = apiMemberAdapter.visitField(field.getAccess(), field.getName(), field.getTypeDesc(), field.getSignature(), field.getValue());
        writeFieldAnnotations(fieldVisitor, field.getAnnotations());
        fieldVisitor.visitEnd();
    }
    for (InnerClassMember innerClass : innerClasses) {
        apiMemberAdapter.visitInnerClass(innerClass.getName(), innerClass.getOuterName(), innerClass.getInnerName(), innerClass.getAccess());
    }
    apiMemberAdapter.visitEnd();
}
Also used : FieldVisitor(org.objectweb.asm.FieldVisitor)

Example 39 with FieldVisitor

use of org.objectweb.asm.FieldVisitor in project SpongeCommon by SpongePowered.

the class ClassEventListenerFactory method generateClass.

private static byte[] generateClass(String name, final Class<?> handle, final ListenerClassVisitor.DiscoveredMethod method, final Class<?> eventClass, final Class<? extends EventFilter> filter) {
    name = name.replace('.', '/');
    final String handleName = Type.getInternalName(handle);
    final String handleDescriptor = Type.getDescriptor(handle);
    final String filterName = Type.getInternalName(filter);
    final String eventDescriptor = method.descriptor();
    final ClassWriter cw = new LoaderClassWriter(handle.getClassLoader(), ClassWriter.COMPUTE_FRAMES | ClassWriter.COMPUTE_MAXS);
    MethodVisitor mv;
    final FieldVisitor fv;
    cw.visit(V1_6, ACC_PUBLIC + ACC_FINAL + ACC_SUPER, name, null, ClassEventListenerFactory.BASE_HANDLER, null);
    {
        fv = cw.visitField(ACC_PRIVATE + ACC_STATIC, "FILTER", "L" + filterName + ";", null, null);
        fv.visitEnd();
    }
    {
        mv = cw.visitMethod(ACC_STATIC, "<clinit>", "()V", null, null);
        mv.visitCode();
        mv.visitTypeInsn(NEW, filterName);
        mv.visitInsn(DUP);
        mv.visitMethodInsn(INVOKESPECIAL, filterName, "<init>", "()V", false);
        mv.visitFieldInsn(PUTSTATIC, name, "FILTER", "L" + filterName + ";");
        mv.visitInsn(RETURN);
        mv.visitMaxs(0, 0);
        mv.visitEnd();
    }
    {
        mv = cw.visitMethod(ACC_PUBLIC, "<init>", '(' + handleDescriptor + ")V", null, null);
        mv.visitCode();
        mv.visitVarInsn(ALOAD, 0);
        mv.visitVarInsn(ALOAD, 1);
        mv.visitMethodInsn(INVOKESPECIAL, ClassEventListenerFactory.BASE_HANDLER, "<init>", "(Ljava/lang/Object;)V", false);
        mv.visitInsn(RETURN);
        mv.visitMaxs(0, 0);
        mv.visitEnd();
    }
    {
        mv = cw.visitMethod(ACC_PUBLIC, "handle", ClassEventListenerFactory.HANDLE_METHOD_DESCRIPTOR, null, new String[] { "java/lang/Exception" });
        mv.visitCode();
        mv.visitFieldInsn(GETSTATIC, name, "FILTER", "L" + filterName + ";");
        mv.visitVarInsn(ALOAD, 1);
        mv.visitMethodInsn(INVOKEINTERFACE, Type.getInternalName(EventFilter.class), "filter", ClassEventListenerFactory.FILTER_DESCRIPTOR, true);
        mv.visitVarInsn(ASTORE, 2);
        mv.visitVarInsn(ALOAD, 2);
        final Label l2 = new Label();
        mv.visitJumpInsn(IFNULL, l2);
        mv.visitVarInsn(ALOAD, 0);
        mv.visitFieldInsn(GETFIELD, name, "handle", "Ljava/lang/Object;");
        mv.visitTypeInsn(CHECKCAST, handleName);
        for (int i = 0; i < method.parameterTypes().length; i++) {
            mv.visitVarInsn(ALOAD, 2);
            mv.visitIntInsn(BIPUSH, i);
            mv.visitInsn(AALOAD);
            final Type paramType = method.parameterTypes()[i].type();
            GeneratorUtils.visitUnboxingMethod(mv, paramType);
        }
        mv.visitMethodInsn(INVOKEVIRTUAL, handleName, method.methodName(), eventDescriptor, false);
        mv.visitLabel(l2);
        mv.visitInsn(RETURN);
        mv.visitMaxs(0, 0);
        mv.visitEnd();
    }
    cw.visitEnd();
    return cw.toByteArray();
}
Also used : Type(org.objectweb.asm.Type) Label(org.objectweb.asm.Label) FieldVisitor(org.objectweb.asm.FieldVisitor) ClassWriter(org.objectweb.asm.ClassWriter) LoaderClassWriter(org.spongepowered.common.event.gen.LoaderClassWriter) LoaderClassWriter(org.spongepowered.common.event.gen.LoaderClassWriter) MethodVisitor(org.objectweb.asm.MethodVisitor)

Example 40 with FieldVisitor

use of org.objectweb.asm.FieldVisitor in project robovm by robovm.

the class ObjCProtocolProxyPlugin method generateProxyMethods.

private void generateProxyMethods(Config config, List<String> interfazes, ClassWriter cw) throws IOException {
    Clazzes clazzes = config.getClazzes();
    final Set<String> addedMethods = new HashSet<>();
    for (String interfaze : interfazes) {
        Clazz clazz = clazzes.load(interfaze);
        if (clazz == null) {
            continue;
        }
        // Copy all abstract method (we skip default methods) to the proxy 
        // and make them native instead of abstract.
        ClassReader classReader = new ClassReader(clazz.getBytes());
        classReader.accept(new ClassVisitor(ASM4, cw) {

            @Override
            public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions) {
                String key = name + desc;
                if ((access & ACC_ABSTRACT) > 0 && !addedMethods.contains(key)) {
                    access &= ~ACC_ABSTRACT;
                    access |= ACC_NATIVE;
                    addedMethods.add(key);
                    return super.visitMethod(access, name, desc, signature, exceptions);
                }
                return null;
            }

            @Override
            public void visit(int version, int access, String name, String signature, String superName, String[] interfaces) {
            // Ignored
            }

            @Override
            public void visitEnd() {
            // Ignored
            }

            @Override
            public AnnotationVisitor visitAnnotation(String desc, boolean visible) {
                // Ignored
                return null;
            }

            @Override
            public void visitAttribute(Attribute attr) {
            // Ignored
            }

            @Override
            public FieldVisitor visitField(int access, String name, String desc, String signature, Object value) {
                // Ignored
                return null;
            }

            @Override
            public void visitInnerClass(String name, String outerName, String innerName, int access) {
            // Ignored
            }

            @Override
            public void visitOuterClass(String owner, String name, String desc) {
            // Ignored
            }

            @Override
            public void visitSource(String source, String debug) {
            // Ignored
            }
        }, 0);
    }
}
Also used : Attribute(org.objectweb.asm.Attribute) ClassVisitor(org.objectweb.asm.ClassVisitor) FieldVisitor(org.objectweb.asm.FieldVisitor) MethodVisitor(org.objectweb.asm.MethodVisitor) AnnotationVisitor(org.objectweb.asm.AnnotationVisitor) ClassReader(org.objectweb.asm.ClassReader) Clazz(org.robovm.compiler.clazz.Clazz) Clazzes(org.robovm.compiler.clazz.Clazzes) HashSet(java.util.HashSet)

Aggregations

FieldVisitor (org.objectweb.asm.FieldVisitor)90 MethodVisitor (org.objectweb.asm.MethodVisitor)59 Label (org.objectweb.asm.Label)33 ClassWriter (org.objectweb.asm.ClassWriter)28 AnnotationVisitor (org.objectweb.asm.AnnotationVisitor)9 ClassReader (org.objectweb.asm.ClassReader)8 Type (org.objectweb.asm.Type)7 Method (java.lang.reflect.Method)6 ClassVisitor (org.objectweb.asm.ClassVisitor)6 IOException (java.io.IOException)4 GeneratorAdapter (org.objectweb.asm.commons.GeneratorAdapter)4 ArrayList (java.util.ArrayList)3 HashSet (java.util.HashSet)3 LitString (lucee.transformer.expression.literal.LitString)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 File (java.io.File)2 FileOutputStream (java.io.FileOutputStream)2 PrintWriter (java.io.PrintWriter)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 URL (java.net.URL)2