2011-03-23 80 views
3

我正在寫一個程序,它顯示了一個Class中的方法以及它的訪問修飾符,返回類型和參數。getTypeParameters返回一個null 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(); 

    } 

} 

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空白陣列TypeVariable[] parameters = meth.getTypeParameters();

有人能指出我正確的方向嗎?

謝謝。

回答

11

getTypeParameters()返回方法定義中使用的type parameters的數組。它沒有而不是返回參數類型的數組。考慮這個方法:

public <T> void foo(int bar); 

getTypeParameters()將返回含有T的陣列(即TypeVariable名爲T和邊界{ Object.class })。但是,將返回包含int.class的數組。

注意:如果您的參數類型包含類型參數,則需要使用getGenericParameterTypes()

+0

我打過電話getParameterTypes但它仍然給了我一個空白陣列。請參閱編輯,我認爲我的代碼中有錯誤。 – Searock 2011-03-23 10:01:01

+0

@Searock:將您的更改合併到您的原始代碼後,它會爲我打印參數類型。請注意,不要在類型之後打印換行符,但是,因此可能很容易錯過。 – 2011-03-23 10:03:44

+0

我真的很抱歉,這是我的錯。有一個換行符的問題,因爲我沒有注意到參數類型。非常感謝您的快速響應。 – Searock 2011-03-23 10:07:14

1

我認爲你應該使用getParameterTypes()返回Class[]