Java反射实例

实体类:

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;
		//System.out.println("运行构造器1");
	}
	public CityInfo() {
		//System.out.println("运行构造器2");
	}
	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反射

/**
 * 
 * @函数名称:reflectTest
 * @创建日期:2019年10月21日
 * @功能说明:三种方式获取reflect class
 * @参数说明:
 * @返回说明:void
 */
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.获取构造器,其中可以获取构造器参数个数,构造名等等

/**
 * 
 * @函数名称:reflectConstructor
 * @创建日期:2019年10月21日
 * @功能说明:获取构造器,其中可以获取构造器参数个数,构造名等等
 * @参数说明:
 * @返回说明:void
 */
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

/**
	 * 
	 * @函数名称:reflectClass
	 * @创建日期:2019年10月21日
	 * @功能说明:获取class中类属性
	 * @参数说明:
	 * @返回说明:void
	 */
	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();
		//getFields可以获取私有和共有类
		for(Field field	: fields) {
			System.out.println(field);
		}
		System.out.println("-------用DeclaredFields获取类共有,私有属性------");
		//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);//private无法修改,设置强制权限
		field.set(cityInfo, "深圳");
		System.out.println("强制设置属性cityName为 -- "+cityInfo.getCityName());
	}

4.反射私有方法

/**
 * 
 * @函数名称:reflectPrivateMethod
 * @创建日期:2019年10月21日
 * @功能说明:反射私有方法
 * @参数说明:
 * @返回说明:void
 */
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);
	//newInstance 新建实例
	Object object = crf.newInstance();
	//invoke激活某个函数
	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;

/**
 * 
 * @功能说明:反射reflect Main
 * @创建人员:HjwJames
 * @变更记录:<BR>
 * 1、2019年10月21日 HjwJames 新建类
 */
public class Main {
	public static void main(String args[]) throws Exception {
		System.out.println("start");
		//第一问:如何获取Class反射?
		//reflectGetClass();
		//第二问:获取构造器
		//reflectConstructor();
		//第三问:获取类信息
		//reflectClass();
		//第四问:获取私有类
		//reflectPrivateMethod();
		//第五问:如何进入已封装的私有类
		//??待解决
	}
	
	/**
	 * 
	 * @函数名称:reflectTest
	 * @创建日期:2019年10月21日
	 * @功能说明:三种方式获取reflect class
	 * @参数说明:
	 * @返回说明:void
	 */
	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();
		}
	}
	/**
	 * 
	 * @throws SecurityException 
	 * @throws NoSuchMethodException 
	 * @throws InvocationTargetException 
	 * @throws IllegalArgumentException 
	 * @throws IllegalAccessException 
	 * @throws InstantiationException 
	 * @函数名称:reflectConstructor
	 * @创建日期:2019年10月21日
	 * @功能说明:获取构造器,其中可以获取构造器参数个数,构造名等等
	 * @参数说明:
	 * @返回说明:void
	 */
	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());
	}
	/**
	 * 
	 * @throws SecurityException 
	 * @throws NoSuchFieldException 
	 * @throws IllegalAccessException 
	 * @throws IllegalArgumentException 
	 * @函数名称:reflectClass
	 * @创建日期:2019年10月21日
	 * @功能说明:获取class中类属性
	 * @参数说明:
	 * @返回说明:void
	 */
	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();
		//getFields可以获取私有和共有类
		for(Field field	: fields) {
			System.out.println(field);
		}
		System.out.println("-------用DeclaredFields获取类共有,私有属性------");
		//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);//private无法修改,设置强制权限
		field.set(cityInfo, "深圳");
		System.out.println("强制设置属性cityName为 -- "+cityInfo.getCityName());
	}
	/**
	 * 
	 * @throws ClassNotFoundException 
	 * @throws SecurityException 
	 * @throws NoSuchMethodException 
	 * @throws IllegalAccessException 
	 * @throws InstantiationException 
	 * @throws InvocationTargetException 
	 * @throws IllegalArgumentException 
	 * @函数名称:reflectPrivateMethod
	 * @创建日期:2019年10月21日
	 * @功能说明:反射私有方法
	 * @参数说明:
	 * @返回说明:void
	 */
	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);
		//newInstance 新建实例
		Object object = crf.newInstance();
		//invoke激活某个函数
		privateMethod.invoke(object);
	}

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值