Lookup objects and setAccessible

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

Lookup objects and setAccessible

Kasper Nielsen
Hi,

I'm trying to bridge some old code using java.lang.reflect and some new code
that uses Lookup objects. And I'm wondering if there is some way to make
a member accessible using a Lookup object? Or if open packages/modules via
module-info.java + member.setAccessible(true) is the only way?

Thanks
  Kasper
Reply | Threaded
Open this post in threaded view
|

Re: Lookup objects and setAccessible

Remi Forax
see https://docs.oracle.com/en/java/javase/13/docs/api/java.base/java/lang/invoke/MethodHandles.html#privateLookupIn(java.lang.Class,java.lang.invoke.MethodHandles.Lookup)

Rémi

----- Mail original -----
> De: "Kasper Nielsen" <[hidden email]>
> À: "jigsaw-dev" <[hidden email]>
> Envoyé: Mercredi 8 Janvier 2020 12:14:30
> Objet: Lookup objects and setAccessible

> Hi,
>
> I'm trying to bridge some old code using java.lang.reflect and some new code
> that uses Lookup objects. And I'm wondering if there is some way to make
> a member accessible using a Lookup object? Or if open packages/modules via
> module-info.java + member.setAccessible(true) is the only way?
>
> Thanks
>   Kasper
Reply | Threaded
Open this post in threaded view
|

Re: Lookup objects and setAccessible

Kasper Nielsen
Sorry, I could have been a bit more specific. I meant something like
Method.setAccessible(Lookup, true)

I want to base an API on lookup objects. But I want to still support some use
cases where people rely on java.lang.reflect.Field|Constructor|Method.
Specifically, being able to create an Executable that can be
instantiated/invoked. So I'm looking for a way to make members
(Field|Constructor|Method) accessible in some way using a Lookup object.

Obviously, this would not be a problem if my API relied on open
packages/modules via module-info.java and member.setAccessible(true)
instead of using Lookup objects.

/Kasper

On Wed, 8 Jan 2020 at 12:11, Remi Forax <[hidden email]> wrote:

>
> see https://docs.oracle.com/en/java/javase/13/docs/api/java.base/java/lang/invoke/MethodHandles.html#privateLookupIn(java.lang.Class,java.lang.invoke.MethodHandles.Lookup)
>
> Rémi
>
> ----- Mail original -----
> > De: "Kasper Nielsen" <[hidden email]>
> > À: "jigsaw-dev" <[hidden email]>
> > Envoyé: Mercredi 8 Janvier 2020 12:14:30
> > Objet: Lookup objects and setAccessible
>
> > Hi,
> >
> > I'm trying to bridge some old code using java.lang.reflect and some new code
> > that uses Lookup objects. And I'm wondering if there is some way to make
> > a member accessible using a Lookup object? Or if open packages/modules via
> > module-info.java + member.setAccessible(true) is the only way?
> >
> > Thanks
> >   Kasper
Reply | Threaded
Open this post in threaded view
|

Re: Lookup objects and setAccessible

Mandy Chung
Have you looked at Lookup::revealDirect and MethodHandleInfo::reflectAs?

Mandy

On 1/8/20 5:49 AM, Kasper Nielsen wrote:

> Sorry, I could have been a bit more specific. I meant something like
> Method.setAccessible(Lookup, true)
>
> I want to base an API on lookup objects. But I want to still support some use
> cases where people rely on java.lang.reflect.Field|Constructor|Method.
> Specifically, being able to create an Executable that can be
> instantiated/invoked. So I'm looking for a way to make members
> (Field|Constructor|Method) accessible in some way using a Lookup object.
>
> Obviously, this would not be a problem if my API relied on open
> packages/modules via module-info.java and member.setAccessible(true)
> instead of using Lookup objects.
>
> /Kasper
>
> On Wed, 8 Jan 2020 at 12:11, Remi Forax <[hidden email]> wrote:
>> see https://docs.oracle.com/en/java/javase/13/docs/api/java.base/java/lang/invoke/MethodHandles.html#privateLookupIn(java.lang.Class,java.lang.invoke.MethodHandles.Lookup)
>>
>> Rémi
>>
>> ----- Mail original -----
>>> De: "Kasper Nielsen" <[hidden email]>
>>> À: "jigsaw-dev" <[hidden email]>
>>> Envoyé: Mercredi 8 Janvier 2020 12:14:30
>>> Objet: Lookup objects and setAccessible
>>> Hi,
>>>
>>> I'm trying to bridge some old code using java.lang.reflect and some new code
>>> that uses Lookup objects. And I'm wondering if there is some way to make
>>> a member accessible using a Lookup object? Or if open packages/modules via
>>> module-info.java + member.setAccessible(true) is the only way?
>>>
>>> Thanks
>>>    Kasper

Reply | Threaded
Open this post in threaded view
|

Re: Lookup objects and setAccessible

Kasper Nielsen
Hi Mandy,

Yes, but they create a member which is not accessible.

As far as I can see, all places where AccessibleObject.override is ever set
there is a check of the current caller with Reflection.getCallerClass().

/Kasper

On Wed, 8 Jan 2020 at 19:25, Mandy Chung <[hidden email]> wrote:
>
> Have you looked at Lookup::revealDirect and MethodHandleInfo::reflectAs?
>
> Mandy
>