In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
This article shows you how to call bytecode in Java. The content is concise and easy to understand. It will definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.
Instance source code
There are two java files in the instance, one is the interface and the other is the class. Look at the interface source code first. It is very simple that there is only one method declaration:
Package com.bolingcavalry;public interface Action {void doAction ();}
The next class implements this interface and has its own public, private, static methods:
Package com.bolingcavalry;public class Test001 implements Action {private int add (int a, int b) {return astatb;} public String getValue (int a, int b) {return String.valueOf (add (Azov));} public static void output (String str) {System.out.println (str);} @ Override public void doAction () {System.out.println ("123") } public static void main (String [] args) {Test001 t = new Test001 (); Action a = t; String str = t.getValue (1Magne2); t.output (str); t.doAction (); a.doAction ();} public void createThread () {Runnable r = ()-> System.out.println ("123");}}
To summarize, the main methods in Test001's code are as follows:
A private method add
A public method getValue in which the add method is called
A static method output
DoAction that implements the interface definition
A public method that uses lambda expressions
In the main method, create an object and call getValue,output,doAction
Next, we use the javac command or ide tool to get the Action.class and Test001.class files. If you are using Intellij IDEA, you can run Test001 first, and then find the out folder in the project directory. After opening it, there is a production folder inside, and then you can find the corresponding package and class files, as shown below:
Open the command line, execute javap-c Test001.class in the Test001.class directory, and you can disassemble the class file. The result is as follows:
Compiled from "Test001.java" public class com.bolingcavalry.Test001 implements com.bolingcavalry.Action {public com.bolingcavalry.Test001 (); Code: 0: aload_0 1: invokespecial # 1 / / Method java/lang/Object. "": () V 4: return public java.lang.String getValue (int, int) Code: 0: aload_0 1: iload_1 2: iload_2 3: invokespecial # 2 / / Method add: (II) I 6: invokestatic # 3 / / Method java/lang/String.valueOf: (I) Ljava/lang/String; 9: areturn public static void output (java.lang.String) Code: 0: getstatic # 4 / / Field java/lang/System.out:Ljava/io/PrintStream; 3: aload_0 4: invokevirtual # 5 / / Method java/io/PrintStream.println: (Ljava/lang/String;) V 7: return public void doAction () Code: 0: getstatic # 4 / / Field java/lang/System.out:Ljava/io/PrintStream; 3: ldc # 6 / / String 1235: invokevirtual # 5 / / Method java/io/PrintStream.println: (Ljava/lang/String;) V 8: return public static void main (java.lang.String []) Code: 0: new # 7 / / class com/bolingcavalry/Test001 3: dup 4: invokespecial # 8 / / Method "": () V 7: astore_1 8: aload_1 9: astore_2 10: aload_1 11: iconst_1 12: iconst_2 13: invokevirtual # 9 / / Method getValue: (II) Ljava/lang/String 16: astore_3 17: aload_1 18: pop 19: aload_3 20: invokestatic # 10 / / Method output: (Ljava/lang/String ) V 23: aload_1 24: invokevirtual # 11 / / Method doAction: () V 27: aload_2 28: invokeinterface # 12, 1 / / InterfaceMethod com/bolingcavalry/Action.doAction: () V 33: returnpublic void createThread (); Code: 0: invokedynamic # 13, 0 / / InvokeDynamic # 0:run: () Ljava/lang/Runnable; 5: astore_1 6: return}
Now we can compare the disassembly results to learn the use of bytecode:
Invokespecial: call the private instance method
The private instance method add (int a, int b) is called in the getValue () method. The decompilation results are shown below. Notice the line numbered 3:
Public java.lang.String getValue (int, int); Code: 0: aload_0 1: iload_1 2: iload_2 3: invokespecial # 2 / / Method add: (II) I 6: invokestatic # 3 / / Method java/lang/String.valueOf: (I) Ljava/lang/String; 9: areturn
It can be seen that the invocation of the private instance method is realized through the invokespecial instruction
Invokestatic: calling static methods
In the getValue () method, the static method String.valueOf () is called, and the decompilation result is shown below. Notice the line numbered 6:
Public java.lang.String getValue (int, int); Code: 0: aload_0 1: iload_1 2: iload_2 3: invokespecial # 2 / / Method add: (II) I 6: invokestatic # 3 / / Method java/lang/String.valueOf: (I) Ljava/lang/String; 9: areturn
It can be seen that the call of static method is realized by invokestatic instruction.
Invokevirtual: call instance method
In the main () method, the t.getValue (1Jue 2) method is called, and the decompilation results are shown below. Notice the line numbered 13:
Public static void main (java.lang.String []) Code: 0: new # 7 / / class com/bolingcavalry/Test001 3: dup 4: invokespecial # 8 / / Method "": () V 7: astore_1 8: aload_1 9: astore_2 10: aload_1 11: iconst_1 12: iconst_2 13: invokevirtual # 9 / / Method getValue: (II) Ljava/lang/String 16: astore_3 17: aload_1 18: pop 19: aload_3 20: invokestatic # 10 / / Method output: (Ljava/lang/String;) V 23: aload_1 24: invokevirtual # 11 / / Method doAction: () V 27: aload_2 28: invokeinterface # 12, 1 / / InterfaceMethod com/bolingcavalry/Action.doAction: () V 33: return}
It can be seen that when calling the method of an instance, it is realized through the invokevirtual instruction.
Invokeinterface: call interface method
In the main () method, we declare the interface Action a, and then call a.doAction (). The decompilation results are as follows. Notice the line numbered 28:
Public static void main (java.lang.String []) Code: 0: new # 7 / / class com/bolingcavalry/Test001 3: dup 4: invokespecial # 8 / / Method "": () V 7: astore_1 8: aload_1 9: astore_2 10: aload_1 11: iconst_1 12: iconst_2 13: invokevirtual # 9 / / Method getValue: (II) Ljava/lang/String 16: astore_3 17: aload_1 18: pop 19: aload_3 20: invokestatic # 10 / / Method output: (Ljava/lang/String;) V 23: aload_1 24: invokevirtual # 11 / / Method doAction: () V 27: aload_2 28: invokeinterface # 12, 1 / / InterfaceMethod com/bolingcavalry/Action.doAction: () V 33: return}
It can be seen that the method of calling an interface is realized through the invokeinterface instruction; in fact, both t.doAction () and a.doAction () end up calling the doAction of the instance of Test001, but the declaration of t is the class and the declaration of an is the interface, so the two call instructions are different.
Invokedynamic: calling dynamic methods
In the main () method, we declare a lambda ()-> System.out.println ("123"), and the result of decompilation is as follows:
0: invokedynamic # 13, 0 / / InvokeDynamic # 0:run: () Ljava/lang/Runnable; 5: astore_1 6: return
It can be seen that the lambda expression actually corresponds to an invokedynamic call. For the specific content of the call, you can use Bytecode viewer as a tool to open Test001.class and study it. Since the Operand of invokedynamic after decompilation is # 13, let's go to the constant pool to see the corresponding content of 13:
It is a Name and type and Bootstrap method, and then take a closer look at the operands of Bootstrap method, as shown below:
Is a reference to the MethodHandler that points to the lambda method implemented by the user
The above is how to call bytecode in Java. Have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.