bytecode

Where does bytecode injection happen?

家住魔仙堡 提交于 2019-12-03 21:12:30
Motivation I have a SomeObject.java file: class SomeObject { String name; } Compiling it creates a bytecode-containing SomeObject.class file. 0xCAFEBABE... If we use SomeObject on the JVM, it is loaded by the current classloader and all works fine. Now let's assume that I'd like to have some dynamic code generation. I can write my custom annotation @Target(ElementType.TYPE) public @interface Data { ... } and add it as a modifier to the class declaration: @Data class SomeObject { String name; } I can also retain it for the runtime with @Retention(RetentionPolicy.RUNTIME) : @Retention

Stand-alone Bytecode Verifier

橙三吉。 提交于 2019-12-03 18:00:03
问题 In my bytecode instrumentation project, I stumble frequently on VerifyErrors. However, the default java Verifier gives little information on which instruction resulted in the error (it only gives the method and a small message). Is there any stand-alone bytecode verifier which provides with a little more advanced help in locating the error, at least the precise instruction location? Thank you. 回答1: As with any project involving JVM bytecode, I would first check to see whether the BCEL has

Generate .pyc from Python AST?

前提是你 提交于 2019-12-03 17:30:20
问题 How would I generate a .pyc file from a Python AST such that I could import the file from Python? I've used compile to create a code object, then written the co_code attribute to a file, but when I try to import the file from Python, I get an ImportError: Bad magic number in output.pyc . 回答1: The solution can be adapted from the py_compile module: import marshal import py_compile import time import ast codeobject = compile(ast.parse('print "Hello World"'), '<string>', 'exec') with open(

native java bytecode instrumentation

孤者浪人 提交于 2019-12-03 16:36:53
for bytecode instrumentation in java, there is the asm framework and the bcel and javaassist libraries. However I need to do instrumentation in native code, since some java classes are already loaded by the time the javaagent runs, eg java.lang.Thread, java.lang.Class, etc is there any library for instrumenting java classes in native code? Edit: Seems there is a bit of confusion. What I want is: Create a native java agent, which uses JVMTI apis to change the bytecode of a class while its being loaded, using the OnClassLoad event hook. I encountered this problem during my doctoral research. The

How can a JVM decide if a class “belongs” (e.g. inner or nested classes) to another class?

与世无争的帅哥 提交于 2019-12-03 16:05:34
I want to understand class files and inner/nested classes a bit better and I'm wondering about the following things: Is the InnerClasses attribute used to refer tothe inner/nested classes in the ´containing´ class or is it used in the inner/nested classes to refer to the ‘container’ class? Is the InnerClasses attribute in class files sufficient? E.g. Do inner/nested classes have to follow the name mangling with $ or is this just a convention? Is there a way to make a class look like an inner/nested class to the JVM without setting the InnerClasses attribute and does this depend on the JLM

How does Google App Engine precompile Java?

徘徊边缘 提交于 2019-12-03 15:34:31
App Engine uses a "precompilation" process with the Java bytecode of an app to enhance the performance of the app in the Java runtime environment. Precompiled code functions identically to the original bytecode. Is there any detailed information what this does? I found this in a googlegroups message : Yes, pre-compilation reduces the time to load an application. This will benefit you on your first request after a deploy, after you've been cycled out or if more application instances are created to scale up with your load. You will see up to 30% improved loading time on your first request. Pre

Interpret something, and run the generated bytecode in Java?

佐手、 提交于 2019-12-03 14:46:47
I'm writing a toy interpreter with a REPL in Java. I'd like to generate bytecode from the language and run that, instead of interpreting an AST and running that instead. Since my Java is a bit rusty, is it possible to run generated bytecode on the fly on the JVM? You can use java.lang.Classloader.defineClass(), which turns bytecode into a Class object. You can the call newInstance() on the resulting Class object, and off you go. Have a look at Javassist which contains a snippet compiler allowing you to compile Java snippets to bytecode and define them as a method in a class which you can then

Inject a function into a Java .class file using Haskell

你说的曾经没有我的故事 提交于 2019-12-03 14:37:52
I have written a Java bytecode parser using Haskell, and it works just fine. However the next step has me completely stumped. My Haskell program needs to modify the .class file so that, when executed, the Java program prints: " Entering [method name here] " before executing a method, and " Exiting [method name here] " after executing a method. All I know is that we will need to append the constant pool and method table with calls to System.out.println , but I feel I'm still missing something. What is the best way to approach this problem? How do you know how to call System.out.println in

Difference between byte code .<init>()V vs .<init>(Z)V

六眼飞鱼酱① 提交于 2019-12-03 14:06:08
When I observe my Java project byte code, I see the following byte code : java.lang.Object.()V java.lang.Boolean.(Z)V What is the meaning of <init>()V and <init>(Z)V java.lang.Object.()V is a void method ( V ) on java.lang.Object that takes no arguments. java.lang.Boolean.(Z)V is a void method on java.lang.Boolean that takes a single boolean ( Z since B is byte ) argument. In short, abc.def.WXYZ(IIIIIIIIIIIIII)J ^ ^ ^ target_class argument-types return_type See JNI Type Signatures for more detail. The JNI uses the Java VM’s representation of type signatures. Table 3-2 shows these type

Intercepting calls to Java 8 lambda-expressions using Byte Buddy

时光总嘲笑我的痴心妄想 提交于 2019-12-03 13:31:15
问题 I try to intercept calls to methods and calls to Java 8 lambda expressions using a Byte Buddy AgentBuilder as follows: static { final Instrumentation inst = ByteBuddyAgent.install(); new AgentBuilder.Default() .type(ElementMatchers.nameContainsIgnoreCase("foo")) .transform((builder, typeDescription) -> builder.method(ElementMatchers.any()) .intercept(MethodDelegation.to(LogInterceptor.class))) .installOn(inst); } public static class LogInterceptor { @RuntimeType public static Object log(