Testing whether or not a Lookup object has access to members
    Kasper Nielsen 
    kasperni at gmail.com
       
    Tue Oct 23 19:15:49 UTC 2018
    
    
  
Hi Mandy,
Yes, that it was my code is doing now, I unreflect a member and then test
if an exception is thrown.
However, it is just a bit of an antipattern, catching exception to test a
condition.
I would prefer if something like this was available:
boolean Lookup.isAccessible(Member member)
boolean Lookup.isAccessible(Class<?> member)
/Kasper
On Tue, 23 Oct 2018 at 00:07, Mandy Chung <mandy.chung at oracle.com> wrote:
> 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