<legend id='7Oil0'><style id='7Oil0'><dir id='7Oil0'><q id='7Oil0'></q></dir></style></legend>
    <bdo id='7Oil0'></bdo><ul id='7Oil0'></ul>

  • <i id='7Oil0'><tr id='7Oil0'><dt id='7Oil0'><q id='7Oil0'><span id='7Oil0'><b id='7Oil0'><form id='7Oil0'><ins id='7Oil0'></ins><ul id='7Oil0'></ul><sub id='7Oil0'></sub></form><legend id='7Oil0'></legend><bdo id='7Oil0'><pre id='7Oil0'><center id='7Oil0'></center></pre></bdo></b><th id='7Oil0'></th></span></q></dt></tr></i><div id='7Oil0'><tfoot id='7Oil0'></tfoot><dl id='7Oil0'><fieldset id='7Oil0'></fieldset></dl></div>

    <small id='7Oil0'></small><noframes id='7Oil0'>

        <tfoot id='7Oil0'></tfoot>

        ASM 中的 Java 方法参数值

        Java method parameters values in ASM(ASM 中的 Java 方法参数值)

        • <tfoot id='zxbl9'></tfoot>
            • <bdo id='zxbl9'></bdo><ul id='zxbl9'></ul>
                <tbody id='zxbl9'></tbody>

                <small id='zxbl9'></small><noframes id='zxbl9'>

                  <i id='zxbl9'><tr id='zxbl9'><dt id='zxbl9'><q id='zxbl9'><span id='zxbl9'><b id='zxbl9'><form id='zxbl9'><ins id='zxbl9'></ins><ul id='zxbl9'></ul><sub id='zxbl9'></sub></form><legend id='zxbl9'></legend><bdo id='zxbl9'><pre id='zxbl9'><center id='zxbl9'></center></pre></bdo></b><th id='zxbl9'></th></span></q></dt></tr></i><div id='zxbl9'><tfoot id='zxbl9'></tfoot><dl id='zxbl9'><fieldset id='zxbl9'></fieldset></dl></div>
                  <legend id='zxbl9'><style id='zxbl9'><dir id='zxbl9'><q id='zxbl9'></q></dir></style></legend>
                  本文介绍了ASM 中的 Java 方法参数值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在尝试获取 Java 程序方法参数的值.我正在使用 ASM 来检测字节码并获取这些值.但是,我遇到了一些麻烦.

                  I am trying to get the values of a Java program's method's parameters. I am using ASM to instrument the bytecode and getting these values. However, I'm running into some troubles.

                  这是用于检测代码的 visitCode() 方法.它正在做的是:

                  Here is the visitCode() method used to instrument the code. What it is doing is :

                  1. 创建一个空数组来存储收集到的参数.
                  2. 对于每个参数,将其值加载到数组中.
                  3. 将此数组发送到我的代理的 OnMethodEntry 方法(将在其中使用值).

                  .

                  @Override
                  public void visitCode() {
                      int paramLength = paramTypes.length;
                  
                      // Create array with length equal to number of parameters
                      mv.visitIntInsn(Opcodes.BIPUSH, paramLength);
                      mv.visitTypeInsn(Opcodes.ANEWARRAY, "java/lang/Object");
                      mv.visitVarInsn(Opcodes.ASTORE, paramLength);
                  
                      // Fill the created array with method parameters
                      int i = 0;
                      for (Type tp : paramTypes) {
                          mv.visitVarInsn(Opcodes.ALOAD, paramLength);
                          mv.visitIntInsn(Opcodes.BIPUSH, i);
                  
                          if (tp.equals(Type.BOOLEAN_TYPE) || tp.equals(Type.BYTE_TYPE) || tp.equals(Type.CHAR_TYPE) || tp.equals(Type.SHORT_TYPE) || tp.equals(Type.INT_TYPE))
                              mv.visitVarInsn(Opcodes.ILOAD, i);
                          else if (tp.equals(Type.LONG_TYPE)) {
                              mv.visitVarInsn(Opcodes.LLOAD, i);
                              i++;
                          }
                          else if (tp.equals(Type.FLOAT_TYPE))
                              mv.visitVarInsn(Opcodes.FLOAD, i);
                          else if (tp.equals(Type.DOUBLE_TYPE)) {
                              mv.visitVarInsn(Opcodes.DLOAD, i);
                              i++;
                          }
                          else
                              mv.visitVarInsn(Opcodes.ALOAD, i);
                  
                          mv.visitInsn(Opcodes.AASTORE);
                          i++;
                      }
                  
                      // Load id, class name and method name
                      this.visitLdcInsn(new Integer(this.methodID));
                      this.visitLdcInsn(this.className);
                      this.visitLdcInsn(this.methodName);
                  
                      // Load the array of parameters that we created
                      this.visitVarInsn(Opcodes.ALOAD, paramLength);
                  
                      mv.visitMethodInsn(Opcodes.INVOKESTATIC,
                              "jalen/MethodStats",
                              "onMethodEntry",
                              "(ILjava/lang/String;Ljava/lang/String;[Ljava/lang/Object;)V");
                      super.visitCode();
                  }
                  

                  但是,当方法显然有多个参数时,这不起作用.

                  However, this is not working when apparently the method have more than one parameter.

                  获得的类文件显示如下:

                  The class file obtained shows things like this :

                  static void moveDisk(char arg0, char arg1, PrintStream arg2) {
                  Object[] arrayOfObject = new Object[3]; arrayOfObject[0] = ???; arrayOfObject[1] = ???;
                  Object localObject;
                  arrayOfObject[2] = localObject; MethodStats.onMethodEntry(5, "hanoi/TowersOfHanoi", "moveDisk", arrayOfObject);
                  

                  这里创建了 2 个本地对象,而不是加载参数.

                  Where 2 local objects are created instead of loading the parameters.

                  字节码没有显示任何奇怪的东西:

                  The bytecode doesn't show anything weird :

                  static void moveDisk(char, char, java.io.PrintStream);
                  Code:
                     0: bipush        3
                     2: anewarray     #4                  // class java/lang/Object
                     5: astore_3      
                     6: aload_3       
                     7: bipush        0
                     9: iload_0       
                    10: aastore       
                    11: aload_3       
                    12: bipush        1
                    14: iload_1       
                    15: aastore       
                    16: aload_3       
                    17: bipush        2
                    19: aload_2       
                    20: aastore       
                    21: ldc           #118                // int 5
                    23: ldc           #12                 // String hanoi/TowersOfHanoi
                    25: ldc           #119                // String moveDisk
                    27: aload_3       
                    28: invokestatic  #19                 // Method jalen/MethodStats.onMethodEntry:(ILjava/lang/String;Ljava/lang/String;[Ljava/lang/Object;)V
                  

                  最后,显示的错误是(使用 -noverify 时):

                  And finally, the error showed is (when using -noverify):

                  param: [Ljava.lang.String;@420e54f3
                  Exception in thread "Jalen Agent" java.lang.NullPointerException
                  at hanoi.TowersOfHanoi.solveHanoi(TowersOfHanoi.java)
                  at hanoi.TowersOfHanoi.main(TowersOfHanoi.java:29)
                  

                  否则就是:

                  Exception in thread "Jalen Agent" java.lang.VerifyError: (class: hanoi/TowersOfHanoi, method: moveDisk signature: (CCLjava/io/PrintStream;)V) Expecting to find object/array on stack
                      at java.lang.Class.getDeclaredMethods0(Native Method)
                      at java.lang.Class.privateGetDeclaredMethods(Class.java:2442)
                      at java.lang.Class.getMethod0(Class.java:2685)
                      at java.lang.Class.getMethod(Class.java:1620)
                      at sun.launcher.LauncherHelper.getMainMethod(LauncherHelper.java:492)
                      at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:484)
                  

                  通常,这应该更有效,因为我只是从堆栈帧中加载信息.我也尝试检查静态 &非静态方法(此处解释的堆栈:http://www.artima.com/insidejvm/ed2/jvm8.html),但仍然没有成功.

                  Normally, this should rather work as I am just loading information from the stack frame. I tried also to check static & non static methods (as of the stack explained here : http://www.artima.com/insidejvm/ed2/jvm8.html), but still with no success.

                  对为什么会发生这种情况有任何想法,或者可能有解决方案的想法吗?

                  Any idea on why this is happening, or possibly an idea of a solution ?

                  谢谢:)

                  它现在可以在打包原始类型时工作(感谢下面 int3 的建议 :)).这是 visitCode() 方法的工作代码:

                  It is now working when boxing up primitive types (thanks to suggestions by int3 below :) ). Here is the working code of visitCode() method :

                  @Override
                  public void visitCode() {
                      int paramLength = paramTypes.length;
                  
                      // Create array with length equal to number of parameters
                      mv.visitIntInsn(Opcodes.BIPUSH, paramLength);
                      mv.visitTypeInsn(Opcodes.ANEWARRAY, "java/lang/Object");
                      mv.visitVarInsn(Opcodes.ASTORE, paramLength);
                  
                      // Fill the created array with method parameters
                      int i = 0;
                      for (Type tp : paramTypes) {
                          mv.visitVarInsn(Opcodes.ALOAD, paramLength);
                          mv.visitIntInsn(Opcodes.BIPUSH, i);
                  
                          if (tp.equals(Type.BOOLEAN_TYPE)) {
                              mv.visitVarInsn(Opcodes.ILOAD, i);
                              mv.visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/Boolean", "valueOf", "(Z)Ljava/lang/Boolean;");
                          }
                          else if (tp.equals(Type.BYTE_TYPE)) {
                              mv.visitVarInsn(Opcodes.ILOAD, i);
                              mv.visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/Byte", "valueOf", "(B)Ljava/lang/Byte;");
                          }
                          else if (tp.equals(Type.CHAR_TYPE)) {
                              mv.visitVarInsn(Opcodes.ILOAD, i);
                              mv.visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/Character", "valueOf", "(C)Ljava/lang/Character;");
                          }
                          else if (tp.equals(Type.SHORT_TYPE)) {
                              mv.visitVarInsn(Opcodes.ILOAD, i);
                              mv.visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/Short", "valueOf", "(S)Ljava/lang/Short;");
                          }
                          else if (tp.equals(Type.INT_TYPE)) {
                              mv.visitVarInsn(Opcodes.ILOAD, i);
                              mv.visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/Integer", "valueOf", "(I)Ljava/lang/Integer;");
                          }
                          else if (tp.equals(Type.LONG_TYPE)) {
                              mv.visitVarInsn(Opcodes.LLOAD, i);
                              mv.visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/Long", "valueOf", "(J)Ljava/lang/Long;");
                              i++;
                          }
                          else if (tp.equals(Type.FLOAT_TYPE)) {
                              mv.visitVarInsn(Opcodes.FLOAD, i);
                              mv.visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/Float", "valueOf", "(F)Ljava/lang/Float;");
                          }
                          else if (tp.equals(Type.DOUBLE_TYPE)) {
                              mv.visitVarInsn(Opcodes.DLOAD, i);
                              mv.visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/Double", "valueOf", "(D)Ljava/lang/Double;");
                              i++;
                          }
                          else
                              mv.visitVarInsn(Opcodes.ALOAD, i);
                  
                          mv.visitInsn(Opcodes.AASTORE);
                          i++;
                      }
                  
                      // Load id, class name and method name
                      this.visitLdcInsn(new Integer(this.methodID));
                      this.visitLdcInsn(this.className);
                      this.visitLdcInsn(this.methodName);
                  
                      // Load the array of parameters that we created
                      this.visitVarInsn(Opcodes.ALOAD, paramLength);
                  
                      mv.visitMethodInsn(Opcodes.INVOKESTATIC,
                              "jalen/MethodStats",
                              "onMethodEntry",
                              "(ILjava/lang/String;Ljava/lang/String;[Ljava/lang/Object;)V");
                      super.visitCode();
                  }
                  

                  推荐答案

                  您正在使用 aastorechar 存储到对象数组中,这是类型错误.aastore 只能用于存储对象和数组,这可能就是错误提示预期的对象/数组在堆栈上"的原因.字符应使用 castore 存储在 char 数组中.但是,由于您希望它适用于任意签名,您可能希望将原始类型打包成对象,然后您可以在其上使用 aastore - 例如char 应该装在 java.lang.Character 对象中.

                  You are using aastore to store a char into an object array, which is a type error. aastore should only be used to store objects and arrays, which is probably why the error says 'expected object/array on stack'. Characters should be stored in a char array using castore. However, since you want this to work for arbitrary signatures, you'll probably want to box up the primitive types into objects which you can then use aastore upon -- e.g. char should be boxed up in a java.lang.Character object.

                  这篇关于ASM 中的 Java 方法参数值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Bytecode features not available in the Java language(Java 语言中不可用的字节码功能)
                  ClassCastException because of classloaders?(ClassCastException 因为类加载器?)
                  How can I add a Javaagent to a JVM without stopping the JVM?(如何在不停止 JVM 的情况下将 Javaagent 添加到 JVM?)
                  Cannot load 64-bit SWT libraries on 32-bit JVM ( replacing SWT file )(无法在 32 位 JVM 上加载 64 位 SWT 库(替换 SWT 文件))
                  Encourage the JVM to GC rather than grow the heap?(鼓励 JVM 进行 GC 而不是增加堆?)
                  Why a sawtooth shaped graph?(为什么是锯齿形图形?)
                  <tfoot id='endr5'></tfoot>

                  <small id='endr5'></small><noframes id='endr5'>

                    <tbody id='endr5'></tbody>
                        <bdo id='endr5'></bdo><ul id='endr5'></ul>

                        • <legend id='endr5'><style id='endr5'><dir id='endr5'><q id='endr5'></q></dir></style></legend>

                            <i id='endr5'><tr id='endr5'><dt id='endr5'><q id='endr5'><span id='endr5'><b id='endr5'><form id='endr5'><ins id='endr5'></ins><ul id='endr5'></ul><sub id='endr5'></sub></form><legend id='endr5'></legend><bdo id='endr5'><pre id='endr5'><center id='endr5'></center></pre></bdo></b><th id='endr5'></th></span></q></dt></tr></i><div id='endr5'><tfoot id='endr5'></tfoot><dl id='endr5'><fieldset id='endr5'></fieldset></dl></div>