reflection

There are three ways to get reflections:

  • Class.forname (full Class name): bytecode file is loaded into memory, return to the Class object, = = full name of the Class with Class package name, in top linzeliang. Example. RelfectTest = = (how to configure file, the name of the Class definition in the configuration file, read the file first, access to the file name, and then load the Class)
  • The name of the class. The class: obtained by the class attribute of the class name (mostly used for parameter passing)
  • Object. GetClass (): Used to obtain bytecode for an object

The functionality of the Class object

Get member variables/member variables

  • Field[] getFields(): gets all variables modified by the public modifier, includingInheriting from a parent classProtected, default, private fields are not available
  • Field getField(String name): Gets the variable that the public modifier of the specified variable name modifies
  • Field[] getDeclaredFields(): Retrieves all member variables, regardless of modifiers (as long as a member variable can be retrieved)
  • Filed getDeclaredField(String name): Can get all fields of this class,Including private, butCannot get inheritanceField from. Only private is available herefield, but cannot access the private fieldvalueUnless addedsetAccessible(true))

Gets member methods/member methods

  • Method[] getMethods()
  • Mathod getMethod(String name, class <? >... parameterTypes)
  • Method[] getDeclaredMethods()
  • Method getDeclaredMethod(String name, class <? >... parameterTypes)

Get the constructors

  • Constructor<? >[] getConstructors()
  • Constructor < T > getConstructor (class <? >... parameterTypes)
  • Constructor < T > getDeclaredConstructor (class <? >... parameterTypes)
  • Constructor<? >[] getDeclaredConstructors()

Gets the full class name

  • String getName()

Field: member variable

  • void set(Object obj, Object value)The setting of the:
  • get(Object obj): get the value
  • setAccessible(true): Ignores security checks for access modifier, i.e., violent reflection

Method: indicates the member Method

  • invoke(Object obj, Object args): Execution method
  • String getName(): Gets the method name

(4) Constructor

  • newInstance(Object... initargs)Create an object
  • If you create an object using an empty parameter constructor, you can do so directlyThe Class object. NewInstance ()

Create objects through the framework

public class Demo {

	public static void main(String[] args) throws Exception {
		// Load the configuration file in the SRC directory
		Properties pro = new Properties();
		InputStream is = Demo.class.getClassLoader().getResourceAsStream("pro.properties");
		pro.load(is);
		
		// Get the data in the configuration file
		String className = pro.getProperty("className");
		// The class is loaded into memory with full name
		Class c = Class.forName(className);
		// Create an objectObject obj = c.newInstance(); System.out.println(obj); }}Copy the code
public class ReflectTest {
	public int a = 1;
	protected int b = 2;
	int c = 3;
	private int d = 4;

	public void m1(a) {
		System.out.println("miaoa");
	}
	protected void m2(a) {}void m3(a) {}private void m4(a) {}}Copy the code