java反射
1、涉及反射的一些方法
(1)getField getMethod getConstructor
getFields getMethods getConstructors
分别返回public域、方法和构造器数组,也包括其超类的共有成员
(2)getDeclareField getDeclareMethod getDeclaredConstructor
getDeclareFields getDeclareMethods getDeclaredConstructors
分别返回类中声明的全部域、方法和构造器数组,包括私有和受保护成员,但不包括超类的成员
public class Cat {
public String name = "tomcat";
public void hi(){
System.out.println("hi " + name);
}
}
①getField
Class cl = Class.forName("com.reflection.Cat");
Object o = cl.newInstance();
Field field = cl.getField("name");
System.out.println(field.get(o));
②getMethod
Class cl = Class.forName("com.reflection.Cat");
Object o = cl.newInstance();
Method method = cl.getMethod("hi");
method.invoke(o);
tip:对于调用静态方法时,不需要对象,在invoke()第一个参数设为 null
int s = 4;
Method me = Math.class.getMethod("sqrt", double.class);
System.out.println(me.invoke(null,s));
③getConstructor
Class cl = Class.forName("com.reflection.Cat");
Object o = cl.newInstance();
Constructor constructor = cl.getConstructor();
System.out.println(constructor);
tip:
①加s形式时,则会获得多组数据,以数组形式返回
public class Cat {
public String name = "tomcat";
public int age = 10;
public void hi(){
System.out.println("hi " + name);
}
}
Class cl = Class.forName("com.reflection.Cat");
Object o = cl.newInstance();
Field[] field = cl.getFields();
System.out.println(field[0].get(o));
System.out.println(field[1].get(o));
②当类中成员变量为私有时,无法用getFiled获取,可使用getDeclaredField,并添加setAccessible();
Class cl = Class.forName("com.reflection.Cat");
Object o = cl.newInstance();
Field field = cl.getDeclaredField("name");
field.setAccessible(true);
System.out.println(field.get(o));
③也可使用 类名+.class方式
例如:
Object o = com.reflection.Cat.class.newInstance();
Field field = com.reflection.Cat.class.getField("name");
System.out.println(field.get(o));
- 一个例子
package com.xqsoft.modules.test;
import org.junit.Test;
import org.springframework.web.bind.annotation.RestController;
import java.lang.reflect.Field;
import java.nio.channels.ScatteringByteChannel;
public class ReflectionTest {
@Test
public void method() throws Exception {
String args ="";
// Cat cat = new Cat();
// cat.scram();
// Class cl = Class.forName("com.xqsoft.modules.test.Cat");
// Cat o = (Cat) cl.newInstance();
// Field field = cl.getDeclaredField("age");
// field.setAccessible(true);
// field.set(o,"18");
// System.out.println(o);
// Cat cat = new Cat();
// cat.setAge("12");
// cat.setEye("brown");
// cat.setNose("tall");
// cat.setSex("NuM");
// System.out.println(cat);
// Cat cat = new Cat("12", "brown", "tall", "NuM");
String[] attributeValue = {"12","brown","tall","NuM"};
String[] attribute = {"age","eye","nose","sex"};
Cat cat = utilsMethod("com.xqsoft.modules.test.Cat", attribute, attributeValue, Cat.class);
System.out.println(cat);
String[] attributeValue1 = {"12","brown"};
String[] attribute1 = {"age","sex"};
Dog dog = utilsMethod("com.xqsoft.modules.test.Dog", attribute1, attributeValue1, Dog.class);
System.out.println(dog);
}
public <T> T utilsMethod (String args,String[] attribute,String[] attributeValue,Class<T> a) throws Exception{
Class cl = Class.forName(args);
T o = (T)cl.newInstance();
Field[] fields = cl.getDeclaredFields();
for(int i=0;i<fields.length;i++){
Field field = cl.getDeclaredField(attribute[i]);
field.setAccessible(true);
field.set(o,attributeValue[i]);
}
return o;
}
}
class Cat{
private String age;
private String sex;
private String eye;
private String nose;
public void scram(){
System.out.println("I am scraming");
}
@Override
public String toString() {
return "Cat{" +
"age='" + age + '\'' +
", sex='" + sex + '\'' +
", eye='" + eye + '\'' +
", nose='" + nose + '\'' +
'}';
}
public Cat() {
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getEye() {
return eye;
}
public void setEye(String eye) {
this.eye = eye;
}
public String getNose() {
return nose;
}
public void setNose(String nose) {
this.nose = nose;
}
}
class Dog{
private String age;
private String sex;
@Override
public String toString() {
return "Dog{" +
"age='" + age + '\'' +
", sex='" + sex + '\'' +
'}';
}
}