You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@groovy.apache.org by em...@apache.org on 2024/04/06 00:58:48 UTC
(groovy) branch GROOVY_4_0_X updated: GROOVY-11319: STC: `super.x` shouldn't consider private methods
This is an automated email from the ASF dual-hosted git repository.
emilles pushed a commit to branch GROOVY_4_0_X
in repository https://gitbox.apache.org/repos/asf/groovy.git
The following commit(s) were added to refs/heads/GROOVY_4_0_X by this push:
new 305c499ae7 GROOVY-11319: STC: `super.x` shouldn't consider private methods
305c499ae7 is described below
commit 305c499ae756201d81cdf8b4360f0e25794c20e6
Author: Eric Milles <er...@thomsonreuters.com>
AuthorDate: Fri Apr 5 15:40:23 2024 -0500
GROOVY-11319: STC: `super.x` shouldn't consider private methods
4_0_X backport
---
.../transform/stc/StaticTypeCheckingVisitor.java | 41 +-
src/test/groovy/bugs/Groovy9292.groovy | 429 +++++++++------------
src/test/groovy/bugs/Groovy9293.groovy | 315 ++++++---------
.../groovy/transform/stc/CategoriesSTCTest.groovy | 7 +-
.../stc/FieldsAndPropertiesSTCTest.groovy | 41 +-
.../groovy/classgen/asm/sc/bugs/Groovy7300.groovy | 9 +-
.../packageScope/DifferentPackageTest.groovy | 7 +-
7 files changed, 387 insertions(+), 462 deletions(-)
diff --git a/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java b/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java
index ef79bf4c86..b9107cae1d 100644
--- a/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java
+++ b/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java
@@ -720,8 +720,10 @@ public class StaticTypeCheckingVisitor extends ClassCodeVisitorSupport {
if (!extension.handleUnresolvedProperty(expression)) {
Expression objectExpression = expression.getObjectExpression();
- addStaticTypeError("No such property: " + expression.getPropertyAsString() + " for class: " +
- prettyPrintTypeName(findCurrentInstanceOfClass(objectExpression, getType(objectExpression))), expression);
+ ClassNode objectExpressionType = (objectExpression instanceof ClassExpression
+ ? objectExpression.getType() : wrapTypeIfNecessary(getType(objectExpression)));
+ objectExpressionType = findCurrentInstanceOfClass(objectExpression, objectExpressionType);
+ addStaticTypeError("No such property: " + expression.getPropertyAsString() + " for class: " + prettyPrintTypeName(objectExpressionType), expression);
}
}
@@ -731,8 +733,10 @@ public class StaticTypeCheckingVisitor extends ClassCodeVisitorSupport {
if (!extension.handleUnresolvedAttribute(expression)) {
Expression objectExpression = expression.getObjectExpression();
- addStaticTypeError("No such attribute: " + expression.getPropertyAsString() + " for class: " +
- prettyPrintTypeName(findCurrentInstanceOfClass(objectExpression, getType(objectExpression))), expression);
+ ClassNode objectExpressionType = (objectExpression instanceof ClassExpression
+ ? objectExpression.getType() : wrapTypeIfNecessary(getType(objectExpression)));
+ objectExpressionType = findCurrentInstanceOfClass(objectExpression, objectExpressionType);
+ addStaticTypeError("No such attribute: " + expression.getPropertyAsString() + " for class: " + prettyPrintTypeName(objectExpressionType), expression);
}
}
@@ -1545,8 +1549,9 @@ public class StaticTypeCheckingVisitor extends ClassCodeVisitorSupport {
property = allowStaticAccessToMember(property, staticOnly);
// prefer explicit getter or setter over property if receiver is not 'this'
if (property == null || !enclosingTypes.contains(receiverType)) {
+ ClassNode enclosingType = enclosingTypes.iterator().next();
if (readMode) {
- if (getter != null) {
+ if (getter != null && hasAccessToMember(enclosingType, getter.getDeclaringClass(), getter.getModifiers())) {
ClassNode returnType = inferReturnTypeGenerics(receiverType, getter, ArgumentListExpression.EMPTY_ARGUMENTS);
storeInferredTypeForPropertyExpression(pexp, returnType);
storeTargetMethod(pexp, getter);
@@ -1555,9 +1560,11 @@ public class StaticTypeCheckingVisitor extends ClassCodeVisitorSupport {
pexp.putNodeMetaData(IMPLICIT_RECEIVER, delegationData);
}
return true;
+ } else {
+ getter = null; // GROOVY-11319
}
} else {
- if (!setters.isEmpty()) {
+ if (setters.stream().anyMatch(setter -> hasAccessToMember(enclosingType, setter.getDeclaringClass(), setter.getModifiers()))) {
if (visitor != null) {
for (MethodNode setter : setters) {
// visiting setter will not infer the property type since return type is void, so visit a dummy field instead
@@ -1577,8 +1584,10 @@ public class StaticTypeCheckingVisitor extends ClassCodeVisitorSupport {
}
pexp.removeNodeMetaData(READONLY_PROPERTY);
return true;
- } else if (getter != null && field == null) {
+ } else if (field == null && getter != null && hasAccessToMember(enclosingType, getter.getDeclaringClass(), getter.getModifiers())) {
pexp.putNodeMetaData(READONLY_PROPERTY, Boolean.TRUE); // GROOVY-9127
+ } else {
+ setters.clear(); // GROOVY-11319
}
}
}
@@ -1587,7 +1596,7 @@ public class StaticTypeCheckingVisitor extends ClassCodeVisitorSupport {
if (field != null && storeField(field, pexp, receiverType, visitor, receiver.getData(), !readMode)) return true;
- foundGetterOrSetter = (foundGetterOrSetter || !setters.isEmpty() || getter != null);
+ foundGetterOrSetter = (foundGetterOrSetter || getter != null || !setters.isEmpty());
}
// GROOVY-5568: the property may be defined by DGM
@@ -1794,11 +1803,15 @@ public class StaticTypeCheckingVisitor extends ClassCodeVisitorSupport {
private boolean storeField(final FieldNode field, final PropertyExpression expressionToStoreOn, final ClassNode receiver, final ClassCodeVisitorSupport visitor, final String delegationData, final boolean lhsOfAssignment) {
if (visitor != null) visitor.visitField(field);
checkOrMarkPrivateAccess(expressionToStoreOn, field, lhsOfAssignment);
- boolean accessible = hasAccessToMember(isSuperExpression(expressionToStoreOn.getObjectExpression()) ? typeCheckingContext.getEnclosingClassNode() : receiver, field.getDeclaringClass(), field.getModifiers());
-
- if (expressionToStoreOn instanceof AttributeExpression) { // TODO: expand to include PropertyExpression
- if (!accessible) {
- addStaticTypeError("The field " + field.getDeclaringClass().getNameWithoutPackage() + "." + field.getName() + " is not accessible", expressionToStoreOn.getProperty());
+ boolean superField = isSuperExpression(expressionToStoreOn.getObjectExpression());
+ boolean accessible = ( !superField && receiver.equals(field.getDeclaringClass()) ) // GROOVY-7300
+ || hasAccessToMember(typeCheckingContext.getEnclosingClassNode(), field.getDeclaringClass(), field.getModifiers());
+
+ if (!accessible) {
+ if (expressionToStoreOn instanceof AttributeExpression) {
+ addStaticTypeError("Cannot access field: " + field.getName() + " of class: " + prettyPrintTypeName(field.getDeclaringClass()), expressionToStoreOn.getProperty());
+ } else if (field.isPrivate()) {
+ return false;
}
}
@@ -1811,7 +1824,7 @@ public class StaticTypeCheckingVisitor extends ClassCodeVisitorSupport {
if (enclosing == null || !enclosing.getName().endsWith("init>"))
expressionToStoreOn.putNodeMetaData(READONLY_PROPERTY, Boolean.TRUE); // GROOVY-5450
} else if (accessible) {
- expressionToStoreOn.removeNodeMetaData(READONLY_PROPERTY);
+ expressionToStoreOn.removeNodeMetaData(READONLY_PROPERTY); // GROOVY-9127
}
return true;
}
diff --git a/src/test/groovy/bugs/Groovy9292.groovy b/src/test/groovy/bugs/Groovy9292.groovy
index 3ffe823f8e..2dc077c246 100644
--- a/src/test/groovy/bugs/Groovy9292.groovy
+++ b/src/test/groovy/bugs/Groovy9292.groovy
@@ -20,363 +20,304 @@ package groovy.bugs
import org.junit.Test
+import static groovy.test.GroovyAssert.assertScript
import static groovy.test.GroovyAssert.shouldFail
final class Groovy9292 {
- private final GroovyShell shell = new GroovyShell()
+ private final GroovyShell shell = GroovyShell.withConfig {
+ ast(groovy.transform.CompileStatic)
+ }
@Test
void 'test accessing a private super class field inside a closure - same module'() {
- shouldFail(MissingPropertyException) {
- shell.evaluate '''
- package a
+ shouldFail shell, MissingPropertyException, '''
+ package a
- class A {
- private String superField
- }
+ class A {
+ private String superField
+ }
- class B extends A {
- @groovy.transform.CompileStatic
- def test() {
- 'something'.with {
- return superField
- }
- }
+ class B extends A {
+ def test() {
+ "".with { superField }
}
+ }
- new B().test()
- '''
- }
+ new B().test()
+ '''
}
@Test
void 'test accessing a private super class field inside a closure - same package'() {
- shouldFail(MissingPropertyException) {
- shell.evaluate '''
- package a
-
- class A {
- private String superField
- }
+ assertScript shell, '''
+ package a
- assert true
- '''
+ class A {
+ private String superField
+ }
- shell.evaluate '''
- package a
+ assert true
+ '''
+ shouldFail shell, MissingPropertyException, '''
+ package a
- class B extends A {
- @groovy.transform.CompileStatic
- def test() {
- 'something'.with {
- return superField
- }
- }
+ class B extends A {
+ def test() {
+ "".with { superField }
}
+ }
- new B().test()
- '''
- }
+ new B().test()
+ '''
}
@Test
void 'test accessing a private super class field inside a closure - diff package'() {
- shouldFail(MissingPropertyException) {
- shell.evaluate '''
- package a
-
- class A {
- private String superField
- }
+ assertScript shell, '''
+ package a
- assert true
- '''
+ class A {
+ private String superField
+ }
- shell.evaluate '''
- package b
+ assert true
+ '''
+ shouldFail shell, MissingPropertyException, '''
+ package b
- class B extends a.A {
- @groovy.transform.CompileStatic
- def test() {
- 'something'.with {
- return superField
- }
- }
+ class B extends a.A {
+ def test() {
+ "".with { superField }
}
+ }
- new B().test()
- '''
- }
+ new B().test()
+ '''
}
@Test
void 'test accessing a private super class field inside a closure - same package, it qualifier'() {
- shouldFail(MissingPropertyException) {
- shell.evaluate '''
- package a
+ def err = shouldFail shell, '''
+ package a
- class A {
- private String superField
- }
+ class A {
+ private String superField
+ }
- class B extends A {
- @groovy.transform.CompileStatic
- def test() {
- with {
- return it.superField
- }
- }
+ class B extends A {
+ def test() {
+ with { it.superField }
}
+ }
- new B().test()
- '''
- }
+ new B().test()
+ '''
+ assert err =~ /No such property: superField for class: a.B/
}
@Test
void 'test accessing a private super class field inside a closure - diff package, it qualifier'() {
- shouldFail(MissingPropertyException) {
- shell.evaluate '''
- package a
-
- class A {
- private String superField
- }
+ assertScript shell, '''
+ package a
- assert true
- '''
+ class A {
+ private String superField
+ }
- shell.evaluate '''
- package b
+ assert true
+ '''
+ def err = shouldFail shell, '''
+ package b
- class B extends a.A {
- @groovy.transform.CompileStatic
- def test() {
- with {
- return it.superField
- }
- }
+ class B extends a.A {
+ def test() {
+ with { it.superField }
}
+ }
- new B().test()
- '''
- }
+ new B().test()
+ '''
+ assert err =~ /No such property: superField for class: b.B/
}
@Test
void 'test accessing a private super class field inside a closure - same package, this qualifier'() {
- shouldFail(MissingPropertyException) {
- shell.evaluate '''
- package a
+ def err = shouldFail shell, '''
+ package a
- class A {
- private String superField
- }
+ class A {
+ private String superField
+ }
- class B extends A {
- @groovy.transform.CompileStatic
- def test() {
- 'something'.with {
- return this.superField
- }
- }
+ class B extends A {
+ def test() {
+ "".with { this.superField }
}
+ }
- new B().test()
- '''
- }
+ new B().test()
+ '''
+ assert err =~ /No such property: superField for class: a.B/
}
@Test
void 'test accessing a private super class field inside a closure - diff package, this qualifier'() {
- shouldFail(MissingPropertyException) {
- shell.evaluate '''
- package a
+ assertScript shell, '''
+ package a
- class A {
- private String superField
- }
+ class A {
+ private String superField
+ }
- assert true
- '''
+ assert true
+ '''
+ def err = shouldFail shell, '''
+ package b
- shell.evaluate '''
- package b
-
- class B extends a.A {
- @groovy.transform.CompileStatic
- def test() {
- 'something'.with {
- return this.superField
- }
- }
+ class B extends a.A {
+ def test() {
+ "".with { this.superField }
}
+ }
- new B().test()
- '''
- }
+ new B().test()
+ '''
+ assert err =~ /No such property: superField for class: b.B/
}
@Test
void 'test accessing a private super class field inside a closure - same package, owner qualifier'() {
- shouldFail(MissingPropertyException) {
- shell.evaluate '''
- package a
+ def err = shouldFail shell, '''
+ package a
- class A {
- private String superField
- }
+ class A {
+ private String superField
+ }
- class B extends A {
- @groovy.transform.CompileStatic
- def test() {
- 'something'.with {
- return owner.superField
- }
- }
+ class B extends A {
+ def test() {
+ "".with { owner.superField }
}
+ }
- new B().test()
- '''
- }
+ new B().test()
+ '''
+ assert err =~ /No such property: superField for class: a.B/
}
@Test
void 'test accessing a private super class field inside a closure - diff package, owner qualifier'() {
- shouldFail(MissingPropertyException) {
- shell.evaluate '''
- package a
+ assertScript shell, '''
+ package a
- class A {
- private String superField
- }
+ class A {
+ private String superField
+ }
- assert true
- '''
+ assert true
+ '''
+ def err = shouldFail shell, '''
+ package b
- shell.evaluate '''
- package b
-
- class B extends a.A {
- @groovy.transform.CompileStatic
- def test() {
- 'something'.with {
- return owner.superField
- }
- }
+ class B extends a.A {
+ def test() {
+ "".with { owner.superField }
}
+ }
- new B().test()
- '''
- }
+ new B().test()
+ '''
+ assert err =~ /No such property: superField for class: b.B/
}
@Test
void 'test accessing a private super class field inside a closure - same package, delegate qualifier'() {
- shouldFail(MissingPropertyException) {
- shell.evaluate '''
- package a
+ def err = shouldFail shell, '''
+ package a
- class A {
- private String superField
- }
+ class A {
+ private String superField
+ }
- class B extends A {
- @groovy.transform.CompileStatic
- def test() {
- with {
- return delegate.superField
- }
- }
+ class B extends A {
+ def test() {
+ with { delegate.superField }
}
+ }
- new B().test()
- '''
- }
+ new B().test()
+ '''
+ assert err =~ /No such property: superField for class: a.B/
}
@Test
void 'test accessing a private super class field inside a closure - diff package, delegate qualifier'() {
- shouldFail(MissingPropertyException) {
- shell.evaluate '''
- package a
+ assertScript shell, '''
+ package a
- class A {
- private String superField
- }
-
- assert true
- '''
+ class A {
+ private String superField
+ }
- shell.evaluate '''
- package b
+ assert true
+ '''
+ def err = shouldFail shell, '''
+ package b
- class B extends a.A {
- @groovy.transform.CompileStatic
- def test() {
- with {
- return delegate.superField
- }
- }
+ class B extends a.A {
+ def test() {
+ with { delegate.superField }
}
+ }
- new B().test()
- '''
- }
+ new B().test()
+ '''
+ assert err =~ /No such property: superField for class: b.B/
}
@Test
void 'test accessing a private super class field inside a closure - same package, thisObject qualifier'() {
- shouldFail(MissingPropertyException) {
- shell.evaluate '''
- package a
+ def err = shouldFail shell, '''
+ package a
- class A {
- private String superField
- }
+ class A {
+ private String superField
+ }
- class B extends A {
- @groovy.transform.CompileStatic
- def test() {
- 'something'.with {
- return thisObject.superField
- }
- }
+ class B extends A {
+ def test() {
+ "".with { thisObject.superField }
}
+ }
- new B().test()
- '''
- }
+ new B().test()
+ '''
+ assert err =~ /No such property: superField for class: a.B/
}
@Test
void 'test accessing a private super class field inside a closure - diff package, thisObject qualifier'() {
- shouldFail(MissingPropertyException) {
- shell.evaluate '''
- package a
-
- class A {
- private String superField
- }
+ assertScript shell, '''
+ package a
- assert true
- '''
+ class A {
+ private String superField
+ }
- shell.evaluate '''
- package b
+ assert true
+ '''
+ def err = shouldFail shell, '''
+ package b
- class B extends a.A {
- @groovy.transform.CompileStatic
- def test() {
- 'something'.with {
- return thisObject.superField
- }
- }
+ class B extends a.A {
+ def test() {
+ "".with { thisObject.superField }
}
+ }
- new B().test()
- '''
- }
+ new B().test()
+ '''
+ assert err =~ /No such property: superField for class: b.B/
}
}
diff --git a/src/test/groovy/bugs/Groovy9293.groovy b/src/test/groovy/bugs/Groovy9293.groovy
index 2d989060d4..1a2ae0a028 100644
--- a/src/test/groovy/bugs/Groovy9293.groovy
+++ b/src/test/groovy/bugs/Groovy9293.groovy
@@ -18,343 +18,280 @@
*/
package groovy.bugs
-import groovy.transform.CompileStatic
import org.junit.Test
+import static groovy.test.GroovyAssert.assertScript
import static groovy.test.GroovyAssert.shouldFail
-@CompileStatic
final class Groovy9293 {
- private final GroovyShell shell = new GroovyShell()
+ private final GroovyShell shell = GroovyShell.withConfig {
+ ast(groovy.transform.CompileStatic)
+ imports {
+ normal 'groovy.transform.PackageScope'
+ }
+ }
@Test
void 'test accessing a package-private super class field inside a closure - same package'() {
- shell.evaluate '''
+ assertScript shell, '''
package a
class A {
- @groovy.transform.PackageScope
- String superField = 'works'
+ @PackageScope String superField = 'works'
}
class B extends A {
- @groovy.transform.CompileStatic
def test() {
- 'something'.with {
- return superField
- }
+ "".with { superField }
}
}
- def obj = new B()
- assert obj.test() == "works"
+ assert new B().test() == "works"
'''
}
@Test
void 'test accessing a package-private super class field inside a closure - diff package'() {
- shouldFail(MissingPropertyException) {
- shell.evaluate '''
- package a
-
- class A {
- @groovy.transform.PackageScope
- String superField = 'works'
- }
+ assertScript shell, '''
+ package a
- assert true
- '''
+ class A {
+ @PackageScope String superField
+ }
- shell.evaluate '''
- package b
+ assert true
+ '''
+ shouldFail shell, MissingPropertyException, '''
+ package b
- class B extends a.A {
- @groovy.transform.CompileStatic
- def test() {
- 'something'.with {
- return superField
- }
- }
+ class B extends a.A {
+ def test() {
+ "".with { superField }
}
+ }
- new B().test()
- '''
- }
+ new B().test()
+ '''
}
@Test
void 'test accessing a package-private super class field inside a closure - same package, it qualifier'() {
- shell.evaluate '''
+ assertScript shell, '''
package a
class A {
- @groovy.transform.PackageScope
- String superField = 'works'
+ @PackageScope String superField = 'works'
}
class B extends A {
- @groovy.transform.CompileStatic
def test() {
- with {
- return it.superField
- }
+ with { it.superField }
}
}
- def obj = new B()
- assert obj.test() == "works"
+ assert new B().test() == "works"
'''
}
@Test // GROOVY-9293
void 'test accessing a package-private super class field inside a closure - diff package, it qualifier'() {
- shouldFail(MissingPropertyException) {
- shell.evaluate '''
- package a
-
- class A {
- @groovy.transform.PackageScope
- String superField = 'works'
- }
+ assertScript shell, '''
+ package a
- assert true
- '''
+ class A {
+ @PackageScope String superField
+ }
- shell.evaluate '''
- package b
+ assert true
+ '''
+ def err = shouldFail shell, '''
+ package b
- class B extends a.A {
- @groovy.transform.CompileStatic
- def test() {
- with {
- return it.superField
- }
- }
+ class B extends a.A {
+ def test() {
+ with { it.superField }
}
+ }
- new B().test()
- '''
- }
+ new B().test()
+ '''
+ assert err =~ /No such property: superField for class: b.B/
}
@Test
void 'test accessing a package-private super class field inside a closure - same package, this qualifier'() {
- shell.evaluate '''
+ assertScript shell, '''
package a
class A {
- @groovy.transform.PackageScope
- String superField = 'works'
+ @PackageScope String superField = 'works'
}
class B extends A {
- @groovy.transform.CompileStatic
def test() {
- 'something'.with {
- return this.superField
- }
+ "".with { this.superField }
}
}
- def obj = new B()
- assert obj.test() == "works"
+ assert new B().test() == "works"
'''
}
@Test
void 'test accessing a package-private super class field inside a closure - diff package, this qualifier'() {
- shouldFail(MissingPropertyException) {
- shell.evaluate '''
- package a
-
- class A {
- @groovy.transform.PackageScope
- String superField = 'works'
- }
+ assertScript shell, '''
+ package a
- assert true
- '''
+ class A {
+ @PackageScope String superField
+ }
- shell.evaluate '''
- package b
+ assert true
+ '''
+ def err = shouldFail shell, '''
+ package b
- class B extends a.A {
- @groovy.transform.CompileStatic
- def test() {
- 'something'.with {
- return this.superField
- }
- }
+ class B extends a.A {
+ def test() {
+ "".with { this.superField }
}
+ }
- new B().test()
- '''
- }
+ new B().test()
+ '''
+ assert err =~ /No such property: superField for class: b.B/
}
@Test
void 'test accessing a package-private super class field inside a closure - same package, owner qualifier'() {
- shell.evaluate '''
+ assertScript shell, '''
package a
class A {
- @groovy.transform.PackageScope
- String superField = 'works'
+ @PackageScope String superField = 'works'
}
class B extends A {
- @groovy.transform.CompileStatic
def test() {
- 'something'.with {
- return owner.superField
- }
+ "".with { owner.superField }
}
}
- def obj = new B()
- assert obj.test() == "works"
+ assert new B().test() == "works"
'''
}
@Test // GROOVY-9293
void 'test accessing a package-private super class field inside a closure - diff package, owner qualifier'() {
- shouldFail(MissingPropertyException) {
- shell.evaluate '''
- package a
-
- class A {
- @groovy.transform.PackageScope
- String superField = 'works'
- }
+ assertScript shell, '''
+ package a
- assert true
- '''
+ class A {
+ @PackageScope String superField
+ }
- shell.evaluate '''
- package b
+ assert true
+ '''
+ def err = shouldFail shell, '''
+ package b
- class B extends a.A {
- @groovy.transform.CompileStatic
- def test() {
- 'something'.with {
- return owner.superField
- }
- }
+ class B extends a.A {
+ def test() {
+ "".with { owner.superField }
}
+ }
- new B().test()
- '''
- }
+ new B().test()
+ '''
+ assert err =~ /No such property: superField for class: b.B/
}
@Test
void 'test accessing a package-private super class field inside a closure - same package, delegate qualifier'() {
- shell.evaluate '''
+ assertScript shell, '''
package a
class A {
- @groovy.transform.PackageScope
- String superField = 'works'
+ @PackageScope String superField = 'works'
}
class B extends A {
- @groovy.transform.CompileStatic
def test() {
- with {
- return delegate.superField
- }
+ with { delegate.superField }
}
}
- def obj = new B()
- assert obj.test() == "works"
+ assert new B().test() == "works"
'''
}
@Test // GROOVY-9293
void 'test accessing a package-private super class field inside a closure - diff package, delegate qualifier'() {
- shouldFail(MissingPropertyException) {
- shell.evaluate '''
- package a
-
- class A {
- @groovy.transform.PackageScope
- String superField = 'works'
- }
+ assertScript shell, '''
+ package a
- assert true
- '''
+ class A {
+ @PackageScope String superField
+ }
- shell.evaluate '''
- package b
+ assert true
+ '''
+ def err = shouldFail shell, '''
+ package b
- class B extends a.A {
- @groovy.transform.CompileStatic
- def test() {
- with {
- return delegate.superField
- }
- }
+ class B extends a.A {
+ def test() {
+ with { delegate.superField }
}
+ }
- new B().test()
- '''
- }
+ new B().test()
+ '''
+ assert err =~ /No such property: superField for class: b.B/
}
@Test
void 'test accessing a package-private super class field inside a closure - same package, thisObject qualifier'() {
- shell.evaluate '''
+ assertScript shell, '''
package a
class A {
- @groovy.transform.PackageScope
- String superField = 'works'
+ @PackageScope String superField = 'works'
}
class B extends A {
- @groovy.transform.CompileStatic
def test() {
- 'something'.with {
- return thisObject.superField
- }
+ "".with { thisObject.superField }
}
}
- def obj = new B()
- assert obj.test() == "works"
+ assert new B().test() == "works"
'''
}
@Test // GROOVY-9293
void 'test accessing a package-private super class field inside a closure - diff package, thisObject qualifier'() {
- shouldFail(MissingPropertyException) {
- shell.evaluate '''
- package a
-
- class A {
- @groovy.transform.PackageScope
- String superField = 'works'
- }
+ assertScript shell, '''
+ package a
- assert true
- '''
+ class A {
+ @PackageScope String superField
+ }
- shell.evaluate '''
- package b
+ assert true
+ '''
+ def err = shouldFail shell, '''
+ package b
- class B extends a.A {
- @groovy.transform.CompileStatic
- def test() {
- 'something'.with {
- return thisObject.superField
- }
- }
+ class B extends a.A {
+ def test() {
+ "".with { thisObject.superField }
}
+ }
- new B().test()
- '''
- }
+ new B().test()
+ '''
+ assert err =~ /No such property: superField for class: b.B/
}
}
diff --git a/src/test/groovy/transform/stc/CategoriesSTCTest.groovy b/src/test/groovy/transform/stc/CategoriesSTCTest.groovy
index 4b30c5660b..0555f16cb0 100644
--- a/src/test/groovy/transform/stc/CategoriesSTCTest.groovy
+++ b/src/test/groovy/transform/stc/CategoriesSTCTest.groovy
@@ -18,7 +18,6 @@
*/
package groovy.transform.stc
-
/**
* Unit tests for static type checking : categories.
*/
@@ -29,8 +28,8 @@ class CategoriesSTCTest extends StaticTypeCheckingTestCase {
use(TimeCategory) {
1.day
}
- ''', 'Due to their dynamic nature, usage of categories is not possible with static type checking active', 'No such property: day for class: int'
+ ''',
+ 'Due to their dynamic nature, usage of categories is not possible with static type checking active',
+ 'No such property: day for class: java.lang.Integer'
}
-
}
-
diff --git a/src/test/groovy/transform/stc/FieldsAndPropertiesSTCTest.groovy b/src/test/groovy/transform/stc/FieldsAndPropertiesSTCTest.groovy
index 8c62ed70f5..64f72f40dd 100644
--- a/src/test/groovy/transform/stc/FieldsAndPropertiesSTCTest.groovy
+++ b/src/test/groovy/transform/stc/FieldsAndPropertiesSTCTest.groovy
@@ -18,7 +18,6 @@
*/
package groovy.transform.stc
-import groovy.test.NotYetImplemented
import groovy.transform.PackageScope
/**
@@ -194,8 +193,41 @@ class FieldsAndPropertiesSTCTest extends StaticTypeCheckingTestCase {
'No such property: x for class: C'
}
- @NotYetImplemented
+ // GROOVY-11319
void testShouldComplainAboutMissingProperty3() {
+ shouldFailWithMessages '''
+ class C {
+ private int getX() { 1 }
+ }
+ class D extends C {
+ void test() {
+ super.x
+ }
+ }
+ new D().test()
+ ''',
+ 'No such property: x for class: C'
+ }
+
+ // GROOVY-11319
+ void testShouldComplainAboutMissingProperty4() {
+ shouldFailWithMessages '''
+ class C {
+ private void setX(int i) {
+ assert false : 'cannot access'
+ }
+ }
+ class D extends C {
+ void test() {
+ super.x = 1
+ }
+ }
+ new D().test()
+ ''',
+ 'No such property: x for class: C'
+ }
+
+ void testShouldComplainAboutMissingProperty5() {
shouldFailWithMessages '''
class C {
private x
@@ -206,7 +238,7 @@ class FieldsAndPropertiesSTCTest extends StaticTypeCheckingTestCase {
}
}
''',
- 'The field C.x is not accessible'
+ 'No such property: x for class: D'
}
void testShouldComplainAboutMissingAttribute() {
@@ -259,8 +291,9 @@ class FieldsAndPropertiesSTCTest extends StaticTypeCheckingTestCase {
this.@x
}
}
+ new D().test()
''',
- 'The field C.x is not accessible'
+ 'Cannot access field: x of class: C'
}
void testPropertyWithInheritance() {
diff --git a/src/test/org/codehaus/groovy/classgen/asm/sc/bugs/Groovy7300.groovy b/src/test/org/codehaus/groovy/classgen/asm/sc/bugs/Groovy7300.groovy
index ba2f4a5a9a..f90d12a6b3 100644
--- a/src/test/org/codehaus/groovy/classgen/asm/sc/bugs/Groovy7300.groovy
+++ b/src/test/org/codehaus/groovy/classgen/asm/sc/bugs/Groovy7300.groovy
@@ -37,7 +37,7 @@ final class Groovy7300 extends StaticTypeCheckingTestCase implements StaticCompi
'''
}
- void testUseSuperToBypassOverride1a() {
+ void testUseSuperToBypassOverride2() {
assertScript '''
abstract class A {
protected x = 1
@@ -51,7 +51,7 @@ final class Groovy7300 extends StaticTypeCheckingTestCase implements StaticCompi
'''
}
- void testUseSuperToBypassOverride2() {
+ void testUseSuperToBypassOverride3() {
assertScript '''
abstract class A {
private x = 1
@@ -65,7 +65,7 @@ final class Groovy7300 extends StaticTypeCheckingTestCase implements StaticCompi
'''
}
- void testUseSuperToBypassOverride2a() {
+ void testUseSuperToBypassOverride4() {
shouldFailWithMessages '''
abstract class A {
private x = 1
@@ -76,6 +76,7 @@ final class Groovy7300 extends StaticTypeCheckingTestCase implements StaticCompi
def getX() { super.@x }
}
assert false
- ''', 'The field A.x is not accessible'
+ ''',
+ 'Cannot access field: x of class: A'
}
}
diff --git a/src/test/org/codehaus/groovy/transform/packageScope/DifferentPackageTest.groovy b/src/test/org/codehaus/groovy/transform/packageScope/DifferentPackageTest.groovy
index 4030f62a8f..f5650b3d19 100644
--- a/src/test/org/codehaus/groovy/transform/packageScope/DifferentPackageTest.groovy
+++ b/src/test/org/codehaus/groovy/transform/packageScope/DifferentPackageTest.groovy
@@ -161,7 +161,8 @@ final class DifferentPackageTest {
assert loader.loadClass('p.Peer').newInstance().half() == 21
}
- @Test // GROOVY-9106
+ // GROOVY-9106
+ @Test
void testSamePackageShouldSeeStaticProps5() {
def loader = addSources(
One: P_DOT_ONE,
@@ -186,8 +187,8 @@ final class DifferentPackageTest {
assert loader.loadClass('p.Peer').half() == 21
}
- @Test
// GROOVY-9093
+ @Test
void testDifferentPackageShouldNotSeeInstanceProps() {
def err = shouldFail CompilationFailedException, {
addSources(
@@ -207,8 +208,8 @@ final class DifferentPackageTest {
assert err.message =~ /Access to q.Two#value is forbidden/
}
- @Test
// GROOVY-9093
+ @Test
void testDifferentPackageShouldNotSeeStaticProps1() {
def err = shouldFail CompilationFailedException, {
addSources(