如何获取方法中参数值的完全限定名称

3
我需要获取方法参数的完全限定名称。
例如:
public void display(Custom1 a, Custom2 b) {
    String x = a.getValue();
    String y = b.getValue();
}

这里,Custom1Custom2com.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]);
}
3个回答

3
以下是一种替代方案,如果您想避免使用反射并且更喜欢纯JDT解决方案(在Eclipse插件中,反射API不太友好)。
为了解码JDT编码的类型名称,您应该使用Signature类。
解决类型名称部分的重要步骤是解析方法声明IType对象,以便能够使用此类型导入重新计算完整名称。然后,#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编码)获取全名的方法与不使用Signature类的方法相同。结果类型的解决方案也是相同的,这里是代码。

1
你可以使用反射加载相应的方法,并逐个获取参数值。
    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();


                                }

                            }
                        }

                    }
             }

谢谢您的帮助,我在加载类之后尝试了这个逻辑,它有效! - HilmiAziz

0

这是bdulacs answer的变体。它更多地使用了Signature类。因此,源代码甚至更短。它仍然避免了反射,并且是一个纯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;

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接