문제

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!

도움이 되었습니까?

해결책

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

Modifier.isProtected(field.getModifiers());
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top