Search in sources :

Example 36 with ClassVisitor

use of org.objectweb.asm.ClassVisitor in project Lucee by lucee.

the class SourceLastModifiedClassAdapter method setSourceLastModified.

public static byte[] setSourceLastModified(byte[] barr, long lastModified) {
    ClassReader cr = new ClassReader(barr);
    ClassWriter cw = ASMUtil.getClassWriter();
    ClassVisitor ca = new SourceLastModifiedClassAdapter(cw, lastModified);
    cr.accept(ca, 0);
    return cw.toByteArray();
}
Also used : ClassReader(org.objectweb.asm.ClassReader) ClassVisitor(org.objectweb.asm.ClassVisitor) ClassWriter(org.objectweb.asm.ClassWriter)

Example 37 with ClassVisitor

use of org.objectweb.asm.ClassVisitor in project quasar by puniverse.

the class QuasarInstrumentor method instrumentClass.

@VisibleForTesting
byte[] instrumentClass(ClassLoader loader, String className, InputStream is, boolean forceInstrumentation) throws IOException {
    className = className != null ? className.replace('.', '/') : null;
    byte[] cb = toByteArray(is);
    MethodDatabase db = getMethodDatabase(loader);
    if (className != null) {
        log(LogLevel.INFO, "TRANSFORM: %s %s", className, (db.getClassEntry(className) != null && db.getClassEntry(className).requiresInstrumentation()) ? "request" : "");
        examine(className, "quasar-1-preinstr", cb);
    } else {
        log(LogLevel.INFO, "TRANSFORM: null className");
    }
    // Phase 1, add a label before any suspendable calls, event API is enough
    final ClassReader r1 = new ClassReader(cb);
    final ClassWriter cw1 = new ClassWriter(r1, 0);
    final LabelSuspendableCallSitesClassVisitor ic1 = new LabelSuspendableCallSitesClassVisitor(cw1, db);
    r1.accept(ic1, 0);
    cb = cw1.toByteArray();
    examine(className, "quasar-2", cb);
    // Phase 2, instrument, tree API
    final ClassReader r2 = new ClassReader(cb);
    final ClassWriter cw2 = new DBClassWriter(db, r2);
    final ClassVisitor cv2 = (check && EXAMINED_CLASS == null) ? new CheckClassAdapter(cw2) : cw2;
    final InstrumentClass ic2 = new InstrumentClass(cv2, db, forceInstrumentation);
    try {
        r2.accept(ic2, ClassReader.SKIP_FRAMES);
        cb = cw2.toByteArray();
    } catch (final Exception e) {
        if (ic2.hasSuspendableMethods()) {
            error("Unable to instrument class " + className, e);
            throw e;
        } else {
            if (!MethodDatabase.isProblematicClass(className))
                log(LogLevel.DEBUG, "Unable to instrument class " + className);
            return null;
        }
    }
    examine(className, "quasar-4", cb);
    // Phase 4, fill suspendable call offsets, event API is enough
    final OffsetClassReader r3 = new OffsetClassReader(cb);
    final ClassWriter cw3 = new ClassWriter(r3, 0);
    final SuspOffsetsAfterInstrClassVisitor ic3 = new SuspOffsetsAfterInstrClassVisitor(cw3, db);
    r3.accept(ic3, 0);
    cb = cw3.toByteArray();
    // DEBUG
    if (EXAMINED_CLASS != null) {
        examine(className, "quasar-5-final", cb);
        if (check) {
            ClassReader r4 = new ClassReader(cb);
            ClassVisitor cv4 = new CheckClassAdapter(new TraceClassVisitor(null), true);
            r4.accept(cv4, 0);
        }
    }
    return cb;
}
Also used : TraceClassVisitor(org.objectweb.asm.util.TraceClassVisitor) ClassVisitor(org.objectweb.asm.ClassVisitor) ClassWriter(org.objectweb.asm.ClassWriter) TraceClassVisitor(org.objectweb.asm.util.TraceClassVisitor) CheckClassAdapter(org.objectweb.asm.util.CheckClassAdapter) ClassReader(org.objectweb.asm.ClassReader) VisibleForTesting(co.paralleluniverse.common.util.VisibleForTesting)

Example 38 with ClassVisitor

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

the class ArtifactInspector method isPlugin.

/**
   * Detects if a class is annotated with {@link Plugin} without loading the class.
   *
   * @param className name of the class
   * @param classLoader ClassLoader for loading the class file of the given class
   * @return true if the given class is annotated with {@link Plugin}
   */
private boolean isPlugin(String className, ClassLoader classLoader) {
    try (InputStream is = classLoader.getResourceAsStream(className.replace('.', '/') + ".class")) {
        if (is == null) {
            return false;
        }
        // Use ASM to inspect the class bytecode to see if it is annotated with @Plugin
        final boolean[] isPlugin = new boolean[1];
        ClassReader cr = new ClassReader(is);
        cr.accept(new ClassVisitor(Opcodes.ASM5) {

            @Override
            public AnnotationVisitor visitAnnotation(String desc, boolean visible) {
                if (Plugin.class.getName().equals(Type.getType(desc).getClassName()) && visible) {
                    isPlugin[0] = true;
                }
                return super.visitAnnotation(desc, visible);
            }
        }, ClassReader.SKIP_CODE | ClassReader.SKIP_DEBUG | ClassReader.SKIP_FRAMES);
        return isPlugin[0];
    } catch (IOException e) {
        // If failed to open the class file, then it cannot be a plugin
        LOG.warn("Failed to open class file for {}", className, e);
        return false;
    }
}
Also used : InputStream(java.io.InputStream) AnnotationVisitor(org.objectweb.asm.AnnotationVisitor) ClassReader(org.objectweb.asm.ClassReader) ClassVisitor(org.objectweb.asm.ClassVisitor) IOException(java.io.IOException) Plugin(co.cask.cdap.api.annotation.Plugin)

Example 39 with ClassVisitor

use of org.objectweb.asm.ClassVisitor in project j2objc by google.

the class PackageInfoLookup method parseDataFromClassFile.

private PackageData parseDataFromClassFile(InputFile file) throws IOException {
    PackageDataBuilder builder = new PackageDataBuilder();
    ClassReader classReader = new ClassReader(file.getInputStream());
    classReader.accept(new ClassVisitor(Opcodes.ASM5) {

        @Override
        public AnnotationVisitor visitAnnotation(String desc, boolean visible) {
            if (desc.equals("Lcom/google/j2objc/annotations/ObjectiveCName;")) {
                return new AnnotationVisitor(Opcodes.ASM5) {

                    @Override
                    public void visit(String name, Object value) {
                        if (name.equals("value")) {
                            builder.setObjectiveCName(value.toString());
                        }
                    }
                };
            } else if (desc.equals("Ljavax/annotation/ParametersAreNonnullByDefault;")) {
                builder.setParametersAreNonnullByDefault();
            } else if (desc.equals("Lcom/google/j2objc/annotations/ReflectionSupport;")) {
                return new AnnotationVisitor(Opcodes.ASM5) {

                    @Override
                    public void visitEnum(String name, String desc, String value) {
                        if (desc.equals("Lcom/google/j2objc/annotations/ReflectionSupport$Level;") && name.equals("value")) {
                            builder.setReflectionSupportLevel(ReflectionSupport.Level.valueOf(value));
                        }
                    }
                };
            }
            return null;
        }
    }, 0);
    return builder.build();
}
Also used : AnnotationVisitor(org.objectweb.asm.AnnotationVisitor) ClassReader(org.objectweb.asm.ClassReader) ClassVisitor(org.objectweb.asm.ClassVisitor)

Example 40 with ClassVisitor

use of org.objectweb.asm.ClassVisitor in project blade by biezhi.

the class DebuggingClassWriter method toByteArray.

public byte[] toByteArray() {
    return (byte[]) java.security.AccessController.doPrivileged(new java.security.PrivilegedAction() {

        public Object run() {
            byte[] b = ((ClassWriter) DebuggingClassWriter.super.cv).toByteArray();
            if (debugLocation != null) {
                String dirs = className.replace('.', File.separatorChar);
                try {
                    new File(debugLocation + File.separatorChar + dirs).getParentFile().mkdirs();
                    File file = new File(new File(debugLocation), dirs + ".class");
                    OutputStream out = new BufferedOutputStream(new FileOutputStream(file));
                    try {
                        out.write(b);
                    } finally {
                        out.close();
                    }
                    if (traceCtor != null) {
                        file = new File(new File(debugLocation), dirs + ".asm");
                        out = new BufferedOutputStream(new FileOutputStream(file));
                        try {
                            ClassReader cr = new ClassReader(b);
                            PrintWriter pw = new PrintWriter(new OutputStreamWriter(out));
                            ClassVisitor tcv = (ClassVisitor) traceCtor.newInstance(new Object[] { null, pw });
                            cr.accept(tcv, 0);
                            pw.flush();
                        } finally {
                            out.close();
                        }
                    }
                } catch (Exception e) {
                    throw new CodeGenerationException(e);
                }
            }
            return b;
        }
    });
}
Also used : ClassVisitor(org.objectweb.asm.ClassVisitor) ClassWriter(org.objectweb.asm.ClassWriter) ClassReader(org.objectweb.asm.ClassReader)

Aggregations

ClassVisitor (org.objectweb.asm.ClassVisitor)75 ClassReader (org.objectweb.asm.ClassReader)52 ClassWriter (org.objectweb.asm.ClassWriter)45 MethodVisitor (org.objectweb.asm.MethodVisitor)23 InputStream (java.io.InputStream)12 IOException (java.io.IOException)10 Type (org.objectweb.asm.Type)9 Test (org.junit.Test)7 File (java.io.File)6 AnnotationVisitor (org.objectweb.asm.AnnotationVisitor)6 FieldList (net.bytebuddy.description.field.FieldList)5 MethodList (net.bytebuddy.description.method.MethodList)5 TypeDescription (net.bytebuddy.description.type.TypeDescription)5 TypePool (net.bytebuddy.pool.TypePool)5 TraceClassVisitor (org.objectweb.asm.util.TraceClassVisitor)5 URL (java.net.URL)4 MethodDescription (net.bytebuddy.description.method.MethodDescription)4 FileOutputStream (java.io.FileOutputStream)3 Path (java.nio.file.Path)3 GeneratorAdapter (org.objectweb.asm.commons.GeneratorAdapter)3