Summary of common methods for Java reflection get class details _java

Source: Internet
Author: User
Tags constructor pack reflection

Class Reflectiondemo

Copy Code code as follows:

Package Reflection;

@Deprecated
public class Reflectiondemo {
Private String Pri_field;
Public String Pub_field;
Public Reflectiondemo () {}
Public Reflectiondemo (String name) {}
Private Reflectiondemo (String name,int int1) {}
public void ReflectMethod () {}
public void ReflectMethod (String str) {}
private void ReflectMethod (int int1) {}
Class innerclss_relfection{}

}


Test class Reflectiondemotest


Copy Code code as follows:

Package Reflection;

Import java.lang.annotation.Annotation;
Import Java.lang.reflect.Constructor;
Import Java.lang.reflect.Field;
Import Java.lang.reflect.Method;

public class Reflectiondemotest {
public static void Main (string[] args) {
Creating class objects, using generic adornments to avoid strong-turn
Class Cla=reflectiondemo.class;
Methods to get all common domains
Field[] Field=cla.getfields ();
for (Field F:field) {
System.out.println ("Get all common domain methods:" +f.tostring ());
}
Gets the method of a specified public domain
try {
Field Field1=cla.getfield ("Pub_field");
System.out.println ("Get the method of the specified public domain:" +field1.tostring ());
catch (SecurityException e) {
TODO auto-generated Catch block
E.printstacktrace ();
catch (Nosuchfieldexception e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
Gets the method for all fields (including private fields), Declare
Field[] Field2=cla.getdeclaredfields ();
for (Field f:field2) {
System.out.println ("Get all of the domain's methods (including private domain):" +f.tostring ());
}
Gets the method for the specified domain (including private domain)
try {
Field Field3=cla.getdeclaredfield ("Pri_field");
System.out.println ("Get the method of the specified domain (including private domain):" +field3.tostring ());
catch (SecurityException e) {
TODO auto-generated Catch block
E.printstacktrace ();
catch (Nosuchfieldexception e) {
TODO auto-generated Catch block
E.printstacktrace ();
}

Get methods for all public methods (including parent classes)
Method[] Method=cla.getmethods ();
For (method M:method) {
System.out.println ("Methods to obtain all public methods:" +m.tostring ());
}
try {
Gets the specified parameter-free method
The second parameter represents the parameter type, and the parameter null represents the parameterless method
Method Method1=cla.getmethod ("ReflectMethod", null);
System.out.println ("Method of obtaining a parameterless public method:" +method1.tostring ());
catch (SecurityException e) {
TODO auto-generated Catch block
E.printstacktrace ();
catch (Nosuchmethodexception e) {
TODO auto-generated Catch block
E.printstacktrace ();
}

Gets the method for all methods of the class (excluding the parent class)
Method[] Method2=cla.getdeclaredmethods ();
For (method M:method2) {
System.out.println ("Get all methods (including parent classes):" +m.tostring ());
}

Gets the method (excluding the parent class) of the method specified by the class
The first argument is the method name, the second parameter is the method return type, and Null is the parameterless method
try {
Method Method3=cla.getdeclaredmethod ("ReflectMethod", Int.class);
System.out.println ("Gets the method (excluding the parent class) of the method specified by the class:" +method3);
catch (SecurityException e) {
TODO auto-generated Catch block
E.printstacktrace ();
catch (Nosuchmethodexception e) {
TODO auto-generated Catch block
E.printstacktrace ();
}

Get public Construction method
Getting a parameterless public construction method
try {
Constructor cs=cla.getconstructor (null);
System.out.println ("Get the parameterless construction method:" +cs);
catch (SecurityException e) {
TODO auto-generated Catch block
E.printstacktrace ();
catch (Nosuchmethodexception e) {
TODO auto-generated Catch block
E.printstacktrace ();
}

To obtain a method of a joined public construction
such as the construction method of multiple parameters, the construction method is multiple, attention order
try {
Constructor Cs=cla.getconstructor (String.class);
System.out.println ("Get the Parametric construction method:" +cs);
catch (SecurityException e) {
TODO auto-generated Catch block
E.printstacktrace ();
catch (Nosuchmethodexception e) {
TODO auto-generated Catch block
E.printstacktrace ();
}

Get all construction methods

try {
Constructor[] Cs=cla.getdeclaredconstructors ();
For (constructor C:cs) {
System.out.println ("Get All construction Methods:" +c);
}
catch (SecurityException e) {
TODO auto-generated Catch block
E.printstacktrace ();

}
Get Package Name
Package Pack=cla.getpackage ();
System.out.println ("Get the name of the current package:" +pack);

Get comments
Annotation[] Ann=cla.getannotations ();
for (Annotation An:ann) {
System.out.println (An.tostring ());
}

Get parent class
Class Supercla=cla.getsuperclass ();
System.out.println (SUPERCLA);
Get Inner class
Class[] Innercla=cla.getdeclaredclasses ();
for (Class Cl:innercla) {
SYSTEM.OUT.PRINTLN (CL);

}


}
}


Summary: Steps to get information about a class object:

1. Create Class object Method 1 class Cla=class.forname ("PATH") Method 2 class Cla= instance. GetClass (); Method 3 class Cla= classes.

2. Use field (field), Method (methods), constructor (construction method), Package (package), Annotation (note), to obtain relevant information by calling the Cla.getxxxx method

3. Get the parent class and the inner class in a slightly different way

4. When using Cla.getmethods, it is called the entire public method of the class and the parent class.

5. When using declared, all methods of the class contain private methods, which are also applicable in the domain, constructor method

6. When the reflection class has no public constructor method, the Newinstance method cannot be used directly.

Method Cs=cla.getmethod ("getinstance", null);//Get methods
Calendar calendar= (Calendar) cs.invoke (CLA, null); The method to execute the fetch, parameter 1 is the object that executes the method, and parameter 2 represents the parameter of the method
Date Df=calendar.gettime ();
System.out.println (Df.tostring ());

To get.

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.