Search in sources :

Example 1 with ClassReader

use of com.sun.btrace.org.objectweb.asm.ClassReader in project btrace by btraceio.

the class Compiler method compile.

private Map<String, byte[]> compile(MemoryJavaFileManager manager, Iterable<? extends JavaFileObject> compUnits, Writer err, String sourcePath, final String classPath) {
    // to collect errors, warnings etc.
    DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<>();
    // javac options
    List<String> options = new ArrayList<>();
    options.add("-Xlint:all");
    options.add("-g:lines");
    options.add("-deprecation");
    options.add("-source");
    options.add("1.7");
    options.add("-target");
    options.add("1.7");
    if (sourcePath != null) {
        options.add("-sourcepath");
        options.add(sourcePath);
    }
    if (classPath != null) {
        options.add("-classpath");
        options.add(classPath);
    }
    // create a compilation task
    JavacTask task = (JavacTask) compiler.getTask(err, manager, diagnostics, options, null, compUnits);
    Verifier btraceVerifier = new Verifier();
    task.setTaskListener(btraceVerifier);
    // we add BTrace Verifier as a (JSR 269) Processor
    List<Processor> processors = new ArrayList<>(1);
    processors.add(btraceVerifier);
    task.setProcessors(processors);
    final PrintWriter perr = (err instanceof PrintWriter) ? (PrintWriter) err : new PrintWriter(err);
    // print dignostics messages in case of failures.
    if (task.call() == false || containsErrors(diagnostics)) {
        for (Diagnostic diagnostic : diagnostics.getDiagnostics()) {
            printDiagnostic(diagnostic, perr);
        }
        perr.flush();
        return null;
    }
    // collect .class bytes of all compiled classes
    Map<String, byte[]> result = new HashMap<>();
    try {
        Map<String, byte[]> classBytes = manager.getClassBytes();
        List<String> classNames = btraceVerifier.getClassNames();
        for (String name : classNames) {
            if (classBytes.containsKey(name)) {
                dump(name + "_before", classBytes.get(name));
                ClassReader cr = new ClassReader(classBytes.get(name));
                ClassWriter cw = new CompilerClassWriter(classPath, perr);
                cr.accept(new Postprocessor(cw), ClassReader.EXPAND_FRAMES + ClassReader.SKIP_DEBUG);
                byte[] classData = cw.toByteArray();
                dump(name + "_after", classData);
                if (generatePack) {
                    // temp hack; need turn off verifier
                    SharedSettings.GLOBAL.setTrusted(true);
                    BTraceProbeNode bpn = (BTraceProbeNode) new BTraceProbeFactory(SharedSettings.GLOBAL).createProbe(classData);
                    ByteArrayOutputStream bos = new ByteArrayOutputStream();
                    try (DataOutputStream dos = new DataOutputStream(bos)) {
                        BTraceProbePersisted bpp = BTraceProbePersisted.from(bpn);
                        bpp.write(dos);
                    }
                    classData = bos.toByteArray();
                }
                result.put(name, classData);
            }
        }
    } catch (IOException e) {
        e.printStackTrace(perr);
    } finally {
        try {
            manager.close();
        } catch (IOException exp) {
        }
    }
    return result;
}
Also used : Processor(javax.annotation.processing.Processor) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Diagnostic(javax.tools.Diagnostic) JavaFileObject(javax.tools.JavaFileObject) DiagnosticCollector(javax.tools.DiagnosticCollector) JavacTask(com.sun.source.util.JavacTask) ClassWriter(com.sun.btrace.org.objectweb.asm.ClassWriter) BTraceProbeFactory(com.sun.btrace.runtime.BTraceProbeFactory) BTraceProbePersisted(com.sun.btrace.runtime.BTraceProbePersisted) ClassReader(com.sun.btrace.org.objectweb.asm.ClassReader) BTraceProbeNode(com.sun.btrace.runtime.BTraceProbeNode)

Example 2 with ClassReader

use of com.sun.btrace.org.objectweb.asm.ClassReader in project btrace by btraceio.

the class StackTrackingMethodVisitorTest method setUp.

@Before
public void setUp() throws IOException {
    byte[] data = loadTargetClass("StackTrackerTest");
    System.err.println(asmify(data));
    reader = new ClassReader(data);
}
Also used : ClassReader(com.sun.btrace.org.objectweb.asm.ClassReader) Before(org.junit.Before)

Aggregations

ClassReader (com.sun.btrace.org.objectweb.asm.ClassReader)2 ClassWriter (com.sun.btrace.org.objectweb.asm.ClassWriter)1 BTraceProbeFactory (com.sun.btrace.runtime.BTraceProbeFactory)1 BTraceProbeNode (com.sun.btrace.runtime.BTraceProbeNode)1 BTraceProbePersisted (com.sun.btrace.runtime.BTraceProbePersisted)1 JavacTask (com.sun.source.util.JavacTask)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Processor (javax.annotation.processing.Processor)1 Diagnostic (javax.tools.Diagnostic)1 DiagnosticCollector (javax.tools.DiagnosticCollector)1 JavaFileObject (javax.tools.JavaFileObject)1 Before (org.junit.Before)1