Illegal reflective access on Streams using reflection
I'm using reflection to invoke methods on java.util.stream.Stream but because the actual implementations (ReferencePipeline etc.) have the actual code which runs, I get illegal reflective access warnings when calling method.setAccessible(true)
, and without that call, it doesn't work. I was wondering whether there is a way to automatically delegate this to a super method where access isn't illegal? That is, I want to call filter
where it's legal on java.util.stream.Stream
and not ReferencePipeline
or whatever the implementation is.
EDIT Here is some code. target
is a concrete instance of a Stream obtained via reflection.
assert target instanceof java.util.stream.Stream;
Method candidate = Stream.of(target.getClass().getMethods())
.filter(method -> method.getName().equals("filter"))
//.filter(myComplicatedCriteria) - omitted for brevity
.findAny().orElseThrow();
try
candidate.setAccessible(true);
return candidate.invoke(target, candidateParameterValues);
catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException ex)
throw new EolRuntimeException(ex);
java reflection illegalaccessexception
add a comment |
I'm using reflection to invoke methods on java.util.stream.Stream but because the actual implementations (ReferencePipeline etc.) have the actual code which runs, I get illegal reflective access warnings when calling method.setAccessible(true)
, and without that call, it doesn't work. I was wondering whether there is a way to automatically delegate this to a super method where access isn't illegal? That is, I want to call filter
where it's legal on java.util.stream.Stream
and not ReferencePipeline
or whatever the implementation is.
EDIT Here is some code. target
is a concrete instance of a Stream obtained via reflection.
assert target instanceof java.util.stream.Stream;
Method candidate = Stream.of(target.getClass().getMethods())
.filter(method -> method.getName().equals("filter"))
//.filter(myComplicatedCriteria) - omitted for brevity
.findAny().orElseThrow();
try
candidate.setAccessible(true);
return candidate.invoke(target, candidateParameterValues);
catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException ex)
throw new EolRuntimeException(ex);
java reflection illegalaccessexception
1
Can you please provide code to illustrate the problem?
– Karol Dowbecki
Nov 14 '18 at 16:53
A minimal example would require too much code since it's reflection. The problem is that I'm invoking what is supposedly a perfectly legal method (e.g.filter
) but because the instance it's being invoked on is a subclass of Stream there's no way to convince Java that I'm trying to use the "legal" one
– Sina Madani
Nov 14 '18 at 17:07
An overriden method can't limit the visibility of a super method. You are most likely trying to access an overloaded method in a child class. There is no way to tell what's happening since you don't want to provide the code :(
– Karol Dowbecki
Nov 14 '18 at 17:11
Surely there must be something that can be done withMethodHandles
?
– Sina Madani
Nov 14 '18 at 17:17
add a comment |
I'm using reflection to invoke methods on java.util.stream.Stream but because the actual implementations (ReferencePipeline etc.) have the actual code which runs, I get illegal reflective access warnings when calling method.setAccessible(true)
, and without that call, it doesn't work. I was wondering whether there is a way to automatically delegate this to a super method where access isn't illegal? That is, I want to call filter
where it's legal on java.util.stream.Stream
and not ReferencePipeline
or whatever the implementation is.
EDIT Here is some code. target
is a concrete instance of a Stream obtained via reflection.
assert target instanceof java.util.stream.Stream;
Method candidate = Stream.of(target.getClass().getMethods())
.filter(method -> method.getName().equals("filter"))
//.filter(myComplicatedCriteria) - omitted for brevity
.findAny().orElseThrow();
try
candidate.setAccessible(true);
return candidate.invoke(target, candidateParameterValues);
catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException ex)
throw new EolRuntimeException(ex);
java reflection illegalaccessexception
I'm using reflection to invoke methods on java.util.stream.Stream but because the actual implementations (ReferencePipeline etc.) have the actual code which runs, I get illegal reflective access warnings when calling method.setAccessible(true)
, and without that call, it doesn't work. I was wondering whether there is a way to automatically delegate this to a super method where access isn't illegal? That is, I want to call filter
where it's legal on java.util.stream.Stream
and not ReferencePipeline
or whatever the implementation is.
EDIT Here is some code. target
is a concrete instance of a Stream obtained via reflection.
assert target instanceof java.util.stream.Stream;
Method candidate = Stream.of(target.getClass().getMethods())
.filter(method -> method.getName().equals("filter"))
//.filter(myComplicatedCriteria) - omitted for brevity
.findAny().orElseThrow();
try
candidate.setAccessible(true);
return candidate.invoke(target, candidateParameterValues);
catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException ex)
throw new EolRuntimeException(ex);
java reflection illegalaccessexception
java reflection illegalaccessexception
edited Nov 14 '18 at 17:32
Sina Madani
asked Nov 14 '18 at 16:49
Sina MadaniSina Madani
6221820
6221820
1
Can you please provide code to illustrate the problem?
– Karol Dowbecki
Nov 14 '18 at 16:53
A minimal example would require too much code since it's reflection. The problem is that I'm invoking what is supposedly a perfectly legal method (e.g.filter
) but because the instance it's being invoked on is a subclass of Stream there's no way to convince Java that I'm trying to use the "legal" one
– Sina Madani
Nov 14 '18 at 17:07
An overriden method can't limit the visibility of a super method. You are most likely trying to access an overloaded method in a child class. There is no way to tell what's happening since you don't want to provide the code :(
– Karol Dowbecki
Nov 14 '18 at 17:11
Surely there must be something that can be done withMethodHandles
?
– Sina Madani
Nov 14 '18 at 17:17
add a comment |
1
Can you please provide code to illustrate the problem?
– Karol Dowbecki
Nov 14 '18 at 16:53
A minimal example would require too much code since it's reflection. The problem is that I'm invoking what is supposedly a perfectly legal method (e.g.filter
) but because the instance it's being invoked on is a subclass of Stream there's no way to convince Java that I'm trying to use the "legal" one
– Sina Madani
Nov 14 '18 at 17:07
An overriden method can't limit the visibility of a super method. You are most likely trying to access an overloaded method in a child class. There is no way to tell what's happening since you don't want to provide the code :(
– Karol Dowbecki
Nov 14 '18 at 17:11
Surely there must be something that can be done withMethodHandles
?
– Sina Madani
Nov 14 '18 at 17:17
1
1
Can you please provide code to illustrate the problem?
– Karol Dowbecki
Nov 14 '18 at 16:53
Can you please provide code to illustrate the problem?
– Karol Dowbecki
Nov 14 '18 at 16:53
A minimal example would require too much code since it's reflection. The problem is that I'm invoking what is supposedly a perfectly legal method (e.g.
filter
) but because the instance it's being invoked on is a subclass of Stream there's no way to convince Java that I'm trying to use the "legal" one– Sina Madani
Nov 14 '18 at 17:07
A minimal example would require too much code since it's reflection. The problem is that I'm invoking what is supposedly a perfectly legal method (e.g.
filter
) but because the instance it's being invoked on is a subclass of Stream there's no way to convince Java that I'm trying to use the "legal" one– Sina Madani
Nov 14 '18 at 17:07
An overriden method can't limit the visibility of a super method. You are most likely trying to access an overloaded method in a child class. There is no way to tell what's happening since you don't want to provide the code :(
– Karol Dowbecki
Nov 14 '18 at 17:11
An overriden method can't limit the visibility of a super method. You are most likely trying to access an overloaded method in a child class. There is no way to tell what's happening since you don't want to provide the code :(
– Karol Dowbecki
Nov 14 '18 at 17:11
Surely there must be something that can be done with
MethodHandles
?– Sina Madani
Nov 14 '18 at 17:17
Surely there must be something that can be done with
MethodHandles
?– Sina Madani
Nov 14 '18 at 17:17
add a comment |
1 Answer
1
active
oldest
votes
Use the interface class Stream
instead of the implementing class target.getClass()
. Change the code to:
Method candidate = Stream.of(Stream.class.getMethods())
.filter(method -> method.getName().equals("filter"))
...
Root cause of the problem is java.util.stream.ReferencePipeline
as well as java.util.stream.ReferencePipeline.Head
being package protected. Your class can't access these classes using reflection even if the filter()
method itself is defined as public
.
The Stream.class.getMethods()
approach will work because your class can access the public Stream
class. See sun.reflect.Reflection.ensureMemberAccess()
check if you need details.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53305122%2fillegal-reflective-access-on-streams-using-reflection%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Use the interface class Stream
instead of the implementing class target.getClass()
. Change the code to:
Method candidate = Stream.of(Stream.class.getMethods())
.filter(method -> method.getName().equals("filter"))
...
Root cause of the problem is java.util.stream.ReferencePipeline
as well as java.util.stream.ReferencePipeline.Head
being package protected. Your class can't access these classes using reflection even if the filter()
method itself is defined as public
.
The Stream.class.getMethods()
approach will work because your class can access the public Stream
class. See sun.reflect.Reflection.ensureMemberAccess()
check if you need details.
add a comment |
Use the interface class Stream
instead of the implementing class target.getClass()
. Change the code to:
Method candidate = Stream.of(Stream.class.getMethods())
.filter(method -> method.getName().equals("filter"))
...
Root cause of the problem is java.util.stream.ReferencePipeline
as well as java.util.stream.ReferencePipeline.Head
being package protected. Your class can't access these classes using reflection even if the filter()
method itself is defined as public
.
The Stream.class.getMethods()
approach will work because your class can access the public Stream
class. See sun.reflect.Reflection.ensureMemberAccess()
check if you need details.
add a comment |
Use the interface class Stream
instead of the implementing class target.getClass()
. Change the code to:
Method candidate = Stream.of(Stream.class.getMethods())
.filter(method -> method.getName().equals("filter"))
...
Root cause of the problem is java.util.stream.ReferencePipeline
as well as java.util.stream.ReferencePipeline.Head
being package protected. Your class can't access these classes using reflection even if the filter()
method itself is defined as public
.
The Stream.class.getMethods()
approach will work because your class can access the public Stream
class. See sun.reflect.Reflection.ensureMemberAccess()
check if you need details.
Use the interface class Stream
instead of the implementing class target.getClass()
. Change the code to:
Method candidate = Stream.of(Stream.class.getMethods())
.filter(method -> method.getName().equals("filter"))
...
Root cause of the problem is java.util.stream.ReferencePipeline
as well as java.util.stream.ReferencePipeline.Head
being package protected. Your class can't access these classes using reflection even if the filter()
method itself is defined as public
.
The Stream.class.getMethods()
approach will work because your class can access the public Stream
class. See sun.reflect.Reflection.ensureMemberAccess()
check if you need details.
edited Nov 14 '18 at 18:31
answered Nov 14 '18 at 18:18
Karol DowbeckiKarol Dowbecki
24k93657
24k93657
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53305122%2fillegal-reflective-access-on-streams-using-reflection%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
Can you please provide code to illustrate the problem?
– Karol Dowbecki
Nov 14 '18 at 16:53
A minimal example would require too much code since it's reflection. The problem is that I'm invoking what is supposedly a perfectly legal method (e.g.
filter
) but because the instance it's being invoked on is a subclass of Stream there's no way to convince Java that I'm trying to use the "legal" one– Sina Madani
Nov 14 '18 at 17:07
An overriden method can't limit the visibility of a super method. You are most likely trying to access an overloaded method in a child class. There is no way to tell what's happening since you don't want to provide the code :(
– Karol Dowbecki
Nov 14 '18 at 17:11
Surely there must be something that can be done with
MethodHandles
?– Sina Madani
Nov 14 '18 at 17:17