This is an automated email from the ASF dual-hosted git repository.
emilles pushed a commit to branch GROOVY_2_5_X
in repository https://gitbox.apache.org/repos/asf/groovy.git
The following commit(s) were added to refs/heads/GROOVY_2_5_X by this push:
new d8359c2764 add test cases
d8359c2764 is described below
commit d8359c276437cf0b83932fbc166e154aa3c62343
Author: Eric Milles <[email protected]>
AuthorDate: Thu Aug 25 12:45:01 2022 -0500
add test cases
---
.../stc/FieldsAndPropertiesSTCTest.groovy | 750 +++++++++++++--------
.../sc/FieldsAndPropertiesStaticCompileTest.groovy | 389 ++++++-----
2 files changed, 645 insertions(+), 494 deletions(-)
diff --git a/src/test/groovy/transform/stc/FieldsAndPropertiesSTCTest.groovy
b/src/test/groovy/transform/stc/FieldsAndPropertiesSTCTest.groovy
index 52582616ff..09c0b05b42 100644
--- a/src/test/groovy/transform/stc/FieldsAndPropertiesSTCTest.groovy
+++ b/src/test/groovy/transform/stc/FieldsAndPropertiesSTCTest.groovy
@@ -27,78 +27,66 @@ import groovy.transform.PackageScope
class FieldsAndPropertiesSTCTest extends StaticTypeCheckingTestCase {
void testAssignFieldValue() {
- assertScript """
- class A {
+ assertScript '''
+ class C {
int x
}
-
- A a = new A()
- a.x = 1
- """
+ C c = new C()
+ c.x = 1
+ '''
}
void testAssignFieldValueWithWrongType() {
shouldFailWithMessages '''
- class A {
+ class C {
int x
}
-
- A a = new A()
- a.x = '1'
- ''', 'Cannot assign value of type java.lang.String to variable of type
int'
- }
-
- void testMapDotPropertySyntax() {
- assertScript '''
- HashMap map = [:]
- map['a'] = 1
- map.b = 2
- assert map.get('a') == 1
- assert map.get('b') == 2
- '''
+ C c = new C()
+ c.x = '1'
+ ''',
+ 'Cannot assign value of type java.lang.String to variable of type int'
}
void testInferenceFromFieldType() {
assertScript '''
- class A {
+ class C {
String name = 'Cedric'
}
- A a = new A()
- def b = a.name
- b.toUpperCase() // type of b should be inferred from field type
+ C c = new C()
+ def x = c.name
+ x.toUpperCase() // type of x should be inferred from field type
'''
}
void testAssignFieldValueWithAttributeNotation() {
- assertScript """
- class A {
+ assertScript '''
+ class C {
int x
}
-
- A a = new A()
- a.@x = 1
- """
+ C c = new C()
+ c.@x = 1
+ '''
}
void testAssignFieldValueWithWrongTypeAndAttributeNotation() {
shouldFailWithMessages '''
- class A {
+ class C {
int x
}
-
- A a = new A()
- a.@x = '1'
- ''', 'Cannot assign value of type java.lang.String to variable of
type int'
+ C c = new C()
+ c.@x = '1'
+ ''',
+ 'Cannot assign value of type java.lang.String to variable of type int'
}
void testInferenceFromAttributeType() {
assertScript '''
- class A {
+ class C {
String name = 'Cedric'
}
- A a = new A()
- def b = a.@name
- b.toUpperCase() // type of b should be inferred from field type
+ C c = new C()
+ def x = c.@name
+ x.toUpperCase() // type of x should be inferred from field type
'''
}
@@ -106,120 +94,142 @@ class FieldsAndPropertiesSTCTest extends
StaticTypeCheckingTestCase {
shouldFailWithMessages '''
Object o = new Object()
o.x = 0
- ''', 'No such property: x for class: java.lang.Object'
+ ''',
+ 'No such property: x for class: java.lang.Object'
}
void testShouldComplainAboutMissingProperty2() {
shouldFailWithMessages '''
- class A {
+ class C {
}
- A a = new A()
- a.x = 0
- ''', 'No such property: x for class: A'
+ C c = new C()
+ c.x = 0
+ ''',
+ 'No such property: x for class: C'
+ }
+
+ @NotYetImplemented
+ void testShouldComplainAboutMissingProperty3() {
+ shouldFailWithMessages '''
+ class C {
+ private x
+ }
+ class D extends C {
+ void test() {
+ this.x
+ }
+ }
+ ''',
+ 'The field C.x is not accessible'
}
void testShouldComplainAboutMissingAttribute() {
shouldFailWithMessages '''
Object o = new Object()
o.@x = 0
- ''', 'No such attribute: x for class: java.lang.Object'
+ ''',
+ 'No such attribute: x for class: java.lang.Object'
}
void testShouldComplainAboutMissingAttribute2() {
shouldFailWithMessages '''
- class A {
+ class C {
}
- A a = new A()
- a.@x = 0
- ''', 'No such attribute: x for class: A'
+ C c = new C()
+ c.@x = 0
+ ''',
+ 'No such attribute: x for class: C'
}
void testShouldComplainAboutMissingAttribute3() {
shouldFailWithMessages '''
- class A {
+ class C {
def getX() { }
}
- A a = new A()
- println a.@x
- ''', 'No such attribute: x for class: A'
+ C c = new C()
+ println c.@x
+ ''',
+ 'No such attribute: x for class: C'
}
void testShouldComplainAboutMissingAttribute4() {
shouldFailWithMessages '''
- class A {
+ class C {
def setX(x) { }
}
- A a = new A()
- a.@x = 0
- ''', 'No such attribute: x for class: A'
+ C c = new C()
+ c.@x = 0
+ ''',
+ 'No such attribute: x for class: C'
}
@NotYetImplemented
void testShouldComplainAboutMissingAttribute5() {
shouldFailWithMessages '''
- class A {
+ class C {
private x
}
- class B extends A {
+ class D extends C {
void test() {
this.@x
}
}
- ''', 'The field A.x is not accessible'
+ ''',
+ 'The field C.x is not accessible'
}
void testPropertyWithInheritance() {
assertScript '''
- class A {
+ class C {
int x
}
- class B extends A {
+ class D extends C {
}
-
- B b = new B()
- assert b.x == 0
-
- b.x = 2
- assert b.x == 2
+ D d = new D()
+ assert d.x == 0
+ d.x = 2
+ assert d.x == 2
'''
}
void testPropertyTypeWithInheritance() {
shouldFailWithMessages '''
- class A {
+ class C {
int x
}
- class B extends A {
+ class D extends C {
}
- B b = new B()
- b.x = '2'
- ''', 'Cannot assign value of type java.lang.String to variable of type
int'
+ D d = new D()
+ d.x = '2'
+ ''',
+ 'Cannot assign value of type java.lang.String to variable of type int'
}
void testPropertyWithInheritanceFromAnotherSourceUnit() {
assertScript '''
- class B extends
groovy.transform.stc.FieldsAndPropertiesSTCTest.BaseClass {
+ class C extends
groovy.transform.stc.FieldsAndPropertiesSTCTest.BaseClass {
}
- B b = new B()
- b.x = 2
+ C c = new C()
+ c.x = 2
'''
}
void testPropertyWithInheritanceFromAnotherSourceUnit2() {
shouldFailWithMessages '''
- class B extends
groovy.transform.stc.FieldsAndPropertiesSTCTest.BaseClass {
+ class C extends
groovy.transform.stc.FieldsAndPropertiesSTCTest.BaseClass {
}
- B b = new B()
- b.x = '2'
- ''', 'Cannot assign value of type java.lang.String to variable of type
int'
+ C c = new C()
+ c.x = '2'
+ ''',
+ 'Cannot assign value of type java.lang.String to variable of type int'
}
void testPropertyWithSuperInheritanceFromAnotherSourceUnit() {
assertScript '''
- class B extends
groovy.transform.stc.FieldsAndPropertiesSTCTest.BaseClass2 {
+ class C extends
groovy.transform.stc.FieldsAndPropertiesSTCTest.BaseClass2 {
}
- B b = new B()
- b.x = 2
+ C c = new C()
+ c.x = 2
'''
}
@@ -227,6 +237,7 @@ class FieldsAndPropertiesSTCTest extends
StaticTypeCheckingTestCase {
void testStaticPropertyWithInheritanceFromAnotherSourceUnit() {
assertScript '''
import groovy.transform.stc.FieldsAndPropertiesSTCTest.Public
+
assert Public.answer == 42
assert Public.CONST == 'XX'
assert Public.VALUE == null
@@ -234,6 +245,7 @@ class FieldsAndPropertiesSTCTest extends
StaticTypeCheckingTestCase {
assert Public.VALUE == 'YY'
Public.@VALUE = 'ZZ'
assert Public.@VALUE == 'ZZ'
+ Public.VALUE = null
'''
}
@@ -264,7 +276,6 @@ class FieldsAndPropertiesSTCTest extends
StaticTypeCheckingTestCase {
Integer m() { 123456 - p }
Integer m(int i) { i - p }
}
-
def c = new C()
assert c.m() == 123456 // BUG! exception in phase 'class
generation' ...
assert c.m(123) == 123 // ClassCastException: class
org.codehaus.groovy.ast.Parameter cannot be cast to ...
@@ -276,7 +287,6 @@ class FieldsAndPropertiesSTCTest extends
StaticTypeCheckingTestCase {
assertScript '''
class Person {
String name
-
static Person create() {
def p = new Person()
p.setName("Guillaume")
@@ -284,7 +294,6 @@ class FieldsAndPropertiesSTCTest extends
StaticTypeCheckingTestCase {
return p
}
}
-
Person.create()
'''
}
@@ -292,95 +301,102 @@ class FieldsAndPropertiesSTCTest extends
StaticTypeCheckingTestCase {
// GROOVY-5443
void testFieldInitShouldPass() {
assertScript '''
- class Foo {
+ class C {
int bar = 1
}
- new Foo()
+ new C()
'''
}
// GROOVY-5443
void testFieldInitShouldNotPassBecauseOfIncompatibleTypes() {
shouldFailWithMessages '''
- class Foo {
+ class C {
int bar = new Date()
}
- new Foo()
- ''', 'Cannot assign value of type java.util.Date to variable of type
int'
+ new C()
+ ''',
+ 'Cannot assign value of type java.util.Date to variable of type int'
}
// GROOVY-5443
void testFieldInitShouldNotPassBecauseOfIncompatibleTypesWithClosure() {
shouldFailWithMessages '''
- class Foo {
+ class C {
Closure<List> bar = { Date date -> date.getTime() }
}
- new Foo()
- ''', 'Incompatible generic argument types. Cannot assign
groovy.lang.Closure <java.lang.Long> to: groovy.lang.Closure <List>'
+ new C()
+ ''',
+ 'Incompatible generic argument types. Cannot assign
groovy.lang.Closure <java.lang.Long> to: groovy.lang.Closure <List>'
}
void testFieldInitShouldNotPassBecauseOfIncompatibleTypesWithClosure2() {
shouldFailWithMessages '''
- class Foo {
+ class C {
java.util.concurrent.Callable<String> bar = { 123 }
}
- new Foo()
- ''', 'Incompatible generic argument types. Cannot assign
java.util.concurrent.Callable <java.lang.Integer> to:
java.util.concurrent.Callable <String>'
+ new C()
+ ''',
+ 'Incompatible generic argument types. Cannot assign
java.util.concurrent.Callable <java.lang.Integer> to:
java.util.concurrent.Callable <String>'
}
// GROOVY-9882
- void testFieldInitShouldPassForCcompatibleTypesWithClosure() {
+ void testFieldInitShouldPassForCompatibleTypesWithClosure() {
assertScript '''
- class Foo {
+ class C {
java.util.concurrent.Callable<String> bar = { 'abc' }
}
- assert new Foo().bar.call() == 'abc'
+ assert new C().bar.call() == 'abc'
'''
}
- void testFieldInitClosureParameterMismatch() {
+ void testClosureParameterMismatch() {
shouldFailWithMessages '''
- class Foo {
- java.util.concurrent.Callable<String> bar = { a -> '' }
+ class C {
+ java.util.concurrent.Callable<String> bar = { baz -> '' }
+ }
+ ''',
+ 'Wrong number of parameters'
+
+ if (!groovy.test.GroovyAssert.isAtLeastJdk('1.8')) return
+
+ shouldFailWithMessages '''
+ class C {
+ java.util.function.Consumer<String> bar = { -> null }
}
- new Foo()
- ''', 'Wrong number of parameters'
+ ''',
+ 'Wrong number of parameters'
+ }
+
+ // GROOVY-9991
+ void testClosureParameterMatch() {
+ if (!groovy.test.GroovyAssert.isAtLeastJdk('1.8')) return
+
+ assertScript '''
+ java.util.function.Consumer<String> s = { print it }
+ '''
+ assertScript '''
+ java.util.function.Predicate p = { x -> false }
+ '''
+ assertScript '''
+ java.util.function.Predicate p = { false }
+ '''
}
// GROOVY-5517
void testShouldFindStaticPropertyEvenIfObjectImplementsMap() {
assertScript '''
- class MyHashMap extends HashMap {
+ class C extends HashMap {
public static int version = 666
}
- def map = new MyHashMap()
+ def map = new C()
map['foo'] = 123
- Object value = map.foo
+ def value = map.foo
assert value == 123
+ map['foo'] = 4.5
value = map['foo']
- assert value == 123
- int v = MyHashMap.version
- assert v == 666
- '''
- }
-
- void testListDotProperty() {
- assertScript '''class Elem { int value }
- List<Elem> list = new LinkedList<Elem>()
- list.add(new Elem(value:123))
- list.add(new Elem(value:456))
- assert list.value == [ 123, 456 ]
- list.add(new Elem(value:789))
- assert list.value == [ 123, 456, 789 ]
- '''
-
- assertScript '''class Elem { String value }
- List<Elem> list = new LinkedList<Elem>()
- list.add(new Elem(value:'123'))
- list.add(new Elem(value:'456'))
- assert list.value == [ '123', '456' ]
- list.add(new Elem(value:'789'))
- assert list.value == [ '123', '456', '789' ]
+ assert value == 4.5
+ assert C.version == 666
'''
}
@@ -403,102 +419,128 @@ class FieldsAndPropertiesSTCTest extends
StaticTypeCheckingTestCase {
void testSetterUsingPropertyNotation() {
assertScript '''
- class A {
- boolean ok = false;
- void setFoo(String foo) { ok = foo == 'foo' }
+ class C {
+ boolean ok = false
+ void setFoo(String foo) { ok = (foo == 'foo') }
}
- def a = new A()
- a.foo = 'foo'
- assert a.ok
+ def c = new C()
+ c.foo = 'foo'
+ assert c.ok
'''
}
void testSetterUsingPropertyNotationOnInterface() {
assertScript '''
- interface FooAware { void setFoo(String arg) }
- class A implements FooAware {
- void setFoo(String foo) { }
- }
- void test(FooAware a) {
- a.foo = 'foo'
- }
- def a = new A()
- test(a)
- '''
+ interface FooAware { void setFoo(String arg) }
+ class C implements FooAware {
+ void setFoo(String foo) { }
+ }
+ void test(FooAware fa) {
+ fa.foo = 'foo'
+ }
+ def c = new C()
+ test(c)
+ '''
}
- // GROOVY-5700
- void testInferenceOfMapDotProperty() {
+ void testListDotProperty1() {
+ assertScript '''class Elem { int value }
+ List<Elem> list = new LinkedList<Elem>()
+ list.add(new Elem(value:123))
+ list.add(new Elem(value:456))
+ assert list.value == [ 123, 456 ]
+ list.add(new Elem(value:789))
+ assert list.value == [ 123, 456, 789 ]
+ '''
+ assertScript '''class Elem { String value }
+ List<Elem> list = new LinkedList<Elem>()
+ list.add(new Elem(value:'123'))
+ list.add(new Elem(value:'456'))
+ assert list.value == [ '123', '456' ]
+ list.add(new Elem(value:'789'))
+ assert list.value == [ '123', '456', '789' ]
+ '''
+ }
+
+ void testListDotProperty2() {
assertScript '''
- def m = [retries: 10]
+ class C { int x }
+ def list = [new C(x:1), new C(x:2)]
@ASTTest(phase=INSTRUCTION_SELECTION, value={
- assert node.getNodeMetaData(INFERRED_TYPE) == Integer_TYPE
+ def type = node.getNodeMetaData(INFERRED_TYPE)
+ assert type.toString(false) == 'java.util.List
<java.lang.Integer>'
})
- def r1 = m['retries']
+ def x = list.x
+ assert x == [1,2]
+ '''
+ }
+ // GROOVY-5700
+ void testInferenceOfMapDotProperty() {
+ assertScript '''
+ def map = [key: 123]
@ASTTest(phase=INSTRUCTION_SELECTION, value={
assert node.getNodeMetaData(INFERRED_TYPE) == Integer_TYPE
})
- def r2 = m.retries
+ def val = map.key
+ assert val == 123
'''
}
- void testInferenceOfListDotProperty() {
- assertScript '''class Foo { int x }
- def list = [new Foo(x:1), new Foo(x:2)]
+ // GROOVY-5700, GROOVY-8788
+ void testInferenceOfMapSubProperty() {
+ assertScript '''
+ def map = [key: 123]
@ASTTest(phase=INSTRUCTION_SELECTION, value={
- def iType = node.getNodeMetaData(INFERRED_TYPE)
- assert iType == make(List)
- assert iType.isUsingGenerics()
- assert iType.genericsTypes[0].type == Integer_TYPE
+ assert node.getNodeMetaData(INFERRED_TYPE) == Integer_TYPE
})
- def r2 = list.x
- assert r2 == [ 1,2 ]
+ def val = map['key']
+ assert val == 123
'''
}
void testTypeCheckerDoesNotThinkPropertyIsReadOnly() {
assertScript '''
// a base class defining a read-only property
- class Top {
+ class C {
private String foo = 'foo'
String getFoo() { foo }
- String getFooFromTop() { foo }
+ String getFooFromC() { foo }
}
- // a subclass defining it's own field
- class Bottom extends Top {
+ // a subclass defining its own field
+ class D extends C {
private String foo
- Bottom(String msg) {
+ D(String msg) {
this.foo = msg
}
public String getFoo() { this.foo }
}
- def b = new Bottom('bar')
- assert b.foo == 'bar'
- assert b.fooFromTop == 'foo'
+ def d = new D('bar')
+ assert d.foo == 'bar'
+ assert d.fooFromC == 'foo'
'''
}
// GROOVY-5779
void testShouldNotUseNonStaticProperty() {
assertScript '''import java.awt.Color
- Color c = Color.red // should not be interpreted as Color.getRed()
+ Color c = Color.red // should not be interpreted as Color.getRed()
'''
}
// GROOVY-5725
void testAccessFieldDefinedInInterface() {
assertScript '''
- class Foo implements
groovy.transform.stc.FieldsAndPropertiesSTCTest.InterfaceWithField {
+ class C implements
groovy.transform.stc.FieldsAndPropertiesSTCTest.InterfaceWithField {
void test() {
assert boo == "I don't fancy fields in interfaces"
}
}
- new Foo().test()
+ new C().test()
'''
}
@@ -518,8 +560,70 @@ class FieldsAndPropertiesSTCTest extends
StaticTypeCheckingTestCase {
'''
}
- // GROOVY-9598
void testOuterPropertyAccess2() {
+ assertScript '''
+ class Outer {
+ class Inner {
+ def m() {
+ getP()
+ }
+ }
+ def p = 1
+ }
+ def i = new Outer.Inner(new Outer())
+ def x = i.m()
+ assert x == 1
+ '''
+ }
+
+ @NotYetImplemented // GROOVY-10414
+ void testOuterPropertyAccess3() {
+ assertScript '''
+ class Outer {
+ class Inner {
+ def m() {
+ setP(2)
+ getP()
+ }
+ }
+ def p = 1
+ }
+ def i = new Outer.Inner(new Outer())
+ def x = i.m()
+ assert x == 2
+ '''
+ }
+
+ // GROOVY-8050
+ void testOuterPropertyAccess4() {
+ shouldFailWithMessages '''
+ class Outer {
+ class Inner {
+ }
+ def p = 1
+ }
+ def i = new Outer.Inner(new Outer())
+ def x = i.p
+ ''',
+ 'No such property: p for class: Outer$Inner'
+ }
+
+ // GROOVY-8050
+ void testOuterPropertyAccess5() {
+ shouldFailWithMessages '''
+ class Outer {
+ class Inner {
+ }
+ def p = 1
+ }
+ def i = new Outer.Inner(new Outer())
+ def x = i.getP()
+ ''',
+ 'Cannot find matching method Outer$Inner#getP()'
+ }
+
+ // GROOVY-9598
+ void testOuterPropertyAccess6() {
shouldFailWithMessages '''
class Outer {
static class Inner {
@@ -531,10 +635,11 @@ class FieldsAndPropertiesSTCTest extends
StaticTypeCheckingTestCase {
}
def i = new Outer.Inner()
def x = i.m()
- ''', 'The variable [p] is undeclared.'
+ ''',
+ 'The variable [p] is undeclared.'
}
- void testOuterPropertyAccess3() {
+ void testOuterPropertyAccess7() {
shouldFailWithMessages '''
class Outer {
static class Inner {
@@ -546,11 +651,12 @@ class FieldsAndPropertiesSTCTest extends
StaticTypeCheckingTestCase {
}
def i = new Outer.Inner()
def x = i.m()
- ''', 'No such property: p for class: Outer$Inner'
+ ''',
+ 'No such property: p for class: Outer$Inner'
}
// GROOVY-7024
- void testOuterPropertyAccess4() {
+ void testOuterPropertyAccess8() {
assertScript '''
class Outer {
static Map props = [bar: 10, baz: 20]
@@ -566,7 +672,52 @@ class FieldsAndPropertiesSTCTest extends
StaticTypeCheckingTestCase {
'''
}
- void testPrivateFieldAccessInClosure() {
+ void testPrivateFieldAccessInAIC() {
+ assertScript '''
+ class C {
+ private int x
+ void foo() {
+ def aic = new Runnable() { void run() { x = 666 } }
+ aic.run()
+ }
+ void ensure() {
+ assert x == 666
+ }
+ }
+ def c = new C()
+ c.foo()
+ c.ensure()
+ '''
+ }
+
+ // GROOVY-9562
+ void testSuperPropertyAccessInAIC() {
+ assertScript '''
+ abstract class One {
+ int prop = 1
+ }
+
+ abstract class Two {
+ int prop = 2
+
+ abstract baz()
+ }
+
+ class Foo extends One {
+ Two bar() {
+ new Two() {
+ def baz() {
+ prop
+ }
+ }
+ }
+ }
+
+ assert new Foo().bar().baz() == 2
+ '''
+ }
+
+ void testPrivateFieldAccessInClosure1() {
assertScript '''
class C {
private int x
@@ -632,67 +783,19 @@ class FieldsAndPropertiesSTCTest extends
StaticTypeCheckingTestCase {
'''
}
- void testPrivateFieldAccessInAIC() {
- assertScript '''
- class A {
- private int x
- void foo() {
- def aic = new Runnable() { void run() { x = 666 } }
- aic.run()
- }
- void ensure() {
- assert x == 666
- }
- }
- def a = new A()
- a.foo()
- a.ensure()
- '''
- }
-
- // GROOVY-9562
- void testSuperPropertyAccessInAIC() {
- assertScript '''
- abstract class One {
- int prop = 1
- }
-
- abstract class Two {
- int prop = 2
-
- abstract baz()
- }
-
- class Foo extends One {
- Two bar() {
- new Two() {
- def baz() {
- prop
- }
- }
- }
- }
-
- assert new Foo().bar().baz() == 2
- '''
- }
-
// GROOVY-5737
void testGeneratedFieldAccessInClosure() {
assertScript '''
- import groovy.transform.*
import groovy.util.logging.*
@Log
class GreetingActor {
-
- def receive = {
- log.info "test"
- }
-
+ def receive = {
+ log.info "test"
+ }
}
new GreetingActor()
- '''
+ '''
}
// GROOVY-6610
@@ -719,57 +822,57 @@ class FieldsAndPropertiesSTCTest extends
StaticTypeCheckingTestCase {
// GROOVY-5872
void testAssignNullToFieldWithGenericsShouldNotThrowError() {
assertScript '''
- class Foo {
+ class C {
List<String> list = null // should not throw an error
}
- new Foo()
+ new C()
'''
}
void testSetterInWith() {
assertScript '''
- class Builder {
+ class C {
private int y
- void setFoo(int x) { y = x}
+ void setFoo(int x) { y = x }
int value() { y }
}
- def b = new Builder()
- b.with {
+ def c = new C()
+ c.with {
setFoo(5)
}
- assert b.value() == 5
+ assert c.value() == 5
'''
}
void testSetterInWithUsingPropertyNotation() {
assertScript '''
- class Builder {
+ class C {
private int y
- void setFoo(int x) { y = x}
+ void setFoo(int x) { y = x }
int value() { y }
}
- def b = new Builder()
- b.with {
+ def c = new C()
+ c.with {
foo = 5
}
- assert b.value() == 5
+ assert c.value() == 5
'''
}
void testSetterInWithUsingPropertyNotationAndClosureSharedVariable() {
assertScript '''
- class Builder {
+ class C {
private int y
- void setFoo(int x) { y = x}
+ void setFoo(int x) { y = x }
int value() { y }
}
- def b = new Builder()
+ def c = new C()
def csv = 0
- b.with {
+ c.with {
foo = 5
csv = 10
}
- assert b.value() == 5
+ assert c.value() == 5
assert csv == 10
'''
}
@@ -811,33 +914,29 @@ class FieldsAndPropertiesSTCTest extends
StaticTypeCheckingTestCase {
// GROOVY-6489
void testShouldNotThrowUnmatchedGenericsError() {
- assertScript '''public class Foo {
-
- private List<String> names;
-
- public List<String> getNames() {
- return names;
- }
-
- public void setNames(List<String> names) {
- this.names = names;
- }
-}
-
-class FooWorker {
-
- public void doSomething() {
- new Foo().with {
- names = new ArrayList()
- }
- }
-}
-
-new FooWorker().doSomething()'''
+ assertScript '''
+ public class Foo {
+ private List<String> names;
+ public List<String> getNames() {
+ return names;
+ }
+ public void setNames(List<String> names) {
+ this.names = names;
+ }
+ }
+ class FooWorker {
+ public void doSomething() {
+ new Foo().with {
+ names = new ArrayList()
+ }
+ }
+ }
+ new FooWorker().doSomething()
+ '''
}
void testShouldFailWithIncompatibleGenericTypes() {
- shouldFailWithMessages '''\
+ shouldFailWithMessages '''
public class Foo {
private List<String> names;
@@ -873,17 +972,18 @@ new FooWorker().doSomething()'''
}
void testPropertyWithMultipleSetters() {
- assertScript '''import org.codehaus.groovy.ast.expr.BinaryExpression
-import org.codehaus.groovy.ast.expr.BooleanExpression
-import org.codehaus.groovy.ast.stmt.AssertStatement
- class A {
+ assertScript '''
+ import org.codehaus.groovy.ast.expr.*
+ import org.codehaus.groovy.ast.stmt.*
+
+ class C {
private field
void setX(Integer a) {field=a}
void setX(String b) {field=b}
def getX(){field}
}
- @ASTTest(phase=INSTRUCTION_SELECTION,value={
+ @ASTTest(phase=INSTRUCTION_SELECTION, value={
lookup('test1').each { stmt ->
def exp = stmt.expression
assert exp instanceof BinaryExpression
@@ -904,36 +1004,86 @@ import org.codehaus.groovy.ast.stmt.AssertStatement
}
})
void testBody() {
- def a = new A()
+ def c = new C()
test1:
- a.x = 1
- assert a.x==1
+ c.x = 1
+ assert c.x==1
test2:
- a.x = "3"
- assert a.x == "3"
+ c.x = "3"
+ assert c.x == "3"
}
testBody()
'''
}
+ // GROOVY-9893
+ void testPropertyWithMultipleSetters2() {
+ assertScript '''
+ abstract class A { String which
+ void setX(String s) { which = 'String' }
+ }
+ class C extends A {
+ void setX(boolean b) { which = 'boolean' }
+ }
+
+ def c = new C()
+ c.x = 'value'
+ assert c.which == 'String'
+ '''
+ }
+
+ // GROOVY-9893
+ void testPropertyWithMultipleSetters3() {
+ assertScript '''
+ interface I {
+ void setX(String s)
+ }
+ abstract class A implements I { String which
+ void setX(boolean b) { which = 'boolean' }
+ }
+
+ void test(A a) {
+ a.x = 'value'
+ assert a.which == 'String'
+ }
+ test(new A() { void setX(String s) { which = 'String' } })
+ '''
+ }
+
+ // GROOVY-9893
+ void testPropertyWithMultipleSetters4() {
+ assertScript '''
+ trait T { String which
+ void setX(String s) { which = 'String' }
+ }
+ class C implements T {
+ void setX(boolean b) { which = 'boolean' }
+ }
+
+ def c = new C()
+ c.x = 'value'
+ assert c.which == 'String'
+ '''
+ }
+
void testPropertyAssignmentAsExpression() {
assertScript '''
- class Foo {
+ class C {
int x = 2
}
- def f = new Foo()
- def v = f.x = 3
- assert v == 3
-'''
+ def c = new C()
+ def x = c.x = 3
+ assert x == 3
+ '''
}
void testPropertyAssignmentInSubClassAndMultiSetter() {
10.times {
assertScript '''
- class A {
+ class C {
int which
- A() {
+ C() {
contentView = 42L
assert which == 2
}
@@ -942,7 +1092,7 @@ import org.codehaus.groovy.ast.stmt.AssertStatement
void setContentView(Long value) { which = 2 }
}
- class B extends A {
+ class D extends C {
void m() {
contentView = 42L
assert which == 2
@@ -951,25 +1101,25 @@ import org.codehaus.groovy.ast.stmt.AssertStatement
}
}
- new B().m()
+ new D().m()
'''
}
}
void testPropertyAssignmentInSubClassAndMultiSetterThroughDelegation() {
10.times {
- assertScript '''\
- class A {
+ assertScript '''
+ class C {
int which
void setContentView(Date value) { which = 1 }
void setContentView(Long value) { which = 2 }
}
- class B extends A {
+ class D extends C {
}
- new B().with {
+ new D().with {
contentView = 42L
assert which == 2
contentView = new Date()
@@ -1054,7 +1204,8 @@ import org.codehaus.groovy.ast.stmt.AssertStatement
Foo foo = new Foo()
foo.bar = new Bar()
- ''', 'Cannot assign value of type Bar to variable of type int'
+ ''',
+ 'Cannot assign value of type Bar to variable of type int'
assertScript '''
class Foo {
@@ -1091,7 +1242,8 @@ import org.codehaus.groovy.ast.stmt.AssertStatement
Foo foo = new Foo(bar: new Bar(x: 1))
Bar bar = foo.bar
- ''', 'Cannot assign value of type int to variable of type Bar'
+ ''',
+ 'Cannot assign value of type int to variable of type Bar'
assertScript '''
class Foo {
@@ -1112,6 +1264,8 @@ import org.codehaus.groovy.ast.stmt.AssertStatement
'''
}
+
//--------------------------------------------------------------------------
+
static interface InterfaceWithField {
String boo = "I don't fancy fields in interfaces"
}
diff --git
a/src/test/org/codehaus/groovy/classgen/asm/sc/FieldsAndPropertiesStaticCompileTest.groovy
b/src/test/org/codehaus/groovy/classgen/asm/sc/FieldsAndPropertiesStaticCompileTest.groovy
index 4a2d48f0e2..1fc5c60ed3 100644
---
a/src/test/org/codehaus/groovy/classgen/asm/sc/FieldsAndPropertiesStaticCompileTest.groovy
+++
b/src/test/org/codehaus/groovy/classgen/asm/sc/FieldsAndPropertiesStaticCompileTest.groovy
@@ -18,6 +18,7 @@
*/
package org.codehaus.groovy.classgen.asm.sc
+import groovy.transform.NotYetImplemented
import groovy.transform.stc.FieldsAndPropertiesSTCTest
final class FieldsAndPropertiesStaticCompileTest extends
FieldsAndPropertiesSTCTest implements StaticCompilationTestSupport {
@@ -41,6 +42,16 @@ final class FieldsAndPropertiesStaticCompileTest extends
FieldsAndPropertiesSTCT
'''
}
+ @NotYetImplemented
+ void testPropertyWithMultipleSetters2() {
+ super.testPropertyWithMultipleSetters2()
+ }
+
+ @NotYetImplemented
+ void testPropertyWithMultipleSetters3() {
+ super.testPropertyWithMultipleSetters3()
+ }
+
// GROOVY-5561
void testShouldNotThrowAccessForbidden() {
assertScript '''
@@ -49,7 +60,6 @@ final class FieldsAndPropertiesStaticCompileTest extends
FieldsAndPropertiesSTCT
def bar = createBar()
bar.foo
}
-
Bar createBar() { new Bar() }
}
class Bar {
@@ -74,7 +84,7 @@ final class FieldsAndPropertiesStaticCompileTest extends
FieldsAndPropertiesSTCT
void testUseDirectWriteFieldAccess() {
assertScript '''
- class A {
+ class C {
boolean setterCalled
protected int x
@@ -83,22 +93,22 @@ final class FieldsAndPropertiesStaticCompileTest extends
FieldsAndPropertiesSTCT
x = a
}
}
- class B extends A {
+ class D extends C {
void directAccess() {
this.@x = 2
}
}
- B b = new B()
- b.directAccess()
- assert b.isSetterCalled() == false
- assert b.x == 2
+ D d = new D()
+ d.directAccess()
+ assert d.isSetterCalled() == false
+ assert d.x == 2
'''
- assert astTrees['B'][1].contains('PUTFIELD A.x')
+ assert astTrees['D'][1].contains('PUTFIELD C.x')
}
void testUseDirectWriteStaticFieldAccess() {
assertScript '''
- class A {
+ class C {
static boolean setterCalled
static protected int x
@@ -107,21 +117,21 @@ final class FieldsAndPropertiesStaticCompileTest extends
FieldsAndPropertiesSTCT
x = a
}
}
- class B extends A {
+ class D extends C {
static void directAccess() {
this.@x = 2
}
}
- B.directAccess()
- assert B.isSetterCalled() == false
- assert B.x == 2
+ D.directAccess()
+ assert D.isSetterCalled() == false
+ assert D.x == 2
'''
- assert astTrees['B'][1].contains('PUTSTATIC A.x')
+ assert astTrees['D'][1].contains('PUTSTATIC C.x')
}
void testUseSetterFieldAccess() {
assertScript '''
- class A {
+ class C {
boolean setterCalled
protected int x
@@ -130,135 +140,133 @@ final class FieldsAndPropertiesStaticCompileTest
extends FieldsAndPropertiesSTCT
x = a
}
}
- class B extends A {
+ class D extends C {
void setterAccess() {
this.x = 2
}
}
- B b = new B()
- b.setterAccess()
- assert b.isSetterCalled() == true
- assert b.x == 2
+ D d = new D()
+ d.setterAccess()
+ assert d.isSetterCalled() == true
+ assert d.x == 2
'''
- assert astTrees['B'][1].contains('INVOKEVIRTUAL B.setX')
+ assert astTrees['D'][1].contains('INVOKEVIRTUAL D.setX')
}
void testUseDirectWriteFieldAccessFromOutsideClass() {
assertScript '''
- class A {
+ class C {
public int x
}
- class B {
- void directAccess(A a) {
- a.@x = 2
+ class D {
+ void directAccess(C c) {
+ c.@x = 2
}
}
- B b = new B()
- A a = new A()
- b.directAccess(a)
- assert a.x == 2
+ D d = new D()
+ C c = new C()
+ d.directAccess(c)
+ assert c.x == 2
'''
- assert astTrees['B'][1].contains('PUTFIELD A.x')
+ assert astTrees['D'][1].contains('PUTFIELD C.x')
}
void testUseDirectWriteFieldAccessPrivateWithRuntimeClassBeingDifferent() {
assertScript '''
- class A {
+ class C {
private int x
- public A(int x) {
+ public C(int x) {
this.@x = x
}
- public boolean sameAs(A a) {
- return this.@x == a.@x
+ public boolean sameAs(C c) {
+ return this.@x == c.@x
}
}
- class B extends A {
- // B.x visible in B A.x in A, but reflection depending on the
runtime type
- // would see B.x in A#sameAs and not A.x
+ class D extends C {
+ // D.x visible in D C.x in C, but reflection depending on the
runtime type would see C.x in C#sameAs and not C.x
private int x
- public B(int x) {
+ public D(int x) {
super(x)
this.@x = x + 50
}
}
- B b = new B(1)
- A a = new A(1)
- assert b.sameAs(a)
+ D d = new D(1)
+ C c = new C(1)
+ assert d.sameAs(c)
'''
// same with property style access:
assertScript '''
- class A {
+ class C {
private int x
- public A(int x) {
+ public C(int x) {
this.x = x
}
- public boolean sameAs(A a) {
- return this.x == a.x
+ public boolean sameAs(C c) {
+ return this.x == c.x
}
}
- class B extends A {
- // B.x visible in B A.x in A, but reflection depending on the
runtime type
- // would see B.x in A#sameAs and not A.x
+ class D extends C {
+ // D.x visible in D C.x in C, but reflection depending on the
runtime type would see D.x in C#sameAs and not C.x
private int x
- public B(int x) {
+ public D(int x) {
super(x)
this.x = x + 50
}
}
- B b = new B(1)
- A a = new A(1)
- assert b.sameAs(a)
+ D d = new D(1)
+ C c = new C(1)
+ assert d.sameAs(c)
'''
}
void testReadFieldFromSameClass() {
['', 'public', 'private', 'protected',
'@groovy.transform.PackageScope'].each { mod ->
assertScript """
- class A {
+ class C {
$mod int x
int m() {
x
}
}
- assert new A().m() == 0
+ assert new C().m() == 0
"""
- def a = astTrees['A'][1]
- assert (a =~ 'GETFIELD A.x').collect().size() == mod.empty ? 2 : 1
+ def a = astTrees['C'][1]
+ assert (a =~ 'GETFIELD C.x').collect().size() == mod.empty ? 2 : 1
}
}
void testWriteFieldFromSameClass() {
['', 'public', 'private', 'protected',
'@groovy.transform.PackageScope'].each { mod ->
assertScript """
- class A {
+ class C {
$mod int x
int m() {
x = 5
x
}
}
- new A().m() == 5
+ new C().m() == 5
"""
- def a = astTrees['A'][1]
- assert (a =~ 'PUTFIELD A.x').collect().size() == mod.empty ? 2 : 1
+ def a = astTrees['C'][1]
+ assert (a =~ 'PUTFIELD C.x').collect().size() == mod.empty ? 2 : 1
}
}
void testReadFieldFromSuperClass() {
['public', 'protected', '@groovy.transform.PackageScope'].each { mod ->
assertScript """
- class A {
+ class C {
$mod int x
}
- class B extends A {
+ class D extends C {
int m() {
x
}
}
- assert new B().m() == 0
+ assert new D().m() == 0
"""
- def b = astTrees['B'][1]
- assert b.contains('GETFIELD A.x')
+ def b = astTrees['D'][1]
+ assert b.contains('GETFIELD C.x')
}
}
@@ -266,21 +274,21 @@ final class FieldsAndPropertiesStaticCompileTest extends
FieldsAndPropertiesSTCT
void testReadFieldFromSuperClass2() {
assertScript '''
package p
- class A {
+ class C {
protected int x
}
- new p.A()
+ new p.C()
'''
assertScript '''
- class B extends p.A {
+ class D extends p.C {
int m() {
x
}
}
- assert new B().m() == 0
+ assert new D().m() == 0
'''
- def b = astTrees['B'][1]
- assert b.contains('GETFIELD p/A.x')
+ def b = astTrees['D'][1]
+ assert b.contains('GETFIELD p/C.x')
assert !b.contains('INVOKEINTERFACE
groovy/lang/GroovyObject.getProperty')
}
@@ -288,47 +296,47 @@ final class FieldsAndPropertiesStaticCompileTest extends
FieldsAndPropertiesSTCT
void testReadFieldFromSuperClass3() {
assertScript '''
package p
- class A {
+ class C {
protected static int x
}
- new p.A()
+ new p.C()
'''
assertScript '''
- class B extends p.A {
+ class D extends p.C {
static int m() {
x
}
}
- assert B.m() == 0
+ assert D.m() == 0
'''
- def b = astTrees['B'][1]
- assert b.contains('GETSTATIC B.x')
+ def b = astTrees['D'][1]
+ assert b.contains('GETSTATIC D.x')
assert !b.contains('INVOKESTATIC
org/codehaus/groovy/runtime/ScriptBytecodeAdapter.getGroovyObjectProperty')
}
void testReadPropertyFromSuperClass() {
['', 'public', 'private', 'protected',
'@groovy.transform.PackageScope'].each { mod ->
assertScript """
- class A {
+ class C {
$mod int x
int getX() { x }
}
- class B extends A {
+ class D extends C {
int m() {
x
}
}
- assert new B().m() == 0
+ assert new D().m() == 0
"""
- def b = astTrees['B'][1]
- assert !b.contains('GETFIELD A.x') : 'no GETFIELD in B'
- assert b.contains('INVOKEVIRTUAL B.getX') : 'getX() in B'
+ def b = astTrees['D'][1]
+ assert !b.contains('GETFIELD C.x') : 'no GETFIELD in D'
+ assert b.contains('INVOKEVIRTUAL D.getX') : 'getX() in D'
}
}
void testUseDirectReadFieldAccess() {
assertScript '''
- class A {
+ class C {
boolean getterCalled
protected int x
@@ -337,22 +345,22 @@ final class FieldsAndPropertiesStaticCompileTest extends
FieldsAndPropertiesSTCT
x
}
}
- class B extends A {
+ class D extends C {
void m() {
this.@x
}
}
- B b = new B()
- b.m()
- assert b.isGetterCalled() == false
+ D d = new D()
+ d.m()
+ assert d.isGetterCalled() == false
'''
- def b = astTrees['B'][1]
- assert b.contains('GETFIELD A.x')
+ def b = astTrees['D'][1]
+ assert b.contains('GETFIELD C.x')
}
void testUseAttributeExternal() {
assertScript '''
- class A {
+ class C {
boolean setterCalled
public int x
@@ -361,16 +369,16 @@ final class FieldsAndPropertiesStaticCompileTest extends
FieldsAndPropertiesSTCT
x = a
}
}
- A a = new A()
- a.@x = 100
- assert a.x == 100
- assert a.isSetterCalled() == false
+ C c = new C()
+ c.@x = 100
+ assert c.x == 100
+ assert c.isSetterCalled() == false
'''
}
void testUseAttributeExternalSafe() {
assertScript '''
- class A {
+ class C {
boolean setterCalled
public int x
@@ -379,16 +387,16 @@ final class FieldsAndPropertiesStaticCompileTest extends
FieldsAndPropertiesSTCT
x = a
}
}
- A a = new A()
- a?.@x = 100
- assert a.x == 100
- assert a.isSetterCalled() == false
+ C c = new C()
+ c?.@x = 100
+ assert c.x == 100
+ assert c.isSetterCalled() == false
'''
}
void testUseAttributeExternalSafeWithNull() {
assertScript '''
- class A {
+ class C {
boolean setterCalled
public int x
@@ -397,14 +405,14 @@ final class FieldsAndPropertiesStaticCompileTest extends
FieldsAndPropertiesSTCT
x = a
}
}
- A a = null
- a?.@x = 100
+ C c = null
+ c?.@x = 100
'''
}
void testUseSetterExternal() {
assertScript '''
- class A {
+ class C {
boolean setterCalled
public int x
@@ -413,16 +421,16 @@ final class FieldsAndPropertiesStaticCompileTest extends
FieldsAndPropertiesSTCT
x = a
}
}
- A a = new A()
- a.x = 100
- assert a.x == 100
- assert a.isSetterCalled() == true
+ C c = new C()
+ c.x = 100
+ assert c.x == 100
+ assert c.isSetterCalled() == true
'''
}
void testUseAttributeExternalSpread() {
assertScript '''
- class A {
+ class C {
boolean setterCalled
public int x
@@ -431,16 +439,16 @@ final class FieldsAndPropertiesStaticCompileTest extends
FieldsAndPropertiesSTCT
x = a
}
}
- List<A> a = [new A(), new A()]
- a*.@x = 100
- println a[0].x == 100
- println a[0].isSetterCalled() == false
+ List<C> list = [new C(), new C()]
+ list*.@x = 100
+ println list[0].x == 100
+ println list[0].isSetterCalled() == false
'''
}
void testUseAttributeExternalSpreadSafeWithNull() {
assertScript '''
- class A {
+ class C {
boolean setterCalled
public int x
@@ -449,17 +457,17 @@ final class FieldsAndPropertiesStaticCompileTest extends
FieldsAndPropertiesSTCT
x = a
}
}
- List<A> a = [new A(), null]
- a*.@x = 100
- assert a[0].x == 100
- assert a[0].isSetterCalled() == false
- assert a[1] == null
+ List<C> list = [new C(), null]
+ list*.@x = 100
+ assert list[0].x == 100
+ assert list[0].isSetterCalled() == false
+ assert list[1] == null
'''
}
void testUseAttributeExternalSpreadUsingSetter() {
assertScript '''
- class A {
+ class C {
boolean setterCalled
public int x
@@ -468,16 +476,16 @@ final class FieldsAndPropertiesStaticCompileTest extends
FieldsAndPropertiesSTCT
x = a
}
}
- List<A> a = [new A(), new A()]
- a*.x = 100
- assert a[0].x == 100
- assert a[0].isSetterCalled() == true
+ List<C> list = [new C(), new C()]
+ list*.x = 100
+ assert list[0].x == 100
+ assert list[0].isSetterCalled() == true
'''
}
void testUseAttributeExternalSpreadSafeWithNullUsingSetter() {
assertScript '''
- class A {
+ class C {
boolean setterCalled
public int x
@@ -486,11 +494,11 @@ final class FieldsAndPropertiesStaticCompileTest extends
FieldsAndPropertiesSTCT
x = a
}
}
- List<A> a = [new A(), null]
- a*.x = 100
- assert a[0].x == 100
- assert a[0].isSetterCalled() == true
- assert a[1] == null
+ List<C> list = [new C(), null]
+ list*.x = 100
+ assert list[0].x == 100
+ assert list[0].isSetterCalled() == true
+ assert list[1] == null
'''
}
@@ -529,19 +537,18 @@ final class FieldsAndPropertiesStaticCompileTest extends
FieldsAndPropertiesSTCT
void testPropertyWithMultipleSetters() {
// we need to override the test because the AST is going to be changed
assertScript '''
- import org.codehaus.groovy.ast.expr.BinaryExpression
- import org.codehaus.groovy.ast.expr.BooleanExpression
- import org.codehaus.groovy.ast.stmt.AssertStatement
+ import org.codehaus.groovy.ast.expr.*
+ import org.codehaus.groovy.ast.stmt.*
import org.codehaus.groovy.transform.sc.ListOfExpressionsExpression
- class A {
+ class C {
private field
void setX(Integer a) {field=a}
void setX(String b) {field=b}
def getX(){field}
}
- @ASTTest(phase=INSTRUCTION_SELECTION,value={
+ @ASTTest(phase=INSTRUCTION_SELECTION, value={
lookup('test1').each { stmt ->
def exp = stmt.expression
assert exp instanceof ListOfExpressionsExpression
@@ -551,38 +558,35 @@ final class FieldsAndPropertiesStaticCompileTest extends
FieldsAndPropertiesSTCT
assert exp instanceof ListOfExpressionsExpression
}
})
- void testBody() {
- def a = new A()
+ void test() {
+ def c = new C()
test1:
- a.x = 1
- assert a.x==1
+ c.x = 1
+ assert c.x==1
test2:
- a.x = "3"
- assert a.x == "3"
+ c.x = "3"
+ assert c.x == "3"
}
- testBody()
+ test()
'''
}
void testCallSetterAsPropertyWithinFinallyBlockShouldNotThrowVerifyError()
{
try {
assertScript '''
- class Multi {
+ class C {
void setOut(int a) {}
}
- void foo() {
- def m = new Multi()
- try {
- } finally {
- m.out = 1
- }
+ def c = new C()
+ try {
+ } finally {
+ c.out = 1
}
- foo()
'''
} finally {
assert astTrees.values().any {
- it.toString().contains 'INVOKEVIRTUAL Multi.setOut (I)V'
+ it.toString().contains 'INVOKEVIRTUAL C.setOut (I)V'
}
}
}
@@ -590,26 +594,23 @@ final class FieldsAndPropertiesStaticCompileTest extends
FieldsAndPropertiesSTCT
void
testCallMultiSetterAsPropertyWithinFinallyBlockShouldNotThrowVerifyError() {
try {
assertScript '''
- class Multi {
+ class C {
void setOut(int a) {}
void setOut(String a) {}
}
- void foo() {
- def m = new Multi()
- try {
- } finally {
- m.out = 1
- m.out = 'foo'
- }
+ def c = new C()
+ try {
+ } finally {
+ c.out = 1
+ c.out = 'foo'
}
- foo()
'''
} finally {
assert astTrees.values().any {
def code = it.toString()
- code.contains('INVOKEVIRTUAL Multi.setOut (I)V') &&
- code.contains('INVOKEVIRTUAL Multi.setOut
(Ljava/lang/String;)V')
+ code.contains('INVOKEVIRTUAL C.setOut (I)V') &&
+ code.contains('INVOKEVIRTUAL C.setOut
(Ljava/lang/String;)V')
}
}
}
@@ -617,15 +618,15 @@ final class FieldsAndPropertiesStaticCompileTest extends
FieldsAndPropertiesSTCT
// GROOVY-7698
void testSafePropertyStyleSetterCalls() {
assertScript '''
- class Foo {
+ class C {
private String id
void setId(String id) {
this.id = id
}
}
- Foo foo = null
- foo?.id = 'new'
+ C c = null
+ c?.id = 'new'
'''
}
@@ -719,25 +720,25 @@ final class FieldsAndPropertiesStaticCompileTest extends
FieldsAndPropertiesSTCT
void testPrivateFieldMutationInAICUsesBridgeMethod() {
try {
assertScript '''
- class A {
+ class C {
private int x
void test() {
- def aic = new Runnable() { void run() { A.this.x = 666
} }
+ def aic = new Runnable() { void run() { C.this.x = 666
} }
aic.run()
assert x == 666
}
}
- new A().test()
+ new C().test()
'''
} finally {
- assert astTrees['A$1'][1].contains('INVOKESTATIC A.pfaccess$00
(LA;I)I')
+ assert astTrees['C$1'][1].contains('INVOKESTATIC C.pfaccess$00
(LC;I)I')
}
}
void testImplicitPrivateFieldMutationInAICUsesBridgeMethod() {
try {
assertScript '''
- class A {
+ class C {
private int x
void test() {
def aic = new Runnable() { void run() { x = 666 } }
@@ -745,17 +746,17 @@ final class FieldsAndPropertiesStaticCompileTest extends
FieldsAndPropertiesSTCT
assert x == 666
}
}
- new A().test()
+ new C().test()
'''
} finally {
- assert astTrees['A$1'][1].contains('INVOKESTATIC A.pfaccess$00
(LA;I)I')
+ assert astTrees['C$1'][1].contains('INVOKESTATIC C.pfaccess$00
(LC;I)I')
}
}
void testPrivateStaticFieldMutationInAICUsesBridgeMethod() {
try {
assertScript '''
- class A {
+ class C {
private static int x
void test() {
def aic = new Runnable() { void run() { x = 666 } }
@@ -763,17 +764,17 @@ final class FieldsAndPropertiesStaticCompileTest extends
FieldsAndPropertiesSTCT
assert x == 666
}
}
- new A().test()
+ new C().test()
'''
} finally {
- assert astTrees['A$1'][1].contains('INVOKESTATIC A.pfaccess$00
(LA;I)I')
+ assert astTrees['C$1'][1].contains('INVOKESTATIC C.pfaccess$00
(LC;I)I')
}
}
void testMultiplePrivateFieldMutatorBridgeMethods() {
try {
assertScript '''
- class A {
+ class C {
private int x
private String y
Closure mutate = { x = 1; y = 'abc' }
@@ -784,22 +785,22 @@ final class FieldsAndPropertiesStaticCompileTest extends
FieldsAndPropertiesSTCT
assert y == 'abc'
}
}
- new A().test()
+ new C().test()
'''
} finally {
- assert astTrees['A$_closure1'][1].contains('INVOKESTATIC
A.pfaccess$00 (LA;I)I')
- assert astTrees['A$_closure1'][1].contains('INVOKESTATIC
A.pfaccess$01 (LA;Ljava/lang/String;)Ljava/lang/String;')
+ assert astTrees['C$_closure1'][1].contains('INVOKESTATIC
C.pfaccess$00 (LC;I)I')
+ assert astTrees['C$_closure1'][1].contains('INVOKESTATIC
C.pfaccess$01 (LC;Ljava/lang/String;)Ljava/lang/String;')
}
}
void testPrivateFieldBridgeMethodsAreGeneratedAsNecessary() {
try {
assertScript '''
- class A {
+ class C {
private int accessed = 0
private String mutated
private String accessedAndMutated = ''
- Closure c = {
+ Closure cl = {
println accessed
mutated = 'abc'
println accessedAndMutated
@@ -807,50 +808,46 @@ final class FieldsAndPropertiesStaticCompileTest extends
FieldsAndPropertiesSTCT
}
void test() {
- c()
+ cl()
assert mutated == 'abc'
assert accessedAndMutated == 'def'
}
}
- new A().test()
+ new C().test()
'''
} finally {
- def dump = astTrees['A'][1]
+ def dump = astTrees['C'][1]
assert dump.contains('pfaccess$0') // accessor bridge method for
'accessed'
assert !dump.contains('pfaccess$00') // no mutator bridge method
for 'accessed'
assert dump.contains('pfaccess$01') // mutator bridge method for
'mutated'
assert dump.contains('pfaccess$1') // accessor bridge method for
'mutated' -- GROOVY-9385
assert dump.contains('pfaccess$2') // accessor bridge method for
'accessedAndMutated'
assert dump.contains('pfaccess$02') // mutator bridge method for
'accessedAndMutated'
- dump = astTrees['A$_closure1'][1]
- assert dump.contains('INVOKESTATIC A.pfaccess$2
(LA;)Ljava/lang/String;')
- assert dump.contains('INVOKESTATIC A.pfaccess$02
(LA;Ljava/lang/String;)Ljava/lang/String;')
+ dump = astTrees['C$_closure1'][1]
+ assert dump.contains('INVOKESTATIC C.pfaccess$2
(LC;)Ljava/lang/String;')
+ assert dump.contains('INVOKESTATIC C.pfaccess$02
(LC;Ljava/lang/String;)Ljava/lang/String;')
}
}
// GROOVY-8369
void testPropertyAccessOnEnumClass() {
assertScript '''
- enum Foo {}
-
- def test() {
- assert Foo.getModifiers() == Foo.modifiers
- }
- test()
+ enum E { }
+ assert E.getModifiers() == E.modifiers
'''
}
// GROOVY-8753
void testPrivateFieldWithPublicGetter() {
assertScript '''
- class A {
+ class C {
private List<String> fooNames = []
- public A(Collection<String> names) {
+ public C(Collection<String> names) {
names.each { fooNames << it }
}
public List<String> getFooNames() { fooNames }
}
- assert new A(['foo1', 'foo2']).fooNames.size() == 2
+ assert new C(['foo1', 'foo2']).fooNames.size() == 2
'''
}
}