反射取得クラスの基本構造情報
2038 ワード
1、パッケージ名の取得 Class> clazz = Person.class;
//
Package p = clazz.getPackage();
//
String packageName = p.getName();
2、親classオブジェクトの取得
Class> clazz = Person.class;
Class> superClazz = clazz.getSuperclass();
3、実装されたインタフェースのclassオブジェクトを取得する
Class> clazz = Person.class;
// class
Class>[] clazzInterfaces = clazz.getInterfaces();
4、コンストラクタの取得及び実例化
Class> clazz = Person.class;
//
Constructor>[] declaredConstructors = clazz.getDeclaredConstructors();
// ( )public
Constructor>[] constructors = clazz.getConstructors();
//
Constructor> specifiedConstructor = clazz.getDeclaredConstructor(String.class);
//
Object instance = specifiedConstructor.newInstance("hellow");
5、取得方法及び方法呼び出し
Class> clazz = Person.class;
//
Method[] declaredMethods = clazz.getDeclaredMethods();
// ( )public
Method[] methods = clazz.getMethods();
//
Method method = clazz.getDeclaredMethod("setName");
// ,
Object obj = clazz.getDeclaredConstructors().newInstance();
method.invoke(obj, "hellow");
//
String name = method.getName();
//
int mod = method.getModifiers();
String modName = Modifier.toString(mod);
//
Class> returnType = method.getReturnType();
//
Class>[] parameterTypes = method.getParameterTypes();
//
Class>[] exceptionTypes = method.getExceptionTypes();
6、呼び出しクラスメンバー
Class> clazz = Person.class;
Object obj = clazz.getDeclaredConstructor().newInstance();
//
Field[] declaredFields = clazz.getDeclaredFields();
// ( )public
Field[] fields = clazz.getFields();
//
Field field = clazz.getDeclaredField("name");
// ,
// public, field.setAccessible(true);
Object value = field.get(obj);
//
Class> type = field.getType();
// , :java.lang.String
type.getName();
// , :String
type.getSimpleName();
Class> clazz = Person.class;
//
Package p = clazz.getPackage();
//
String packageName = p.getName();
Class> clazz = Person.class;
Class> superClazz = clazz.getSuperclass();
Class> clazz = Person.class;
// class
Class>[] clazzInterfaces = clazz.getInterfaces();
Class> clazz = Person.class;
//
Constructor>[] declaredConstructors = clazz.getDeclaredConstructors();
// ( )public
Constructor>[] constructors = clazz.getConstructors();
//
Constructor> specifiedConstructor = clazz.getDeclaredConstructor(String.class);
//
Object instance = specifiedConstructor.newInstance("hellow");
Class> clazz = Person.class;
//
Method[] declaredMethods = clazz.getDeclaredMethods();
// ( )public
Method[] methods = clazz.getMethods();
//
Method method = clazz.getDeclaredMethod("setName");
// ,
Object obj = clazz.getDeclaredConstructors().newInstance();
method.invoke(obj, "hellow");
//
String name = method.getName();
//
int mod = method.getModifiers();
String modName = Modifier.toString(mod);
//
Class> returnType = method.getReturnType();
//
Class>[] parameterTypes = method.getParameterTypes();
//
Class>[] exceptionTypes = method.getExceptionTypes();
Class> clazz = Person.class;
Object obj = clazz.getDeclaredConstructor().newInstance();
//
Field[] declaredFields = clazz.getDeclaredFields();
// ( )public
Field[] fields = clazz.getFields();
//
Field field = clazz.getDeclaredField("name");
// ,
// public, field.setAccessible(true);
Object value = field.get(obj);
//
Class> type = field.getType();
// , :java.lang.String
type.getName();
// , :String
type.getSimpleName();