フィールドタイプの取得


1つのフィールドは基本データ型と参照データ型を含む.基本データ型:boolean,byte,short,int long,char,float,double.1つの参照型のタイプはjava.lang.Object直接,間接サブクラス,インタフェース,配列,列挙タイプである.
次の例では、フィールドタイプを取得する方法を示す.

import java.lang.reflect.Field;
import java.util.List;
import static java.lang.System.out;
public class FieldSpy<T> {
		public boolean[][] b = {{false,false},{true,true}};
		public String name = "Alice";
		public List<Integer> List;
		public T val;
		
		public static void main(String... args) {
				try{
						Class<?> c = Class.forName("FieldSpy");
						Field f = c.getField(args[1]);
						out.println(f.getType());
						out.format("Type: %s%n", f.getType());
						out.format("GenericType: %s%n",f.getGenericType());
				}	catch(ClassNotFoundException x) {
						x.printStackTrace();	
				}catch(NoSuchFieldException x) {
						x.printStackTrace();	
				}
		}	
}