How to get the fully qualified name of a parameter value in a method
I need to get the full name of a method parameter.
For example:
public void display(Custom1 a, Custom2 b) {
String x = a.getValue();
String y = b.getValue();
}
Here Custom1
and Custom2
are in com.test.resource
, so I need to get a value like
com.test.resource.Custom1
I need this in my eclipse plugin. I have used IMethod.getParameterTypes()
. The result is similar to
QCustom1;
How do I get the full name of a method parameter?
String[] parameterNames = iMethod.getParameterNames();
ILocalVariable[] parameterTypes = currentmethod.getMethod().getParameters();
for (int j=0; j < parameterNames.length; ++j) {
System.out.println("parameter name:" + parameterNames[j]);
System.out.println("parameter type:" + parameterTypes[j]);
}
source to share
you can load the corresponding method with reflection and get the parameter value one by one.
if(method.getName().equals(iMethod.getMethodname())){
/**
* cheking whether the length of the parameter are equal
*/
if(method.getParameterTypes().length==iMethod.getParam().length){
/**
* getting the fully qualified name of the selected method paramater value
*/
Class<?>[] paramvalue=method.getParameterTypes();
for(int p=0;p<paramvalue.length;p++){
/**
* checking whether teh parameter are same for loading teh datastore
*/
if(paramvalue[p].getSimpleName().equals(temp)){
String fullyqualifiedname=paramvalue[p].getName();
}
}
}
}
}
source to share
An alternative follows here if you want to avoid reflection and prefer a pure JDT solution (in the Eclipse plugin, the reflection API is not very friendly).
To decode the JDT encoded type name, you must use the Signature class .
Name resolution (s) type name (s)
An important step is to resolve the method that declares the IType object to be able to double-check the fully qualified name when importing that type. Then # resolveType (simpleName) will help you . Its use is subtle.
Creating a full name from parts
Here is the code to go from the encoded parameter type name to the fully qualified name, it makes the first decision when resolving names from the declaration type:
ILocalVariable parameterVariable = ...
IType declaringType = method.getDeclaringType();
String name = parameterVariable.getTypeSignature();
String simpleName = Signature.getSignatureSimpleName(name);
String[][] allResults = declaringType.resolveType(simpleName);
String fullName = null;
if(allResults != null) {
String[] nameParts = allResults[0];
if(nameParts != null) {
fullName = new String();
for(int i=0 ; i < nameParts.length ; i++) {
if(fullName.length() > 0) {
fullName += '.';
}
String part = nameParts[i];
if(part != null) {
fullName += part;
}
}
}
}
return fullName;
Getting the full name from a simple name (not JDT encoded) is done the same way without using the Signature class. The solution for the result type is the same, here is the code.
source to share
This is a variation from bdulacs answer . It uses the Signature class more . As a result, the source code is even shorter. It still avoids reflection and is a pure JDT solution.
As the original answer, this solution is code to go from the name of the codified parameter type to the fully qualified name, it takes the first result declaringType.resolveType()
when resolving names from the declaration type:
ILocalVariable parameterVariable = ...
IType declaringType = method.getDeclaringType();
String name = parameterVariable.getTypeSignature();
String simpleName = Signature.getSignatureSimpleName(name);
String[][] allResults = declaringType.resolveType(simpleName);
if(allResults != null && allResults[0] != null) {
return Signature.toQualifiedName(allResults[0])
}
return null;
source to share