How can I measure number of bytecode instructions in class file?

丶灬走出姿态 提交于 2019-12-07 15:26:24

问题


I have calculated the average cyclomatic complexity for each of my class files. I was wondering how I could measure the number of bytecode instructions in each of my class files using Eclipse perhaps?


回答1:


I don't know if you can do that easily from Eclipse, but you can use javap -c to get a disassembly of your class files. Shouldn't be too hard to wrap that in a script if you only care about the number of instructions per method.

Example:

 $ javap -c Test
Compiled from "Test.java"
public class Test extends java.lang.Object{
public Test();
  Code:
   0:   aload_0
   1:   invokespecial   #1; //Method java/lang/Object."<init>":()V
   4:   return

public static void main(java.lang.String[]);
  Code:
   0:   getstatic   #2; //Field java/lang/System.out:Ljava/io/PrintStream;
   3:   ldc #3; //String Hello
   5:   invokevirtual   #4; //Method java/io/PrintStream.println:(Ljava/lang/String;)V
   8:   return

}



回答2:


Use javap as mentioned by Mat, ore use Commons BCEL to access the byte code programmatically.



来源:https://stackoverflow.com/questions/7132171/how-can-i-measure-number-of-bytecode-instructions-in-class-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!