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;
}
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);
}
Aggregations