Determining access level of a field

173 views Asked by At

I am wondering what - if this is even possible - the method of determining the access level of a field (private/protected/public) in java.

What I want to do is find out if there is any protected fields in a class. This is the method I have so far in my base abstract class:

public boolean hasOptions() {
    for (Field field : getClass().getDeclaredFields()) {
        // Method to determine if field is protected.
        return true;
    }
    return false;
}

I've looked around without being able to find an answer.

Thanks!

1

There are 1 answers

0
Sotirios Delimanolis On BEST ANSWER

Yes, use the static utility methods of the Modifier class. For example

Modifier.isProtected(field.getModifiers());