Illegal reflective access on Streams using reflection












1















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);
}









share|improve this question




















  • 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
















1















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);
}









share|improve this question




















  • 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














1












1








1


0






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);
}









share|improve this question
















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 14 '18 at 17:32







Sina Madani

















asked Nov 14 '18 at 16:49









Sina MadaniSina Madani

6241819




6241819








  • 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














  • 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








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












1 Answer
1






active

oldest

votes


















1














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.






share|improve this answer

























    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
    });


    }
    });














    draft saved

    draft discarded


















    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









    1














    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.






    share|improve this answer






























      1














      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.






      share|improve this answer




























        1












        1








        1







        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.






        share|improve this answer















        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.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 14 '18 at 18:31

























        answered Nov 14 '18 at 18:18









        Karol DowbeckiKarol Dowbecki

        21.4k93154




        21.4k93154
































            draft saved

            draft discarded




















































            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.




            draft saved


            draft discarded














            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





















































            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







            Popular posts from this blog

            Xamarin.iOS Cant Deploy on Iphone

            Glorious Revolution

            Dulmage-Mendelsohn matrix decomposition in Python