Java Reflection mechanism

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.