bytecode

Java Bytecode DUP

允我心安 提交于 2019-12-05 20:22:37
问题 I am wondering why the Exception in the following bytecode (used to throw an Exception) is duplicated. NEW java/lang/IllegalArgumentException DUP INVOKESPECIAL java/lang/IllegalArgumentException <init> ()V ATHROW 回答1: I'll analyze this line by line where [] = new stack after that op is used: NEW puts a new IllegalArgumentException onto the stack [SomeIllegalArgumentException] DUP duplicates it [SomeIllegalArgumentException, SomeIllegalArgumentException] INVOKESPECIAL pops off the top one and

How in Java to assign to fields with Byte Buddy?

烂漫一生 提交于 2019-12-05 18:42:00
I'm having difficulty understanding the documentation for Byte Buddy . To help me learn the API I would like to generate the byte code equivalent of this Java: public final class GeneratedByByteBuddy { private final int a; public GeneratedByByteBuddy(final int a) { this.a = a; } } I've had difficulty working out the right way to use Instrumentation to create the field assignment. You are creating a class with customized byte code. For this, you cannot use a built-in Instrumentation but you need to write your own instrumentation which creates the particular byte code for your constructor. This

Differences in java bytecode produced by Oracle's and Eclipse's compilers

对着背影说爱祢 提交于 2019-12-05 16:48:16
问题 Our project does some Java bytecode instrumentation. And we stumbled upon some strange behavior. Suppose the following code snippet: public void a() { new Integer(2); } Oracle's javac compiles the above into the following bytecode: 0: new #2; //class java/lang/Integer 3: dup 4: iconst_2 5: invokespecial #3; //Method java/lang/Integer."<init>":(I)V 8: pop 9: return and Eclipse's compiler into: 0: new #15; //class java/lang/Integer 3: iconst_2 4: invokespecial #17; //Method java/lang/Integer."

How do I replace a class with new one with Java instrumentation?

倖福魔咒の 提交于 2019-12-05 15:46:35
I need to create a java agent that when is enabled it gets the path to a jar file as argument and then it replaces any loaded class the the one inside the jar file if their names are matched. For example, we have an application with a class called com.something.ClassTest. Now if the mentioned jar (is not in the class path) has a class exactly the same name as com.something.ClassTest, I want to replace it with the one in the jar. I have this class transformer but not sure if that's correct or not. I get IOException with message Class not found. @Override public byte[] transform(ClassLoader

invokestatic on static method in interface

空扰寡人 提交于 2019-12-05 10:33:32
Disassembling some Java 8 code I found out that some invokestatic calls on static methods in interface (particularly this was java.util.function.Function.identity() ) uses InterfaceMethodRef in const pool; this is what javap -s -c -v p show me: 15: invokestatic #66 // InterfaceMethod java/util/function/Function.identity:()Ljava/util/function/Function; According to JVM 8 spec this is not possible, and when I've used this instruction in classfile with version Java 7 ( major version=51 ), it has thrown VerifyError on this instruction. However, when I've changed the major version to 52 , it

java bytecode - representation of types smaller than int

孤者浪人 提交于 2019-12-05 10:25:39
问题 In one of the projects at my university I am working directly with Java bytecode. After browsing the list of instructions available for the JVM (http://en.wikipedia.org/wiki/Java_bytecode_instruction_listings) I saw that there is no such thing as [b|c|s]store, only istore for storing integers in a local variable. Does it mean that if in my program I write: short a; int b; I am not saving any memory, because every local variable entry occupies 4 bytes? I was always under the impression that

Bytecode changes over time in undocumented manner

ε祈祈猫儿з 提交于 2019-12-05 08:35:12
Today I was exploring classes of huge applications (like jboss server with apps) with javaagent and instrumentation on my openjdk 7. I called retransform on all classes every 10 seconds, so their bytecode got in my ClassFileTransformer implementation. My implementation simply keeps track of how bytecode of classes changes over time. First of all, I was surprised, that order of fields and methods, method access modifiers, contents of constant pool and other such things vary from one check to the other. But, still, it is documented . What is not documented - that some items may be created in

Are all the “Magic” methods on the JVM marked as Native?

别来无恙 提交于 2019-12-05 08:01:55
By "Magic" I mean the methods which have semantics which are not expressed in pure Java. I know all native methods are magic, in that their implementation is provided by the underlying runtime and not by Java bytecodes. Is the reverse true? Are all magic methods native , or are there some magic methods apparently implemented in pure Java, but with some extra help from some JVM-special-casing? The use case is that I want to modify the semantics of Java by instrumenting its bytecodes. All these magic methods are special cases which I will have to handle some way or another. The native ones are

Number of instructions in JVM

[亡魂溺海] 提交于 2019-12-05 06:49:25
问题 I was asked the following question in an exam today. I still don't know the answer. Java uses stack for byte code in JVM. Each instruction is of one byte, so how many such instructions (per byte code) are possible in an operating system. All I know is that the stack is 32 bits wide. Can anybody help me (I am a beginner in JVM)? 回答1: The expected answer was almost certainly 256, because there are 256 possible values of a byte. This of course has nothing to do with the actual JVM instruction

Where does Scala store information that cannot be represented in Java?

放肆的年华 提交于 2019-12-05 06:36:28
There are some constructs that don't have equivalents in java. Examples would be named parameters instance private members Where/How does Scala store the information necessary for this stuff (some kind of flag in the first case, the parameter names in the second case? If I get it right this has to get stored in the byte code, since it works even if I just have a compiled library without the source code!? Travis Brown This information is captured in an annotation named ScalaSig in the class file (see this answer for an example). You can view the (not very human-friendly) annotation with javap