Java uses class reflection to get all the information about a class-code instances

Source: Internet
Author: User

Package Huang.de.wei;import Java.lang.annotation.annotation;import Java.lang.reflect.constructor;import Java.lang.reflect.Method, @SuppressWarnings (value= "Unchecked") @Deprecatedpublic class Classtest {// The private constructor for this class is defined privately classtest () {}//defines a parameterized constructor public classtest (String name) {System.out.println ("This is a private constructor");} Define an info method without parameters public void info () {System.out.println ("This is a parameterless info method");} public void info (String str) { System.out.println ("This is a parameter of the info method, the parameter is:" +STR); }//Define an internal test class inner{} public static void Main (string[] args) throws SecurityException, Nosuchmethodexception, Clas snotfoundexception{//The following code can get Classtest to drink the Class class<classtest> clazz=classtest.class; Gets all constructors for the class object constructor[] Ctors=clazz.getdeclaredconstructors (); System.out.println ("Constructors of classes are all as follows"); for (Constructor c:ctors) {System.out.println (c);}//Gets all the public constructors corresponding to the class object System.out.println (" All public constructors corresponding to the class object: "); Constructor[] Publicctors=clazz.getconstructors (); For (Constructor C:p ublicctors) {System. OUT.PRINTLN (c); }//Get all public methods corresponding to the Calss object method[] Mtds=clazz.getmethods (); SYSTEM.OUT.PRINTLN ("All public methods of Classtest are as follows:"); For (method C:mtds) {System.out.println (c);}//Gets the specified method of the class object corresponding to System.out.println ("classtest with a string in the info method is:" + Clazz.getmethod ("info", String.class)); System.out.println ("Classtest with no string in the info method is:" +clazz.getmethod ("info")); Get all comments on classes corresponding to class object annotation[] Anns=clazz.getannotations (); System.out.println ("All notes of Classtest are as follows"); for (Annotation C:anns) {System.out.println (c);} System.out.println ("Note: annotation information cannot find @suppersswarnings information"); System.out.println ("The @suppersswarnings comment on the class element is:" +clazz.getannotation (Suppresswarnings.class)); Gets the entire inner class of the class object class<?>[] Inners=clazz.getdeclaredclasses (); SYSTEM.OUT.PRINTLN ("All internal classes of the Classtest are as follows:"); for (class C:inners) {System.out.println (c);}//Use Class.forName method to load classtest inner inner class Inclazz =class.forname ( "Huang.de.wei.classtest$inner"); Access the external class System.out.println ("Inclaz") where the class resides through Getdeclaringclass ()The outer class of the corresponding class is: "+ inclazz.getdeclaringclass ()); System.out.println ("Classtest's package is:" +clazz.getpackage ()); System.out.println ("Classtest's parent class is:" +clazz.getsuperclass ());//Todo:handle Exception}}


The result of the run (the exception that is thrown may be different):

The constructors for the class are all as follows
Private Huang.de.wei.ClassTest ()
Public Huang.de.wei.ClassTest (java.lang.String)
All public constructors for the class object:
Public Huang.de.wei.ClassTest (java.lang.String)
The full public method for Classtest is as follows:
public static void Huang.de.wei.ClassTest.main (java.lang.string[]) throws Java.lang.SecurityException, Java.lang.nosuchmethodexception,java.lang.classnotfoundexception
public void Huang.de.wei.ClassTest.info ()
public void Huang.de.wei.ClassTest.info (java.lang.String)
Public final void Java.lang.Object.wait () throws Java.lang.InterruptedException
Public final void Java.lang.Object.wait (Long,int) throws Java.lang.InterruptedException
Public final native void Java.lang.Object.wait (long) throws Java.lang.InterruptedException
public native int Java.lang.Object.hashCode ()
Public final native Java.lang.Class Java.lang.Object.getClass ()
public boolean java.lang.Object.equals (Java.lang.Object)
Public java.lang.String java.lang.Object.toString ()
Public final native void Java.lang.Object.notify ()
Public final native void Java.lang.Object.notifyAll ()
Classtest with a string inside the info method is: public void Huang.de.wei.ClassTest.info (java.lang.String)
Classtest the Info method without a string is: public void Huang.de.wei.ClassTest.info ()
The full comments of Classtest are as follows
@java. lang.deprecated ()
Note: The annotation information cannot be found @suppersswarnings information
The @suppersswarnings comment on the class element is: null
The full inner classes of classtest are as follows:
Class Huang.de.wei.classtest$inner
The outer class of the Inclazz class is: Class Huang.de.wei.ClassTest
Classtest Packages: Package Huang.de.wei
Classtest's parent class is: Class Java.lang.Object

Java uses class reflection to get all the information about a class-code instances

Related Article

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.