Testing whether or not a Lookup object has access to members

Kasper Nielsen kasperni at gmail.com
Mon Oct 22 20:17:27 UTC 2018


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