2011-03-23 4 views
3

클래스 내에서 메소드를 표시하는 프로그램을 액세스 수정 자, 반환 유형 및 매개 변수와 함께 작성하고 있습니다.getTypeParameters는 null TypeVariable 배열을 반환합니다.

여기에 Test.java

class Test{ 

    private int x; 
    public double y; 
    protected String z; 
    static long a; 

    public Test(){ 
     x = 10; 
     y = 20; 
     z = "Hello"; 
     a = 15L; 

    } 

    public void Print(String a){ 
     a = a; 
     System.out.println("Executing Print function."); 
    } 

    private void hidden(double b){ 
     b = b; 
     //private function 
    } 
} 

모든 일이 잘 작동하지만 난 줄 TypeVariable의 빈 배열을 얻을 왜 이해가 안 내 코드

import java.lang.reflect.*; 
class RefTest1{ 

    public static void main(String[] args) throws Exception{ 
     Test obj = new Test();  
     Class<?> c = obj.getClass(); 

     System.out.printf("%n%s fields :-%n", obj.getClass()); 

     Field[] fields = c.getDeclaredFields(); 

     for(Field f : fields){ 
      f.setAccessible(true); 
      int m = f.getModifiers(); 

      if(Modifier.isStatic(m)){ 
       System.out.printf("%s is static variable and its value is %s%n", f.getName(), f.get(obj)); 
      }else if(Modifier.isPublic(m)){ 
       System.out.printf("%s is public variable and its value is %s%n", f.getName(), f.get(obj)); 
      }else if(Modifier.isPrivate(m)){ 
       System.out.printf("%s is private variable and its value is %s%n", f.getName(), f.get(obj)); 
      }else if(Modifier.isProtected(m)){ 
       System.out.printf("%s is protected variable and its value is %s%n", f.getName(), f.get(obj)); 
      } 
     } 
     System.out.printf("%n%s methods :-%n", obj.getClass());  

     Method[] methods = c.getDeclaredMethods(); 

     for(Method meth : methods){ 
      int m = meth.getModifiers(); 
      meth.setAccessible(true); 
      if(Modifier.isStatic(m)){ 
       System.out.printf("%s is static method%n", meth.getName()); 
      }else if(Modifier.isPublic(m)){ 
       System.out.printf("%s is public method%n", meth.getName()); 
      }else if(Modifier.isPrivate(m)){ 
       System.out.printf("%s is private method%n", meth.getName()); 
      }else if(Modifier.isProtected(m)){ 
       System.out.printf("%s is protected method%n", meth.getName()); 
      } 

      System.out.printf("%nReturn Type :- %s%n", meth.getReturnType()); 
      System.out.printf("%nParameters:-%n"); 
      TypeVariable[] parameters = meth.getTypeParameters(); 

      for(TypeVariable param : parameters){ 
       System.out.printf("%s", param.getName()); 
      } 


     } 
     System.out.println(); 

    } 

} 

TypeVariable[] parameters = meth.getTypeParameters();

어떤 사람이 올바른 방향으로 나를 가리킬 수 있습니까?

감사합니다.

답변

11

getTypeParameters()은 메서드 정의에 사용 된 type parameters의 배열을 반환합니다. 이 아닌은 인수 유형의 배열을 반환합니다. 이 방법을 고려해

public <T> void foo(int bar); 

getTypeParameters()T가 격납되어있는 배열을 반환 (즉, 이름과 T 경계 { Object.class }TypeVariable).

getParameterTypes() 그러나 int.class을 포함하는 배열을 반환합니다.

참고 : 매개 변수 유형에 유형 매개 변수가 포함 된 경우 getGenericParameterTypes()을 사용해야합니다.

+0

반환 getParameterTypes()를 사용한다고 생각합니다. 편집을 참조하십시오, 내 코드에 오류가 있다고 생각합니다. – Searock

+0

@Searock : 변경 사항을 원래 코드에 통합 한 후 매개 변수 유형을 인쇄합니다. 그러나 유형 다음에 개행 문자를 인쇄하지 않으므로 놓치기 쉽습니다. –

+0

정말 미안 해요, 제 잘못입니다. 어딘가에 매개 변수 유형을 알지 못했던 개행 문제가 있습니다. 빠른 응답을 보내 주셔서 감사합니다. – Searock

1

난 당신이 내가 getParameterTypes를 호출 시도했지만 여전히 나에게 빈 배열을 제공 Class[]

관련 문제