Get a name of a method parameter using Javassist

javassist replace method body
javassist find method calls
javassist wrap method
javassist constructor
javassist compiler
javassist local variable
javassist cannot compile exception
javassist remove final

I have a CtMethod instance, but I don't know how to get names of parameters (not types) from it. I tried getParameterTypes, but it seems it returns only types.

I'm assuming it's possible, because libraries I'm using don't have sources, just class files and I can see names of method parameters in IDE.

How to get method parameter names with javassist? · Issue #14 , I want use this code to get parameter names: CtMethod cm = cc.​getDeclaredMethod(method.getName(), parameterCtClasses); MethodInfo  There is a method public void setOrg(Org org) in the code. I can get the java.lang.reflect.Method object by java reflection, then with getParameterTypes(), I know it has one Org parameter. Now I want get it's name 'org'. So I read the local variable table with javassist, I think the first will be the name 'this', the second will be the name 'org'.

If you don't actually want the names of the parameters, but just want to be able to access them, you can use "$1, $2, ..." as seen in this tutorial.

Javassist Tutorial, String name, CtClass[] params). Retrieves the method with the specified name and parameter types among the Gets all methods declared in the class. names - an array of parameter names. The i-th element is the name of the i-th parameter. The i-th element is the name of the i-th parameter. flags - an array of parameter access flags.

It works with Javaassist 3.18.2 (and later, at least up to 3.19 anyway) if you cast, like so:

LocalVariableAttribute nameTable = (LocalVariableAttribute)methodInfo.getCodeAttribute().getAttribute(LocalVariableAttribute.tag);

Uses of Class javassist.CtMethod (Javassist API), It loop through Action parameters, and tries to find the parameter value in url. get method parameter names with javassist * @return */ private  Each element of the returned array represents an array of annotations that are associated with each method parameter. See Also: setAnnotations(Annotation[][]) setAnnotations public void setAnnotations (Annotation[][] params)

Paranamer has methods which can be used to retrieve parameter names even if it is not compiled with debug symbols. I have used it and works pretty well for me. Give it a try. paranamer link

jar link

Get Method Parameter Names in JAVA, get method parameter names * @param cls * @param method * @return * @​throws Exception */ public static String[] getParamNames(Class<?> cls, Method  I just need a simple example demonstrating addition of a parameter to an existing method of an existing class using javassist. As far as I have searched it seems to me that this is not possible and we

Java Code Examples javassist.bytecode.LocalVariableAttribute, I have a CtMethod instance, but I don't know how to get names of parameters (not types) from it. I tried getParameterTypes , but it seems it returns only types. CtMethod and CtConstructor provide methods insertBefore(), insertAfter(), and addCatch(). They are used for inserting a code fragment into the body of an existing method. The users can specify those code fragments with source text written in Java. Javassist includes a simple Java compiler for processing source text.

Get a name of a method parameter using Javassist, In particular, these tools would have to handle larger .class files, and the Java Virtual Machine (JVM) would use more memory. In addition, some parameter names,  You've used Javassist, a bytecode modifier framework, to engineer your compiled class to add more bytecode that represents a new method. You can have one of the two scenarios: Scenario 1: Code which is compiled along with your Car class before injection

Obtaining Names of Method Parameters (The Java™ Tutorials > The , @param classname the class name * @param methodname the method name Proxy" }); exceptionForProxy = new CtClass[] { pool.get("javassist.tools.rmi. mname - the method name. parameters - a list of the parameter types. exceptions - a list of the exception types. body - the source text of the method body. It must be a block surrounded by {}. If it is null, the created method does nothing except returning zero or null.

Comments
  • Thank you for explaining :), I'll try to implement it via Javassist using your code snippets (even though it might a bit harder than using the paranamer). Also I found some code which might be doing the parameter searching as well (haven't tried it, but it could be useful for other readers) - grepcode.com/file/repo1.maven.org/maven2/org.slf4j/slf4j-ext/….
  • it does work with Javaassist 3.18.2 (and later, at least up to 3.19 anyway) if you cast, like so: LocalVariableAttribute nameTable = (LocalVariableAttribute) methodInfo.getCodeAttribute().getAttribute(LocalVariableAttribute.tag);
  • Is it possible also to set the variable names, using a similar inverse procedure?
  • @Panayotis: Never tried it, but conceptually is possible. You first would add new names to the constant pool using addUtf8Info method. This method returns an integer that represents the frame's index. You would then need to replace the LocalVariableAttribute (this would be the trickiest part) by a new one where you would add the new entries using addEntry method and finally write the class.