Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does getstatic really mean in bytecode?

Tags:

java

bytecode

I have this byte code:

new                 java.lang.Object
// stack is [newObjectRef]
dup 
// Stack is [newObjectRef newObjectRef]
invokespecial       void java.lang.Object.<init>()
// Stack is [initializedAsTypeObjectObjectRef]
putstatic           java.lang.Object class.a
// variable a has the reference of new object
getstatic           java.io.PrintStream java.lang.System.out
// Take the static value of System.out
// Stack is [initializedAsTypeObjectObjectRef System.out]

Update this is the continuation:

> ldc                 "test" // Stack is
> [initializedAsTypeObjectObjectRef System.out "test"]
> jsr                  pos.0000026C // call a subrutine invokevirtual       void
> java.io.PrintStream.println(java.lang.String) // actually print the
> result // stack is (I think) Empty at this time ?

Does the translation is:

  Object a = new Object();
  a = "test";
  System.out.print(a);

Is my stack good ?

I am not sure to well understand out(). Probably I will have to use out() setter and to print() after ?

I always use out() to print habitually..

like image 619
Pier-Alexandre Bouchard Avatar asked Dec 17 '11 16:12

Pier-Alexandre Bouchard


People also ask

How is bytecode interpreted?

A bytecode program may be executed by parsing and directly executing the instructions, one at a time. This kind of bytecode interpreter is very portable. Some systems, called dynamic translators, or just-in-time (JIT) compilers, translate bytecode into machine code as necessary at runtime.

What is a bytecode format?

The bytecode format Bytecodes are the machine language of the Java virtual machine. When a JVM loads a class file, it gets one stream of bytecodes for each method in the class. The bytecodes streams are stored in the method area of the JVM.

How does bytecode look like?

class files consist of a bunch of bytecodes. Bytecode is to Java what assembler is to C++. Each bytecode is a number no larger than a byte and has a mnemonic. The numbers and their mnemonic are what you have listed in your question.

What is bytecode manipulation?

Bytecode is the instruction set of the Java Virtual Machine (JVM), and all languages that run on the JVM must eventually compile down to bytecode. Bytecode is manipulated for a variety of reasons: Program analysis: find bugs in your application.


1 Answers

If I compile the code

public static void main(String[] args) {
    Object a;
    a = "test";
    System.out.println(a);
}

and run

javap -c Main

I see

public static void main(java.lang.String[]);
Code:
   0: ldc           #2                  // String test
   2: astore_1      
   3: getstatic     #3                  // Field java/lang/System.out:Ljava/io/PrintStream;
   6: aload_1       
   7: invokevirtual #4                  // Method java/io/PrintStream.println:(Ljava/lang/Object;)V
  10: return    

You can see that getstatic loads the field System.out


Object doesn't have a method called out() so I don't believe you are looking at the code you believe you are.

getstatic gets a static fields e.g. System.out is a static field of System so if you write

System.out.println();

This will result in a use of getstatic

like image 51
Peter Lawrey Avatar answered Oct 16 '22 11:10

Peter Lawrey