How to reflect in Java to get a class _java

Source: Internet
Author: User

Reflection is to get all the information of a class, including both methods and properties.

1. The method of acquisition includes obtaining the name of the method, the return type of the method, the access modifier of the method, and the execution of this method through reflection.

2. Get the property including the name of the property, type, access modifier, and the value of this property.

All of these gains have the appropriate API to provide operations.

The code is as follows:

Package poi;
Import Java.lang.reflect.Constructor;
Import Java.lang.reflect.Field;
Import Java.lang.reflect.Method;
Import Java.lang.reflect.Modifier;
Import org.apache.poi.xwpf.usermodel.XWPFSettings; public class Reflectmain {public static void main (string[] arg) throws SecurityException, Nosuchfieldexception, Illegala Rgumentexception, Illegalaccessexception, classnotfoundexception, instantiationexception{XWPFSettings ct = new
 Xwpfsettings ();
 Class C = Ct.getclass ();
 System.out.println ("---------------------the member variable-----------------------of the specified class");
 System.out.println ("Number of member variables for reflected class");
 System.out.println (C.getdeclaredfields (). length);
  For (Field Fil:c.getdeclaredfields ()) {System.out.print (Fil.gettype () + "");
 System.out.println (Fil.getname ());
 } System.out.println ("Method of constructing------------------------class-----------------------"); For (constructor constructor:c.getdeclaredconstructors ()) {System.out.print (modifier.tostring (
  Constructor.getmodifiers ()) + ""); System.out.println(Constructor.getname ());
 } System.out.println ("--------------------------member Method--------------------------");
  For (Method Method:c.getdeclaredmethods ()) {System.out.print (modifier.tostring (Method.getmodifiers ()) + "");
  System.out.print (Method.getreturntype () + "");
 System.out.println (Method.getname ());
 } System.out.println ("Modifiers------------------------for---------------------------class");
 int mod = C.getmodifiers ();
 String modifier = modifier.tostring (mod);
 System.out.println ("modifier =" + modifier);
 System.out.println ("------------------------the fully qualified name--------------------of the specified class");
 System.out.println (C.getname ());
 System.out.println ("------------------------the parent class qualified name--------------------" of the specified class);
 System.out.println (C.getsuperclass (). GetName ()); }
}

The above is a description of how the Java reflection to get a class of all the content, I hope that the future study of everyone to help, but also hope to work with you to learn and progress.

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.