2011-03-23 7 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[]

関連する問題