Using java.awt.Toolkit.getDefaultToolkit().getScreenSize() reflectively causes InaccessibleObjectException

Phil Race philip.race at oracle.com
Thu Jan 5 19:58:59 UTC 2017


Just for clarification .. I was also testing what happened with
getDeclaredMethod (which is not delcared on that X11 sub-class) .. and that
caused the NoSuchMethodException I pasted in my previous email.
With "getMethod" I see what Rony reports.


~/jdk9b142/bin/java TK8
class sun.awt.X11.XToolkit
Exception in thread "main" java.lang.IllegalAccessException: class TK8 
cannot access class sun.awt.SunToolkit (in module java.desktop) because 
module java.desktop does not export sun.awt to unnamed module @6f195bc3
     at 
jdk.internal.reflect.Reflection.throwIllegalAccessException(java.base at 9-ea/Reflection.java:415)
     at 
jdk.internal.reflect.Reflection.throwIllegalAccessException(java.base at 9-ea/Reflection.java:406)
     at 
jdk.internal.reflect.Reflection.ensureMemberAccess(java.base at 9-ea/Reflection.java:111)
     at 
java.lang.reflect.AccessibleObject.slowCheckMemberAccess(java.base at 9-ea/AccessibleObject.java:355)
     at 
java.lang.reflect.AccessibleObject.checkAccess(java.base at 9-ea/AccessibleObject.java:347)
     at java.lang.reflect.Method.invoke(java.base at 9-ea/Method.java:529)
     at TK8.main(TK8.java:9)


-phil.

On 01/05/2017 11:45 AM, Phil Race wrote:
> ah yes .. I can see how one might code that way.
> I don't know how common a problem this is, but
> such code will need to be updated.
>
> ----
> import java.awt.Toolkit;
>
> public class TK8 {
>     public static void main(String args[]) throws Exception {
>        Toolkit tk = Toolkit.getDefaultToolkit();
>        Class tkc = tk.getClass();
>        System.out.println(tkc);
>        java.lang.reflect.Method m = tkc.getMethod("getScreenSize");
>        System.out.println(m.invoke(tk));
>     }
> }
>
> ~/jdk8u45/bin/java TK8
> class sun.awt.X11.XToolkit
> java.awt.Dimension[width=1920,height=1200]
>
> ~/jdk9b142/bin/java TK8
> class sun.awt.X11.XToolkit
> Exception in thread "main" java.lang.NoSuchMethodException: 
> sun.awt.X11.XToolkit.getScreenSize()
>     at java.lang.Class.getDeclaredMethod(java.base at 9-ea/Class.java:2318)
>     at TK8.main(TK8.java:8)
> ----
>
> -phil.
>
> On 01/05/2017 11:34 AM, Alex Buckley wrote:
>> Rony mentioned "the returned Toolkit is of type sun.awt.X11.XToolkit" 
>> which suggests that he is calling getClass() on the result of 
>> getDefaultToolkit(), and then reflecting further on that Class object 
>> and its Methods. As has been discussed previously on jigsaw-dev, this 
>> won't work for implementation classes; it's necessary to reflect over 
>> exported classes and interfaces instead.
>>
>> Alex
>>
>> On 1/5/2017 11:22 AM, Phil Race wrote:
>>> This should be discussed on jigsaw-dev.
>>>
>>> But can you share your code ? -  since the following works for me :
>>>
>>> import java.awt.Toolkit;
>>>
>>> public class TK {
>>>      public static void main(String args[]) throws Exception {
>>>         Toolkit tk = Toolkit.getDefaultToolkit();
>>>         Class tkc = Class.forName("java.awt.Toolkit");
>>>         java.lang.reflect.Method m = tkc.getMethod("getScreenSize");
>>>         System.out.println(m.invoke(tk));
>>>      }
>>> }
>>>
>>> ~/jdk9b142/bin/java TK
>>> java.awt.Dimension[width=1920,height=1200]
>>>
>>> -phil.
>>>
>>> On 01/05/2017 11:03 AM, Rony G. Flatscher wrote:
>>>> Experimenting with further scripts indicates that this is a systematic
>>>> problem whenever the official
>>>> APIs return members that are not made available to an unnamed module:
>>>> e.g. JavaFX some
>>>> "com.sun.javafx.collections.VetoableListDecorator" or in a "plain"
>>>> awt/swing app some
>>>> "sun.java2d.SungGraphics2D".
>>>>
>>>> ---rony
>>>>
>>>> On 05.01.2017 19:42, Rony G. Flatscher wrote:
>>>>> Trying to run a program that gets the screen dimensions using
>>>>> java.awt.Toolkit.getDefaultToolkit().getScreenSize() reflectively.
>>>>>
>>>>> On a 64-bit Ubuntu the returned Toolkit is of type
>>>>> sun.awt.X11.XToolkit. Reflectively invoking its
>>>>> method getScreenSize() causes the following exception to be thrown on
>>>>> 9-ea+134:
>>>>>
>>>>>      java.lang.reflect.InaccessibleObjectException: unable to make
>>>>> member of class sun.awt.SunToolkit
>>>>>      accessible: module java.desktop does not export sun.awt to
>>>>> unnamed module ...
>>>>>
>>>>> A little bit baffled as this is from a script that has been working
>>>>> flawlessly throughout more than
>>>>> a decade on various Java versions and which employs documented public
>>>>> methods only (the sun.awt
>>>>> object is returned by java.awt.Toolkit). Is this a known
>>>>> "legacy"problem :) that I could circumvent
>>>>> somehow or a bug that needs to be reported?
>>>>>
>>>>> ---rony
>>>>>
>>>
>



More information about the jigsaw-dev mailing list