Background knowledge
- Class (The basis of reflection mechanism implementation)
Class in Java is a special class that, when the JVM loads bytecode files, creates a corresponding class class object in the method area for each class, which contains information about the class: Methods, fields, annotations, and so on.
What is it
You can get information about a class dynamically at run time. (e.g., methods defined within a class, fields, annotations, etc.)
What's the use?
Can improve the scalability of the program, did not add a new class before you need to modify the original program to be compatible with the new class, now only need to be reflected to get the new custom class information, do not need to modify the original program every time.
Programming instances, the code contains implementations of the following sections.
- How to get information about a class (three methods)
- How to pass constructor string parameters
- How to get to a specified function and execute the
First, you first define a class for reflection
Packagepriv.darrenqiao.reflection;/** @Author: Darrenqiao **/ Public classReflectionclass { PublicString name; PublicInteger age; PrivateString result; Public voidtestname () {} PublicReflectionclass (String result) {Super(); This. result =result; } Public voidShow (String test) {System.out.println ("Show result" + This. Result + "and" +test); } Public voidtestage () {}}
Then we implement the reflection mechanism through the Main method class package priv.darrenqiao.reflection;
ImportJava.lang.reflect.Constructor;Importjava.lang.reflect.InvocationTargetException;ImportJava.lang.reflect.Method;/** @Author: Darrenqiao **/ Public classMain { Public Static voidMain (string[] args)throwsclassnotfoundexception, Nosuchmethodexception, SecurityException, Instantiationexception, Illegalaccessexception, IllegalArgumentException, invocationtargetexception {
//Get classMethod OneReflectionclass reflection =NewReflectionclass ("Success"); Class<?> classinfoforreflection =Reflection.getclass (); //Get classMethod Two//class<reflectionclass> classinfoforreflection = Reflectionclass.class; //Get classmethod Three, more commonly used is this, because the following class full path can be placed as a configuration item in the configuration file dynamically get//class<?> classinfoforreflection = Class.forName ("Priv.darrenqiao.reflection.ReflectionClass");method[] Methods=Classinfoforreflection.getmethods (); System.out.println ("Methods for Class Classinfoforreflection"); for(Method m:methods) {System.out.println (M.getname ()); } //Passing parameters by constructor functionClass<reflectionclass> Classinfoforreflectionclass = Reflectionclass.class; Constructor<?> C = Classinfoforreflectionclass.getconstructor (String.class);
Object Reflectionobject= (Object) c.newinstance ("Failed"); //executes the specified functionmethod = Classinfoforreflectionclass.getmethod ("Show", String.class); Method.invoke (Reflectionobject,"Test Final"); }}
Java Reflection mechanism