[ 
https://issues.apache.org/jira/browse/GROOVY-12310?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=18131210#comment-18131210
 ] 

Paul King commented on GROOVY-12310:
------------------------------------

This is fixed in 6.0.0. The field case is now rejected at compile time:

{noformat}
[Static type checking] - Cannot access field: localValueSet of class: 
jakarta.faces.component.UIInput$PropertyKeys from class: Foo
{noformat}

The change was part of GROOVY-12314 (commit 3e11ee970e): 
{{StaticTypeCheckingVisitor.isFieldAccessible}} now checks that the declaring 
type itself is accessible from the calling class (JLS 6.6.1), not only the 
member's own modifiers. A public enum constant of a package-private enum is 
therefore out of reach, as it is in Java. The regression test is 
{{bugs/Groovy12310.groovy}}. The change was not backported to 5.0.x or 4.0.x, 
where the runtime {{IllegalAccessError}} remains.

On the second question, why {{println(UIInput.PropertyKeys)}} works: static 
compilation loads class literals through a generated {{$get$$class$...}} helper 
that uses {{Class.forName}}, which performs no access check, unlike an {{ldc}} 
of a class constant. Java would reject {{UIInput.PropertyKeys.class}} from 
another package. That behaviour is left in place deliberately: the class 
reference, and subscript access such as 
{{UIInput.PropertyKeys['localValueSet']}}, remain the way to reach such a 
constant from statically compiled code, and both are pinned by the test.

Method and constructor calls on an inaccessible type 
({{UIInput.PropertyKeys.values()}}, or {{new}} on a package-private class with 
a public constructor) still compile and fail at runtime; that is tracked 
separately in GROOVY-12484.

> Accessing enum constant of package-private enum under static compile fails at 
> runtime
> -------------------------------------------------------------------------------------
>
>                 Key: GROOVY-12310
>                 URL: https://issues.apache.org/jira/browse/GROOVY-12310
>             Project: Groovy
>          Issue Type: Bug
>    Affects Versions: 6.0.0-alpha-1, 5.0.3, 4.0.31
>            Reporter: Björn Kautler
>            Priority: Major
>
> Given this snippet:
> {code:java}
> @Grab('jakarta.faces:jakarta.faces-api:4.0.1')
> import [email protected]
> class Foo {
>   def foo() {
>     println(UIInput.PropertyKeys)
>     println(UIInput.PropertyKeys.localValueSet)
>   }
> }
> new Foo().foo() {code}
> The {{PropertyKeys}} enum is package-private.
> The whole script successfully runs through STC and static-compilation.
> At runtime the first {{println}} is successful, while the second one fails 
> with
> {code:java}
> java.lang.IllegalAccessError: failed to access class 
> jakarta.faces.component.UIInput$PropertyKeys from class Foo {code}
> at runtime.
> The STC should probably already recognize this and complain.
> What I also wonder is, that the class-access worked at runtime while the 
> constant access failed, shouldn't the class access fail already?



--
This message was sent by Atlassian Jira
(v10.3.26#10030026)

Reply via email to