2015-01-05 1 views
3

메서드 매개 변수의 정규화 된 이름을 가져와야합니다. 예를 들어메서드에서 매개 변수 값의 정규화 된 이름을 얻는 방법

:

public void display(Custom1 a, Custom2 b) { 
    String x = a.getValue(); 
    String y = b.getValue(); 
} 
여기

, Custom1Custom2 나는 ... 그래서 나는 내 이클립스 플러그인이 필요 해요

com.test.resource.Custom1 

같은 값을 얻을 필요가 com.test.resource에 중고 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]); 
} 

답변

1

리플렉션을 사용하여 해당 메소드를로드하고 매개 변수 값을 하나씩 가져올 수 있습니다. 당신이 반사를 방지하고 순수한 JDT 솔루션을 선호 할 경우

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(); 


           } 

          } 
         } 

        } 
      } 
+0

감사 당신은 도움을 위해 클래스를로드 한 후에이 로직을 시도했지만 작동합니다! – HilmiAziz

3

여기에 대한 대안을 다음 (이클립스 플러그인의를 반사 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 클래스를 사용하지 않고 동일한 방식으로 수행됩니다. 결과 유형에 대한 솔루션은 동일하며 코드는 here입니다.

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; 
관련 문제