TheArchitect
TheArchitect

Reputation: 129

Get the modifiers of a field using the ASM tree API

I am writing an analyzer for Java class files using ASM. One of the things I want to determine is what the modifiers (public, static, final?) of the fields in a class are. But I am not sure how to do this.

In the documentation i found the opcodes of the modifiers, which seems to correlate with the acces value of the FieldNode class. But I don't see how I derive the modifiers of the field form this value.

Any suggestions?

Upvotes: 1

Views: 791

Answers (1)

Jörn Horstmann
Jörn Horstmann

Reputation: 34014

The access member variable is a bitfield, each bit position corresponds to a specific access modifier. To check a bit you have to use a binary AND with the constant from Opcodes and check if the result is not zero. For example:

boolean isPublic = (node.access & Opcodes.ACC_PUBLIC) != 0;
boolean isStatic = (node.access & Opcodes.ACC_STATIC) != 0;

Upvotes: 5

Related Questions