you don't call your agent explicitly, you should specify additional argument to your JVM : java -javaagent:jarpath[=options] where jarpath is a path to jar containing your agent. JVM will invoke premain method before main method of java program. And transform method will be called before classloading by JVM (you don't...

For java.util.prefs.Preferences, you are out of luck. This class cannot be instrumented unless you change the classes that are put onto the bootstrap class path. And even if, note that is considered a breach of the JVM's user license agreement to ship an installation with a changed bootstrap class path....

You are inserting code that requires more stack space than the original one. So you have to either update the max stack declaration manually or tell ASM to calculate the required stack space automatically. Replace your construction new ClassWriter(0) with new ClassWriter(ClassWriter.COMPUTE_MAXS). The flag ClassWriter.COMPUTE_MAXS will tell ASM to (re)calculate the...

I think that Sun engineers chose this approach because of its simplicity. It is trivial to generate servlet java source file from jsp. And then they just use standard compiler. They do not care about low level compiler stuff, they just reuse existing components. Classical KISS - keep it simple,...

Yes, it is possible, with a few minor restrictions. The main restriction is that a single method's bytecode is limited to 65535 bytes, but you're unlikely to run into this restriction in practice. The number of exception handlers, local variable slots, and operand stack size in a single method are...

As far as I know, this is not possible on Android. Dexmaker creates dex files that contain new classes. These classes are then added to an application by using dex class loaders. Such dex files can however not be used to replace classes, only to add new subclasses that serve...

Removing the athrow instruction removes an edge in the control-flow graph, so the verifier now must prove the code is safe if execution falls out of the block formerly containing the athrow. In this case, it cannot ensure a local has been initialized to an int for the iinc instruction...

PEP330 Python Bytecode Verification Python-Bytecode-Verifier 0.1 Both outdated, the first one without code (at least I can't find) but may be useful to give an idea of what/how can be done and what are the limitations. perfectly valid bytecode can still do horrible things ...

You can’t omit the call to visitMax. From the documentation of ClassWriter.COMPUTE_MAXS: If this flag is set, then the arguments of the visitMaxs method of the MethodVisitor returned by the visitMethod method will be ignored, and computed automatically from the signature and the bytecode of each method. In other words,...

As you can read in section 4.7 from Javassist tutorial one of the Javassist's limitations is not supporting the new syntax introduced at J2SE 5.0 (including enums and generics). For future reference, at the concurrent time, javassist is in version 3.20, this information might change in future releases. This limitation...

Start is the start bytecode offset where this variable is visible. Length is number of bytecode bytes during which this variable is visible. Usually start points to the bytecode instruction where the variable is first assigned or to 0 for method parameters and this. In your case it seems that...

You generated your constructor using "java.lang.invoke.MethodHandle" as the field signature rather than the correct "Ljava/lang/invoke/MethodHandle;". This can be seen in the javap output by the lines: 5: putfield #13 // Field guard:java.lang.invoke.MethodHandle … 13: putfield #15 // Field trueTarget:java.lang.invoke.MethodHandle … 21: putfield #17 // Field falseTarget:java.lang.invoke.MethodHandle whereas the Hello method...

As said by others, there is no way of the invoked method getting hands on the caller object, but so far, nobody pointed you to the reason why this will never be possible: The big misconception about your request is that you assume that there has to be a “caller...

Using reflection, you can implement this quite easily: class EasyFieldAlterationAgent { public static void premain(String args) throws Exception { Field field = X.class.getDeclaredField("timeout"); field.setAccessible(true); field.setValue(null, 42L); // set your value here. } } Note that this will change the field not before but right after class loading time. If this...

There are two approaches to this: Track the instruction(s) that placed certain values into the stack (e.g. using SourceInterpreter from ASM bytecode manipulation framework) Access values on the stack at the point those values are used (e.g. right before INVOKEVIRTUAL for defineClass method) Second approach is easier to implement, because...

You have to retrace what a Java compiler does when compiling try … finally … which implies copying your finally action to every point where the protected (source) code block will be left (i.e. return instruction) and install multiple protected (resulting byte code) regions (as they shouldn’t cover your finally...

LLVM already contains a bunch of back-ends for various popular architectures, including x86. So in theory if you want to create a new language you can just write an LLVM front-end for that language and you're done - you got a compiler for numerous architectures. In practice just writing a...

Before the invocation of calculate, there is one int value on the stack which will be used after the invocation. Method invocations which complete normally will only consume the appropriate parameter values and leave all other operand stack values untouched, regardless of what happens inside the invoked method. If the...