Testing whether or not a Lookup object has access to members
Mandy Chung
mandy.chung at oracle.com
Mon Oct 22 23:07:27 UTC 2018
Lookup.accessClass(member.getDeclaringClass()) can be used to test
if the lookup class can access the declaring class of the given member.
This only checks if a class is accessible. I think unreflecting a member
will do what you are looking for to check if the lookup object has access
to the member. What does the code do if the Lookup object has access
vs has no access?
Mandy
On 10/22/18 1:17 PM, Kasper Nielsen wrote:
> Hi,
>
> Are there any elegant way to test if a Lookup object has access to a member
> (field, constructor, method). Right now I'm using the following code
>
> public static boolean hasAccess(MethodHandles.Lookup lookup, Member member)
> {
>
> if (member instanceof Constructor) {
>
> try {
>
> lookup.unreflectConstructor((Constructor<?>) member);
>
> } catch (IllegalAccessException e) {
>
> return false;
>
> }
>
> } else if (member instanceof Method) {
>
> try {
>
> lookup.unreflect((Method) member);
>
> } catch (IllegalAccessException e) {
>
> return false;
>
> }
>
> } else if (member instanceof Field) {
>
> try {
>
> lookup.unreflectVarHandle((Field) member);
>
> } catch (IllegalAccessException e) {
>
> return false;
>
> }
>
> }
>
> return true;
>
> }
>
> Cheers
> Kasper
More information about the jigsaw-dev
mailing list