Can't call certain accessible methods using reflection

Jochen Theodorou blackdrag at gmx.org
Mon Nov 28 13:17:39 UTC 2016



On 28.11.2016 12:23, Peter Levart wrote:
[...]
> // Module m1:
>
> module m1 {
>     exports pkg1;
> }
>
> package internal;
> public class InternalImpl {
>     public void m() {
>         System.out.println("m()");
>     }
> }
>
> package pkg1;
> public class Public extends internal.InternalImpl {
> }

is it legal for an exported class to "expose" an internal class in the 
class signature? I would have assumed this will fail compilation

>
>
> // Module m2:
>
> module m2 {
>     requires m1;
> }
>
> package pkg2;
> import pkg1.Public;
> import java.lang.reflect.Method;
> public class Main {
>     public static void main(String[] args) throws Exception
>         Public p = new Public();
>         // using bytecode
>         p.m();
>         // using reflection
>         Method m = Public.class.getMethod("m");
>         m.invoke(p);
>         // IllegalAccessException: class pkg2.Main (in module m2) cannot
> access class internal.InternalImpl (in module m1) because module m1 does
> not export internal to module m2
>     }
> }

most likely p.m() will do invokevirtual P#m(), while 
Public.class.getMethod("m") will return a Method with the declaring 
class being internal.InternalImpl.

bye Jochen


More information about the jigsaw-dev mailing list