java-bytecode-asm

Generating a 'Hello, World!' class with the Java ASM library

余生颓废 提交于 2019-11-30 09:15:10
I have started messing around with the ASM API for a compiler project I am working on. However, I am finding that the documentation is less than clear for a newcomer in many places and I thought having a good solid example of generating a class that simply prints "Hello, World!" would be a great example to have on here. Currently, I can generate a class with a main() (using the ClassWriter, ClassVisitor and MethodVisitor classes) but I can't seem to work out how to generate main's body. Could anyone give me an example of generating a class file in ASM that: contains a main() creates a local

Is it possible to have the System ClassLoader load .class files specified at run time?

雨燕双飞 提交于 2019-11-29 21:16:39
问题 I am writing a static analysis tool for an assignment, it analyses Java bytecode using the ASM library. One of the parts of ASM that we use requires (or at least, appears to require) that the class be loaded from the ClassLoader. We were hoping the tool would be able to analyse .class files without requiring them on the classpath. We already load the .classes from a specified directory at run time and read them in using an InputStream. This is acceptable for ASM in most cases. There are some

Dynamic Java Bytecode Manipulation Framework Comparison

孤街浪徒 提交于 2019-11-29 20:18:46
There are some frameworks out there for dynamic bytecode generation, manipulation and weaving (BCEL, CGLIB, javassist, ASM, MPS). I want to learn about them, but since I don't have much time to know all the details about all of them, I would like to see a sort of comparison chart saying the advantages and disadvantages of one versus the others and an explanation of why. Here in SO, I found a lot of questions asking something similar, and the answers normally said "you can use cglib or ASM", or "javassist is better than cglib", or "BCEL is old and is dying" or "ASM is the best because it gives

ArrayOutOfBoundsException: asm.ClassReader.readClass(Unknown Source) [duplicate]

烂漫一生 提交于 2019-11-29 16:22:56
This question already has an answer here: Failed to load ApplicationContext caused by ArrayIndexOutOfBoundsException in ClassReader 5 answers I have a hierarchy with the following dependencies: Base(Timer timer) Timer(PublisherService service, BaseDao userDao, DataProcessor processor) There are additional dependencies within DataProcessor but I will not go that far because it seems my error occurs a lot earlier. In correspondence with that dependency graph, I have the following Spring configuration: <bean id="base" class="com.apps.consumer.Base"> <constructor-arg name="timer" ref=

Generating a 'Hello, World!' class with the Java ASM library

大兔子大兔子 提交于 2019-11-29 08:31:39
问题 I have started messing around with the ASM API for a compiler project I am working on. However, I am finding that the documentation is less than clear for a newcomer in many places and I thought having a good solid example of generating a class that simply prints "Hello, World!" would be a great example to have on here. Currently, I can generate a class with a main() (using the ClassWriter, ClassVisitor and MethodVisitor classes) but I can't seem to work out how to generate main's body. Could

Java: Getting Bytecode of Class at Runtime from within the Same JVM

☆樱花仙子☆ 提交于 2019-11-29 03:48:11
Related to: Is there a way to obtain the bytecode for a class at runtime? I'm adding durability to Clojure, and I'm finally at the point where I'm ready to add functions. In Clojure, functions are byte compiled into classes with invoke methods (among others). In this way, functions are first class. To make these durable, I need to serialize and deserialize these classes. How do I get the bytecode for the class without having access to the .class file? Please correct me if I'm mistaken, but using an agent requires spawning a separate VM with the agent connecting to the first VM. I need to do it

Java method parameters values in ASM

心不动则不痛 提交于 2019-11-28 23:49:52
I am trying to get the values of a Java program's method's parameters. I am using ASM to instrument the bytecode and getting these values. However, I'm running into some troubles. Here is the visitCode() method used to instrument the code. What it is doing is : Create an empty array to store the collected parameters. For each parameter, load its value into the array. Send this array to my agent's OnMethodEntry method (which where the values will be used). . @Override public void visitCode() { int paramLength = paramTypes.length; // Create array with length equal to number of parameters mv

Understanding javap's output for the Constant Pool

家住魔仙堡 提交于 2019-11-28 16:25:10
When running javap on a very simple HelloWorld application I have some confusion on the output around the constant pool. Test Code public class TestClass { public static void main(String[] args) { System.out.println("hello world"); } } Javap -c -verbose output (snipped) // Header + consts 1..22 snipped const #22 = String #23; // hello world const #23 = Asciz hello world; public static void main(java.lang.String[]); Signature: ([Ljava/lang/String;)V Code: Stack=2, Locals=1, Args_size=1 0: getstatic #16; //Field java/lang/System.out:Ljava/io/PrintStream; 3: ldc #22; //String hello world 5:

Dynamic Java Bytecode Manipulation Framework Comparison

半城伤御伤魂 提交于 2019-11-28 16:20:09
问题 There are some frameworks out there for dynamic bytecode generation, manipulation and weaving (BCEL, CGLIB, javassist, ASM, MPS). I want to learn about them, but since I don't have much time to know all the details about all of them, I would like to see a sort of comparison chart saying the advantages and disadvantages of one versus the others and an explanation of why. Here in SO, I found a lot of questions asking something similar, and the answers normally said "you can use cglib or ASM",

Pretty printing a method in ASM Bytecode

巧了我就是萌 提交于 2019-11-28 08:36:30
问题 I am trying (with no success) to print only the contents of a given method. The following code almost does the trick: class MyTraceMethodVisitor extends MethodVisitor { public MyTraceMethodVisitor(MethodVisitor mv) { super(Opcodes.ASM4, mv); } @Override public void visitMaxs(int maxStack, int maxLocals) { } } class MyClassVisitor extends ClassVisitor { public MyClassVisitor(ClassVisitor cv) { super(Opcodes.ASM4, cv); } @Override public FieldVisitor visitField(int access, String name, String