如何在方法中获取参数值的完全限定名称
How to get fully qualified name of parameter value in a method
我必须获取方法参数的完全限定名称。
例如:
public void display(Custom1 a, Custom2 b) {
String x = a.getValue();
String y = b.getValue();
}
这里,Custom1
和 Custom2
在 com.test.resource
中,所以我需要得到像
这样的值
com.test.resource.Custom1
我在我的 eclipse 插件中需要这个。我使用了 IMethod.getParameterTypes()
。结果就像
QCustom1;
如何获取方法参数的完全限定名称?
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]);
}
可以使用反射加载对应的方法,逐一获取参数值
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();
}
}
}
}
}
如果你想避免反射,更喜欢纯JDT解决方案(在Eclipse插件中,反射API不是很友好),这里有一个替代方案。
为了解码 JDT 编码类型名称,您应该使用 Signature class.
解析类型名称部分
一个重要的步骤是解决方法声明 IType object to be able to recompute the full name with this type imports. Then the #resolveType(simpleName) 方法应该可以帮助您。它的使用很微妙。
从部分构建全名
这是从参数类型编码名称到全名的代码,它在从声明类型解析名称时采用第一种解决方案:
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;
从简单名称(非 JDT 编码)获取全名的方法相同,无需使用签名 class。结果类型的解决方法是一样的,here就是代码。
这是 . It makes more use of the Signature class 的变体。因此,源代码甚至更短。它仍然避免了反射,是一个纯JDT解决方案。
作为原始答案,此解决方案是从参数类型编码名称到全名的代码,它在从声明类型解析名称时采用 declaringType.resolveType()
的第一个结果:
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;
我必须获取方法参数的完全限定名称。
例如:
public void display(Custom1 a, Custom2 b) {
String x = a.getValue();
String y = b.getValue();
}
这里,Custom1
和 Custom2
在 com.test.resource
中,所以我需要得到像
com.test.resource.Custom1
我在我的 eclipse 插件中需要这个。我使用了 IMethod.getParameterTypes()
。结果就像
QCustom1;
如何获取方法参数的完全限定名称?
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]);
}
可以使用反射加载对应的方法,逐一获取参数值
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();
}
}
}
}
}
如果你想避免反射,更喜欢纯JDT解决方案(在Eclipse插件中,反射API不是很友好),这里有一个替代方案。
为了解码 JDT 编码类型名称,您应该使用 Signature class.
解析类型名称部分
一个重要的步骤是解决方法声明 IType object to be able to recompute the full name with this type imports. Then the #resolveType(simpleName) 方法应该可以帮助您。它的使用很微妙。
从部分构建全名
这是从参数类型编码名称到全名的代码,它在从声明类型解析名称时采用第一种解决方案:
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;
从简单名称(非 JDT 编码)获取全名的方法相同,无需使用签名 class。结果类型的解决方法是一样的,here就是代码。
这是
作为原始答案,此解决方案是从参数类型编码名称到全名的代码,它在从声明类型解析名称时采用 declaringType.resolveType()
的第一个结果:
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;