实体类:
package reflect;
public class CityInfo {
private java.lang.String cityCode;
private java.lang.String cityName;
private java.lang.String cityNameEn;
private java.lang.String countryCode;
public String publicColumn;
public String getPublicColumn() {
return publicColumn;
}
public void setPublicColumn(String publicColumn) {
this.publicColumn = publicColumn;
}
private CityInfo(String cityCode,String cityName) {
this.cityCode = cityCode;
this.cityName = cityName;
}
public CityInfo() {
}
public java.lang.String getCityCode() {
return cityCode;
}
public void setCityCode(java.lang.String cityCode) {
this.cityCode = cityCode;
}
public java.lang.String getCityName() {
return cityName;
}
public void setCityName(java.lang.String cityName) {
this.cityName = cityName;
}
public java.lang.String getCityNameEn() {
return cityNameEn;
}
public void setCityNameEn(java.lang.String cityNameEn) {
this.cityNameEn = cityNameEn;
}
public java.lang.String getCountryCode() {
return countryCode;
}
public void setCountryCode(java.lang.String countryCode) {
this.countryCode = countryCode;
}
private void reflectprivateMethod() {
System.out.println("对象参数是"+this.getCityCode()+" "+this.getCityName()+" "+this.getCityNameEn()+" "+this.getCountryCode()+" "+this.getPublicColumn());
}
}
1.三种方法获取class反射
public static void reflectGetClass() {
CityInfo cityInfo = new CityInfo();
Class crf = cityInfo.getClass();
System.out.println(crf.getName());
Class crf2 = CityInfo.class;
System.out.println(crf2.getName());
try {
Class crf3 = Class.forName("reflect.CityInfo");
System.out.println(crf3.getName());
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
2.获取构造器,其中可以获取构造器参数个数,构造名等等
public static void reflectConstructor() throws NoSuchMethodException, SecurityException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
System.out.println("---------------获取构造器----------------");
CityInfo cityInfo = new CityInfo();
Class crf = cityInfo.getClass();
Constructor[] constructors = crf.getDeclaredConstructors();
for(Constructor constructor:constructors) {
System.out.println(constructor);
}
Constructor<?> declaredConstructor = crf.getDeclaredConstructor(String.class,String.class);
declaredConstructor.setAccessible(true);
Object objectCity = declaredConstructor.newInstance("SZX","深圳");
cityInfo = (CityInfo) objectCity;
System.out.println("私有构造函数强制"+cityInfo.getCityCode() + cityInfo.getCityName());
}
3.获取class中类属性type / name
public static void reflectClass() throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {
System.out.println("---------------获取类属性----------------");
CityInfo cityInfo = new CityInfo();
Class crf = cityInfo.getClass();
System.out.println("-------用getFields获取类共有属性------");
Field[] fields = crf.getFields();
for(Field field : fields) {
System.out.println(field);
}
System.out.println("-------用DeclaredFields获取类共有,私有属性------");
Field[] defields = crf.getDeclaredFields();
for(Field defield:defields) {
System.out.println(defield.getType() + " " +defield.getName());
}
System.out.println("-------获取类共有,私有属性------");
Field field = crf.getDeclaredField(defields[1].getName());
field.setAccessible(true);
field.set(cityInfo, "深圳");
System.out.println("强制设置属性cityName为 -- "+cityInfo.getCityName());
}
4.反射私有方法
public static void reflectPrivateMethod() throws ClassNotFoundException, NoSuchMethodException, SecurityException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
Class<?> crf = Class.forName("reflect.CityInfo");
Method privateMethod = crf.getDeclaredMethod("reflectprivateMethod");
privateMethod.setAccessible(true);
Object object = crf.newInstance();
privateMethod.invoke(object);
}
完整代码
package reflect;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
public class Main {
public static void main(String args[]) throws Exception {
System.out.println("start");
}
public static void reflectGetClass() {
CityInfo cityInfo = new CityInfo();
Class crf = cityInfo.getClass();
System.out.println(crf.getName());
Class crf2 = CityInfo.class;
System.out.println(crf2.getName());
try {
Class crf3 = Class.forName("reflect.CityInfo");
System.out.println(crf3.getName());
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
public static void reflectConstructor() throws NoSuchMethodException, SecurityException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
System.out.println("---------------获取构造器----------------");
CityInfo cityInfo = new CityInfo();
Class crf = cityInfo.getClass();
Constructor[] constructors = crf.getDeclaredConstructors();
for(Constructor constructor:constructors) {
System.out.println(constructor);
}
Constructor<?> declaredConstructor = crf.getDeclaredConstructor(String.class,String.class);
declaredConstructor.setAccessible(true);
Object objectCity = declaredConstructor.newInstance("SZX","深圳");
cityInfo = (CityInfo) objectCity;
System.out.println("私有构造函数强制"+cityInfo.getCityCode() + cityInfo.getCityName());
}
public static void reflectClass() throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {
System.out.println("---------------获取类属性----------------");
CityInfo cityInfo = new CityInfo();
Class crf = cityInfo.getClass();
System.out.println("-------用getFields获取类共有属性------");
Field[] fields = crf.getFields();
for(Field field : fields) {
System.out.println(field);
}
System.out.println("-------用DeclaredFields获取类共有,私有属性------");
Field[] defields = crf.getDeclaredFields();
for(Field defield:defields) {
System.out.println(defield.getType() + " " +defield.getName());
}
System.out.println("-------获取类共有,私有属性------");
Field field = crf.getDeclaredField(defields[1].getName());
field.setAccessible(true);
field.set(cityInfo, "深圳");
System.out.println("强制设置属性cityName为 -- "+cityInfo.getCityName());
}
public static void reflectPrivateMethod() throws ClassNotFoundException, NoSuchMethodException, SecurityException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
Class<?> crf = Class.forName("reflect.CityInfo");
Method privateMethod = crf.getDeclaredMethod("reflectprivateMethod");
privateMethod.setAccessible(true);
Object object = crf.newInstance();
privateMethod.invoke(object);
}
}