[ https://issues.apache.org/jira/browse/GROOVY-8339?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16811680#comment-16811680 ]
Daniel Sun commented on GROOVY-8339: ------------------------------------ {{makeAccessible}} is the evil that causes most of illegal access warnings. [https://github.com/apache/groovy/blob/master/src/main/java/org/codehaus/groovy/reflection/CachedClass.java#L96] The method {{makeAccessible}} will try to call {{setAccessible}} on all declared methods, constructors and fields even if they will not be accessed actually. So I think we should postpone the invocation of {{setAccessible}} util the methods, constructors and fields are accessed. The above idea can avoid illegal access warnings unless we are truely accessing invisible objects. Here is the experiment I have tried to fix the illegal access warning of methods(Note: illegal access warning of constructors and fields can be fixed in a similar way): https://github.com/danielsun1106/groovy/commit/9720df9d616ac785756109313ab19c8983e46fcf As you can see in the following log, illegal access warning of methods(e.g. `finalize`) disappear: https://travis-ci.org/danielsun1106/groovy/jobs/516628040 Though the idea can fix illegal access warning of methods(warnings of constructors and fields can be fixed too), it also introduces a new problem, how to check the class is exported, or the following error will occur. {code:java} org.codehaus.groovy.tools.shell.GroovyshTest > testDefaultResultHookStringArray FAILED java.lang.IllegalAccessException: class org.codehaus.groovy.runtime.callsite.PlainObjectMetaMethodSite cannot access class jdk.internal.jrtfs.JrtFileSystem (in module java.base) because module java.base does not export jdk.internal.jrtfs to unnamed module @6f5cdb53 at java.base/jdk.internal.reflect.Reflection.newIllegalAccessException(Reflection.java:361) at java.base/java.lang.reflect.AccessibleObject.checkAccess(AccessibleObject.java:591) at java.base/java.lang.reflect.Method.invoke(Method.java:558) at org.codehaus.groovy.runtime.callsite.PlainObjectMetaMethodSite.doInvoke(PlainObjectMetaMethodSite.java:43) at org.codehaus.groovy.runtime.callsite.PojoMetaMethodSite$PojoCachedMethodSite.invoke(PojoMetaMethodSite.java:188) at org.codehaus.groovy.runtime.callsite.PojoMetaMethodSite.call(PojoMetaMethodSite.java:53) at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:47) at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:115) at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:127) at Script1.run(Script1.groovy:39) at groovy.lang.GroovyShell.evaluate(GroovyShell.java:441) at groovy.lang.GroovyShell.evaluate(GroovyShell.java:479) at groovy.lang.GroovyShell.evaluate(GroovyShell.java:450) at org.codehaus.groovy.tools.shell.util.PackageHelperImpl.getPackagesAndClassesFromJigsaw(PackageHelperImpl.groovy:149) at org.codehaus.groovy.tools.shell.util.PackageHelperImpl.getPackages(PackageHelperImpl.groovy:122) at org.codehaus.groovy.tools.shell.util.PackageHelperImpl.initializePackages(PackageHelperImpl.groovy:59) at org.codehaus.groovy.tools.shell.util.PackageHelperImpl.<init>(PackageHelperImpl.groovy:49) at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:490) at org.codehaus.groovy.reflection.CachedConstructor.invoke(CachedConstructor.java:80) at org.codehaus.groovy.runtime.callsite.ConstructorSite$ConstructorSiteNoUnwrapNoCoerce.callConstructor(ConstructorSite.java:105) at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCallConstructor(CallSiteArray.java:59) at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callConstructor(AbstractCallSite.java:237) at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callConstructor(AbstractCallSite.java:249) at org.codehaus.groovy.tools.shell.Groovysh.<init>(Groovysh.groovy:108) at org.codehaus.groovy.tools.shell.Groovysh.<init>(Groovysh.groovy:97) at org.codehaus.groovy.tools.shell.Groovysh.<init>(Groovysh.groovy:125) at org.codehaus.groovy.tools.shell.Groovysh.<init>(Groovysh.groovy:129) at org.codehaus.groovy.tools.shell.Groovysh.<init>(Groovysh.groovy:133) at org.codehaus.groovy.tools.shell.GroovyshTest$1.<init>(GroovyshTest.groovy) at org.codehaus.groovy.tools.shell.GroovyshTest.createGroovysh(GroovyshTest.groovy:52) at org.codehaus.groovy.tools.shell.GroovyshTest.testDefaultResultHookStringArray(GroovyshTest.groovy:164) {code} > Fix warning "An illegal reflective access operation has occurred" > ----------------------------------------------------------------- > > Key: GROOVY-8339 > URL: https://issues.apache.org/jira/browse/GROOVY-8339 > Project: Groovy > Issue Type: Improvement > Components: groovy-jdk > Affects Versions: 2.4.11, 2.4.15 > Environment: >gradle --version > Gradle 4.2 > Build time: 2017-09-20 14:48:23 UTC > Revision: 5ba503cc17748671c83ce35d7da1cffd6e24dfbd > Groovy: 2.4.11 > Ant: Apache Ant(TM) version 1.9.6 compiled on June 29 2015 > JVM: 9 (Oracle Corporation 9+181) > OS: Windows 10 10.0 amd64 > Reporter: Benjamin Roedell > Priority: Major > Labels: newbie, security > > I'm running JDK-9 on Windows 10 with Gradle 4.2. > My global gradle.properties file contains the following line: > org.gradle.java.home=C:/Program Files/Java/jdk-9 > When I request the gradle version (gradle --version) I get the following > warning: > {code:none} > WARNING: An illegal reflective access operation has occurred > WARNING: Illegal reflective access by > org.codehaus.groovy.reflection.CachedClass > (file:/C:/Program%20Files/gradle-4.2/lib/groovy-all-2.4.11.jar) to method > java.lang.Object.finalize() > WARNING: Please consider reporting this to the maintainers of > org.codehaus.groovy.reflection.CachedClass > WARNING: Use --illegal-access=warn to enable warnings of further illegal > reflective access operations > WARNING: All illegal access operations will be denied in a future release > {code} > This warning displayed regardless of whether I'm using a regular command > prompt or an elevated rights (Administrator) command prompt. > Here's the full command and output: > {code:none} > gradle --version > WARNING: An illegal reflective access operation has occurred > WARNING: Illegal reflective access by > org.codehaus.groovy.reflection.CachedClass > (file:/C:/Program%20Files/gradle-4.2/lib/groovy-all-2.4.11.jar) to method > java.lang.Object.finalize() > WARNING: Please consider reporting this to the maintainers of > org.codehaus.groovy.reflection.CachedClass > WARNING: Use --illegal-access=warn to enable warnings of further illegal > reflective access operations > WARNING: All illegal access operations will be denied in a future release > ------------------------------------------------------------ > Gradle 4.2 > ------------------------------------------------------------ > Build time: 2017-09-20 14:48:23 UTC > Revision: 5ba503cc17748671c83ce35d7da1cffd6e24dfbd > Groovy: 2.4.11 > Ant: Apache Ant(TM) version 1.9.6 compiled on June 29 2015 > JVM: 9 (Oracle Corporation 9+181) > OS: Windows 10 10.0 amd64 > {code} -- This message was sent by Atlassian JIRA (v7.6.3#76005)