How to obtain the method name that is called during JVM execution

Source: Internet
Author: User

This should be divided into two questions, 1. How to get a parameter value. 2. How to get the parameter name,

1. How to get the parameter value. This is the runtime data, you can handle the process to get the better. Like writing an agent.

2. The parameter name is written to the class file at compile time. , and the parameters of these methods are a local variable in class. Class has a table specifically for the definition and storage of local variables.
Simply through reflection at present there seems to be no way, through the byte code parsing can be
such as the following code

public static void Staticmethod (String args1, String args2) {
}

Local Variables Table:
[Pc:0, Pc:1] local:args1 index:0 type:java.lang.String
[Pc:0, Pc:1] local:args2 index:1 type:java.lang.String

PC 0 is a program counter for each bytecode instruction. [Pc:0, Pc:1] local:args1 index:0 type:java.lang.String means that the variable type labeled 0 under the local variable array of the No. 0 instruction to the 1th instruction is the string variable name is ARGS1.

public static void Nonstaticmethod (String args1, String args2) {
}

local variable table;
[Pc:0, Pc:1] local:this index:0 type:asmtest. Test
[Pc:0, Pc:1] local:args1 index:1 type:java.lang.String
[Pc:0, Pc:1] local:args2 index:2 type:java.lang.String

This method has one more than the above method. Because this method is a non-static method.

So if you want to get the parameter name, you need to parse the bytecode. Here's a piece of code for you to use ASM

Import java.util.ArrayList;
Import Java.util.HashMap;
Import java.util.List;
Import Java.util.Map;

Import Org.objectweb.asm.ClassVisitor;
Import Org.objectweb.asm.MethodVisitor;
Import Org.objectweb.asm.Opcodes;
Import Org.objectweb.asm.Type;

public class Readmethodargnameclassvisitor extends Classvisitor {

Public map<string, list<string>> nameargmap = new hashmap<string, list<string>> ();

Public Readmethodargnameclassvisitor () {
Super (OPCODES.ASM5);
}

@Override
Public methodvisitor visitmethod (int access, string name, String desc,
String signature, string[] exceptions) {
Type methodtype = Type.getmethodtype (DESC);
int len = Methodtype.getargumenttypes (). length;
list<string> argumentnames = new arraylist<string> ();
Nameargmap.put (name, argumentnames);
Readmethodargnamemethodvisitor visitor = new Readmethodargnamemethodvisitor (OPCODES.ASM5);
Visitor.argumentnames = Argumentnames;
Visitor.arglen = Len;
return visitor;
}
}

Import java.util.List;

Import Org.objectweb.asm.Label;
Import Org.objectweb.asm.MethodVisitor;

public class Readmethodargnamemethodvisitor extends Methodvisitor {

Public list<string> Argumentnames;

public int Arglen;

public Readmethodargnamemethodvisitor (int API) {
Super (API);
}

@Override
public void Visitlocalvariable (string name, String desc, string signature,
Label start, label end, int index) {
if ("This". Equals (name)) {
Return
}
if (arglen--> 0) {
Argumentnames.add (name);
}
}

}

public class POJO {

public void Say (String message, int times) {
}

}

Import java.io.IOException;
Import java.util.List;
Import Java.util.Map.Entry;

Import Org.objectweb.asm.ClassReader;

public class Test {

public static void Main (String ... args1) throws IOException {
Classreader cr = new Classreader ("POJO");
Readmethodargnameclassvisitor classvisitor = new Readmethodargnameclassvisitor ();
Cr.accept (classvisitor, 0);
For (entry<string, list<string>> entry:classVisitor.nameArgMap.entrySet ()) {
System.out.println (Entry.getkey ());
For (String S:entry.getvalue ()) {
System.out.println ("" + s);
}
}
}

}

Using the ASM version is

<dependency>
<groupId>org.ow2.asm</groupId>
<artifactId>asm-all</artifactId>
<version>5.0.3</version>
</dependency>

There is a hidden trouble, if some of the class file has been encrypted confused the variable name inside the local variable table has changed, it will not be able to obtain the source-level parameter name.

How to obtain the method name that is called during JVM execution

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.