Discussion:
Testing whether or not a Lookup object has access to members
(too old to reply)
Kasper Nielsen
2018-10-22 20:17:27 UTC
Permalink
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
Mandy Chung
2018-10-22 23:07:27 UTC
Permalink
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
Post by 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
Kasper Nielsen
2018-10-23 19:15:49 UTC
Permalink
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
Post by 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
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
Mandy Chung
2018-10-24 14:51:36 UTC
Permalink
Can you file a JBS issue?

Mandy
Post by 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.
boolean Lookup.isAccessible(Member member)
boolean Lookup.isAccessible(Class<?> member)
/Kasper
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
Post by 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
Kasper Nielsen
2018-10-26 09:48:35 UTC
Permalink
Hi Mandy,

I don't have access to JBS unfortunately.

Cheers
Kasper
Post by Mandy Chung
Can you file a JBS issue?
Mandy
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.
boolean Lookup.isAccessible(Member member)
boolean Lookup.isAccessible(Class<?> member)
/Kasper
Post by 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
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
Mandy Chung
2018-10-26 14:55:17 UTC
Permalink
You can submit from https://bugreport.java.com/bugreport/

Mandy
Post by Kasper Nielsen
Hi Mandy,
I don't have access to JBS unfortunately.
Cheers
  Kasper
Can you file a JBS issue?
Mandy
Post by 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.
boolean Lookup.isAccessible(Member member)
boolean Lookup.isAccessible(Class<?> member)
/Kasper
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
Post by 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
Loading...