Reading a Java bytecode instruction: What does the

2019-02-16 20:27发布

I was reading java bytecode and saw this:

getfield #5 (Field java.lang.String name)

What does #5 mean?

And how can I write a program in bytecode?

标签: java bytecode
3条回答
成全新的幸福
2楼-- · 2019-02-16 20:49

The getfield instruction (IIRC) makes a reference into the class file's constant pool for information about what field should be looked up. The #5 here means "constant pool entry number 5," and this constant pool then contains information saying "look up the field name of type java.lang.String). The reason for this is that it keeps the size of the getfield instruction the same, regardless of the name or type of the field to look up.

I'm not sure I understand what you mean by "how can I write program in bytecode?" This is a pretty open-ended question; it's akin to asking how to write programs in any language, and requires a lot of learning. You may want to look into the Jasmin Java assembler, which can greatly simplify this process.

Hope this helps!

查看更多
小情绪 Triste *
3楼-- · 2019-02-16 20:57

it's constant pool index, constant pool stores all the info of class file, JVM instruction use index to reference class info like fields, methods.

I will use hello world example to show how it works:

source:

System.out.println("hello world");

bytecode

 0: getstatic     #2                  // Field java/lang/System.out:Ljava/io/PrintStream;
 3: ldc           #3                  // String hello world
 5: invokevirtual #4                  // Method java/io/PrintStream.println:(Ljava/lang/String;)V
 8: return

bytecode instruction is formatted as instruction constant pool index

  Constant pool:

   #4 = Methodref          #28.#29        // java/io/PrintStream.println:(Ljava/lang/String;)V
  #28 = Class              #36            // java/io/PrintStream
  #29 = NameAndType        #37:#38        // println:(Ljava/lang/String;)V
  #36 = Utf8               java/io/PrintStream
  #37 = Utf8               println
  #38 = Utf8               (Ljava/lang/String;)V

so this instruction will call a method with index 4

invokevirtual #4

and index 4 is a Methodref of // java/io/PrintStream.println:(Ljava/lang/String;)V

if we follow the reference in the constant pool, we will find all the information is stored in string and composted to complex type like method, filed, class.

查看更多
做个烂人
4楼-- · 2019-02-16 21:03

Java class files and bytecode

Java class files (bytecode-files) is composed by different components:

http://en.wikipedia.org/wiki/Java_class_file

  • Magic Number: 0xCAFEBABE
  • Version of Class File Format: the minor and major versions of the class file
  • Constant Pool: Pool of constants for the class
  • (...)
  • Fields: Any fields in the class
  • Methods: Any methods in the class
  • Attributes: Any attributes of the class (for example the name of the sourcefile, etc.)

The number #5 simply refers to a location in the constant pool. And in that position a CONSTANT_FieldRef is found which contains a reference to a CONSTANT_NameAndType among other attributes. And CONSTANT_NameAndType contains a reference to a CONSTANT_Utf8 (which contains the actual string/name.)

So the flow looks like this:

getfield #number -> FieldRef -> NameAndType -> Utf8 -> string

http://java.sun.com/docs/books/jvms/second_edition/html/ClassFile.doc.html

So instead of saving a whole string in each getfield instruction a number is saved. This improves performance in the interpreter (or JIT) and space in the class file.

Hand-write bytecodes

Hand-written bytecodes can be assembled to a class file with this tool (it contains a lot of examples):

http://jasmin.sourceforge.net/

查看更多
登录 后发表回答