Java 反射的主要功能时用来做框架 别的时候可能我们很少用到
要点
1 得到类字节码对象
Class.forName(类的完整路径); Class.forName("com.gui.test.Person");
对象.getClass(); person.getClass();
类名.lass Person.class
2 反射构造函数(目的 实例化对象)
a.public Constructor clazz.getConstructor("methodName",Param<Class>...);(没有参数 null)
b.private Constructor clazz.getDeclaredMethod("methodName",Param<Class>...);
constructor.setAccessAble(boolean) //私有类型暴力访问
3 反射方法
a.public方法 clazz.getMethod("methodName",Param<Class>...);(没有参数 null)
b.private 方法 clazz.getdeclareMethod("methodName",Param<Class>...);
method.setAccessAble(boolean) //私有类型暴力访问
c main方法(兼容jdk1.4) clazz.getdeclareMethod("main",(Object)new String[]{String...str});
clazz.getDeclaredMethod("main",Object[]{new String[]{String...str}});
4.反射字段
a.public Field clazz.getField("fieldName");
b.private Field clazz.getDeclaredField("fieldName"); field.setAccessAble(boolean) //私有类型暴力访问
c static Field 参照 public 和 private
Example:
package com.gui.test;
public class Person {
public String name = "gui";
private int age = 1;
public static String sex = "N";
public Person() {
System.out.println("Person()");
}
public Person(int i) {
System.out.println("Person(int) " + i);
}
public Person(String str, int i) {
System.out.println("Person(stringint) " + str +" " + i);
}
public void play(){
System.out.println("play");
}
public void run(){
System.out.println("run");
}
public void jump(){
System.out.println("jump");
}
private void run(String str){
System.out.println("run " + str);
}
public int age() {
return 1;
}
public void play(String ball){
System.out.println("paly ball");
}
public void run(int mile){
System.out.println("run mile");
}
public void jump(String name, int high){
System.out.println("jump " + name + " " + high);
}
public int age(int age) {
System.out.println("age " + age);
return 1;
}
public static void main(String[] args) {
System.out.println("main");
}
}
package com.gui.test;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import org.junit.Before;
import org.junit.Test;
public class PersonReflect {
Class clazz;
@Before
public void before() throws ClassNotFoundException {
clazz = Class.forName("com.gui.test.Person");
}
// public Person();
@Test
public void test1() throws SecurityException, NoSuchMethodException, IllegalArgumentException, InstantiationException, IllegalAccessException, InvocationTargetException {
Constructor constructor = clazz.getConstructor(null);
Person person = (Person) constructor.newInstance();
person.run();
}
// public Person(int i)
@Test
public void test2() throws ClassNotFoundException, SecurityException, NoSuchMethodException, IllegalArgumentException, InstantiationException, IllegalAccessException, InvocationTargetException {
Constructor constructor = clazz.getConstructor(int.class);
Person person = (Person) constructor.newInstance(2);
person.run();
}
// public Person(String str, int i)
@Test
public void test3() throws ClassNotFoundException, SecurityException, NoSuchMethodException, IllegalArgumentException, InstantiationException, IllegalAccessException, InvocationTargetException {
Constructor constructor = clazz.getConstructor(String.class, int.class);
Person person = (Person) constructor.newInstance("gui", 2);
person.run();
}
// public void run(){
@Test
public void test4() throws ClassNotFoundException, SecurityException, NoSuchMethodException, IllegalArgumentException, InstantiationException, IllegalAccessException, InvocationTargetException {
Person person = (Person) clazz.newInstance();
person.run();
Method method = clazz.getMethod("run", null);
method.invoke(person, null);
}
// public void run(int){
@Test
public void test5() throws ClassNotFoundException, SecurityException, NoSuchMethodException, IllegalArgumentException, InstantiationException, IllegalAccessException, InvocationTargetException {
Person person = (Person) clazz.newInstance();
Method method = clazz.getMethod("run", int.class);
method.invoke(person, 12);
}
// private void run(String){
@Test
public void test6() throws ClassNotFoundException, SecurityException, NoSuchMethodException, IllegalArgumentException, InstantiationException, IllegalAccessException, InvocationTargetException {
Person person = (Person) clazz.newInstance();
Method method = clazz.getDeclaredMethod("run", String.class);
method.setAccessible(true);
method.invoke(person, "12");
}
// public main 1
@Test
public void test7() throws ClassNotFoundException, SecurityException, NoSuchMethodException, IllegalArgumentException, InstantiationException, IllegalAccessException, InvocationTargetException {
Person person = (Person) clazz.newInstance();
Method method = clazz.getMethod("main", String[].class);
method.invoke(person, (Object)new String[]{"2","1"});
}
// public main 2
@Test
public void test8() throws ClassNotFoundException, SecurityException, NoSuchMethodException, IllegalArgumentException, InstantiationException, IllegalAccessException, InvocationTargetException {
Person person = (Person) clazz.newInstance();
Method method = clazz.getMethod("main", String[].class);
method.invoke(person, new Object[]{new String[]{"2","1"}});
}
// src/config.txt 框架原理
@Test
public void test9() throws ClassNotFoundException, SecurityException, NoSuchMethodException, IllegalArgumentException, InstantiationException, IllegalAccessException, InvocationTargetException, IOException {
BufferedReader br = new BufferedReader(new FileReader("src/config.txt"));
String className = br.readLine();
String methodName = br.readLine();
Class clazz1 = Class.forName(className);
Person person = (Person) clazz.newInstance();
Method method = clazz.getMethod(methodName, null);
method.invoke(person, null);
}
// public String name
@Test
public void test10() throws ClassNotFoundException, SecurityException, NoSuchMethodException, IllegalArgumentException, InstantiationException, IllegalAccessException, InvocationTargetException, NoSuchFieldException {
Person person = (Person) clazz.newInstance();
Field field = clazz.getField("name");
if(field.getType().equals(String.class))
System.out.println(field.get(person));
}
}
src/config.txt
com.gui.test.Person
run