java-bytecode-asm

Implementing abstract methods at runtime?

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-12 08:48:18
问题 Let's say I have an abstract class: abstract class Foo extends Bar { public abstract int foo(); } that I want to extend at runtime to create a Class object. The hope would be that I could have a dynamically generated class: class FooImpl extends Foo { @Override public int foo() { return 5; } } that would be represented by a Class object and that I could then use reflection to create new instances of. The key is that I would like to decide the return value of the method foo() at runtime. My

Java bytecode variable index 0's className is something strange

泪湿孤枕 提交于 2019-12-12 05:24:24
问题 I use ASM library to generate bytecodes and load them using Unsafe.defineAnonymous as a Class. Both work in most of cases, but after for a short time, it fails. Then I add some debug instructions in the emitted bytecodes to print something, and the output confused me for two weeks. (GWT is short for GuardWithTestHandle). 1, Two classes are generated: DYNGWT70 and DYNGWT73, and both are loaded using Unsafe . For each class, there is only one instance is created. 2, The layout of DYNGWT70 is

What ASM Visitor Method gets called for type annotation on catch

Deadly 提交于 2019-12-12 01:49:53
问题 I have the following code snippet, which I analyze with ASM try{ } catch (@TypeAnno7 RuntimeException re){ } I can't find the right method that gets called for the Annotation. I thought MethodVisitor.visitTryCatchAnnotation would do the trick, but it doesn't get called. So: what is the correct method used? And for what kind of code does the method mentioned get called? 回答1: The Java compiler does not generate any bytecode for a try/catch unless the try body contains some code (otherwise, the

Formatting the output of a TraceClassVisitor

谁都会走 提交于 2019-12-11 11:08:13
问题 Let's say I want to pretty print the bytecode of a method with the asm library. public int get777() { return 777; } through TraceClassVisitor will look as // access flags 0x1 public get777()I L0 LINENUMBER 21 L0 SIPUSH 777 IRETURN L1 LOCALVARIABLE this Lsomething/Point; L0 L1 0 MAXSTACK = 1 MAXLOCALS = 1 } Now, the thing is that I only care for SIPUSH 777 IRETURN being everything else largely irrelevant to me, so I want to wipe them out. I've thought of filtering the stuff I don't want by

Java, ASM: How to Get Opcode Name and TagValue from ASM InsnNode?

我与影子孤独终老i 提交于 2019-12-11 07:48:28
问题 I am working on some class file analysis and I am working on using ASM to read classes. In Javap the opcode and the tagName and tagValue are printed inline, but in each AbstractInsnNode, I see only fields for the int (and not the tagValue) for(AbstractInsnNode abstractInsnNode : instructionList) { System.out.println("\tOpcode: " + + abstractInsnNode.getOpcode() + " type: " + abstractInsnNode.getType()); } How do I get the of the instruction with ASM, such as: 5: invokeinterface #6, 2 //

How to reuse original frame information from a methodNode in asm to create `org.objectweb.asm.tree.analysis.Frame`

∥☆過路亽.° 提交于 2019-12-11 07:29:44
问题 How can I construct a org.objectweb.asm.tree.analysis.Frame for each instruction in a method using only FrameNodes and LocalVariableNodes from the MethodNode ? Context While instrumenting some code I need all the locals and stack types for some of the original instructions. Currently I get this by doing: Analyzer analyzer = new Analyzer(new MyBasicInterpreter()); Frame[] frames = analyzer.analyze(ownerClass, methodNode); This gives me a Frame for each instruction in the methodNode . However,

How to modify instructions in Java BootstrapMethods using ASM?

北战南征 提交于 2019-12-11 07:29:36
问题 After compiling with Java 8 and dumping the resulting class files with javap, I see this, of which I am only showing the first 2 items: BootstrapMethods: 0: #174 invokestatic java/lang/invoke/LambdaMetafactory.metafactory:(Ljava/lang/invoke/MethodHandles$Lookup;Ljava/lang/String;Ljava/lang/invoke/MethodType;Ljava/lang/invoke/MethodType;Ljava/lang/invoke/MethodHandle;Ljava/lang/invoke/MethodType;)Ljava/lang/invoke/CallSite; Method arguments: #175 (Ljava/lang/Object;)Z #179 invokestatic llllll

How to copy InsnList

拜拜、爱过 提交于 2019-12-11 06:06:21
问题 InsnList has no method for copy self. I tried to iterate list and add each node to new list. But iterating copy of list perform npe private static InsnList copy(InsnList insnList) { InsnList r = new InsnList(); for (int i = 0; i < insnList.size(); i++) r.add(insnList.get(i)); return r; } InsnList copy = copy(someList); for (int i = 0; i < copy.size(); i++) System.out.println(copy.get(i)); I expected that copy will be, but iterating of copy produce follow error Exception in thread "main" java

How To Modify Constant Pool Using ASM?

时光总嘲笑我的痴心妄想 提交于 2019-12-11 04:24:13
问题 I already understand how to manipulate a class on runtime using ASM from this post. But I have further question regarding how to modify the constant pool. Below is a sample java program that I want modify Main jar file: public class test { private static final String a = "Hello World"; private static final String b = "ASM is awasome"; public static void main(String[] args) { int x = 10; int y = 25; int z = x * y; System.out.println(a); System.out.println(z); System.out.println(b); } } I want

How to use an anonymous class instance in another generate bytecode class

梦想的初衷 提交于 2019-12-11 03:58:56
问题 I have difficulty in using a generated bytecode class which is loaded by Unsafe.defineAnonymousClass() . I am wondering how to use an object of anonymous class to initiliaze another class (or anonymous class). Take an example class Callee below for example, its constructor accepts Callee2 as parameter. Class Callee{ Callee2 _call2; public Callee(Callee2 callee2){ ... } } During runtime, I generated new classes for both Callee2 and Callee, and both new classes are loaded by Unsafe