You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@groovy.apache.org by pa...@apache.org on 2019/03/22 01:41:25 UTC
[groovy] branch GROOVY_2_5_X updated (f83238c -> 15b3299)
This is an automated email from the ASF dual-hosted git repository.
paulk pushed a change to branch GROOVY_2_5_X
in repository https://gitbox.apache.org/repos/asf/groovy.git.
from f83238c GROOVY-9051: The default constructor should be annotated with @Generated
new 5d048f5 GROOVY-9051: The default constructor should be annotated with @Generated (add test)
new 2ea0e96 minor refactor: rename for consistency and remove some redundant modifiers
new 8910c45 minor refactor: add issue to test name
new 74c7512 GROOVY-9053: Missing @Generated annotation on @Bindable
new f8e382b GROOVY-9052: Missing @Generated annotation on @Vetoable
new 7e13517 GROOVY-9050: Missing @Generated annotation in @Sortable
new 15b3299 GROOVY-9051: The default constructor should be annotated with @Generated (add also for inner class case)
The 7 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails. The revisions
listed as "add" were already present in the repository and have only
been added to this reference.
Summary of changes:
.../groovy/beans/BindableASTTransformation.java | 27 +-
.../groovy/beans/VetoableASTTransformation.java | 25 +-
.../apache/groovy/ast/tools/ClassNodeUtils.java | 10 +
.../classgen/InnerClassCompletionVisitor.java | 927 +++++++++++----------
.../transform/SortableASTTransformation.java | 3 +-
...bleTest.groovy => BindableTransformTest.groovy} | 31 +-
...est.groovy => ListenerListTransformTest.groovy} | 10 +-
...bleTest.groovy => VetoableTransformTest.groovy} | 29 +-
...mTest.groovy => GeneratedAnnotationTest.groovy} | 17 +-
9 files changed, 579 insertions(+), 500 deletions(-)
rename src/test/groovy/beans/{BindableTest.groovy => BindableTransformTest.groovy} (92%)
rename src/test/groovy/beans/{ListenerListASTTest.groovy => ListenerListTransformTest.groovy} (98%)
rename src/test/groovy/beans/{VetoableTest.groovy => VetoableTransformTest.groovy} (93%)
rename src/test/org/codehaus/groovy/transform/{GeneratedTransformTest.groovy => GeneratedAnnotationTest.groovy} (80%)
[groovy] 03/07: minor refactor: add issue to test name
Posted by pa...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
paulk pushed a commit to branch GROOVY_2_5_X
in repository https://gitbox.apache.org/repos/asf/groovy.git
commit 8910c45ff69dc9fdd9672904de44e0bdb3529b94
Author: Paul King <pa...@asert.com.au>
AuthorDate: Fri Mar 22 09:49:13 2019 +1000
minor refactor: add issue to test name
---
src/test/org/codehaus/groovy/transform/GeneratedAnnotationTest.groovy | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/src/test/org/codehaus/groovy/transform/GeneratedAnnotationTest.groovy b/src/test/org/codehaus/groovy/transform/GeneratedAnnotationTest.groovy
index 2896091..df02934 100644
--- a/src/test/org/codehaus/groovy/transform/GeneratedAnnotationTest.groovy
+++ b/src/test/org/codehaus/groovy/transform/GeneratedAnnotationTest.groovy
@@ -59,7 +59,7 @@ class GeneratedAnnotationTest extends GroovyShellTestCase {
}
@Test
- void testDefaultConstructorIsAnnotatedWithGenerated() {
+ void testDefaultConstructorIsAnnotatedWithGenerated_GROOVY9051() {
def person = evaluate('''
class Person {
}
[groovy] 06/07: GROOVY-9050: Missing @Generated annotation in
@Sortable
Posted by pa...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
paulk pushed a commit to branch GROOVY_2_5_X
in repository https://gitbox.apache.org/repos/asf/groovy.git
commit 7e13517d2803041aa2b7d80e7bc1a9b5761dec82
Author: Paul King <pa...@asert.com.au>
AuthorDate: Fri Mar 22 10:31:49 2019 +1000
GROOVY-9050: Missing @Generated annotation in @Sortable
---
src/main/java/org/apache/groovy/ast/tools/ClassNodeUtils.java | 10 ++++++++++
.../codehaus/groovy/transform/SortableASTTransformation.java | 3 ++-
2 files changed, 12 insertions(+), 1 deletion(-)
diff --git a/src/main/java/org/apache/groovy/ast/tools/ClassNodeUtils.java b/src/main/java/org/apache/groovy/ast/tools/ClassNodeUtils.java
index 814b127..daf60aa 100644
--- a/src/main/java/org/apache/groovy/ast/tools/ClassNodeUtils.java
+++ b/src/main/java/org/apache/groovy/ast/tools/ClassNodeUtils.java
@@ -104,6 +104,16 @@ public class ClassNodeUtils {
}
/**
+ * Add an inner class that is marked as @Generated.
+ *
+ * @see org.codehaus.groovy.ast.ModuleNode#addClass(ClassNode)
+ */
+ public static void addGeneratedInnerClass(ClassNode cNode, ClassNode inner) {
+ cNode.getModule().addClass(inner);
+ markAsGenerated(cNode, inner);
+ }
+
+ /**
* Add a method that is marked as @Generated.
*
* @see ClassNode#addConstructor(int, Parameter[], ClassNode[], Statement)
diff --git a/src/main/java/org/codehaus/groovy/transform/SortableASTTransformation.java b/src/main/java/org/codehaus/groovy/transform/SortableASTTransformation.java
index 9886d20..e45772c 100644
--- a/src/main/java/org/codehaus/groovy/transform/SortableASTTransformation.java
+++ b/src/main/java/org/codehaus/groovy/transform/SortableASTTransformation.java
@@ -45,6 +45,7 @@ import java.util.HashSet;
import java.util.List;
import java.util.Set;
+import static org.apache.groovy.ast.tools.ClassNodeUtils.addGeneratedInnerClass;
import static org.apache.groovy.ast.tools.ClassNodeUtils.addGeneratedMethod;
import static org.codehaus.groovy.ast.ClassHelper.isPrimitiveType;
import static org.codehaus.groovy.ast.ClassHelper.make;
@@ -189,7 +190,7 @@ public class SortableASTTransformation extends AbstractASTTransformation {
String className = classNode.getName() + "$" + propName + "Comparator";
ClassNode superClass = makeClassSafeWithGenerics(AbstractComparator.class, classNode);
InnerClassNode cmpClass = new InnerClassNode(classNode, className, ACC_PRIVATE | ACC_STATIC, superClass);
- classNode.getModule().addClass(cmpClass);
+ addGeneratedInnerClass(classNode, cmpClass);
addGeneratedMethod(cmpClass,
"compare",
[groovy] 01/07: GROOVY-9051: The default constructor should be
annotated with @Generated (add test)
Posted by pa...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
paulk pushed a commit to branch GROOVY_2_5_X
in repository https://gitbox.apache.org/repos/asf/groovy.git
commit 5d048f519649107b920edf85f224e32852f5f5dd
Author: Paul King <pa...@asert.com.au>
AuthorDate: Fri Mar 22 07:58:59 2019 +1000
GROOVY-9051: The default constructor should be annotated with @Generated (add test)
---
...nsformTest.groovy => GeneratedAnnotationTest.groovy} | 17 +++++++++++++++--
1 file changed, 15 insertions(+), 2 deletions(-)
diff --git a/src/test/org/codehaus/groovy/transform/GeneratedTransformTest.groovy b/src/test/org/codehaus/groovy/transform/GeneratedAnnotationTest.groovy
similarity index 80%
rename from src/test/org/codehaus/groovy/transform/GeneratedTransformTest.groovy
rename to src/test/org/codehaus/groovy/transform/GeneratedAnnotationTest.groovy
index 5823ae8..2896091 100644
--- a/src/test/org/codehaus/groovy/transform/GeneratedTransformTest.groovy
+++ b/src/test/org/codehaus/groovy/transform/GeneratedAnnotationTest.groovy
@@ -30,7 +30,7 @@ import org.junit.runners.JUnit4
* Tests for the @Generated annotation.
*/
@RunWith(JUnit4)
-class GeneratedTransformTest extends GroovyShellTestCase {
+class GeneratedAnnotationTest extends GroovyShellTestCase {
@Rule public TestName nameRule = new TestName()
@Before
@@ -59,7 +59,20 @@ class GeneratedTransformTest extends GroovyShellTestCase {
}
@Test
- void testOverridenDefaultGroovyMethodsAreNotAnnotatedWithGenerated() {
+ void testDefaultConstructorIsAnnotatedWithGenerated() {
+ def person = evaluate('''
+ class Person {
+ }
+ new Person()
+ ''')
+
+ def cons = person.class.declaredConstructors
+ assert cons.size() == 1
+ assert cons[0].annotations*.annotationType().name.contains('groovy.transform.Generated')
+ }
+
+ @Test
+ void testOverriddenDefaultGroovyMethodsAreNotAnnotatedWithGenerated() {
def person = evaluate('''
class Person {
String name
[groovy] 04/07: GROOVY-9053: Missing @Generated annotation on
@Bindable
Posted by pa...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
paulk pushed a commit to branch GROOVY_2_5_X
in repository https://gitbox.apache.org/repos/asf/groovy.git
commit 74c7512984a59c99b343b5e7660fe9fb282c0275
Author: Paul King <pa...@asert.com.au>
AuthorDate: Fri Mar 22 09:56:49 2019 +1000
GROOVY-9053: Missing @Generated annotation on @Bindable
---
.../groovy/beans/BindableASTTransformation.java | 27 ++++++++++---------
...bleTest.groovy => BindableTransformTest.groovy} | 31 ++++++++++++++++++++--
2 files changed, 43 insertions(+), 15 deletions(-)
diff --git a/src/main/groovy/groovy/beans/BindableASTTransformation.java b/src/main/groovy/groovy/beans/BindableASTTransformation.java
index 19d66ba..e0866db 100644
--- a/src/main/groovy/groovy/beans/BindableASTTransformation.java
+++ b/src/main/groovy/groovy/beans/BindableASTTransformation.java
@@ -35,7 +35,6 @@ import org.codehaus.groovy.control.CompilePhase;
import org.codehaus.groovy.control.SourceUnit;
import org.codehaus.groovy.control.messages.SimpleMessage;
import org.codehaus.groovy.control.messages.SyntaxErrorMessage;
-import org.codehaus.groovy.runtime.MetaClassHelper;
import org.codehaus.groovy.syntax.SyntaxException;
import org.codehaus.groovy.transform.ASTTransformation;
import org.codehaus.groovy.transform.GroovyASTTransformation;
@@ -44,6 +43,8 @@ import org.objectweb.asm.Opcodes;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
+import static org.apache.groovy.ast.tools.ClassNodeUtils.addGeneratedMethod;
+import static org.codehaus.groovy.runtime.MetaClassHelper.capitalize;
import static org.codehaus.groovy.ast.tools.GeneralUtils.args;
import static org.codehaus.groovy.ast.tools.GeneralUtils.assignX;
import static org.codehaus.groovy.ast.tools.GeneralUtils.callThisX;
@@ -73,7 +74,7 @@ import static org.codehaus.groovy.ast.tools.GeneralUtils.varX;
* If a {@link Vetoable} annotation is detected it does nothing and
* lets the {@link VetoableASTTransformation} handle all the changes.
*/
-@GroovyASTTransformation(phase= CompilePhase.CANONICALIZATION)
+@GroovyASTTransformation(phase = CompilePhase.CANONICALIZATION)
public class BindableASTTransformation implements ASTTransformation, Opcodes {
protected static ClassNode boundClassNode = ClassHelper.make(Bindable.class);
@@ -182,8 +183,8 @@ public class BindableASTTransformation implements ASTTransformation, Opcodes {
* Wrap an existing setter.
*/
private static void wrapSetterMethod(ClassNode classNode, String propertyName) {
- String getterName = "get" + MetaClassHelper.capitalize(propertyName);
- MethodNode setter = classNode.getSetterMethod("set" + MetaClassHelper.capitalize(propertyName));
+ String getterName = "get" + capitalize(propertyName);
+ MethodNode setter = classNode.getSetterMethod("set" + capitalize(propertyName));
if (setter != null) {
// Get the existing code block
@@ -211,7 +212,7 @@ public class BindableASTTransformation implements ASTTransformation, Opcodes {
}
private void createListenerSetter(ClassNode classNode, PropertyNode propertyNode) {
- String setterName = "set" + MetaClassHelper.capitalize(propertyNode.getName());
+ String setterName = "set" + capitalize(propertyNode.getName());
if (classNode.getMethods(setterName).isEmpty()) {
Statement setterBlock = createBindableStatement(propertyNode, fieldX(propertyNode.getField()));
@@ -253,7 +254,7 @@ public class BindableASTTransformation implements ASTTransformation, Opcodes {
setterBlock);
setter.setSynthetic(true);
// add it to the class
- declaringClass.addMethod(setter);
+ addGeneratedMethod(declaringClass, setter);
}
/**
@@ -339,7 +340,7 @@ public class BindableASTTransformation implements ASTTransformation, Opcodes {
// void addPropertyChangeListener(listener) {
// this$propertyChangeSupport.addPropertyChangeListener(listener)
// }
- declaringClass.addMethod(
+ addGeneratedMethod(declaringClass,
new MethodNode(
"addPropertyChangeListener",
ACC_PUBLIC,
@@ -352,7 +353,7 @@ public class BindableASTTransformation implements ASTTransformation, Opcodes {
// void addPropertyChangeListener(name, listener) {
// this$propertyChangeSupport.addPropertyChangeListener(name, listener)
// }
- declaringClass.addMethod(
+ addGeneratedMethod(declaringClass,
new MethodNode(
"addPropertyChangeListener",
ACC_PUBLIC,
@@ -365,7 +366,7 @@ public class BindableASTTransformation implements ASTTransformation, Opcodes {
// boolean removePropertyChangeListener(listener) {
// return this$propertyChangeSupport.removePropertyChangeListener(listener);
// }
- declaringClass.addMethod(
+ addGeneratedMethod(declaringClass,
new MethodNode(
"removePropertyChangeListener",
ACC_PUBLIC,
@@ -375,7 +376,7 @@ public class BindableASTTransformation implements ASTTransformation, Opcodes {
stmt(callX(fieldX(pcsField), "removePropertyChangeListener", args(varX("listener", pclClassNode))))));
// add method: void removePropertyChangeListener(name, listener)
- declaringClass.addMethod(
+ addGeneratedMethod(declaringClass,
new MethodNode(
"removePropertyChangeListener",
ACC_PUBLIC,
@@ -388,7 +389,7 @@ public class BindableASTTransformation implements ASTTransformation, Opcodes {
// void firePropertyChange(String name, Object oldValue, Object newValue) {
// this$propertyChangeSupport.firePropertyChange(name, oldValue, newValue)
// }
- declaringClass.addMethod(
+ addGeneratedMethod(declaringClass,
new MethodNode(
"firePropertyChange",
ACC_PUBLIC,
@@ -401,7 +402,7 @@ public class BindableASTTransformation implements ASTTransformation, Opcodes {
// PropertyChangeListener[] getPropertyChangeListeners() {
// return this$propertyChangeSupport.getPropertyChangeListeners
// }
- declaringClass.addMethod(
+ addGeneratedMethod(declaringClass,
new MethodNode(
"getPropertyChangeListeners",
ACC_PUBLIC,
@@ -414,7 +415,7 @@ public class BindableASTTransformation implements ASTTransformation, Opcodes {
// PropertyChangeListener[] getPropertyChangeListeners(String name) {
// return this$propertyChangeSupport.getPropertyChangeListeners(name)
// }
- declaringClass.addMethod(
+ addGeneratedMethod(declaringClass,
new MethodNode(
"getPropertyChangeListeners",
ACC_PUBLIC,
diff --git a/src/test/groovy/beans/BindableTest.groovy b/src/test/groovy/beans/BindableTransformTest.groovy
similarity index 92%
rename from src/test/groovy/beans/BindableTest.groovy
rename to src/test/groovy/beans/BindableTransformTest.groovy
index 1b88758..9424450 100644
--- a/src/test/groovy/beans/BindableTest.groovy
+++ b/src/test/groovy/beans/BindableTransformTest.groovy
@@ -25,7 +25,7 @@ import java.beans.PropertyChangeListener
import static java.lang.reflect.Modifier.isPublic
import static java.lang.reflect.Modifier.isSynthetic
-class BindableTest extends GroovyTestCase {
+class BindableTransformTest extends GroovyShellTestCase {
void testSimpleBindableProperty() {
assertScript """
@@ -369,4 +369,31 @@ class BindableTest extends GroovyTestCase {
assert new MyBean()
"""
}
-}
\ No newline at end of file
+
+ void testBindableGeneratedMethodsAreAnnotatedWithGenerated_GROOVY9053() {
+ def person = evaluate('''
+ class Person {
+ @groovy.beans.Bindable
+ String firstName
+
+ void setFirstName(String fn) {
+ this.firstName = fn.toUpperCase()
+ }
+
+ @groovy.beans.Bindable
+ def zipCode
+ }
+ new Person()
+ ''')
+
+ person.class.declaredMethods.each { m ->
+ if (m.name.contains('PropertyChange') || m.name in ['setZipCode']) {
+ assert m.annotations*.annotationType().name.contains('groovy.transform.Generated')
+ }
+ if (m.name in ['setFirstName']) {
+ // wrapped methods should not be marked since they contain non-generated logic
+ assert !m.annotations*.annotationType().name.contains('groovy.transform.Generated')
+ }
+ }
+ }
+}
[groovy] 05/07: GROOVY-9052: Missing @Generated annotation on
@Vetoable
Posted by pa...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
paulk pushed a commit to branch GROOVY_2_5_X
in repository https://gitbox.apache.org/repos/asf/groovy.git
commit f8e382b97cf6f8e437d660b46e34b578dcf60969
Author: Paul King <pa...@asert.com.au>
AuthorDate: Fri Mar 22 10:15:24 2019 +1000
GROOVY-9052: Missing @Generated annotation on @Vetoable
---
.../groovy/beans/VetoableASTTransformation.java | 25 ++++++++++---------
...bleTest.groovy => VetoableTransformTest.groovy} | 29 ++++++++++++++++++++--
2 files changed, 40 insertions(+), 14 deletions(-)
diff --git a/src/main/groovy/groovy/beans/VetoableASTTransformation.java b/src/main/groovy/groovy/beans/VetoableASTTransformation.java
index 44e1da7..14eb7ac 100644
--- a/src/main/groovy/groovy/beans/VetoableASTTransformation.java
+++ b/src/main/groovy/groovy/beans/VetoableASTTransformation.java
@@ -35,7 +35,6 @@ import org.codehaus.groovy.control.CompilePhase;
import org.codehaus.groovy.control.SourceUnit;
import org.codehaus.groovy.control.messages.SimpleMessage;
import org.codehaus.groovy.control.messages.SyntaxErrorMessage;
-import org.codehaus.groovy.runtime.MetaClassHelper;
import org.codehaus.groovy.syntax.SyntaxException;
import org.codehaus.groovy.transform.GroovyASTTransformation;
import org.objectweb.asm.Opcodes;
@@ -44,6 +43,7 @@ import java.beans.PropertyVetoException;
import java.beans.VetoableChangeListener;
import java.beans.VetoableChangeSupport;
+import static org.apache.groovy.ast.tools.ClassNodeUtils.addGeneratedMethod;
import static org.codehaus.groovy.ast.tools.GeneralUtils.args;
import static org.codehaus.groovy.ast.tools.GeneralUtils.assignS;
import static org.codehaus.groovy.ast.tools.GeneralUtils.callThisX;
@@ -58,6 +58,7 @@ import static org.codehaus.groovy.ast.tools.GeneralUtils.params;
import static org.codehaus.groovy.ast.tools.GeneralUtils.returnS;
import static org.codehaus.groovy.ast.tools.GeneralUtils.stmt;
import static org.codehaus.groovy.ast.tools.GeneralUtils.varX;
+import static org.codehaus.groovy.runtime.MetaClassHelper.capitalize;
/**
* Handles generation of code for the {@code @Vetoable} annotation, and {@code @Bindable}
@@ -166,8 +167,8 @@ public class VetoableASTTransformation extends BindableASTTransformation {
* Wrap an existing setter.
*/
private static void wrapSetterMethod(ClassNode classNode, boolean bindable, String propertyName) {
- String getterName = "get" + MetaClassHelper.capitalize(propertyName);
- MethodNode setter = classNode.getSetterMethod("set" + MetaClassHelper.capitalize(propertyName));
+ String getterName = "get" + capitalize(propertyName);
+ MethodNode setter = classNode.getSetterMethod("set" + capitalize(propertyName));
if (setter != null) {
// Get the existing code block
@@ -208,7 +209,7 @@ public class VetoableASTTransformation extends BindableASTTransformation {
if (needsVetoableChangeSupport(declaringClass, source)) {
addVetoableChangeSupport(declaringClass);
}
- String setterName = "set" + MetaClassHelper.capitalize(propertyNode.getName());
+ String setterName = "set" + capitalize(propertyNode.getName());
if (declaringClass.getMethods(setterName).isEmpty()) {
Expression fieldExpression = fieldX(propertyNode.getField());
BlockStatement setterBlock = new BlockStatement();
@@ -318,7 +319,7 @@ public class VetoableASTTransformation extends BindableASTTransformation {
setterBlock);
setter.setSynthetic(true);
// add it to the class
- declaringClass.addMethod(setter);
+ addGeneratedMethod(declaringClass, setter);
}
/**
@@ -352,7 +353,7 @@ public class VetoableASTTransformation extends BindableASTTransformation {
// void addVetoableChangeListener(listener) {
// this$vetoableChangeSupport.addVetoableChangeListener(listener)
// }
- declaringClass.addMethod(
+ addGeneratedMethod(declaringClass,
new MethodNode(
"addVetoableChangeListener",
ACC_PUBLIC,
@@ -365,7 +366,7 @@ public class VetoableASTTransformation extends BindableASTTransformation {
// void addVetoableChangeListener(name, listener) {
// this$vetoableChangeSupport.addVetoableChangeListener(name, listener)
// }
- declaringClass.addMethod(
+ addGeneratedMethod(declaringClass,
new MethodNode(
"addVetoableChangeListener",
ACC_PUBLIC,
@@ -378,7 +379,7 @@ public class VetoableASTTransformation extends BindableASTTransformation {
// boolean removeVetoableChangeListener(listener) {
// return this$vetoableChangeSupport.removeVetoableChangeListener(listener);
// }
- declaringClass.addMethod(
+ addGeneratedMethod(declaringClass,
new MethodNode(
"removeVetoableChangeListener",
ACC_PUBLIC,
@@ -388,7 +389,7 @@ public class VetoableASTTransformation extends BindableASTTransformation {
stmt(callX(fieldX(vcsField), "removeVetoableChangeListener", args(varX("listener", vclClassNode))))));
// add method: void removeVetoableChangeListener(name, listener)
- declaringClass.addMethod(
+ addGeneratedMethod(declaringClass,
new MethodNode(
"removeVetoableChangeListener",
ACC_PUBLIC,
@@ -403,7 +404,7 @@ public class VetoableASTTransformation extends BindableASTTransformation {
// {
// this$vetoableChangeSupport.fireVetoableChange(name, oldValue, newValue)
// }
- declaringClass.addMethod(
+ addGeneratedMethod(declaringClass,
new MethodNode(
"fireVetoableChange",
ACC_PUBLIC,
@@ -416,7 +417,7 @@ public class VetoableASTTransformation extends BindableASTTransformation {
// VetoableChangeListener[] getVetoableChangeListeners() {
// return this$vetoableChangeSupport.getVetoableChangeListeners
// }
- declaringClass.addMethod(
+ addGeneratedMethod(declaringClass,
new MethodNode(
"getVetoableChangeListeners",
ACC_PUBLIC,
@@ -429,7 +430,7 @@ public class VetoableASTTransformation extends BindableASTTransformation {
// VetoableChangeListener[] getVetoableChangeListeners(String name) {
// return this$vetoableChangeSupport.getVetoableChangeListeners(name)
// }
- declaringClass.addMethod(
+ addGeneratedMethod(declaringClass,
new MethodNode(
"getVetoableChangeListeners",
ACC_PUBLIC,
diff --git a/src/test/groovy/beans/VetoableTest.groovy b/src/test/groovy/beans/VetoableTransformTest.groovy
similarity index 93%
rename from src/test/groovy/beans/VetoableTest.groovy
rename to src/test/groovy/beans/VetoableTransformTest.groovy
index 2a8519a..3fce996 100644
--- a/src/test/groovy/beans/VetoableTest.groovy
+++ b/src/test/groovy/beans/VetoableTransformTest.groovy
@@ -20,7 +20,7 @@ package groovy.beans
import org.codehaus.groovy.control.CompilationFailedException
-class VetoableTest extends GroovyTestCase {
+class VetoableTransformTest extends GroovyShellTestCase {
void testSimpleConstrainedProperty() {
GroovyShell shell = new GroovyShell()
@@ -456,4 +456,29 @@ class VetoableTest extends GroovyTestCase {
assert new MyBean()
"""
}
-}
\ No newline at end of file
+
+ void testVetoableGeneratedMethodsAreAnnotatedWithGenerated_GROOVY9052() {
+ def person = evaluate('''
+ @groovy.beans.Vetoable
+ class Person {
+ String firstName
+
+ void setFirstName(String fn) {
+ this.firstName = fn.toUpperCase()
+ }
+
+ def zipCode
+ }
+ new Person()
+ ''')
+
+ person.class.declaredMethods.each { m ->
+ if (m.name.contains('VetoableChange') || m.name in ['setZipCode']) {
+ assert m.annotations*.annotationType().name.contains('groovy.transform.Generated')
+ } else if (m.name in ['setFirstName']) {
+ // wrapped methods should not be marked since they contain non-generated logic
+ assert !m.annotations*.annotationType().name.contains('groovy.transform.Generated')
+ }
+ }
+ }
+}
[groovy] 07/07: GROOVY-9051: The default constructor should be
annotated with @Generated (add also for inner class case)
Posted by pa...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
paulk pushed a commit to branch GROOVY_2_5_X
in repository https://gitbox.apache.org/repos/asf/groovy.git
commit 15b3299db4cc6132bc87f4795dc5dcd68895c647
Author: Paul King <pa...@asert.com.au>
AuthorDate: Fri Mar 22 10:35:44 2019 +1000
GROOVY-9051: The default constructor should be annotated with @Generated (add also for inner class case)
---
.../classgen/InnerClassCompletionVisitor.java | 927 +++++++++++----------
1 file changed, 464 insertions(+), 463 deletions(-)
diff --git a/src/main/java/org/codehaus/groovy/classgen/InnerClassCompletionVisitor.java b/src/main/java/org/codehaus/groovy/classgen/InnerClassCompletionVisitor.java
index 4e07503..452ac65 100644
--- a/src/main/java/org/codehaus/groovy/classgen/InnerClassCompletionVisitor.java
+++ b/src/main/java/org/codehaus/groovy/classgen/InnerClassCompletionVisitor.java
@@ -1,463 +1,464 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-package org.codehaus.groovy.classgen;
-
-import org.codehaus.groovy.ast.ClassHelper;
-import org.codehaus.groovy.ast.ClassNode;
-import org.codehaus.groovy.ast.ConstructorNode;
-import org.codehaus.groovy.ast.FieldNode;
-import org.codehaus.groovy.ast.InnerClassNode;
-import org.codehaus.groovy.ast.MethodNode;
-import org.codehaus.groovy.ast.Parameter;
-import org.codehaus.groovy.ast.expr.ClassExpression;
-import org.codehaus.groovy.ast.expr.ConstructorCallExpression;
-import org.codehaus.groovy.ast.expr.Expression;
-import org.codehaus.groovy.ast.expr.TupleExpression;
-import org.codehaus.groovy.ast.expr.VariableExpression;
-import org.codehaus.groovy.ast.stmt.BlockStatement;
-import org.codehaus.groovy.ast.stmt.ExpressionStatement;
-import org.codehaus.groovy.ast.stmt.Statement;
-import org.codehaus.groovy.classgen.asm.BytecodeHelper;
-import org.codehaus.groovy.control.CompilationUnit;
-import org.codehaus.groovy.control.SourceUnit;
-import org.objectweb.asm.MethodVisitor;
-import org.objectweb.asm.Opcodes;
-
-import java.util.List;
-
-import static org.codehaus.groovy.ast.ClassHelper.CLOSURE_TYPE;
-
-public class InnerClassCompletionVisitor extends InnerClassVisitorHelper implements Opcodes {
-
- private final SourceUnit sourceUnit;
- private ClassNode classNode;
- private FieldNode thisField = null;
-
- private static final String
- CLOSURE_INTERNAL_NAME = BytecodeHelper.getClassInternalName(CLOSURE_TYPE),
- CLOSURE_DESCRIPTOR = BytecodeHelper.getTypeDescription(CLOSURE_TYPE);
-
- public InnerClassCompletionVisitor(CompilationUnit cu, SourceUnit su) {
- sourceUnit = su;
- }
-
- @Override
- protected SourceUnit getSourceUnit() {
- return sourceUnit;
- }
-
- @Override
- public void visitClass(ClassNode node) {
- this.classNode = node;
- thisField = null;
- InnerClassNode innerClass = null;
- if (!node.isEnum() && !node.isInterface() && node instanceof InnerClassNode) {
- innerClass = (InnerClassNode) node;
- thisField = innerClass.getField("this$0");
- if (innerClass.getVariableScope() == null && innerClass.getDeclaredConstructors().isEmpty()) {
- // add dummy constructor
- innerClass.addConstructor(ACC_PUBLIC, Parameter.EMPTY_ARRAY, null, null);
- }
- }
- if (node.isEnum() || node.isInterface()) return;
- // use Iterator.hasNext() to check for available inner classes
- if (node.getInnerClasses().hasNext()) addDispatcherMethods(node);
- if (innerClass == null) return;
- super.visitClass(node);
- addDefaultMethods(innerClass);
- }
-
- @Override
- public void visitConstructor(ConstructorNode node) {
- addThisReference(node);
- super.visitConstructor(node);
- }
-
- private static String getTypeDescriptor(ClassNode node, boolean isStatic) {
- return BytecodeHelper.getTypeDescription(getClassNode(node, isStatic));
- }
-
- private static String getInternalName(ClassNode node, boolean isStatic) {
- return BytecodeHelper.getClassInternalName(getClassNode(node, isStatic));
- }
-
- private static void addDispatcherMethods(ClassNode classNode) {
- final int objectDistance = getObjectDistance(classNode);
-
- // since we added an anonymous inner class we should also
- // add the dispatcher methods
-
- // add method dispatcher
- Parameter[] parameters = new Parameter[]{
- new Parameter(ClassHelper.STRING_TYPE, "name"),
- new Parameter(ClassHelper.OBJECT_TYPE, "args")
- };
- MethodNode method = classNode.addSyntheticMethod(
- "this$dist$invoke$" + objectDistance,
- ACC_PUBLIC + ACC_SYNTHETIC,
- ClassHelper.OBJECT_TYPE,
- parameters,
- ClassNode.EMPTY_ARRAY,
- null
- );
-
- BlockStatement block = new BlockStatement();
- setMethodDispatcherCode(block, VariableExpression.THIS_EXPRESSION, parameters);
- method.setCode(block);
-
- // add property setter
- parameters = new Parameter[]{
- new Parameter(ClassHelper.STRING_TYPE, "name"),
- new Parameter(ClassHelper.OBJECT_TYPE, "value")
- };
- method = classNode.addSyntheticMethod(
- "this$dist$set$" + objectDistance,
- ACC_PUBLIC + ACC_SYNTHETIC,
- ClassHelper.VOID_TYPE,
- parameters,
- ClassNode.EMPTY_ARRAY,
- null
- );
-
- block = new BlockStatement();
- setPropertySetterDispatcher(block, VariableExpression.THIS_EXPRESSION, parameters);
- method.setCode(block);
-
- // add property getter
- parameters = new Parameter[]{
- new Parameter(ClassHelper.STRING_TYPE, "name")
- };
- method = classNode.addSyntheticMethod(
- "this$dist$get$" + objectDistance,
- ACC_PUBLIC + ACC_SYNTHETIC,
- ClassHelper.OBJECT_TYPE,
- parameters,
- ClassNode.EMPTY_ARRAY,
- null
- );
-
- block = new BlockStatement();
- setPropertyGetterDispatcher(block, VariableExpression.THIS_EXPRESSION, parameters);
- method.setCode(block);
- }
-
- private void getThis(MethodVisitor mv, String classInternalName, String outerClassDescriptor, String innerClassInternalName) {
- mv.visitVarInsn(ALOAD, 0);
- if (CLOSURE_TYPE.equals(thisField.getType())) {
- mv.visitFieldInsn(GETFIELD, classInternalName, "this$0", CLOSURE_DESCRIPTOR);
- mv.visitMethodInsn(INVOKEVIRTUAL, CLOSURE_INTERNAL_NAME, "getThisObject", "()Ljava/lang/Object;", false);
- mv.visitTypeInsn(CHECKCAST, innerClassInternalName);
- } else {
- mv.visitFieldInsn(GETFIELD, classInternalName, "this$0", outerClassDescriptor);
- }
- }
-
- private void addDefaultMethods(InnerClassNode node) {
- final boolean isStatic = isStatic(node);
-
- ClassNode outerClass = node.getOuterClass();
- final String classInternalName = org.codehaus.groovy.classgen.asm.BytecodeHelper.getClassInternalName(node);
- final String outerClassInternalName = getInternalName(outerClass, isStatic);
- final String outerClassDescriptor = getTypeDescriptor(outerClass, isStatic);
- final int objectDistance = getObjectDistance(outerClass);
-
- // add missing method dispatcher
- Parameter[] parameters = new Parameter[]{
- new Parameter(ClassHelper.STRING_TYPE, "name"),
- new Parameter(ClassHelper.OBJECT_TYPE, "args")
- };
-
- String methodName = "methodMissing";
- if (isStatic)
- addCompilationErrorOnCustomMethodNode(node, methodName, parameters);
-
- MethodNode method = node.addSyntheticMethod(
- methodName,
- Opcodes.ACC_PUBLIC,
- ClassHelper.OBJECT_TYPE,
- parameters,
- ClassNode.EMPTY_ARRAY,
- null
- );
-
- BlockStatement block = new BlockStatement();
- if (isStatic) {
- setMethodDispatcherCode(block, new ClassExpression(outerClass), parameters);
- } else {
- block.addStatement(
- new BytecodeSequence(new BytecodeInstruction() {
- public void visit(MethodVisitor mv) {
- getThis(mv,classInternalName,outerClassDescriptor,outerClassInternalName);
- mv.visitVarInsn(ALOAD, 1);
- mv.visitVarInsn(ALOAD, 2);
- mv.visitMethodInsn(INVOKEVIRTUAL, outerClassInternalName, "this$dist$invoke$" + objectDistance, "(Ljava/lang/String;Ljava/lang/Object;)Ljava/lang/Object;", false);
- mv.visitInsn(ARETURN);
- }
- })
- );
- }
- method.setCode(block);
-
- // add static missing method dispatcher
- methodName = "$static_methodMissing";
- if (isStatic)
- addCompilationErrorOnCustomMethodNode(node, methodName, parameters);
-
- method = node.addSyntheticMethod(
- methodName,
- Opcodes.ACC_PUBLIC | Opcodes.ACC_STATIC,
- ClassHelper.OBJECT_TYPE,
- parameters,
- ClassNode.EMPTY_ARRAY,
- null
- );
-
- block = new BlockStatement();
- setMethodDispatcherCode(block, new ClassExpression(outerClass), parameters);
- method.setCode(block);
-
- // add property setter dispatcher
- parameters = new Parameter[]{
- new Parameter(ClassHelper.STRING_TYPE, "name"),
- new Parameter(ClassHelper.OBJECT_TYPE, "val")
- };
-
- methodName = "propertyMissing";
- if (isStatic)
- addCompilationErrorOnCustomMethodNode(node, methodName, parameters);
-
- method = node.addSyntheticMethod(
- methodName,
- Opcodes.ACC_PUBLIC,
- ClassHelper.VOID_TYPE,
- parameters,
- ClassNode.EMPTY_ARRAY,
- null
- );
-
- block = new BlockStatement();
- if (isStatic) {
- setPropertySetterDispatcher(block, new ClassExpression(outerClass), parameters);
- } else {
- block.addStatement(
- new BytecodeSequence(new BytecodeInstruction() {
- public void visit(MethodVisitor mv) {
- getThis(mv,classInternalName,outerClassDescriptor,outerClassInternalName);
- mv.visitVarInsn(ALOAD, 1);
- mv.visitVarInsn(ALOAD, 2);
- mv.visitMethodInsn(INVOKEVIRTUAL, outerClassInternalName, "this$dist$set$" + objectDistance, "(Ljava/lang/String;Ljava/lang/Object;)V", false);
- mv.visitInsn(RETURN);
- }
- })
- );
- }
- method.setCode(block);
-
- // add static property missing setter dispatcher
- methodName = "$static_propertyMissing";
- if (isStatic)
- addCompilationErrorOnCustomMethodNode(node, methodName, parameters);
-
- method = node.addSyntheticMethod(
- methodName,
- Opcodes.ACC_PUBLIC | Opcodes.ACC_STATIC,
- ClassHelper.VOID_TYPE,
- parameters,
- ClassNode.EMPTY_ARRAY,
- null
- );
-
- block = new BlockStatement();
- setPropertySetterDispatcher(block, new ClassExpression(outerClass), parameters);
- method.setCode(block);
-
- // add property getter dispatcher
- parameters = new Parameter[]{
- new Parameter(ClassHelper.STRING_TYPE, "name")
- };
-
- methodName = "propertyMissing";
- if (isStatic)
- addCompilationErrorOnCustomMethodNode(node, methodName, parameters);
-
- method = node.addSyntheticMethod(
- methodName,
- Opcodes.ACC_PUBLIC,
- ClassHelper.OBJECT_TYPE,
- parameters,
- ClassNode.EMPTY_ARRAY,
- null
- );
-
- block = new BlockStatement();
- if (isStatic) {
- setPropertyGetterDispatcher(block, new ClassExpression(outerClass), parameters);
- } else {
- block.addStatement(
- new BytecodeSequence(new BytecodeInstruction() {
- public void visit(MethodVisitor mv) {
- getThis(mv,classInternalName,outerClassDescriptor,outerClassInternalName);
- mv.visitVarInsn(ALOAD, 1);
- mv.visitMethodInsn(INVOKEVIRTUAL, outerClassInternalName, "this$dist$get$" + objectDistance, "(Ljava/lang/String;)Ljava/lang/Object;", false);
- mv.visitInsn(ARETURN);
- }
- })
- );
- }
- method.setCode(block);
-
- // add static property missing getter dispatcher
- methodName = "$static_propertyMissing";
- if (isStatic)
- addCompilationErrorOnCustomMethodNode(node, methodName, parameters);
-
- method = node.addSyntheticMethod(
- methodName,
- Opcodes.ACC_PUBLIC | Opcodes.ACC_STATIC,
- ClassHelper.OBJECT_TYPE,
- parameters,
- ClassNode.EMPTY_ARRAY,
- null
- );
-
- block = new BlockStatement();
- setPropertyGetterDispatcher(block, new ClassExpression(outerClass), parameters);
- method.setCode(block);
- }
-
- /**
- * Adds a compilation error if a {@link MethodNode} with the given <tt>methodName</tt> and
- * <tt>parameters</tt> exists in the {@link InnerClassNode}.
- */
- private void addCompilationErrorOnCustomMethodNode(InnerClassNode node, String methodName, Parameter[] parameters) {
- MethodNode existingMethodNode = node.getMethod(methodName, parameters);
- // if there is a user-defined methodNode, add compiler error msg and continue
- if (existingMethodNode != null && !isSynthetic(existingMethodNode)) {
- addError("\"" +methodName + "\" implementations are not supported on static inner classes as " +
- "a synthetic version of \"" + methodName + "\" is added during compilation for the purpose " +
- "of outer class delegation.",
- existingMethodNode);
- }
- }
-
- // GROOVY-8914: pre-compiled classes lose synthetic boolean - TODO fix earlier as per GROOVY-4346 then remove extra check here
- private boolean isSynthetic(MethodNode existingMethodNode) {
- return existingMethodNode.isSynthetic() || hasSyntheticModifier(existingMethodNode);
- }
-
- private boolean hasSyntheticModifier(MethodNode existingMethodNode) {
- return (existingMethodNode.getModifiers() & Opcodes.ACC_SYNTHETIC) != 0;
- }
-
- private void addThisReference(ConstructorNode node) {
- if (!shouldHandleImplicitThisForInnerClass(classNode)) return;
- Statement code = node.getCode();
-
- // add "this$0" field init
-
- //add this parameter to node
- Parameter[] params = node.getParameters();
- Parameter[] newParams = new Parameter[params.length + 1];
- System.arraycopy(params, 0, newParams, 1, params.length);
- String name = getUniqueName(params, node);
-
- Parameter thisPara = new Parameter(classNode.getOuterClass().getPlainNodeReference(), name);
- newParams[0] = thisPara;
- node.setParameters(newParams);
-
- BlockStatement block = null;
- if (code == null) {
- block = new BlockStatement();
- } else if (!(code instanceof BlockStatement)) {
- block = new BlockStatement();
- block.addStatement(code);
- } else {
- block = (BlockStatement) code;
- }
- BlockStatement newCode = new BlockStatement();
- addFieldInit(thisPara, thisField, newCode);
- ConstructorCallExpression cce = getFirstIfSpecialConstructorCall(block);
- if (cce == null) {
- cce = new ConstructorCallExpression(ClassNode.SUPER, new TupleExpression());
- block.getStatements().add(0, new ExpressionStatement(cce));
- }
- if (shouldImplicitlyPassThisPara(cce)) {
- // add thisPara to this(...)
- TupleExpression args = (TupleExpression) cce.getArguments();
- List<Expression> expressions = args.getExpressions();
- VariableExpression ve = new VariableExpression(thisPara.getName());
- ve.setAccessedVariable(thisPara);
- expressions.add(0, ve);
- }
- if (cce.isSuperCall()) {
- // we have a call to super here, so we need to add
- // our code after that
- block.getStatements().add(1, newCode);
- }
- node.setCode(block);
- }
-
- private boolean shouldImplicitlyPassThisPara(ConstructorCallExpression cce) {
- boolean pass = false;
- ClassNode superCN = classNode.getSuperClass();
- if (cce.isThisCall()) {
- pass = true;
- } else if (cce.isSuperCall()) {
- // if the super class is another non-static inner class in the same outer class hierarchy, implicit this
- // needs to be passed
- if (!superCN.isEnum() && !superCN.isInterface() && superCN instanceof InnerClassNode) {
- InnerClassNode superInnerCN = (InnerClassNode) superCN;
- if (!isStatic(superInnerCN) && classNode.getOuterClass().isDerivedFrom(superCN.getOuterClass())) {
- pass = true;
- }
- }
- }
- return pass;
- }
-
- private String getUniqueName(Parameter[] params, ConstructorNode node) {
- String namePrefix = "$p";
- outer:
- for (int i = 0; i < 100; i++) {
- namePrefix = namePrefix + "$";
- for (Parameter p : params) {
- if (p.getName().equals(namePrefix)) continue outer;
- }
- return namePrefix;
- }
- addError("unable to find a unique prefix name for synthetic this reference in inner class constructor", node);
- return namePrefix;
- }
-
- private static ConstructorCallExpression getFirstIfSpecialConstructorCall(BlockStatement code) {
- if (code == null) return null;
-
- final List<Statement> statementList = code.getStatements();
- if (statementList.isEmpty()) return null;
-
- final Statement statement = statementList.get(0);
- if (!(statement instanceof ExpressionStatement)) return null;
-
- Expression expression = ((ExpressionStatement) statement).getExpression();
- if (!(expression instanceof ConstructorCallExpression)) return null;
- ConstructorCallExpression cce = (ConstructorCallExpression) expression;
- if (cce.isSpecialCall()) return cce;
- return null;
- }
-}
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.codehaus.groovy.classgen;
+
+import org.codehaus.groovy.ast.ClassHelper;
+import org.codehaus.groovy.ast.ClassNode;
+import org.codehaus.groovy.ast.ConstructorNode;
+import org.codehaus.groovy.ast.FieldNode;
+import org.codehaus.groovy.ast.InnerClassNode;
+import org.codehaus.groovy.ast.MethodNode;
+import org.codehaus.groovy.ast.Parameter;
+import org.codehaus.groovy.ast.expr.ClassExpression;
+import org.codehaus.groovy.ast.expr.ConstructorCallExpression;
+import org.codehaus.groovy.ast.expr.Expression;
+import org.codehaus.groovy.ast.expr.TupleExpression;
+import org.codehaus.groovy.ast.expr.VariableExpression;
+import org.codehaus.groovy.ast.stmt.BlockStatement;
+import org.codehaus.groovy.ast.stmt.ExpressionStatement;
+import org.codehaus.groovy.ast.stmt.Statement;
+import org.codehaus.groovy.classgen.asm.BytecodeHelper;
+import org.codehaus.groovy.control.CompilationUnit;
+import org.codehaus.groovy.control.SourceUnit;
+import org.objectweb.asm.MethodVisitor;
+import org.objectweb.asm.Opcodes;
+
+import java.util.List;
+
+import static org.apache.groovy.ast.tools.ClassNodeUtils.addGeneratedConstructor;
+import static org.codehaus.groovy.ast.ClassHelper.CLOSURE_TYPE;
+
+public class InnerClassCompletionVisitor extends InnerClassVisitorHelper implements Opcodes {
+
+ private final SourceUnit sourceUnit;
+ private ClassNode classNode;
+ private FieldNode thisField = null;
+
+ private static final String
+ CLOSURE_INTERNAL_NAME = BytecodeHelper.getClassInternalName(CLOSURE_TYPE),
+ CLOSURE_DESCRIPTOR = BytecodeHelper.getTypeDescription(CLOSURE_TYPE);
+
+ public InnerClassCompletionVisitor(CompilationUnit cu, SourceUnit su) {
+ sourceUnit = su;
+ }
+
+ @Override
+ protected SourceUnit getSourceUnit() {
+ return sourceUnit;
+ }
+
+ @Override
+ public void visitClass(ClassNode node) {
+ this.classNode = node;
+ thisField = null;
+ InnerClassNode innerClass = null;
+ if (!node.isEnum() && !node.isInterface() && node instanceof InnerClassNode) {
+ innerClass = (InnerClassNode) node;
+ thisField = innerClass.getField("this$0");
+ if (innerClass.getVariableScope() == null && innerClass.getDeclaredConstructors().isEmpty()) {
+ // add empty default constructor
+ addGeneratedConstructor(innerClass, ACC_PUBLIC, Parameter.EMPTY_ARRAY, null, null);
+ }
+ }
+ if (node.isEnum() || node.isInterface()) return;
+ // use Iterator.hasNext() to check for available inner classes
+ if (node.getInnerClasses().hasNext()) addDispatcherMethods(node);
+ if (innerClass == null) return;
+ super.visitClass(node);
+ addDefaultMethods(innerClass);
+ }
+
+ @Override
+ public void visitConstructor(ConstructorNode node) {
+ addThisReference(node);
+ super.visitConstructor(node);
+ }
+
+ private static String getTypeDescriptor(ClassNode node, boolean isStatic) {
+ return BytecodeHelper.getTypeDescription(getClassNode(node, isStatic));
+ }
+
+ private static String getInternalName(ClassNode node, boolean isStatic) {
+ return BytecodeHelper.getClassInternalName(getClassNode(node, isStatic));
+ }
+
+ private static void addDispatcherMethods(ClassNode classNode) {
+ final int objectDistance = getObjectDistance(classNode);
+
+ // since we added an anonymous inner class we should also
+ // add the dispatcher methods
+
+ // add method dispatcher
+ Parameter[] parameters = new Parameter[]{
+ new Parameter(ClassHelper.STRING_TYPE, "name"),
+ new Parameter(ClassHelper.OBJECT_TYPE, "args")
+ };
+ MethodNode method = classNode.addSyntheticMethod(
+ "this$dist$invoke$" + objectDistance,
+ ACC_PUBLIC + ACC_SYNTHETIC,
+ ClassHelper.OBJECT_TYPE,
+ parameters,
+ ClassNode.EMPTY_ARRAY,
+ null
+ );
+
+ BlockStatement block = new BlockStatement();
+ setMethodDispatcherCode(block, VariableExpression.THIS_EXPRESSION, parameters);
+ method.setCode(block);
+
+ // add property setter
+ parameters = new Parameter[]{
+ new Parameter(ClassHelper.STRING_TYPE, "name"),
+ new Parameter(ClassHelper.OBJECT_TYPE, "value")
+ };
+ method = classNode.addSyntheticMethod(
+ "this$dist$set$" + objectDistance,
+ ACC_PUBLIC + ACC_SYNTHETIC,
+ ClassHelper.VOID_TYPE,
+ parameters,
+ ClassNode.EMPTY_ARRAY,
+ null
+ );
+
+ block = new BlockStatement();
+ setPropertySetterDispatcher(block, VariableExpression.THIS_EXPRESSION, parameters);
+ method.setCode(block);
+
+ // add property getter
+ parameters = new Parameter[]{
+ new Parameter(ClassHelper.STRING_TYPE, "name")
+ };
+ method = classNode.addSyntheticMethod(
+ "this$dist$get$" + objectDistance,
+ ACC_PUBLIC + ACC_SYNTHETIC,
+ ClassHelper.OBJECT_TYPE,
+ parameters,
+ ClassNode.EMPTY_ARRAY,
+ null
+ );
+
+ block = new BlockStatement();
+ setPropertyGetterDispatcher(block, VariableExpression.THIS_EXPRESSION, parameters);
+ method.setCode(block);
+ }
+
+ private void getThis(MethodVisitor mv, String classInternalName, String outerClassDescriptor, String innerClassInternalName) {
+ mv.visitVarInsn(ALOAD, 0);
+ if (CLOSURE_TYPE.equals(thisField.getType())) {
+ mv.visitFieldInsn(GETFIELD, classInternalName, "this$0", CLOSURE_DESCRIPTOR);
+ mv.visitMethodInsn(INVOKEVIRTUAL, CLOSURE_INTERNAL_NAME, "getThisObject", "()Ljava/lang/Object;", false);
+ mv.visitTypeInsn(CHECKCAST, innerClassInternalName);
+ } else {
+ mv.visitFieldInsn(GETFIELD, classInternalName, "this$0", outerClassDescriptor);
+ }
+ }
+
+ private void addDefaultMethods(InnerClassNode node) {
+ final boolean isStatic = isStatic(node);
+
+ ClassNode outerClass = node.getOuterClass();
+ final String classInternalName = org.codehaus.groovy.classgen.asm.BytecodeHelper.getClassInternalName(node);
+ final String outerClassInternalName = getInternalName(outerClass, isStatic);
+ final String outerClassDescriptor = getTypeDescriptor(outerClass, isStatic);
+ final int objectDistance = getObjectDistance(outerClass);
+
+ // add missing method dispatcher
+ Parameter[] parameters = new Parameter[]{
+ new Parameter(ClassHelper.STRING_TYPE, "name"),
+ new Parameter(ClassHelper.OBJECT_TYPE, "args")
+ };
+
+ String methodName = "methodMissing";
+ if (isStatic)
+ addCompilationErrorOnCustomMethodNode(node, methodName, parameters);
+
+ MethodNode method = node.addSyntheticMethod(
+ methodName,
+ Opcodes.ACC_PUBLIC,
+ ClassHelper.OBJECT_TYPE,
+ parameters,
+ ClassNode.EMPTY_ARRAY,
+ null
+ );
+
+ BlockStatement block = new BlockStatement();
+ if (isStatic) {
+ setMethodDispatcherCode(block, new ClassExpression(outerClass), parameters);
+ } else {
+ block.addStatement(
+ new BytecodeSequence(new BytecodeInstruction() {
+ public void visit(MethodVisitor mv) {
+ getThis(mv,classInternalName,outerClassDescriptor,outerClassInternalName);
+ mv.visitVarInsn(ALOAD, 1);
+ mv.visitVarInsn(ALOAD, 2);
+ mv.visitMethodInsn(INVOKEVIRTUAL, outerClassInternalName, "this$dist$invoke$" + objectDistance, "(Ljava/lang/String;Ljava/lang/Object;)Ljava/lang/Object;", false);
+ mv.visitInsn(ARETURN);
+ }
+ })
+ );
+ }
+ method.setCode(block);
+
+ // add static missing method dispatcher
+ methodName = "$static_methodMissing";
+ if (isStatic)
+ addCompilationErrorOnCustomMethodNode(node, methodName, parameters);
+
+ method = node.addSyntheticMethod(
+ methodName,
+ Opcodes.ACC_PUBLIC | Opcodes.ACC_STATIC,
+ ClassHelper.OBJECT_TYPE,
+ parameters,
+ ClassNode.EMPTY_ARRAY,
+ null
+ );
+
+ block = new BlockStatement();
+ setMethodDispatcherCode(block, new ClassExpression(outerClass), parameters);
+ method.setCode(block);
+
+ // add property setter dispatcher
+ parameters = new Parameter[]{
+ new Parameter(ClassHelper.STRING_TYPE, "name"),
+ new Parameter(ClassHelper.OBJECT_TYPE, "val")
+ };
+
+ methodName = "propertyMissing";
+ if (isStatic)
+ addCompilationErrorOnCustomMethodNode(node, methodName, parameters);
+
+ method = node.addSyntheticMethod(
+ methodName,
+ Opcodes.ACC_PUBLIC,
+ ClassHelper.VOID_TYPE,
+ parameters,
+ ClassNode.EMPTY_ARRAY,
+ null
+ );
+
+ block = new BlockStatement();
+ if (isStatic) {
+ setPropertySetterDispatcher(block, new ClassExpression(outerClass), parameters);
+ } else {
+ block.addStatement(
+ new BytecodeSequence(new BytecodeInstruction() {
+ public void visit(MethodVisitor mv) {
+ getThis(mv,classInternalName,outerClassDescriptor,outerClassInternalName);
+ mv.visitVarInsn(ALOAD, 1);
+ mv.visitVarInsn(ALOAD, 2);
+ mv.visitMethodInsn(INVOKEVIRTUAL, outerClassInternalName, "this$dist$set$" + objectDistance, "(Ljava/lang/String;Ljava/lang/Object;)V", false);
+ mv.visitInsn(RETURN);
+ }
+ })
+ );
+ }
+ method.setCode(block);
+
+ // add static property missing setter dispatcher
+ methodName = "$static_propertyMissing";
+ if (isStatic)
+ addCompilationErrorOnCustomMethodNode(node, methodName, parameters);
+
+ method = node.addSyntheticMethod(
+ methodName,
+ Opcodes.ACC_PUBLIC | Opcodes.ACC_STATIC,
+ ClassHelper.VOID_TYPE,
+ parameters,
+ ClassNode.EMPTY_ARRAY,
+ null
+ );
+
+ block = new BlockStatement();
+ setPropertySetterDispatcher(block, new ClassExpression(outerClass), parameters);
+ method.setCode(block);
+
+ // add property getter dispatcher
+ parameters = new Parameter[]{
+ new Parameter(ClassHelper.STRING_TYPE, "name")
+ };
+
+ methodName = "propertyMissing";
+ if (isStatic)
+ addCompilationErrorOnCustomMethodNode(node, methodName, parameters);
+
+ method = node.addSyntheticMethod(
+ methodName,
+ Opcodes.ACC_PUBLIC,
+ ClassHelper.OBJECT_TYPE,
+ parameters,
+ ClassNode.EMPTY_ARRAY,
+ null
+ );
+
+ block = new BlockStatement();
+ if (isStatic) {
+ setPropertyGetterDispatcher(block, new ClassExpression(outerClass), parameters);
+ } else {
+ block.addStatement(
+ new BytecodeSequence(new BytecodeInstruction() {
+ public void visit(MethodVisitor mv) {
+ getThis(mv,classInternalName,outerClassDescriptor,outerClassInternalName);
+ mv.visitVarInsn(ALOAD, 1);
+ mv.visitMethodInsn(INVOKEVIRTUAL, outerClassInternalName, "this$dist$get$" + objectDistance, "(Ljava/lang/String;)Ljava/lang/Object;", false);
+ mv.visitInsn(ARETURN);
+ }
+ })
+ );
+ }
+ method.setCode(block);
+
+ // add static property missing getter dispatcher
+ methodName = "$static_propertyMissing";
+ if (isStatic)
+ addCompilationErrorOnCustomMethodNode(node, methodName, parameters);
+
+ method = node.addSyntheticMethod(
+ methodName,
+ Opcodes.ACC_PUBLIC | Opcodes.ACC_STATIC,
+ ClassHelper.OBJECT_TYPE,
+ parameters,
+ ClassNode.EMPTY_ARRAY,
+ null
+ );
+
+ block = new BlockStatement();
+ setPropertyGetterDispatcher(block, new ClassExpression(outerClass), parameters);
+ method.setCode(block);
+ }
+
+ /**
+ * Adds a compilation error if a {@link MethodNode} with the given <tt>methodName</tt> and
+ * <tt>parameters</tt> exists in the {@link InnerClassNode}.
+ */
+ private void addCompilationErrorOnCustomMethodNode(InnerClassNode node, String methodName, Parameter[] parameters) {
+ MethodNode existingMethodNode = node.getMethod(methodName, parameters);
+ // if there is a user-defined methodNode, add compiler error msg and continue
+ if (existingMethodNode != null && !isSynthetic(existingMethodNode)) {
+ addError("\"" +methodName + "\" implementations are not supported on static inner classes as " +
+ "a synthetic version of \"" + methodName + "\" is added during compilation for the purpose " +
+ "of outer class delegation.",
+ existingMethodNode);
+ }
+ }
+
+ // GROOVY-8914: pre-compiled classes lose synthetic boolean - TODO fix earlier as per GROOVY-4346 then remove extra check here
+ private boolean isSynthetic(MethodNode existingMethodNode) {
+ return existingMethodNode.isSynthetic() || hasSyntheticModifier(existingMethodNode);
+ }
+
+ private boolean hasSyntheticModifier(MethodNode existingMethodNode) {
+ return (existingMethodNode.getModifiers() & Opcodes.ACC_SYNTHETIC) != 0;
+ }
+
+ private void addThisReference(ConstructorNode node) {
+ if (!shouldHandleImplicitThisForInnerClass(classNode)) return;
+ Statement code = node.getCode();
+
+ // add "this$0" field init
+
+ //add this parameter to node
+ Parameter[] params = node.getParameters();
+ Parameter[] newParams = new Parameter[params.length + 1];
+ System.arraycopy(params, 0, newParams, 1, params.length);
+ String name = getUniqueName(params, node);
+
+ Parameter thisPara = new Parameter(classNode.getOuterClass().getPlainNodeReference(), name);
+ newParams[0] = thisPara;
+ node.setParameters(newParams);
+
+ BlockStatement block = null;
+ if (code == null) {
+ block = new BlockStatement();
+ } else if (!(code instanceof BlockStatement)) {
+ block = new BlockStatement();
+ block.addStatement(code);
+ } else {
+ block = (BlockStatement) code;
+ }
+ BlockStatement newCode = new BlockStatement();
+ addFieldInit(thisPara, thisField, newCode);
+ ConstructorCallExpression cce = getFirstIfSpecialConstructorCall(block);
+ if (cce == null) {
+ cce = new ConstructorCallExpression(ClassNode.SUPER, new TupleExpression());
+ block.getStatements().add(0, new ExpressionStatement(cce));
+ }
+ if (shouldImplicitlyPassThisPara(cce)) {
+ // add thisPara to this(...)
+ TupleExpression args = (TupleExpression) cce.getArguments();
+ List<Expression> expressions = args.getExpressions();
+ VariableExpression ve = new VariableExpression(thisPara.getName());
+ ve.setAccessedVariable(thisPara);
+ expressions.add(0, ve);
+ }
+ if (cce.isSuperCall()) {
+ // we have a call to super here, so we need to add
+ // our code after that
+ block.getStatements().add(1, newCode);
+ }
+ node.setCode(block);
+ }
+
+ private boolean shouldImplicitlyPassThisPara(ConstructorCallExpression cce) {
+ boolean pass = false;
+ ClassNode superCN = classNode.getSuperClass();
+ if (cce.isThisCall()) {
+ pass = true;
+ } else if (cce.isSuperCall()) {
+ // if the super class is another non-static inner class in the same outer class hierarchy, implicit this
+ // needs to be passed
+ if (!superCN.isEnum() && !superCN.isInterface() && superCN instanceof InnerClassNode) {
+ InnerClassNode superInnerCN = (InnerClassNode) superCN;
+ if (!isStatic(superInnerCN) && classNode.getOuterClass().isDerivedFrom(superCN.getOuterClass())) {
+ pass = true;
+ }
+ }
+ }
+ return pass;
+ }
+
+ private String getUniqueName(Parameter[] params, ConstructorNode node) {
+ String namePrefix = "$p";
+ outer:
+ for (int i = 0; i < 100; i++) {
+ namePrefix = namePrefix + "$";
+ for (Parameter p : params) {
+ if (p.getName().equals(namePrefix)) continue outer;
+ }
+ return namePrefix;
+ }
+ addError("unable to find a unique prefix name for synthetic this reference in inner class constructor", node);
+ return namePrefix;
+ }
+
+ private static ConstructorCallExpression getFirstIfSpecialConstructorCall(BlockStatement code) {
+ if (code == null) return null;
+
+ final List<Statement> statementList = code.getStatements();
+ if (statementList.isEmpty()) return null;
+
+ final Statement statement = statementList.get(0);
+ if (!(statement instanceof ExpressionStatement)) return null;
+
+ Expression expression = ((ExpressionStatement) statement).getExpression();
+ if (!(expression instanceof ConstructorCallExpression)) return null;
+ ConstructorCallExpression cce = (ConstructorCallExpression) expression;
+ if (cce.isSpecialCall()) return cce;
+ return null;
+ }
+}
[groovy] 02/07: minor refactor: rename for consistency and remove
some redundant modifiers
Posted by pa...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
paulk pushed a commit to branch GROOVY_2_5_X
in repository https://gitbox.apache.org/repos/asf/groovy.git
commit 2ea0e96efd39f1e447d787205c777f85cbd046bc
Author: Paul King <pa...@asert.com.au>
AuthorDate: Fri Mar 22 09:45:16 2019 +1000
minor refactor: rename for consistency and remove some redundant modifiers
---
...enerListASTTest.groovy => ListenerListTransformTest.groovy} | 10 +++++-----
1 file changed, 5 insertions(+), 5 deletions(-)
diff --git a/src/test/groovy/beans/ListenerListASTTest.groovy b/src/test/groovy/beans/ListenerListTransformTest.groovy
similarity index 98%
rename from src/test/groovy/beans/ListenerListASTTest.groovy
rename to src/test/groovy/beans/ListenerListTransformTest.groovy
index d36f66b..93e5ed1 100644
--- a/src/test/groovy/beans/ListenerListASTTest.groovy
+++ b/src/test/groovy/beans/ListenerListTransformTest.groovy
@@ -22,9 +22,9 @@ package groovy.beans
* Unit test for ListenerList.
*/
-class ListenerListASTTest extends GroovyTestCase {
+class ListenerListTransformTest extends GroovyTestCase {
- public void testDefaultFireAndName() {
+ void testDefaultFireAndName() {
GroovyShell shell = new GroovyShell()
def tc = shell.evaluate("""
import groovy.beans.*
@@ -52,7 +52,7 @@ class ListenerListASTTest extends GroovyTestCase {
assert evt.message.is(message)
}
- public void testCustomFireAndName() {
+ void testCustomFireAndName() {
GroovyShell shell = new GroovyShell()
def tc = shell.evaluate("""
package b
@@ -85,7 +85,7 @@ class ListenerListASTTest extends GroovyTestCase {
assert evt.message.is(message)
}
- public void testMultipleMethodListener() {
+ void testMultipleMethodListener() {
GroovyShell shell = new GroovyShell()
def tc = shell.evaluate("""
package b
@@ -120,7 +120,7 @@ class ListenerListASTTest extends GroovyTestCase {
assert evt2.message.is(message2)
}
- public void testMultipleListenersConflictsDetected() {
+ void testMultipleListenersConflictsDetected() {
def message = shouldFail {
new GroovyShell().evaluate("""
package b