use of org.objectweb.asm.util.TraceClassVisitor in project Galacticraft by micdoodle8.
the class ASMHelper method dump.
public static void dump(Acceptor acceptor, File file, boolean filterImportant, boolean sortLocals, boolean textify) {
try {
if (!file.getParentFile().exists()) {
file.getParentFile().mkdirs();
}
if (!file.exists()) {
file.createNewFile();
}
PrintWriter pout = new PrintWriter(file);
ClassVisitor cv = new TraceClassVisitor(null, textify ? new Textifier() : new ASMifier(), pout);
if (filterImportant) {
cv = new ImportantInsnVisitor(cv);
}
if (sortLocals) {
cv = new LocalVariablesSorterVisitor(cv);
}
acceptor.accept(cv);
pout.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
use of org.objectweb.asm.util.TraceClassVisitor in project soot by Sable.
the class AbstractASMBackend method generateTextualRepresentation.
/**
* Outputs the bytecode generated as a textual representation
* @param pw The PrintWriter the textual representation is written to
*/
public void generateTextualRepresentation(PrintWriter pw) {
cv = new TraceClassVisitor(pw);
generateByteCode();
}
use of org.objectweb.asm.util.TraceClassVisitor in project drill by apache.
the class ReplaceMethodInvoke method main.
// private static final org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(ReplaceMethodInvoke.class);
public static void main(String[] args) throws Exception {
final String k2 = "org/apache/drill/Pickle.class";
final URL url = Resources.getResource(k2);
final byte[] clazz = Resources.toByteArray(url);
final ClassReader cr = new ClassReader(clazz);
final ClassWriter cw = writer();
final TraceClassVisitor visitor = new TraceClassVisitor(cw, new Textifier(), new PrintWriter(System.out));
final ValueHolderReplacementVisitor v2 = new ValueHolderReplacementVisitor(visitor, true);
// | ClassReader.SKIP_DEBUG);
cr.accept(v2, ClassReader.EXPAND_FRAMES);
final byte[] output = cw.toByteArray();
Files.write(output, new File("/src/scratch/bytes/S.class"));
check(output);
final DrillConfig c = DrillConfig.forClient();
final SystemOptionManager m = new SystemOptionManager(PhysicalPlanReaderTestFactory.defaultLogicalPlanPersistence(c), new LocalPersistentStoreProvider(c), c);
m.init();
try (QueryClassLoader ql = new QueryClassLoader(DrillConfig.create(), m)) {
ql.injectByteCode("org.apache.drill.Pickle$OutgoingBatch", output);
Class<?> clz = ql.loadClass("org.apache.drill.Pickle$OutgoingBatch");
clz.getMethod("x").invoke(null);
}
}
use of org.objectweb.asm.util.TraceClassVisitor in project drill by apache.
the class AsmUtil method logClass.
/**
* Write a class to the log.
*
* <p>
* Writes at level TRACE.
*
* @param logger
* the logger to write to
* @param logTag
* a tag to print to the log
* @param classNode
* the class
*/
public static void logClass(final Logger logger, final String logTag, final ClassNode classNode) {
if (logger.isTraceEnabled()) {
logger.trace(logTag);
final StringWriter stringWriter = new StringWriter();
final PrintWriter printWriter = new PrintWriter(stringWriter);
final TraceClassVisitor traceClassVisitor = new TraceClassVisitor(printWriter);
classNode.accept(traceClassVisitor);
logger.trace(stringWriter.toString());
}
}
use of org.objectweb.asm.util.TraceClassVisitor in project presto by prestodb.
the class ClassGenerator method defineClasses.
public Map<String, Class<?>> defineClasses(List<ClassDefinition> classDefinitions) {
ClassInfoLoader classInfoLoader = createClassInfoLoader(classDefinitions, classLoader);
Map<String, byte[]> bytecodes = new LinkedHashMap<>();
for (ClassDefinition classDefinition : classDefinitions) {
// We call the simpler class writer first to get any errors out using simpler setting.
// This helps when we have large queries that can potentially cause COMPUTE_FRAMES
// (used by SmartClassWriter for doing more thorough analysis)
ClassWriter simpleClassWriter = new ClassWriter(ClassWriter.COMPUTE_MAXS);
classDefinition.visit(simpleClassWriter);
try {
simpleClassWriter.toByteArray();
} catch (ClassTooLargeException | MethodTooLargeException largeCodeException) {
throw new ByteCodeTooLargeException(largeCodeException);
} catch (RuntimeException e) {
throw new CompilationException("Error compiling class: " + classDefinition.getName(), e);
}
ClassWriter writer = new SmartClassWriter(classInfoLoader);
try {
classDefinition.visit(fakeLineNumbers ? new AddFakeLineNumberClassVisitor(writer) : writer);
} catch (IndexOutOfBoundsException | NegativeArraySizeException e) {
StringWriter out = new StringWriter();
classDefinition.visit(new TraceClassVisitor(null, new Textifier(), new PrintWriter(out)));
throw new IllegalArgumentException("Error processing class definition:\n" + out, e);
}
byte[] bytecode;
try {
bytecode = writer.toByteArray();
} catch (ClassTooLargeException | MethodTooLargeException largeCodeException) {
throw new ByteCodeTooLargeException(largeCodeException);
} catch (RuntimeException e) {
throw new CompilationException("Error compiling class: " + classDefinition.getName(), e);
}
bytecodes.put(classDefinition.getType().getJavaClassName(), bytecode);
if (runAsmVerifier) {
ClassReader reader = new ClassReader(bytecode);
CheckClassAdapter.verify(reader, classLoader, true, new PrintWriter(output));
}
}
dumpClassPath.ifPresent(path -> bytecodes.forEach((className, bytecode) -> {
String name = typeFromJavaClassName(className).getClassName() + ".class";
Path file = path.resolve(name).toAbsolutePath();
try {
createDirectories(file.getParent());
Files.write(file, bytecode);
} catch (IOException e) {
throw new UncheckedIOException("Failed to write generated class file: " + file, e);
}
}));
if (dumpRawBytecode) {
for (byte[] bytecode : bytecodes.values()) {
ClassReader classReader = new ClassReader(bytecode);
classReader.accept(new TraceClassVisitor(new PrintWriter(output)), ClassReader.EXPAND_FRAMES);
}
}
Map<String, Class<?>> classes = classLoader.defineClasses(bytecodes);
try {
for (Class<?> clazz : classes.values()) {
Reflection.initialize(clazz);
}
} catch (VerifyError e) {
throw new RuntimeException(e);
}
return classes;
}
Aggregations