In-depth exploration of Java reflection mechanism (class)

Source: Internet
Author: User
Classes

Through the reflection mechanism, we can explore the internal structure of the class at runtime and obtain the following information:

  • Class Name
  • Class modifiers (public, protected, synchronized, etc)
  • Package info
  • Super class
  • Implemented interfaces
  • Constructors
  • Methods
  • Fields
  • Annotations

The Class Object

All Java types (including basic types such as INT) and arrays (arrays) have associated classobject.

The class name

If you know the class name during compilation, you can obtain the class instance of the class as follows:

import java.lang.reflect.Method;public class HelloJava {    public static void main(String[] args) {        Class myClass = HelloJava.class;    }}

If you know the name of the class (string type) at runtime, you can obtain the class instance of the class as follows:

package tao.xiao.action;import java.lang.reflect.Method;public class HelloJava {    public static void main(String[] args) throws ClassNotFoundException {        Class myClass = Class.forName("tao.xiao.action.HelloJava");        System.out.println(myClass.getName());        // tao.xiao.action.HelloJava        System.out.println(myClass.getSimpleName());// HelloJava    }}

Note that the class name must be a fully qualified name (including the package name ). If the class cannot be found in classpath during running, classnoffoundexception is thrown.

Modifiers

package tao.xiao.action;import java.lang.reflect.Modifier;public class HelloJava {public static void main(String[] args) throws ClassNotFoundException {Class myClass = Class.forName("tao.xiao.action.HelloJava");int modifiers = myClass.getModifiers();System.out.println(Modifier.isPublic(modifiers));// trueSystem.out.println(Modifier.isPrivate(modifiers));// falseSystem.out.println(Modifier.isSynchronized(modifiers));// falseSystem.out.println(Modifier.isStatic(modifiers));// false}}

Package info

package tao.xiao.action;public class HelloJava {public static void main(String[] args) throws ClassNotFoundException {Class myClass = Class.forName("tao.xiao.action.HelloJava");Package pack = myClass.getPackage();System.out.println(pack);}}

Superclass

Package Tao. xiao. action; public class hellojava {public static void main (string [] ARGs) throws classnotfoundexception {class classb = Class. forname ("Tao. xiao. action. B "); // here B extends Aclass classa = classb. getsuperclass (); system. out. println (classa. getname (); // The output is Tao. xiao. action. A }}

 

Implemented interfaces

Package Tao. xiao. action; public class hellojava {public static void main (string [] ARGs) throws classnotfoundexception {class classb = Class. forname ("Tao. xiao. action. B "); Class [] interfaces = classb. getinterfaces (); // a class can implement multiple interfaces. Therefore, the returned class [] type is for (Class I: interfaces) system. out. println (I. getname ());}}

Here, there are two interfaces, it1 and it2. The declaration of Class A and Class B is

class A implements IT1 ... class B extends A implements IT2 ...

The getinterface method can only return interfaces directly implemented by this class. Therefore, the output here is Tao. Xiao. Action. it2.

Constructors, methods, fields and annotations

Package Tao. xiao. action; import Java. lang. annotation. annotation; import Java. lang. reflect. constructor; import Java. lang. reflect. field; import Java. lang. reflect. method; public class hellojava {public int A; private double B; protected long C; Public hellojava () {} public hellojava (string s) {} public void F1 () {} Public String F2 (float f) {return "XXX" ;}@ overridepublic string tostring () {return "XXX" ;}public static void main (string [] ARGs) throws classnotfoundexception {class myclass = Class. forname ("Tao. xiao. action. hellojava "); constructor [] constructors = myclass. getconstructors (); For (constructor C: constructors) system. out. println ("constructor =>" + C); method [] Methods = myclass. getmethods (); For (method M: Methods) system. out. println ("Method =>" + M); field [] fields = myclass. getfields (); // only the Public Member for (field F: fields) system is returned. out. println ("field =>" + F); annotation [] annotations = myclass. getannotations (); // The @ override annotation for (annotation an: Annotations) system is not returned. out. println ("annotation =>" + );}}

Output:

Constructor ==> public tao.xiao.action.HelloJava()Constructor ==> public tao.xiao.action.HelloJava(java.lang.String)Method ==> public void tao.xiao.action.HelloJava.f1()Method ==> public java.lang.String tao.xiao.action.HelloJava.f2(float)Method ==> public static void tao.xiao.action.HelloJava.main(java.lang.String[]) throws java.lang.ClassNotFoundExceptionMethod ==> public java.lang.String tao.xiao.action.HelloJava.toString()Method ==> public final native void java.lang.Object.wait(long) throws java.lang.InterruptedExceptionMethod ==> public final void java.lang.Object.wait() throws java.lang.InterruptedExceptionMethod ==> public final void java.lang.Object.wait(long,int) throws java.lang.InterruptedExceptionMethod ==> public boolean java.lang.Object.equals(java.lang.Object)Method ==> public native int java.lang.Object.hashCode()Method ==> public final native java.lang.Class java.lang.Object.getClass()Method ==> public final native void java.lang.Object.notify()Method ==> public final native void java.lang.Object.notifyAll()Field ==> public int tao.xiao.action.HelloJava.a

Next chapter: Exploring Java reflection mechanism (constructors)

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.