Testing whether or not a Lookup object has access to members

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

Testing whether or not a Lookup object has access to members

Kasper Nielsen
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
Reply | Threaded
Open this post in threaded view
|

Re: Testing whether or not a Lookup object has access to members

Mandy Chung
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

Reply | Threaded
Open this post in threaded view
|

Re: Testing whether or not a Lookup object has access to members

Kasper Nielsen
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 <[hidden email]> 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
>
>
>
Reply | Threaded
Open this post in threaded view
|

Re: Testing whether or not a Lookup object has access to members

Mandy Chung
Can you file a JBS issue?

Mandy

On 10/23/18 12:15 PM, Kasper Nielsen wrote:

> 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 <[hidden email]
> <mailto:[hidden email]>> 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
>

Reply | Threaded
Open this post in threaded view
|

Re: Testing whether or not a Lookup object has access to members

Kasper Nielsen
Hi Mandy,

I don't have access to JBS unfortunately.

Cheers
  Kasper

On Wed, 24 Oct 2018 at 16:50, Mandy Chung <[hidden email]> wrote:

> Can you file a JBS issue?
>
> Mandy
>
> On 10/23/18 12:15 PM, Kasper Nielsen wrote:
>
> 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 <[hidden email]> 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
>>
>>
>>
>
Reply | Threaded
Open this post in threaded view
|

Re: Testing whether or not a Lookup object has access to members

Mandy Chung
You can submit from https://bugreport.java.com/bugreport/

Mandy

On 10/26/18 2:48 AM, Kasper Nielsen wrote:

> Hi Mandy,
>
> I don't have access to JBS unfortunately.
>
> Cheers
>   Kasper
>
> On Wed, 24 Oct 2018 at 16:50, Mandy Chung <[hidden email]
> <mailto:[hidden email]>> wrote:
>
>     Can you file a JBS issue?
>
>     Mandy
>
>     On 10/23/18 12:15 PM, Kasper Nielsen wrote:
>>     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 <[hidden email]
>>     <mailto:[hidden email]>> 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
>>
>