Java Reflection Common Classes

Source: Internet
Author: User

    1. Test entity classes
public class TestClass {public    String ClassInfo;    Public String GetClassInfo () {        return classinfo;    }    Public TestClass (String classinfo) {        this.classinfo = ClassInfo;        System.out.println ("Invoke this Param ClassInfo constructor.");    Public TestClass ()    {        System.out.println ("Invoke this default constructor.");    public void Setclassinfo (String classinfo) {        this.classinfo = ClassInfo;    }    static {        System.out.println ("Init first class.");    }    public void Showinfo ()    {        System.out.println ("This is the default Show info method.")    ;    public void Showinfo (String info)    {        System.out.println ("This is has Param method.");        System.out.println ("param context:" +info);}    }
    1. Testing the Implementation class
Import Org.junit.test;import Java.lang.reflect.constructor;import Java.lang.reflect.field;import Java.lang.reflect.method;public class Getclassinfotest {@Test public void Testdemo () throws Exception {/         * * 1. The first step in implementing reflection: Gets the type information of the object, and Java refers to the. class file that holds the type information * 2. Here is all the constructor methods for getting the object.         * 3. Find the required construction method.         * 4. Call the specified construction method to create the object.        */Class Clazz=class.forname ("TestClass");         /** * Get Construction method * Call Newinstance (...) equivalent to new TestClass (...)        */constructor[] constructors=clazz.getconstructors ();        System.out.println ("Constructor Item:" + ' \ n ');        Arraytostringitem (constructors);        Object test=constructors[0].newinstance ();        Object test2=constructors[1].newinstance ("Hello");        /** * Get Properties */field[] fields=clazz.getfields ();        System.out.println ("Field item:" + ' \ n ');        Arraytostringitem (fields);        Field Field=fields[0]; System.out.println ("Field name:" +field.geTname ());        System.out.println ("Get Dafault constructor field value:" +field.get (test));        System.out.println ("Get has Param constructor field value:" +field.get (test2));        /** * Acquisition Method */method[] methods=clazz.getmethods ();        System.out.println ("Method Item:" + ' \ n ');        Arraytostringitem (methods);    System.out.println ("Invoke" +methods[0].getname () + "Get Return Value:" +methods[0].invoke (test2)); } public void Arraytostringitem (object[] args) {for (Object C:args) {System.out.println (c        ); }    }}

  

Java Reflection Common Classes

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.