反射

获取class文件对象的三种方式

* 获取class文件对象的方式:
* A:Object类的getClass()方法
* B:数据类型的静态属性class
* C:Class类中的静态方法
* public static Class forName(String className);
*
* 3选1,如何选择?
* 开发用第3种。
* 因为第3种是一个字符串,而不是一个具体的类名,这样就可以把字符串配置到配置文件中

/*
 * 获取class文件对象的方式:
 * A:Object类的getClass()方法
 * B:数据类型的静态属性class
 * C:Class类中的静态方法
 * public static Class forName(String className);
 * 
 * 3选1,如何选择?
 * 开发用第3种。
 * 因为第3种是一个字符串,而不是一个具体的类名,这样就可以把字符串配置到配置文件中
 * */

public class ClientDemo {
	public static void main(String[] args) throws ClassNotFoundException {
		// 方法1
		Person p = new Person();
		Class c = p.getClass();

		Person p2 = new Person();
		Class c2 = p2.getClass();

		System.out.println(p == p2);// false
		System.out.println(c == c2);// true

		// 方法2
		Class c3 = Person.class;
		System.out.println(c == c3);// true

		// 方法3
		Class c4 = Class.forName("com.jacky.Person");// 必须带包名,右键Person,Copy Qualified Name
		System.out.println(c == c4);// true
	}
}

class Person {
	private String name;
	int age;
	public String address;

	public Person() {
	}

	public Person(String name, int age, String address) {
		this.name = name;
		this.age = age;
		this.address = address;
	}
}

* public Constructor<T> getConstructor(Class<?>... parameterTypes)
* throws NoSuchMethodException,
* SecurityException
* 返回一个Constructor对象,它反映此Class对象所表示的类的指定公共构造方法。
*
* public Constructor<?>[] getConstructors
* throws SecurityException
* 返回一个包含某些Constructor对象的数组,这些对象反映此Class对象所表示的类的所有公共构造方法。
*
* public Constructor<T> getDeclaredConstructor(Class<?>... parameterTypes)
throws NoSuchMethodException,
SecurityException
* 返回一个Constructor对象,它反映此Class对象所表示的类的指定构造方法。
*
* public Constructor<?>[] getDeclaredConstructors
* throws SecurityException
* 返回Constructor对象的一个数组,这些对象反映此Class对象表示的类声明的所有构造方法。
*
* public Field getDeclaredField(String name)
throws NoSuchFieldException,
SecurityException
获取单个成员
*
* public Field[] getDeclaredFields()
throws SecurityException
获取所有成员
*
* public Field[] getFields()
throws SecurityException
获取所有公共成员
*
* public T newInstance(Object... initargs)
* throws InstantiationException,
* IllegalAccessException,
* IllegalArgumentException,
* InvocationTargetException
* 使用此Constructor对象表示的构造方法来创建该构造方法的声明类的新实例,并用指定的初始化参数初始化该实例。
*
* public void set(Object obj,
Object value)
throws IllegalArgumentException,
IllegalAccessException
将指定对象变量上此Field对象表示的字段设置为指定的新值。

 

import java.lang.reflect.Constructor;

/*
 * public Constructor<T> getConstructor(Class<?>... parameterTypes)
 * throws NoSuchMethodException,
 * SecurityException
 * 返回一个Constructor对象,它反映此Class对象所表示的类的指定公共构造方法。
 * 
 * public Constructor<?>[] getConstructors
 * throws SecurityException
 * 返回一个包含某些Constructor对象的数组,这些对象反映此Class对象所表示的类的所有公共构造方法。
 * 
 * public Constructor<?>[] getDeclaredConstructors
 * throws SecurityException
 * 返回Constructor对象的一个数组,这些对象反映此Class对象表示的类声明的所有构造方法。
 * 
 * public T newInstance(Object... initargs)
 * throws InstantiationException,
 * IllegalAccessException,
 * IllegalArgumentException,
 * InvocationTargetException
 * 使用此Constructor对象表示的构造方法来创建该构造方法的声明类的新实例,并用指定的初始化参数初始化该实例。
 * */

public class ClientDemo {
	public static void main(String[] args) throws Exception {
		Class c = Class.forName("com.jacky.Person");

		Constructor constructor = c.getConstructor();
		Object obj = constructor.newInstance();
		System.out.println(obj);
		Person p = (Person) obj;
		p.show();

		System.out.println("----------------");

		Constructor cons1[] = c.getConstructors();
		for (Constructor con : cons1) {
			System.out.println(con);
		}

		System.out.println("----------------");

		Constructor cons2[] = c.getDeclaredConstructors();
		for (Constructor con : cons2) {
			System.out.println(con);
		}
	}
}

class Person {
	private String name;
	int age;
	public String address;

	public Person() {
	}

	public Person(String name, int age, String address) {
		this.name = name;
		this.age = age;
		this.address = address;
	}

	protected Person(int age) {
		this.age = age;
	}

	private Person(String name) {
		this.name = name;
	}

	Person(String name, int age) {// 没有public
		this.name = name;
		this.age = age;
	}

	public void show() {
		System.out.println("show");
	}
}

* public Constructor<T> getConstructor(Class<?>... parameterTypes)
* throws NoSuchMethodException,
* SecurityException
* 返回一个Constructor对象,它反映此Class对象所表示的类的指定公共构造方法。
*
* 通过反射获取带参构造方法并使用

import java.lang.reflect.Constructor;

/*
 * public Constructor<T> getConstructor(Class<?>... parameterTypes)
 * throws NoSuchMethodException,
 * SecurityException
 * 返回一个Constructor对象,它反映此Class对象所表示的类的指定公共构造方法。
 * 
 * 通过反射获取带参构造方法并使用
 * */

public class ClientDemo {
	public static void main(String[] args) throws Exception {
		Class c = Class.forName("com.jacky.Person");

		Constructor constructor = c.getConstructor(String.class, int.class, String.class);
		Object obj = constructor.newInstance("呵呵", 22, "哈哈");
		System.out.println(obj);
	}
}

class Person {
	private String name;
	int age;
	public String address;

	public Person() {
	}

	public Person(String name, int age, String address) {
		this.name = name;
		this.age = age;
		this.address = address;
	}

	protected Person(int age) {
		this.age = age;
	}

	private Person(String name) {
		this.name = name;
	}

	Person(String name, int age) {// 没有public
		this.name = name;
		this.age = age;
	}

	public void show() {
		System.out.println("show");
	}
}

* public Constructor<T> getDeclaredConstructor(Class<?>... parameterTypes)
throws NoSuchMethodException,
SecurityException
* 返回一个Constructor对象,它反映此Class对象所表示的类的指定构造方法。

通过反射获取私有构造方法并使用

import java.lang.reflect.Constructor;

/*
* public Constructor<T> getConstructor(Class<?>... parameterTypes)
* throws NoSuchMethodException,
* SecurityException
* 返回一个Constructor对象,它反映此Class对象所表示的类的指定公共构造方法。
* 
* public Constructor<T> getDeclaredConstructor(Class<?>... parameterTypes)
                                      throws NoSuchMethodException,
                                             SecurityException
* 返回一个Constructor对象,它反映此Class对象所表示的类的指定构造方法。
* 
* public Constructor<?>[] getConstructors
* throws SecurityException
* 返回一个包含某些Constructor对象的数组,这些对象反映此Class对象所表示的类的所有公共构造方法。
* 
* public Constructor<?>[] getDeclaredConstructors
* throws SecurityException
* 返回Constructor对象的一个数组,这些对象反映此Class对象表示的类声明的所有构造方法。
* 
* public T newInstance(Object... initargs)
* throws InstantiationException,
* IllegalAccessException,
* IllegalArgumentException,
* InvocationTargetException
* 使用此Constructor对象表示的构造方法来创建该构造方法的声明类的新实例,并用指定的初始化参数初始化该实例。
 * */

public class ClientDemo {
	public static void main(String[] args) throws Exception {
		Class c = Class.forName("com.jacky.Person");

		Constructor constructor = c.getDeclaredConstructor(String.class, int.class, String.class);
		Object obj = constructor.newInstance("呵呵", 22, "哈哈");
		System.out.println(obj);
	}
}

class Person {
	private String name;
	int age;
	public String address;

	public Person() {
	}

	public Person(String name, int age, String address) {
		this.name = name;
		this.age = age;
		this.address = address;
	}

	protected Person(int age) {
		this.age = age;
	}

	private Person(String name) {
		this.name = name;
	}

	Person(String name, int age) {// 没有public
		this.name = name;
		this.age = age;
	}

	public void show() {
		System.out.println("show");
	}
}

* public Field[] getDeclaredFields()
throws SecurityException
获取所有成员
*
* public Field[] getFields()
throws SecurityException
获取所有公共成员

import java.lang.reflect.Field;

/*
* public Constructor<T> getConstructor(Class<?>... parameterTypes)
* throws NoSuchMethodException,
* SecurityException
* 返回一个Constructor对象,它反映此Class对象所表示的类的指定公共构造方法。
* 
* public Constructor<?>[] getConstructors
* throws SecurityException
* 返回一个包含某些Constructor对象的数组,这些对象反映此Class对象所表示的类的所有公共构造方法。
* 
* public Constructor<T> getDeclaredConstructor(Class<?>... parameterTypes)
                                      throws NoSuchMethodException,
                                             SecurityException
* 返回一个Constructor对象,它反映此Class对象所表示的类的指定构造方法。
* 
* public Constructor<?>[] getDeclaredConstructors
* throws SecurityException
* 返回Constructor对象的一个数组,这些对象反映此Class对象表示的类声明的所有构造方法。
* 
* public Field getDeclaredField(String name)
                       throws NoSuchFieldException,
                              SecurityException
                              获取单个成员
* 
* public Field[] getDeclaredFields()
                          throws SecurityException
                  获取所有成员
* 
* public Field[] getFields()
                  throws SecurityException
                  获取所有公共成员
* 
* public T newInstance(Object... initargs)
* throws InstantiationException,
* IllegalAccessException,
* IllegalArgumentException,
* InvocationTargetException
* 使用此Constructor对象表示的构造方法来创建该构造方法的声明类的新实例,并用指定的初始化参数初始化该实例。
* 
* public void set(Object obj,
                Object value)
         throws IllegalArgumentException,
                IllegalAccessException
                将指定对象变量上此Field对象表示的字段设置为指定的新值。
 * */

public class ClientDemo {
	public static void main(String[] args) throws Exception {
		Class c = Class.forName("com.jacky.Person");
		
		Field fields1[]=c.getFields();
		for(Field field:fields1)
		{
			System.out.println(field);
		}
		
		System.out.println("----------------");
		
		Field fields2[]=c.getDeclaredFields();
		for(Field field:fields2)
		{
			System.out.println(field);
		}
	}
}

class Person {
	private String name;
	int age;
	public String address;

	public Person() {
	}

	public Person(String name, int age, String address) {
		this.name = name;
		this.age = age;
		this.address = address;
	}

	protected Person(int age) {
		this.age = age;
	}

	private Person(String name) {
		this.name = name;
	}

	Person(String name, int age) {// 没有public
		this.name = name;
		this.age = age;
	}

	public void show() {
		System.out.println("show");
	}
}

通过反射越过泛型检查

如何在这个集合ArrayList<Integer>添加一个字符串常量?

import java.lang.reflect.Method;
import java.util.ArrayList;

/*
 * 如何在这个集合ArrayList<Integer>添加一个字符串常量?
 * */

public class Test {
	public static void main(String[] args) throws Exception {

		ArrayList<Integer> array = new ArrayList<Integer>();

		Class c = array.getClass();
		Method m = c.getMethod("add", Object.class);

		m.invoke(array, "hello");
		m.invoke(array, "world");
		m.invoke(array, "java");

		System.out.println(array);
	}
}
原文地址:https://www.cnblogs.com/denggelin/p/6354389.html