You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@groovy.apache.org by "Eric Milles (JIRA)" <ji...@apache.org> on 2019/04/26 17:34:00 UTC
[jira] [Commented] (GROOVY-9093) SC: subclass access to
package-private or private field is not indicated as an error
[ https://issues.apache.org/jira/browse/GROOVY-9093?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16827147#comment-16827147 ]
Eric Milles commented on GROOVY-9093:
-------------------------------------
If {{meth}} is made static, the STC error is present. A non-static method takes a different route through {{AsmClassGenerator.visitPropertyExpression}}.
{code:java}
public void visitPropertyExpression(PropertyExpression expression) {
...
if (controller.getCompileStack().isLHS()) {
//operandStack.box();
adapter = setProperty;
if (isGroovyObject(objectExpression)) adapter = setGroovyObjectProperty;
if (isThisOrSuperInStaticContext(objectExpression)) adapter = setProperty;
} else {
adapter = getProperty;
if (isGroovyObject(objectExpression)) adapter = getGroovyObjectProperty; // evaluates to true for both static and non-static method
if (isThisOrSuperInStaticContext(objectExpression)) adapter = getProperty; // evaluates to true only for static method
}
visitAttributeOrProperty(expression, adapter);
...
}
{code}
This results in a path for the non-static method's property at the end of {{visitAttributeOrProperty}} that does not include an error for private field from super class.
> SC: subclass access to package-private or private field is not indicated as an error
> ------------------------------------------------------------------------------------
>
> Key: GROOVY-9093
> URL: https://issues.apache.org/jira/browse/GROOVY-9093
> Project: Groovy
> Issue Type: Bug
> Affects Versions: 2.4.16, 3.0.0-alpha-4, 2.5.6
> Reporter: Eric Milles
> Priority: Major
>
> Consider the following:
> {code:groovy}
> package p
> import groovy.transform.*
> @CompileStatic class Main {
> @PackageScope static final String CONST = 'value'
> static main(args) {
> new q.Sub().meth()
> }
> }
> package q
> import groovy.transform.*
> @CompileStatic class Sub extends p.Main {
> void meth() {
> print CONST
> }
> }
> {code}
> This code compiles but throws an IllegalAccessError at run-time. Since a subclass should not have access to it's parent's private or package-private members (if located in a different package), there should be a compile-time error like "Access to Main#CONST is forbidden @ line -1, column -1."
> Replacing "@PackageScope" with "private" should get the same compile-time error but produces "MissingPropertyExceptionNoStack: No such property: CONST for class: q.Sub" at run-time.
--
This message was sent by Atlassian JIRA
(v7.6.3#76005)