1
https://blog.csdn.net/weixin_39965161/article/details/112650387
2
package agent; /** * Created by mac on 2021/4/21. */ import jdk.internal.org.objectweb.asm.ClassReader; import jdk.internal.org.objectweb.asm.util.TraceClassVisitor; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; public class Main { public static void main(String []f)throws IOException { //getDouble(); InputStream is = Main.class.getResourceAsStream("Main.class"); ClassReader cr =new ClassReader(is); cr.accept(new TraceClassVisitor(new PrintWriter(System.out)),0); } private static Double getDouble() { Double test =null; System.out.println(test !=null); /** * L4 * LINENUMBER 21 L4 * ALOAD 0 * IFNULL L5 * ALOAD 0 * INVOKEVIRTUAL java/lang/Double.doubleValue ()D // test拆箱應對Math.abs(double) * INVOKESTATIC java/lang/Math.abs (D)D // 執行Math.abs * GOTO L6 * L5 * FRAME SAME * ALOAD 0 * INVOKEVIRTUAL java/lang/Double.doubleValue ()D // test拆箱應對三目運算符返回類型double */ return test !=null ? Math.abs(test) : test; /** * L6 * FRAME SAME1 D * INVOKESTATIC java/lang/Double.valueOf (D)Ljava/lang/Double; // 對三目double的返回值裝箱,應對方法返回類型Double * ARETURN * L7 * LOCALVARIABLE test Ljava/lang/Double; L1 L7 0 // 方法返回 * MAXSTACK = 2 * MAXLOCALS = 1 */ } private static Double getDouble2() { Double test =null; System.out.println(test !=null); /** * L4 * LINENUMBER 27 L4 * ALOAD 0 * IFNULL L5 * ALOAD 0 * INVOKEVIRTUAL java/lang/Double.doubleValue ()D // test拆箱應對Math.abs(double) * INVOKESTATIC java/lang/Math.abs (D)D // 執行Math.abs * INVOKESTATIC java/lang/Double.valueOf (D)Ljava/lang/Double; // Math.abs的返回值double裝箱,應對三目運算符返回類型Double * GOTO L6 * L5 * FRAME SAME * ALOAD 0 */ return test !=null ? (Double)Math.abs(test) : test; /** * L6 * FRAME SAME1 java/lang/Double // 不再對三目double的返回值裝箱,因爲三目返回類型為Double,與方法返回類型一致 * ARETURN * L7 * LOCALVARIABLE test Ljava/lang/Double; L1 L7 0 // 方法返回 * MAXSTACK = 2 * MAXLOCALS = 1 */ } }