use of com.sun.btrace.runtime.BTraceProbeFactory 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.runtime.BTraceProbeFactory in project btrace by btraceio.
the class Client method load.
// Internals only below this point
private BTraceProbe load(byte[] buf, boolean canLoadPack) {
BTraceProbeFactory f = new BTraceProbeFactory(settings, canLoadPack);
debugPrint("loading BTrace class");
BTraceProbe cn = f.createProbe(buf, argsMap);
if (cn != null) {
if (isDebug()) {
if (cn.isVerified()) {
debugPrint("loaded '" + cn.getClassName() + "' successfully");
} else {
debugPrint(cn.getClassName() + " failed verification");
return null;
}
}
}
return BTraceProbePersisted.from(cn);
}
use of com.sun.btrace.runtime.BTraceProbeFactory in project btrace by btraceio.
the class ProbeDump method main.
public static void main(String[] args) throws Exception {
String path = args[0];
BTraceProbeFactory bpf = new BTraceProbeFactory(SharedSettings.GLOBAL);
BTraceProbe bp = bpf.createProbe(new FileInputStream(path));
FileSystem fs = FileSystems.getDefault();
Path p = fs.getPath(args[1]);
Files.write(p.resolve(bp.getClassName().replace(".", "_") + "_full.class"), bp.getFullBytecode());
Files.write(p.resolve(bp.getClassName().replace(".", "_") + "_dh.class"), bp.getDataHolderBytecode());
}
Aggregations