Java reflection: How to Determine the Modifier of classes, variables, and methods (Modifier parsing)

Source: Internet
Author: User

Java has many modifiers for classes, member variables, and methods, such as public, private, static, final, synchronized, and abstract. These modifiers are used to control access permissions or other features.

 

This article uses Field as an example to illustrate how to obtain modifiers of classes and methods that are the same as member variables.

 

First look at a class

Public class MyTest

{

Public int;

Public static int B;

Public static final int c = 0;

Private int d;

}

 

Through reflection, you can get the four variables:

Public static void main (String [] args ){

Class <?> Clazz = MyTest. class;

Field [] fields = clazz. getDeclaredFields (); // obtain all member variables of this class

For (Field field: fields ){

System. out. println (field. getName ());

}

}

 

Output:

A

B

C

D

 

Now, I want to know which modifiers each variable carries, or whether it contains a modifier.

 

Let's take a look at the Member interface: Member represents a Member of a class, including three implementations: Member variables, methods, and constructor. The Field used above is one of Member's.

 

Java documentation:

Java. lang. reflect

Interface Member

All known implementation classes:

Constructor (Constructor), Field (member variable), Method (Method)

 

The Member interface has a method:

Int getModifiers ()

Return the Java language modifier of the Member or constructor represented by this Member as an integer.

 

At the same time, let's look at the same method in the java. lang. Class:

Int getModifiers ()

Returns the Java modifier of this class or interface in integer encoding.

 

This method returns an int type return value, representing the class, member variable, and method modifier.

Public static void main (String [] args ){

Class <?> Clazz = MyTest. class;

Field [] fields = clazz. getDeclaredFields ();

For (Field field: fields)

{

System. out. print (field. getName () + "-> ");

System. out. println (field. getModifiers ());

}

}

Output:

A-> 1

B-> 9

C-> 25

D-> 2

 

It is difficult to determine the modifiers of these variables by returning the int value.

The java. lang. reflect. Modifier class is required here. Modifier provides many static methods. For example, public static String toString (int mod) can output all modifiers corresponding to this integer. Public static boolean isPublic (int mod) can be used to determine whether the corresponding integer contains a public modifier.

 

Modify the code above to convert the returned integer through Modifier:

Public static void main (String [] args ){

Class <?> Clazz = MyTest. class;

Field [] fields = clazz. getDeclaredFields ();

For (Field field: fields)

{

System. out. print (field. getName () + "-> ");

System. out. println (Modifier. toString (field. getModifiers ()));

}

}

 

Output:

A-> public

B-> public static

C-> public static final

D-> private

 

Using Modifier's isPublic, isPrivate, and isStatic methods, you can determine whether there are some modifiers. If you have such a requirement, I need to find variables with only publicstatic modifiers.

Now let's take a look at the source code of Modifier. We can see the following code:

Public static final int PUBLIC = 0x00000001;

Public static final int PRIVATE = 0x00000002;

Public static final int PROTECTED = 0x00000004;

Public static final int STATIC = 0x00000008;

Public static final int FINAL = 0x00000010;

Public static final int SYNCHRONIZED = 0x00000020;

...... Many other words are omitted here.

 

Convert them to binary. We can see that Modifier uses a binary bit to indicate whether a Modifier is contained.

......

Native

Transient

Volatile

Synchronized

Final

Static

Protected

Private

Public

 

That is, if it is public static, the corresponding integer is binary: 1001, that is, 9. For example:

......

Native

Transient

Volatile

Synchronized

Final

Static

Protected

Private

Public

 

0

0

0

0

0

1

0

0

1


If it is public static final, it is 11001, that is, 25.

 

If you want to determine whether there are only two public static modifiers, you can determine whether field. getModifiers () = 25.

 

In addition, we can also see that the design is exquisite: the binary bit is used to mark whether or not to include a modifier.

 

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.