Java Reflection (ii): Classes

Source: Internet
Author: User

Reprinted from Concurrent Programming network –ifeve.com

Using the Java reflection mechanism to check Java class information at run time, checking the Java class information is often the first thing you do when you use the Java reflection mechanism, and by getting the information of the class you can get the following related content:
Class object
Class name
Modifier
Package information
Parent class
The implemented interface
Constructors
Method
Variable
Annotations

In addition to the above, there is a lot of information you can get through the reflection mechanism, if you want to know all the information you can view the corresponding document Javadoc for Java.lang.Class There is a detailed description.


In this section we will briefly refer to the above mentioned information, some of the above topics we will use a separate section for a more detailed description, such as this section will describe how to get all the methods of a class or specify methods, but in a separate section will show you how to invoke the method of reflection Object), how to match a given parameter collection to a specified method in multiple methods with the same name, and throw those exceptions when a method is called through the reflection mechanism? How to get the Getter/setter method accurately and so on. The main content of this section is about the class and what information you can get from the class.

Class object

Before you want to check the information of a class, you first need to get the class object of the classes. All types in Java include the base type (int, long, float, and so on), even if the array has an object of the class class associated with it. If you know the name of a class at compile time, you can use the following method to get a class object.

   Class myobjectclass = Myobject.class;

If you don't know the name of the class at compile time, but you can get a string of the class name at run time, you can do this to get the class object:

   String className = ...;//the class name string obtained at runtime is class class   = Class.forName (className);

When using the Class.forName () method, you must provide the full name of a class, which includes the name of the package in which the class resides. For example, the MyObject class is located in the Com.jenkov.myapp package, then his full name is Com.jenkov.myapp.MyObject.
If the corresponding class is not found under the compilation path (CLASSPATH) when the Class.forName () method is called, the ClassNotFoundException will be thrown.

Class name

You can get two versions of the class name from a class object.

The fully qualified class name of the class (including the package name) is returned by the GetName () method:

    Class AClass = ...//Gets the class object, which is visible in the Class object subsection    String className = Aclass.getname ();

If you just want to get the name of the class (not including the package name), you can use the Getsimplename () method:

    Class AClass = ...//Gets the class object, which is visible in the Class object subsection    String simpleclassname = Aclass.getsimplename ();

Modifier

You can use the class object to access the modifier for a public,private,static, that is, the keyword for the class, and you can get the modifier for it by using the following method:

    Class  AClass = ...//Gets the class object, visible in the class object subsection    int modifiers = Aclass.getmodifiers ();

Modifiers are wrapped into a number of type int so that each modifier is a bit identifier (flag bit), which identifies the type of modifier that can be set and cleared.
You can use the methods in the Java.lang.reflect.Modifier class to check the type of the modifier:

    modifier.isabstract (int modifiers);    modifier.isfinal (int modifiers);    Modifier.isinterface (int modifiers);    modifier.isnative (int modifiers);    modifier.isprivate (int modifiers);    modifier.isprotected (int modifiers);    modifier.ispublic (int modifiers);    modifier.isstatic (int modifiers);    modifier.isstrict (int modifiers);    modifier.issynchronized (int modifiers);    modifier.istransient (int modifiers);    Modifier.isvolatile (int modifiers);

Package information

You can use the class object to get package information in the following ways:

    Class  AClass = ...//Gets the class object, which is visible in the Class object section of package Package    = Aclass.getpackage ();

With the package object you can get information about the packages, such as the package name, you can also access the specified information of the jar package under the compilation path through the manifest file, for example, you can specify the version number of the package in the manifest file.

Parent class

You can access the class's parent class through a class object, as in the following example:

    Class superclass = Aclass.getsuperclass ();

You can see that the superclass object is actually an instance of class, so you can continue to do reflection on the object.

The implemented interface

You can get the collection of interfaces implemented by the specified class in the following ways:

   Class  AClass = ...//Gets the class object, visible in the Class object subsection   class[] interfaces = Aclass.getinterfaces ();

Because a class can implement multiple interfaces, the Getinterfaces () method returns a class array, and the interface in Java has a corresponding class object.
Note: the Getinterfaces () method only returns the interfaces implemented by the current class. If the parent class of the current class implements an interface, these interfaces will not be in the returned class collection, although the current class actually implements the parent class interface.

Constructors

You can access the constructor of a class in the following ways:

  Constructor[] Constructors = aclass.getconstructors ();

Method

You can access all the methods of a class in the following ways:

  Method[] method = Aclass.getmethods ();

Variable

You can access the member variables of a class in the following ways:

  Field[] method = Aclass.getfields ();

Annotations

You can access the annotations of a class in the following ways:

annotation[] annotations = aclass.getannotations ();

Original address Author: Jakob Jenkov Translator: Yevenhai ([email protected])

Java Reflection (ii): 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.