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 2021/11/05 10:53:00 UTC
[groovy] branch master updated: GROOVY-10338: refactor record
copyWith (reinstate commented out tests and fix)
This is an automated email from the ASF dual-hosted git repository.
paulk pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/groovy.git
The following commit(s) were added to refs/heads/master by this push:
new aade108 GROOVY-10338: refactor record copyWith (reinstate commented out tests and fix)
aade108 is described below
commit aade108b04132afd40a0fda2774695b6e2e91660
Author: Paul King <pa...@asert.com.au>
AuthorDate: Fri Nov 5 20:52:52 2021 +1000
GROOVY-10338: refactor record copyWith (reinstate commented out tests and fix)
---
src/main/groovy/groovy/transform/RecordType.groovy | 2 +-
src/main/java/groovy/transform/RecordBase.java | 2 +-
src/main/java/groovy/transform/RecordOptions.java | 2 +-
.../RecordCompletionASTTransformation.java | 100 +++++++++++++++++++++
src/spec/test/RecordSpecificationTest.groovy | 2 -
5 files changed, 103 insertions(+), 5 deletions(-)
diff --git a/src/main/groovy/groovy/transform/RecordType.groovy b/src/main/groovy/groovy/transform/RecordType.groovy
index d476d64..3e50833 100644
--- a/src/main/groovy/groovy/transform/RecordType.groovy
+++ b/src/main/groovy/groovy/transform/RecordType.groovy
@@ -77,7 +77,7 @@ import java.lang.annotation.Target
*/
@RecordBase
@RecordOptions
-@TupleConstructor(namedVariant = true, force = true/*, callSuper = true*/)
+@TupleConstructor(namedVariant = true, force = true)
@PropertyOptions
@KnownImmutable
@POJO
diff --git a/src/main/java/groovy/transform/RecordBase.java b/src/main/java/groovy/transform/RecordBase.java
index 85c7c05..38dd58e 100644
--- a/src/main/java/groovy/transform/RecordBase.java
+++ b/src/main/java/groovy/transform/RecordBase.java
@@ -38,6 +38,6 @@ import java.lang.annotation.Target;
@java.lang.annotation.Documented
@Retention(RetentionPolicy.SOURCE)
@Target({ElementType.TYPE})
-@GroovyASTTransformationClass("org.codehaus.groovy.transform.RecordTypeASTTransformation")
+@GroovyASTTransformationClass({"org.codehaus.groovy.transform.RecordTypeASTTransformation","org.codehaus.groovy.transform.RecordCompletionASTTransformation"})
public @interface RecordBase {
}
diff --git a/src/main/java/groovy/transform/RecordOptions.java b/src/main/java/groovy/transform/RecordOptions.java
index 0447aa5..ade17b8 100644
--- a/src/main/java/groovy/transform/RecordOptions.java
+++ b/src/main/java/groovy/transform/RecordOptions.java
@@ -122,7 +122,7 @@ public @interface RecordOptions {
* new property values and returns a new instance of the record class with
* these values set.
* Example:
- * <pre class="TODO_FIX_groovyTestCase">
+ * <pre class="groovyTestCase">
* {@code @groovy.transform.RecordType}(copyWith = true)
* class Person {
* String first, last
diff --git a/src/main/java/org/codehaus/groovy/transform/RecordCompletionASTTransformation.java b/src/main/java/org/codehaus/groovy/transform/RecordCompletionASTTransformation.java
new file mode 100644
index 0000000..09b7366
--- /dev/null
+++ b/src/main/java/org/codehaus/groovy/transform/RecordCompletionASTTransformation.java
@@ -0,0 +1,100 @@
+/*
+ * 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.transform;
+
+import groovy.transform.RecordBase;
+import org.codehaus.groovy.ast.ASTNode;
+import org.codehaus.groovy.ast.AnnotatedNode;
+import org.codehaus.groovy.ast.AnnotationNode;
+import org.codehaus.groovy.ast.ClassNode;
+import org.codehaus.groovy.ast.ConstructorNode;
+import org.codehaus.groovy.ast.MethodNode;
+import org.codehaus.groovy.ast.Parameter;
+import org.codehaus.groovy.ast.PropertyNode;
+import org.codehaus.groovy.ast.expr.ConstructorCallExpression;
+import org.codehaus.groovy.ast.expr.Expression;
+import org.codehaus.groovy.ast.stmt.ReturnStatement;
+import org.codehaus.groovy.ast.stmt.Statement;
+import org.codehaus.groovy.control.CompilePhase;
+import org.codehaus.groovy.control.SourceUnit;
+import org.codehaus.groovy.transform.stc.StaticTypesMarker;
+
+import java.lang.annotation.Annotation;
+import java.util.ArrayList;
+import java.util.List;
+
+import static org.codehaus.groovy.ast.ClassHelper.MAP_TYPE;
+import static org.codehaus.groovy.ast.ClassHelper.makeWithoutCaching;
+import static org.codehaus.groovy.ast.tools.GeneralUtils.getInstanceProperties;
+import static org.codehaus.groovy.ast.tools.GeneralUtils.param;
+import static org.codehaus.groovy.ast.tools.GeneralUtils.params;
+
+/**
+ * Handles completion of code for the @RecordType annotation.
+ */
+@GroovyASTTransformation(phase = CompilePhase.INSTRUCTION_SELECTION)
+public class RecordCompletionASTTransformation extends AbstractASTTransformation {
+
+ private static final Class<? extends Annotation> MY_CLASS = RecordBase.class;
+ public static final ClassNode MY_TYPE = makeWithoutCaching(MY_CLASS, false);
+ private static final String MY_TYPE_NAME = MY_TYPE.getNameWithoutPackage();
+
+ @Override
+ public String getAnnotationName() {
+ return MY_TYPE_NAME;
+ }
+
+ @Override
+ public void visit(ASTNode[] nodes, SourceUnit source) {
+ init(nodes, source);
+ AnnotatedNode parent = (AnnotatedNode) nodes[1];
+ AnnotationNode anno = (AnnotationNode) nodes[0];
+ if (!MY_TYPE.equals(anno.getClassNode())) return;
+
+ if (parent instanceof ClassNode) {
+ ClassNode cNode = (ClassNode) parent;
+ MethodNode copyWith = cNode.getMethod("copyWith", params(param(MAP_TYPE, "namedArgs")));
+ if (copyWith != null) {
+ adjustCopyWith(cNode, copyWith);
+ }
+ }
+ }
+
+ // when the record classnode was processed, the tuple constructor hadn't been added yet, so manually adjust here
+ private void adjustCopyWith(ClassNode cNode, MethodNode copyWith) {
+ final List<Parameter> params = new ArrayList<>();
+ final List<PropertyNode> pList = getInstanceProperties(cNode);
+ for (int i = 0; i < pList.size(); i++) {
+ PropertyNode pNode = pList.get(i);
+ params.add(param(pNode.getType(), "arg" + i));
+ }
+
+ ConstructorNode consNode = cNode.getDeclaredConstructor(params.toArray(new Parameter[0]));
+ if (consNode != null) {
+ Statement code = copyWith.getCode();
+ if (code instanceof ReturnStatement) {
+ ReturnStatement rs = (ReturnStatement) code;
+ Expression expr = rs.getExpression();
+ if (expr instanceof ConstructorCallExpression) {
+ expr.putNodeMetaData(StaticTypesMarker.DIRECT_METHOD_CALL_TARGET, consNode);
+ }
+ }
+ }
+ }
+}
diff --git a/src/spec/test/RecordSpecificationTest.groovy b/src/spec/test/RecordSpecificationTest.groovy
index e21f6fd..7a9974c 100644
--- a/src/spec/test/RecordSpecificationTest.groovy
+++ b/src/spec/test/RecordSpecificationTest.groovy
@@ -97,7 +97,6 @@ assert new Point3D(10, 20, 30).toString() == 'Point3D[coords=10,20,30]'
}
void testCopyWith() {
- /* TODO FIX
assertScript '''
import groovy.transform.RecordOptions
// tag::record_copywith[]
@@ -111,7 +110,6 @@ def orange = apple.copyWith(name: 'Orange')
assert orange.toString() == 'Fruit[name=Orange, price=11.6]'
// end::record_copywith[]
'''
- */
assertScript '''
import groovy.transform.RecordOptions
import static groovy.test.GroovyAssert.shouldFail