Search in sources :

Example 96 with ClassVisitor

use of org.objectweb.asm.ClassVisitor in project cdap by caskdata.

the class HttpHandlerGenerator method inspectHandler.

/**
 * Inspects the given type and copy/rewrite handler methods from it into the newly generated class.
 *
 * @param delegateType The user handler type
 * @param inspectType The type that needs to be inspected. It's either the delegateType or one of its parents
 */
private void inspectHandler(final TypeToken<?> delegateType, final TypeToken<?> inspectType, final String pathPrefix, final Type classType, final ClassWriter classWriter, final List<Class<?>> preservedClasses) throws IOException {
    Class<?> rawType = inspectType.getRawType();
    // Visit the delegate class, copy and rewrite handler method, with method body just do delegation
    try (InputStream sourceBytes = rawType.getClassLoader().getResourceAsStream(Type.getInternalName(rawType) + ".class")) {
        ClassReader classReader = new ClassReader(sourceBytes);
        classReader.accept(new ClassVisitor(Opcodes.ASM5) {

            // Only need to visit @Path at the class level if we are inspecting the user handler class
            private final boolean inspectDelegate = delegateType.equals(inspectType);

            private boolean visitedPath = !inspectDelegate;

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

            @Override
            public AnnotationVisitor visitAnnotation(String desc, boolean visible) {
                // Copy the class annotation if it is @Path. Only do it for one time
                Type type = Type.getType(desc);
                if (inspectDelegate && type.equals(Type.getType(Path.class))) {
                    visitedPath = true;
                    AnnotationVisitor annotationVisitor = classWriter.visitAnnotation(desc, visible);
                    return new AnnotationVisitor(Opcodes.ASM5, annotationVisitor) {

                        @Override
                        public void visit(String name, Object value) {
                            // "value" is the key for the Path annotation string.
                            if (name.equals("value")) {
                                super.visit(name, pathPrefix + value.toString());
                            } else {
                                super.visit(name, value);
                            }
                        }
                    };
                } else {
                    return super.visitAnnotation(desc, visible);
                }
            }

            @Override
            public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions) {
                // annotation.
                if (!visitedPath) {
                    String pathDesc = Type.getType(Path.class).getDescriptor();
                    AnnotationVisitor annotationVisitor = classWriter.visitAnnotation(pathDesc, true);
                    annotationVisitor.visit("value", pathPrefix);
                    annotationVisitor.visitEnd();
                    visitedPath = true;
                }
                // Copy the method if it is public and annotated with one of the HTTP request method
                MethodVisitor mv = super.visitMethod(access, name, desc, signature, exceptions);
                if (!Modifier.isPublic(access)) {
                    return mv;
                }
                return new HandlerMethodVisitor(delegateType, mv, desc, signature, access, name, exceptions, classType, classWriter, preservedClasses);
            }
        }, ClassReader.SKIP_DEBUG);
    }
}
Also used : Path(javax.ws.rs.Path) InputStream(java.io.InputStream) ClassVisitor(org.objectweb.asm.ClassVisitor) MethodVisitor(org.objectweb.asm.MethodVisitor) Type(org.objectweb.asm.Type) MethodType(java.lang.invoke.MethodType) AnnotationVisitor(org.objectweb.asm.AnnotationVisitor) ClassReader(org.objectweb.asm.ClassReader)

Example 97 with ClassVisitor

use of org.objectweb.asm.ClassVisitor in project HttpApiJava by thevsk.

the class AnnotationReader method isAnnotationed.

public boolean isAnnotationed(InputStream inputStream) {
    ClassReader classReader;
    try {
        classReader = new ClassReader(inputStream);
    } catch (IOException var4) {
        throw new RuntimeException(var4);
    }
    final boolean[] visitorResult = new boolean[] { false };
    classReader.accept(new ClassVisitor(327680) {

        public AnnotationVisitor visitAnnotation(String annotation, boolean visible) {
            if (AnnotationReader.this.annotationMap.containsKey(annotation)) {
                visitorResult[0] = true;
            }
            return super.visitAnnotation(annotation, visible);
        }
    }, 1);
    return visitorResult[0];
}
Also used : AnnotationVisitor(org.objectweb.asm.AnnotationVisitor) ClassReader(org.objectweb.asm.ClassReader) IOException(java.io.IOException) ClassVisitor(org.objectweb.asm.ClassVisitor)

Example 98 with ClassVisitor

use of org.objectweb.asm.ClassVisitor in project phosphor by gmu-swe.

the class TaintLoadCoercer method main.

public static void main(String[] args) throws Throwable {
    Configuration.IMPLICIT_TRACKING = true;
    Configuration.MULTI_TAINTING = true;
    // Configuration.IMPLICIT_LIGHT_TRACKING = true;
    // Configuration.ARRAY_LENGTH_TRACKING = true;
    Configuration.ARRAY_INDEX_TRACKING = true;
    // Configuration.ANNOTATE_LOOPS = true;
    // Instrumenter.instrumentClass("asdf", new FileInputStream("z.class"), false);
    ClassReader cr = new ClassReader(new FileInputStream("z.class"));
    // ClassReader cr = new ClassReader(new FileInputStream("target/classes/VarInLoop.class"));
    final String className = cr.getClassName();
    PrintWriter pw = new PrintWriter("z.txt");
    TraceClassVisitor tcv = new TraceClassVisitor(null, new PhosphorTextifier(), pw);
    ClassWriter cw1 = new ClassWriter(ClassWriter.COMPUTE_FRAMES) {

        @Override
        protected String getCommonSuperClass(String arg0, String arg1) {
            try {
                return super.getCommonSuperClass(arg0, arg1);
            } catch (Throwable t) {
                return "java/lang/Object";
            }
        }
    };
    cr.accept(new ClassVisitor(Opcodes.ASM5, cw1) {

        @Override
        public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions) {
            MethodVisitor mv = super.visitMethod(access, name, desc, signature, exceptions);
            mv = new JSRInlinerAdapter(mv, access, name, desc, signature, exceptions);
            return mv;
        }
    }, ClassReader.EXPAND_FRAMES);
    cr = new ClassReader(cw1.toByteArray());
    ClassVisitor cv = new ClassVisitor(Opcodes.ASM5, tcv) {

        String className;

        @Override
        public void visit(int version, int access, String name, String signature, String superName, String[] interfaces) {
            super.visit(version, access, name, signature, superName, interfaces);
            this.className = name;
        }

        HashSet<FieldNode> fields = new HashSet<FieldNode>();

        @Override
        public FieldVisitor visitField(int access, String name, String desc, String signature, Object value) {
            fields.add(new FieldNode(access, name, desc, signature, value));
            return super.visitField(access, name, desc, signature, value);
        }

        @Override
        public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions) {
            // TODO Auto-generated method stub
            MethodVisitor mv = super.visitMethod(access, name, desc, signature, exceptions);
            mv = new MethodVisitor(Opcodes.ASM5, mv) {

                @Override
                public void visitInsn(int opcode) {
                    super.visitInsn(opcode);
                }
            };
            // mv = new SpecialOpcodeRemovingMV(mv,false,className,false);
            // NeverNullArgAnalyzerAdapter analyzer = new NeverNullArgAnalyzerAdapter(className, access, className, desc, mv);
            mv = new TaintLoadCoercer(className, access, name, desc, signature, exceptions, mv, true);
            // LocalVariableManager lvs = new LocalVariableManager(access, desc, mv, analyzer, mv, false);
            // mv = lvs;
            PrimitiveArrayAnalyzer paa = new PrimitiveArrayAnalyzer(className, access, name, desc, signature, exceptions, mv);
            NeverNullArgAnalyzerAdapter an = new NeverNullArgAnalyzerAdapter(className, access, name, desc, paa);
            paa.setAnalyzer(an);
            // ((PrimitiveArrayAnalyzer) mv).setAnalyzer(an);
            mv = an;
            return mv;
        }
    };
    cr.accept(cv, ClassReader.EXPAND_FRAMES);
    pw.flush();
}
Also used : FieldNode(org.objectweb.asm.tree.FieldNode) ClassVisitor(org.objectweb.asm.ClassVisitor) TraceClassVisitor(org.objectweb.asm.util.TraceClassVisitor) FileInputStream(java.io.FileInputStream) ClassWriter(org.objectweb.asm.ClassWriter) JSRInlinerAdapter(org.objectweb.asm.commons.JSRInlinerAdapter) MethodVisitor(org.objectweb.asm.MethodVisitor) TraceClassVisitor(org.objectweb.asm.util.TraceClassVisitor) NeverNullArgAnalyzerAdapter(edu.columbia.cs.psl.phosphor.instrumenter.analyzer.NeverNullArgAnalyzerAdapter) ClassReader(org.objectweb.asm.ClassReader) PrintWriter(java.io.PrintWriter) HashSet(java.util.HashSet)

Example 99 with ClassVisitor

use of org.objectweb.asm.ClassVisitor in project phosphor by gmu-swe.

the class DebugPrinter method main.

public static void main(String[] args) throws Exception {
    // Configuration.IMPLICIT_TRACKING = true;
    File clazz = new File(args[0]);
    final ClassReader cr1 = new ClassReader(new FileInputStream(clazz));
    PrintWriter pw = new PrintWriter(new FileWriter("z.txt"));
    ClassWriter cw1 = new ClassWriter(ClassWriter.COMPUTE_FRAMES) {

        @Override
        protected String getCommonSuperClass(String type1, String type2) {
            try {
                return super.getCommonSuperClass(type1, type2);
            } catch (Exception ex) {
                // System.err.println("err btwn " + type1 + " " +type2);
                return "java/lang/Unknown";
            }
        }
    };
    cr1.accept(new ClassVisitor(Opcodes.ASM4, cw1) {

        @Override
        public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions) {
            // TODO Auto-generated method stub
            return new JSRInlinerAdapter(super.visitMethod(access, name, desc, signature, exceptions), access, name, desc, signature, exceptions);
        }
    }, ClassReader.EXPAND_FRAMES);
    final ClassReader cr = new ClassReader(cw1.toByteArray());
    TraceClassVisitor tcv = new TraceClassVisitor(new ClassVisitor(Opcodes.ASM5, new ClassWriter(0)) {

        String className;

        @Override
        public void visit(int version, int access, String name, String signature, String superName, String[] interfaces) {
            super.visit(version, access, name, signature, superName, interfaces);
            this.className = name;
        }

        @Override
        public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions) {
            // TODO Auto-generated method stub
            MethodVisitor mv = super.visitMethod(access, name, desc, signature, exceptions);
            mv = new PrimitiveArrayAnalyzer(cr.getClassName(), access, name, desc, signature, exceptions, mv);
            NeverNullArgAnalyzerAdapter an = new NeverNullArgAnalyzerAdapter(cr.getClassName(), access, name, desc, mv);
            ((PrimitiveArrayAnalyzer) mv).setAnalyzer(an);
            mv = an;
            return mv;
        }
    }, pw);
    cr.accept(tcv, ClassReader.EXPAND_FRAMES);
    pw.flush();
}
Also used : FileWriter(java.io.FileWriter) TraceClassVisitor(org.objectweb.asm.util.TraceClassVisitor) ClassVisitor(org.objectweb.asm.ClassVisitor) FileInputStream(java.io.FileInputStream) ClassWriter(org.objectweb.asm.ClassWriter) JSRInlinerAdapter(org.objectweb.asm.commons.JSRInlinerAdapter) MethodVisitor(org.objectweb.asm.MethodVisitor) PrimitiveArrayAnalyzer(edu.columbia.cs.psl.phosphor.instrumenter.PrimitiveArrayAnalyzer) TraceClassVisitor(org.objectweb.asm.util.TraceClassVisitor) NeverNullArgAnalyzerAdapter(edu.columbia.cs.psl.phosphor.instrumenter.analyzer.NeverNullArgAnalyzerAdapter) ClassReader(org.objectweb.asm.ClassReader) File(java.io.File) PrintWriter(java.io.PrintWriter)

Example 100 with ClassVisitor

use of org.objectweb.asm.ClassVisitor in project lombok by rzwitserloot.

the class PreventNullAnalysisRemover method applyTransformations.

@Override
public byte[] applyTransformations(byte[] original, String fileName, DiagnosticsReceiver diagnostics) {
    if (!new ClassFileMetaData(original).usesMethod("lombok/Lombok", "preventNullAnalysis"))
        return null;
    byte[] fixedByteCode = fixJSRInlining(original);
    ClassReader reader = new ClassReader(fixedByteCode);
    ClassWriter writer = new FixedClassWriter(reader, 0);
    final AtomicBoolean changesMade = new AtomicBoolean();
    class PreventNullAnalysisVisitor extends MethodVisitor {

        PreventNullAnalysisVisitor(MethodVisitor mv) {
            super(Opcodes.ASM6, mv);
        }

        @Override
        public void visitMethodInsn(int opcode, String owner, String name, String desc, boolean itf) {
            boolean hit = true;
            if (hit && opcode != Opcodes.INVOKESTATIC)
                hit = false;
            if (hit && !"preventNullAnalysis".equals(name))
                hit = false;
            if (hit && !"lombok/Lombok".equals(owner))
                hit = false;
            if (hit && !"(Ljava/lang/Object;)Ljava/lang/Object;".equals(desc))
                hit = false;
            if (hit) {
                changesMade.set(true);
                // DEBUG for issue 470!
                if (System.getProperty("lombok.debugAsmOnly", null) != null)
                    super.visitMethodInsn(opcode, owner, name, desc, itf);
            } else {
                super.visitMethodInsn(opcode, owner, name, desc, itf);
            }
        }
    }
    reader.accept(new ClassVisitor(Opcodes.ASM6, writer) {

        @Override
        public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions) {
            return new PreventNullAnalysisVisitor(super.visitMethod(access, name, desc, signature, exceptions));
        }
    }, 0);
    return changesMade.get() ? writer.toByteArray() : null;
}
Also used : ClassVisitor(org.objectweb.asm.ClassVisitor) ClassWriter(org.objectweb.asm.ClassWriter) MethodVisitor(org.objectweb.asm.MethodVisitor) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ClassReader(org.objectweb.asm.ClassReader)

Aggregations

ClassVisitor (org.objectweb.asm.ClassVisitor)133 ClassReader (org.objectweb.asm.ClassReader)97 ClassWriter (org.objectweb.asm.ClassWriter)80 MethodVisitor (org.objectweb.asm.MethodVisitor)45 IOException (java.io.IOException)19 InputStream (java.io.InputStream)19 Type (org.objectweb.asm.Type)14 TraceClassVisitor (org.objectweb.asm.util.TraceClassVisitor)14 File (java.io.File)11 PrintWriter (java.io.PrintWriter)11 AnnotationVisitor (org.objectweb.asm.AnnotationVisitor)10 Label (org.objectweb.asm.Label)10 FileInputStream (java.io.FileInputStream)9 Test (org.junit.Test)9 FileOutputStream (java.io.FileOutputStream)6 FieldVisitor (org.objectweb.asm.FieldVisitor)6 GeneratorAdapter (org.objectweb.asm.commons.GeneratorAdapter)6 ArrayList (java.util.ArrayList)5 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)5 FieldList (net.bytebuddy.description.field.FieldList)5