groovy-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Remi Forax <fo...@univ-mlv.fr>
Subject Re: trySetAccessible for Java 9
Date Wed, 05 Jul 2017 16:54:36 GMT
Use reflection, 
have inside groovy a method needSetAccessible coded like this 

private static final Method tryAccessible; 
static { 
Method method; 
try { 
method = AccessibleObject.class.getMethod("tryAccessible", ...); 
} catch(NoMethodFoundException e) { 
method = null; 
} 
tryAccessible = method; 
} 

public static boolena needSetAccessible(...) { 
if (tryAccessible == null) { 
return true; 
} 
return tryAcessible.invoke(...); 
} 

with jdk <= 8 needSetAccessible() always returns true otherwise, it delegates to tryAccessible.

So in the Groovy code, instead of calling setAccessible, one just have to call needSetAccessible
in a test before calling setAccessible. 

cheers, 
Rémi 

> De: "Cédric Champeau" <cchampeau@apache.org>
> À: dev@groovy.apache.org
> Envoyé: Mercredi 5 Juillet 2017 18:28:57
> Objet: trySetAccessible for Java 9

> Hi folks,
> We've made some good progress on making Gradle compatible with Java 9 as a
> runtime. However, since the latest release, builds easily get bloated with
> warnings like this:

> WARNING: An illegal reflective access operation has occurred
> WARNING: Illegal reflective access by org.codehaus.groovy.reflection.CachedClass
> (file:/home/cchampeau/.gradle/caches/modules-2/files-2.1/org.codehaus.groovy/groovy-all/2.4.11/444a64af79c540aad257e49d95050e7c189f1309/groovy-all-2.4.11.jar)
> to method java.lang.Object.finalize()

> Which is, I think, related to calls to AccessibleObject#setAccessible in
> org.codehaus.groovy.reflection.CachedClass#makeAccessible.

> However, this class precisely _tries_ to set accessible members of a class, but
> doesn't necessarily _have to_. That is, precisely the semantics of the new
> `trySetAccessible` method in AccessibleObject on JDK 9, so I would suggest we
> use it.

> However, as you may understand, things are never so easy: for binary and runtime
> compatibility, we need to be able to use this method only if JDK 9 is
> available, so it either requires using reflection, or method handles if we are
> using Java 8.

> It would be nice to have this in the next 2.x release of Groovy, so that we can
> get rid of those nasty error messages, but then it also means no MethodHandle,
> so no fast call of that `trySetAccessible` method. We cannot use the `Java7` or
> `Java8` approach either, without actually _building_ on JDK 9, which would be a
> problem itself.

> Any suggestion?

Mime
View raw message