JAVA反射の役割と具体的な使用
33211 ワード
JAva反射
反射は非常に強く、どんなクラス、方法、属性を知るかを呼び出すことができ、反射はフレームワークの魂です.
1.反射するにはもちろん入口を見つけるには3つの方法があります
Parson ps=new Parson();
Class> cl= ps.getClass();
Class> forName = Class.forName(“Reflect.parson”);
Class> clazz= parson.class;
この3つの方法があれば、反射の強さを体験できます
2.反射は何ができる
1共通メソッドgetMethods()&現在のクラスのすべてのメソッドgetDeclaredMethods()★
共通メソッド:共通メソッド(このクラス、親、インタフェース、アクセス修飾子に一致するクラス)を取得します.
現在のクラスのすべてのメソッド:現在のクラスのみ、修飾子を無視(privateもアクセス可能)
2,取得インタフェースgetInterfaces()★
3,親getSuperclass()を取得する
4,取得構造方法getConstructors()★
5属性getFields()★を取得
6現在の反射が表すクラス(インタフェース)のオブジェクト(インスタンス)newInstance(★
3反射の具体的な使用
オブジェクトインスタンスを取得し、オブジェクトを操作します.操作方法
クラス名とメソッドの動的ロード
1,prop.txtファイルを作成する
2、操作を行う
反射は汎用的な検査を越えられる
万能のsetメソッド
1,PropertyUtilの作成
4まとめ
プライベートメソッドにアクセスするにはgetDeclared***()を使用します.
属性のアクセス権限を変更して反射を使用する場合、アクセス修飾子限界が異常になった場合、Field/Method/Clonstructor.setAccessible(true)field.setAccessible(true)を使用できます.
反射は非常に強く、どんなクラス、方法、属性を知るかを呼び出すことができ、反射はフレームワークの魂です.
1.反射するにはもちろん入口を見つけるには3つの方法があります
.getclass()
Parson ps=new Parson();
Class> cl= ps.getClass();
Class.forName(" . ")
Class> forName = Class.forName(“Reflect.parson”);
class
Class> clazz= parson.class;
この3つの方法があれば、反射の強さを体験できます
2.反射は何ができる
1共通メソッドgetMethods()&現在のクラスのすべてのメソッドgetDeclaredMethods()★
共通メソッド:共通メソッド(このクラス、親、インタフェース、アクセス修飾子に一致するクラス)を取得します.
現在のクラスのすべてのメソッド:現在のクラスのみ、修飾子を無視(privateもアクセス可能)
private static void demo2() throws ClassNotFoundException {
//
Class<?> forName = Class.forName("Reflect.parson");
//
Method[] methods = forName.getMethods();
for (Method method : methods) {
System.out.println(method);
}
System.out.println("===================");
// (1, , (private ))
Method[] declaredMethods = forName.getDeclaredMethods();
for (Method method : declaredMethods) {
System.out.println(method);
}
2,取得インタフェースgetInterfaces()★
private static void demo3() throws ClassNotFoundException {
Class<?> forName = Class.forName("Reflect.parson");
Class<?>[] interfaces = forName.getInterfaces();
for (Class<?> class1 : interfaces) {
System.out.println(class1);
}
}
3,親getSuperclass()を取得する
private static void demo4() throws ClassNotFoundException {
Class<?> forName=Class.forName("Reflect.parson");
Class<?> superclass = forName.getSuperclass();
System.out.println(superclass);
}
4,取得構造方法getConstructors()★
private static void demo5() throws ClassNotFoundException {
Class<?> forName = Class.forName("Reflect.parson");
Constructor<?>[] constructors = forName.getConstructors();
for (Constructor<?> constructor : constructors) {
System.out.println(constructor);
}
}
5属性getFields()★を取得
private static void demo6() throws ClassNotFoundException {
Class<?> forName = Class.forName("Reflect.parson");
Field[] fields = forName.getFields();
for (Field field : fields) {
System.out.println(field);
}
}
6現在の反射が表すクラス(インタフェース)のオブジェクト(インスタンス)newInstance(★
private static void demo7() throws ClassNotFoundException, InstantiationException, IllegalAccessException {
Class<?> forName = Class.forName("Reflect.parson");
Object newInstance = forName.newInstance();
parson ps=(parson)newInstance;
ps.InterfaceMethod();// parson ,
}
3反射の具体的な使用
オブジェクトインスタンスを取得し、オブジェクトを操作します.操作方法
// ,
Class<?> forName = Class.forName("Reflect.parson");
// 1
parson parson = (parson) forName.newInstance();
parson.setName("weng");
System.out.println(parson.getName());
System.out.println("============================");
//
// Field field = forName.getField("id");// ,
Field field = forName.getDeclaredField("id");
//
// , ,
// Field/Method/Constructor.setAccessible(true)
field.setAccessible(true);//
field.set(parson, 1);
System.out.println(parson.getId());
System.out.println("==============================");
//
Method method = forName.getDeclaredMethod("InterfaceMethod", null);
method.invoke(parson, null);// :invoke()
System.out.println("=============");
//
Method method2 = forName.getDeclaredMethod("testname", String.class);
method2.setAccessible(true);
method2.invoke(parson, "weng");
System.out.println("=================");
//
//
// , : (int、char...) (Integer,Character)
Constructor<?> constructor1 = forName.getConstructor(null);
System.out.println(constructor1);
Constructor<?> constructor2 = forName.getDeclaredConstructor(int.class);
System.out.println(constructor2);
// private ,
parson instance2 = (parson) constructor2.newInstance(1);
System.out.println(instance2);
parson par = (parson) constructor1.newInstance();
System.out.println(par);
クラス名とメソッドの動的ロード
1,prop.txtファイルを作成する
classname=Reflect.parson//
methodname=InterfaceMethod//
2、操作を行う
public static void demo2() throws FileNotFoundException, IOException, ClassNotFoundException,
IllegalAccessException, IllegalArgumentException, InvocationTargetException, InstantiationException,
NoSuchMethodException, SecurityException {
Properties prop = new Properties();
//
prop.load(new FileReader("prop.txt"));
//
String classname = prop.getProperty("classname");
String methodname = prop.getProperty("methodname");
Class<?> forName = Class.forName(classname);//
Method method = forName.getMethod(methodname);//
Object instance = forName.newInstance();//
method.invoke(instance);//
}
反射は汎用的な検査を越えられる
public static void demo3() throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
List<Integer> list=new ArrayList<Integer>();
list.add(123);
list.add(2);
Class<?> rf= list.getClass();
Method method = rf.getMethod("add",Object.class);// Object
method.invoke(list, "weng");
System.out.println(list);
}
万能のsetメソッド
1,PropertyUtilの作成
/**
*
* @param obj //
* @param propertyName //set
* @param value //
* @throws SecurityException
* @throws NoSuchFieldException
* @throws IllegalAccessException
* @throws IllegalArgumentException
*/
public static void setPropertys(Object obj,String propertyName,Object value) throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException{
Class<?> clazz=obj.getClass();
Field field = clazz.getDeclaredField(propertyName);
field.setAccessible(true);
field.set(obj, value);
}
public static void demo4() throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {
Student student=new Student();
PropertyUtil.setPropertys(student, "name", "weng");
System.out.println(student.getName());
}
4まとめ
プライベートメソッドにアクセスするにはgetDeclared***()を使用します.
属性のアクセス権限を変更して反射を使用する場合、アクセス修飾子限界が異常になった場合、Field/Method/Clonstructor.setAccessible(true)field.setAccessible(true)を使用できます.