Javaベースステップ_day18_(クラスローダ、反射、ダイナミックエージェント)
Javaベースステップ_day18_(クラスローダ、反射、ダイナミックエージェント)
1.クラスローダ
2.反射
2.1反射の概要
2.2反射取得構造方法
2.3反射メンバー属性の取得
2.4メンバーの取得方法の反射
2.5反射ケース1
2.6反射ケース2
3.動的エージェント
ケースコード:
1.クラスローダ
/* * : * : , , , , . * 1. * class , Class 。 Class 。 * 2. * A: , ; * B: , ; * C: . * 3. * . * : * 1. ; * 2. , ; * 3. ; * 4. java.lang.Class ; * 5. ; * 6. java.exe . * * : .class , Class , Class . * : * Bootstrap ClassLoader: * , Java ; * System,String , JDK JRE lib rt.jar ; * Extension ClassLoader: * JRE jar . * JDK JRE lib ext . * Sysetm ClassLoader: * JVM java class , classpath jar . */
public class My_ClassLoader_Demo01 {
public static void main(String[] args) {
}
}
2.反射
2.1反射の概要
/* * : * JAVA , , ; * , ; * . * Class, Class . * * Class : * A: .class * B: .getClass() * C: Class forName( ) */
public class My_Reflect_Demo01 {
public static void main(String[] args) throws Exception {
// Class
// A: .class
Class c1 = My_Reflect_Demo01.class;
System.out.println(c1);
// B: .getclass()
My_Reflect_Demo01 mrd = new My_Reflect_Demo01();
Class c2 = mrd.getClass();
System.out.println(c2);
// C: Class forName( )
Class c3 = Class.forName("com.itheima.reflect01.My_Reflect_Demo01");
System.out.println(c3);
}
}
2.2反射取得構造方法
package com.itheima.reflect02;
/* * , */
public class Person {
//
private String name;
//
int age;
//
public Person() {
}
//
public Person(String name, int age) {
this.name = name;
this.age = age;
}
//
private Person(int age) {
this.age = age;
}
//
public void show(String s) {
System.out.println("show:" + s);
}
//
private String show2(String s) {
return " ";
}
@Override
public String toString() {
return "Person [name=" + name + ", age=" + age + "]";
}
}
import java.lang.reflect.Constructor;
/* * : * Class : .class * public Constructor[] getConstructors(): ; * public Constructor[] getDeclaredConstructors(): , ; * public Constructor getConstructor(Class... parameterTypes): , ; * public Constructor getDeclaredConstructor(Class... parameterTypes): , ; * :Class * public T newInstance(Object ... initargs): , * , setAccessible(boolean flag), true. */
public class My_Reflect_Demo01 {
public static void main(String[] args) throws Exception {
// Class
Class c1 = Class.forName("com.itheima.reflect02.Person");
//
Constructor[] cs = c1.getConstructors();
for (int i = 0; i < cs.length; i++) {
System.out.println(cs[i]);
}
//
Constructor[] cns = c1.getDeclaredConstructors();
for (Constructor cn : cns) {
System.out.println(cn);
}
// ,
Constructor cns1 = c1.getConstructor();
Object obj = cns1.newInstance();
// ,
Constructor cns2 = c1.getDeclaredConstructor(int.class);
cns2.setAccessible(true);
Object obj2 = cns2.newInstance(23);
System.out.println(obj2); // Person [name=null, age=23]
}
}
2.3反射メンバー属性の取得
package com.itheima.reflect02;
/* * , */
public class Person {
//
private String name;
//
int age;
//
public Person() {
}
//
public Person(String name, int age) {
this.name = name;
this.age = age;
}
//
private Person(int age) {
this.age = age;
}
//
public void show(String s) {
System.out.println("show:" + s);
}
//
private String show2(String s) {
return " ";
}
@Override
public String toString() {
return "Person [name=" + name + ", age=" + age + "]";
}
}
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
/* * :Class * public Field[] getFields(): ; * public Field[] getDeclaredFields(): ; * public Field getField(String name): ; * public Field getDeclaredField(String name): ( ). * :Field * public void set(Object obj,Object value): obj field value; * */
public class My_Reflect_Demo02 {
public static void main(String[] args) throws Exception {
// Class
Class c1 = Class.forName("com.itheima.reflect02.Person");
// ,
Constructor cns = c1.getDeclaredConstructor(int.class);
cns.setAccessible(true);
Object obj = cns.newInstance(34);
//
Field[] fields = c1.getFields();
//
Field[] fields2 = c1.getDeclaredFields();
for (Field field : fields2) {
System.out.println(field);
}
// ,
Field field = c1.getDeclaredField("name");
field.setAccessible(true);
field.set(obj, "java");
System.out.println(obj);
}
}
2.4メンバーの取得方法の反射
package com.itheima.reflect02;
/* * , */
public class Person {
//
private String name;
//
int age;
//
public Person() {
}
//
public Person(String name, int age) {
this.name = name;
this.age = age;
}
//
private Person(int age) {
this.age = age;
}
//
public void show(String s) {
System.out.println("show:" + s);
}
//
private String show2(String s) {
return " ";
}
@Override
public String toString() {
return "Person [name=" + name + ", age=" + age + "]";
}
}
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
/* * :Class * public Method[] getMethods(): , ; * public Method getMethod(String name,Class... parameterTypes): , 。 * public Method[] getDeclaredMethods(): ; * public Method getDeclaredMethod(String name,Class... parameterTypes): . * :Method * public Object invoke(Object obj,Object... args): obj , Object , object . */
public class My_Reflect_Demo03 {
public static void main(String[] args) throws Exception {
// Class
Class c1 = Class.forName("com.itheima.reflect02.Person");
// ,
Constructor cns = c1.getDeclaredConstructor();
Object obj = cns.newInstance();
//
Method[] methods = c1.getMethods();
// Method[] methods = c1.getDeclaredMethods();
//
Method method = c1.getDeclaredMethod("show2", String.class);
method.setAccessible(true);
Object result = method.invoke(obj, " ");
System.out.println(result); //
}
}
2.5反射ケース1
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.util.ArrayList;
/* * ArrayList<Integer> , */
public class My_Reflect_Test01 {
public static void main(String[] args) throws Exception {
ArrayList<Integer> list = new ArrayList<Integer>();
list.add(89);
// Class
Class c1 = list.getClass();
// add , E , object , object
Method method = c1.getDeclaredMethod("add", Object.class);
//
method.invoke(list, "java");
//
System.out.println(list);
}
}
2.6反射ケース2
import java.lang.reflect.Field;
/* * public void setProperty(Object obj, String propertyName, Object value){}, * obj propertyName value。 */
public class My_Reflect_Test02 {
public static void main(String[] args) throws Exception {
Person p = new Person();
MyTool mt = new MyTool();
mt.setProperty(p, "name", " ");
System.out.println(p);
}
}
//
class MyTool {
//
public void setProperty(Object obj, String propertyName, Object value) throws Exception {
// class
Class c = obj.getClass();
//
Field field = c.getDeclaredField(propertyName);
// true
field.setAccessible(true);
// obj value
field.set(obj, value);
}
}
3.動的エージェント
/* * : * : , , . * : , , * . * Java java.lang.reflect Proxy InvocationHandler , * 。JDK . * Proxy * public static Object newProxyInstance(ClassLoader loader,Class<?>[] interfaces,InvocationHandler h) * InvocationHandler * nvocationHandler * Object invoke(Object proxy,Method method,Object[] args) * * Proxy : * ClassLoader , ClassLoader ; * Interface , , * , ( ), ; * InvocationHandler , , InvocationHandler ;; * InvocationHandler , handler, * , InvocationHandler invoke . * * InvocationHandler invoke : * proxy: * method: * args: * * Proxy.newProxyInstance: * jvm , InvocationHandler , * , , , * $ ,proxy , , * System.out.println(u.getClass().getName()). */
public class My_Proxy_Demo01 {
public static void main(String[] args) {
}
}
ケースコード:
// dao
public interface StudentDao {
//
public abstract void login();
//
public abstract void regist();
}
// dao
public class StudentDaoImpl implements StudentDao {
@Override
public void login() {
System.out.println(" ");
}
@Override
public void regist() {
System.out.println(" ");
}
}
// InvocationHandler ,
public class MyInvocationHandler implements InvocationHandler {
private Object target; //
public MyInvocationHandler(Object target) {
this.target = target;
}
@Override
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable {
System.out.println(" ");
Object result = method.invoke(target, args);
System.out.println(" ");
return result; //
}
}
//
public class Test {
public static void main(String[] args) {
//
StudentDao sd = new StudentDaoImpl();
MyInvocationHandler handler2 = new MyInvocationHandler(sd);
StudentDao proxy2 = (StudentDao) Proxy.newProxyInstance(sd.getClass()
.getClassLoader(), sd.getClass().getInterfaces(), handler2);
proxy2.login(); // -- --
proxy2.regist(); // -- --
}
}