groovy-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From sun...@apache.org
Subject groovy git commit: Make EmptyExpression and EmptyStatement immutatble
Date Wed, 28 Jun 2017 13:07:56 GMT
Repository: groovy
Updated Branches:
  refs/heads/GROOVY_2_6_X 6c44e35ba -> 97e1d16cb


Make EmptyExpression and EmptyStatement immutatble


Project: http://git-wip-us.apache.org/repos/asf/groovy/repo
Commit: http://git-wip-us.apache.org/repos/asf/groovy/commit/97e1d16c
Tree: http://git-wip-us.apache.org/repos/asf/groovy/tree/97e1d16c
Diff: http://git-wip-us.apache.org/repos/asf/groovy/diff/97e1d16c

Branch: refs/heads/GROOVY_2_6_X
Commit: 97e1d16cb6b2e8b0ae40fabced35e86951dc0e24
Parents: 6c44e35
Author: sunlan <sunlan@apache.org>
Authored: Wed Jun 28 21:07:39 2017 +0800
Committer: sunlan <sunlan@apache.org>
Committed: Wed Jun 28 21:07:39 2017 +0800

----------------------------------------------------------------------
 .../groovy/ast/expr/EmptyExpression.java        | 92 ++++++++++++++++++++
 .../groovy/ast/stmt/EmptyStatement.java         | 69 +++++++++++++++
 .../groovy/control/StaticImportVisitor.java     |  2 +-
 .../AutoImplementASTTransformation.java         |  2 +-
 .../MapConstructorASTTransformation.java        |  4 +-
 .../TupleConstructorASTTransformation.java      |  4 +-
 .../stc/StaticTypeCheckingVisitor.java          |  2 +-
 .../transform/tailrec/InWhileLoopWrapper.groovy |  2 +-
 .../SingletonASTTransformationTest.java         |  2 +-
 .../tailrec/StatementReplacerTest.groovy        |  4 +-
 .../VariableExpressionReplacerTest.groovy       |  2 +-
 .../markup/MarkupBuilderCodeTransformer.java    |  3 +-
 12 files changed, 174 insertions(+), 14 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/groovy/blob/97e1d16c/src/main/org/codehaus/groovy/ast/expr/EmptyExpression.java
----------------------------------------------------------------------
diff --git a/src/main/org/codehaus/groovy/ast/expr/EmptyExpression.java b/src/main/org/codehaus/groovy/ast/expr/EmptyExpression.java
index d469b67..d1a3596 100644
--- a/src/main/org/codehaus/groovy/ast/expr/EmptyExpression.java
+++ b/src/main/org/codehaus/groovy/ast/expr/EmptyExpression.java
@@ -18,7 +18,14 @@
  */
 package org.codehaus.groovy.ast.expr;
 
+import org.codehaus.groovy.ast.ASTNode;
+import org.codehaus.groovy.ast.AnnotationNode;
+import org.codehaus.groovy.ast.ClassNode;
 import org.codehaus.groovy.ast.GroovyCodeVisitor;
+import org.codehaus.groovy.ast.NodeMetaDataHandler;
+
+import java.util.List;
+import java.util.Map;
 
 /**
  * This class is a place holder for an empty expression. 
@@ -45,4 +52,89 @@ public class EmptyExpression extends Expression {
     public void visit(GroovyCodeVisitor visitor) {
         return;
     }
+
+
+    @Override
+    public void setType(ClassNode t) {
+        throw createUnsupportedOperationException();
+    }
+
+    @Override
+    public void addAnnotation(AnnotationNode value) {
+        throw createUnsupportedOperationException();
+    }
+
+    @Override
+    public void addAnnotations(List<AnnotationNode> annotations) {
+        throw createUnsupportedOperationException();
+    }
+
+    @Override
+    public void setSynthetic(boolean synthetic) {
+        throw createUnsupportedOperationException();
+    }
+
+    @Override
+    public void setDeclaringClass(ClassNode declaringClass) {
+        throw createUnsupportedOperationException();
+    }
+
+    @Override
+    public void setHasNoRealSourcePosition(boolean value) {
+        throw createUnsupportedOperationException();
+    }
+
+    @Override
+    public void setLineNumber(int lineNumber) {
+        throw createUnsupportedOperationException();
+    }
+
+    @Override
+    public void setColumnNumber(int columnNumber) {
+        throw createUnsupportedOperationException();
+    }
+
+    @Override
+    public void setLastLineNumber(int lastLineNumber) {
+        throw createUnsupportedOperationException();
+    }
+
+    @Override
+    public void setLastColumnNumber(int lastColumnNumber) {
+        throw createUnsupportedOperationException();
+    }
+
+    @Override
+    public void setSourcePosition(ASTNode node) {
+        throw createUnsupportedOperationException();
+    }
+
+    @Override
+    public void copyNodeMetaData(NodeMetaDataHandler other) {
+        throw createUnsupportedOperationException();
+    }
+
+    @Override
+    public void setNodeMetaData(Object key, Object value) {
+        throw createUnsupportedOperationException();
+    }
+
+    @Override
+    public Object putNodeMetaData(Object key, Object value) {
+        throw createUnsupportedOperationException();
+    }
+
+    @Override
+    public void removeNodeMetaData(Object key) {
+        throw createUnsupportedOperationException();
+    }
+
+    @Override
+    public void setMetaDataMap(Map<?, ?> metaDataMap) {
+        throw createUnsupportedOperationException();
+    }
+
+    private UnsupportedOperationException createUnsupportedOperationException() {
+        return new UnsupportedOperationException("EmptyExpression.INSTANCE is immutable");
+    }
 }
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/groovy/blob/97e1d16c/src/main/org/codehaus/groovy/ast/stmt/EmptyStatement.java
----------------------------------------------------------------------
diff --git a/src/main/org/codehaus/groovy/ast/stmt/EmptyStatement.java b/src/main/org/codehaus/groovy/ast/stmt/EmptyStatement.java
index c69d9e3..2e6b2da 100644
--- a/src/main/org/codehaus/groovy/ast/stmt/EmptyStatement.java
+++ b/src/main/org/codehaus/groovy/ast/stmt/EmptyStatement.java
@@ -18,7 +18,11 @@
  */
 package org.codehaus.groovy.ast.stmt;
 
+import org.codehaus.groovy.ast.ASTNode;
 import org.codehaus.groovy.ast.GroovyCodeVisitor;
+import org.codehaus.groovy.ast.NodeMetaDataHandler;
+
+import java.util.Map;
 
 /**
  * Represents an empty statement
@@ -40,4 +44,69 @@ public class EmptyStatement extends Statement {
     public boolean isEmpty() {
         return true;
     }
+
+
+    @Override
+    public void setStatementLabel(String label) {
+        throw createUnsupportedOperationException();
+    }
+
+    @Override
+    public void addStatementLabel(String label) {
+        throw createUnsupportedOperationException();
+    }
+
+    @Override
+    public void setLineNumber(int lineNumber) {
+        throw createUnsupportedOperationException();
+    }
+
+    @Override
+    public void setColumnNumber(int columnNumber) {
+        throw createUnsupportedOperationException();
+    }
+
+    @Override
+    public void setLastLineNumber(int lastLineNumber) {
+        throw createUnsupportedOperationException();
+    }
+
+    @Override
+    public void setLastColumnNumber(int lastColumnNumber) {
+        throw createUnsupportedOperationException();
+    }
+
+    @Override
+    public void setSourcePosition(ASTNode node) {
+        throw createUnsupportedOperationException();
+    }
+
+    @Override
+    public void copyNodeMetaData(NodeMetaDataHandler other) {
+        throw createUnsupportedOperationException();
+    }
+
+    @Override
+    public void setNodeMetaData(Object key, Object value) {
+        throw createUnsupportedOperationException();
+    }
+
+    @Override
+    public Object putNodeMetaData(Object key, Object value) {
+        throw createUnsupportedOperationException();
+    }
+
+    @Override
+    public void removeNodeMetaData(Object key) {
+        throw createUnsupportedOperationException();
+    }
+
+    @Override
+    public void setMetaDataMap(Map<?, ?> metaDataMap) {
+        throw createUnsupportedOperationException();
+    }
+
+    private UnsupportedOperationException createUnsupportedOperationException() {
+        return new UnsupportedOperationException("EmptyStatement.INSTANCE is immutable");
+    }
 }

http://git-wip-us.apache.org/repos/asf/groovy/blob/97e1d16c/src/main/org/codehaus/groovy/control/StaticImportVisitor.java
----------------------------------------------------------------------
diff --git a/src/main/org/codehaus/groovy/control/StaticImportVisitor.java b/src/main/org/codehaus/groovy/control/StaticImportVisitor.java
index e055ebb..d4a2566 100644
--- a/src/main/org/codehaus/groovy/control/StaticImportVisitor.java
+++ b/src/main/org/codehaus/groovy/control/StaticImportVisitor.java
@@ -564,7 +564,7 @@ public class StaticImportVisitor extends ClassCodeExpressionTransformer
{
     private Expression findStaticPropertyAccessorByFullName(ClassNode staticImportType, String
accessorMethodName) {
         // anything will do as we only check size == 1
         ArgumentListExpression dummyArgs = new ArgumentListExpression();
-        dummyArgs.addExpression(new EmptyExpression());
+        dummyArgs.addExpression(EmptyExpression.INSTANCE);
         return findStaticMethod(staticImportType, accessorMethodName, (inLeftExpression ?
dummyArgs : ArgumentListExpression.EMPTY_ARGUMENTS));
     }
 

http://git-wip-us.apache.org/repos/asf/groovy/blob/97e1d16c/src/main/org/codehaus/groovy/transform/AutoImplementASTTransformation.java
----------------------------------------------------------------------
diff --git a/src/main/org/codehaus/groovy/transform/AutoImplementASTTransformation.java b/src/main/org/codehaus/groovy/transform/AutoImplementASTTransformation.java
index 74124fd..592b2f2 100644
--- a/src/main/org/codehaus/groovy/transform/AutoImplementASTTransformation.java
+++ b/src/main/org/codehaus/groovy/transform/AutoImplementASTTransformation.java
@@ -85,7 +85,7 @@ public class AutoImplementASTTransformation extends AbstractASTTransformation
{
             }
             createMethods(cNode, exception, message, (ClosureExpression) code);
             if (code != null) {
-                anno.setMember("code", new ClosureExpression(new Parameter[0], new EmptyStatement()));
+                anno.setMember("code", new ClosureExpression(new Parameter[0], EmptyStatement.INSTANCE));
             }
         }
     }

http://git-wip-us.apache.org/repos/asf/groovy/blob/97e1d16c/src/main/org/codehaus/groovy/transform/MapConstructorASTTransformation.java
----------------------------------------------------------------------
diff --git a/src/main/org/codehaus/groovy/transform/MapConstructorASTTransformation.java b/src/main/org/codehaus/groovy/transform/MapConstructorASTTransformation.java
index b5c5d1e..d5de8c1 100644
--- a/src/main/org/codehaus/groovy/transform/MapConstructorASTTransformation.java
+++ b/src/main/org/codehaus/groovy/transform/MapConstructorASTTransformation.java
@@ -108,10 +108,10 @@ public class MapConstructorASTTransformation extends AbstractASTTransformation
{
 
             createConstructor(cNode, includeFields, includeProperties, includeSuperProperties,
useSetters, excludes, includes, (ClosureExpression) pre, (ClosureExpression) post, source,
allNames);
             if (pre != null) {
-                anno.setMember("pre", new ClosureExpression(new Parameter[0], new EmptyStatement()));
+                anno.setMember("pre", new ClosureExpression(new Parameter[0], EmptyStatement.INSTANCE));
             }
             if (post != null) {
-                anno.setMember("post", new ClosureExpression(new Parameter[0], new EmptyStatement()));
+                anno.setMember("post", new ClosureExpression(new Parameter[0], EmptyStatement.INSTANCE));
             }
         }
     }

http://git-wip-us.apache.org/repos/asf/groovy/blob/97e1d16c/src/main/org/codehaus/groovy/transform/TupleConstructorASTTransformation.java
----------------------------------------------------------------------
diff --git a/src/main/org/codehaus/groovy/transform/TupleConstructorASTTransformation.java
b/src/main/org/codehaus/groovy/transform/TupleConstructorASTTransformation.java
index 746eaf4..6ab0b96 100644
--- a/src/main/org/codehaus/groovy/transform/TupleConstructorASTTransformation.java
+++ b/src/main/org/codehaus/groovy/transform/TupleConstructorASTTransformation.java
@@ -138,10 +138,10 @@ public class TupleConstructorASTTransformation extends AbstractASTTransformation
                     callSuper, force, excludes, includes, useSetters, defaults, allNames,
sourceUnit,
                     (ClosureExpression) pre, (ClosureExpression) post);
             if (pre != null) {
-                anno.setMember("pre", new ClosureExpression(new Parameter[0], new EmptyStatement()));
+                anno.setMember("pre", new ClosureExpression(new Parameter[0], EmptyStatement.INSTANCE));
             }
             if (post != null) {
-                anno.setMember("post", new ClosureExpression(new Parameter[0], new EmptyStatement()));
+                anno.setMember("post", new ClosureExpression(new Parameter[0], EmptyStatement.INSTANCE));
             }
         }
     }

http://git-wip-us.apache.org/repos/asf/groovy/blob/97e1d16c/src/main/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java
----------------------------------------------------------------------
diff --git a/src/main/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java b/src/main/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java
index c1548aa..b7f00b6 100644
--- a/src/main/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java
+++ b/src/main/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java
@@ -159,7 +159,7 @@ public class StaticTypeCheckingVisitor extends ClassCodeVisitorSupport
{
     protected static final ClassNode MAP_ENTRY_TYPE = make(Map.Entry.class);
     protected static final ClassNode ENUMERATION_TYPE = make(Enumeration.class);
 
-    public static final Statement GENERATED_EMPTY_STATEMENT = new EmptyStatement();
+    public static final Statement GENERATED_EMPTY_STATEMENT = EmptyStatement.INSTANCE;
 
     public static final MethodNode CLOSURE_CALL_NO_ARG;
     public static final MethodNode CLOSURE_CALL_ONE_ARG;

http://git-wip-us.apache.org/repos/asf/groovy/blob/97e1d16c/src/main/org/codehaus/groovy/transform/tailrec/InWhileLoopWrapper.groovy
----------------------------------------------------------------------
diff --git a/src/main/org/codehaus/groovy/transform/tailrec/InWhileLoopWrapper.groovy b/src/main/org/codehaus/groovy/transform/tailrec/InWhileLoopWrapper.groovy
index 0fe2baa..981f146 100644
--- a/src/main/org/codehaus/groovy/transform/tailrec/InWhileLoopWrapper.groovy
+++ b/src/main/org/codehaus/groovy/transform/tailrec/InWhileLoopWrapper.groovy
@@ -53,7 +53,7 @@ class InWhileLoopWrapper {
 		BlockStatement oldBody = method.code as BlockStatement
         TryCatchStatement tryCatchStatement = new TryCatchStatement(
                 oldBody,
-                new EmptyStatement()
+                EmptyStatement.INSTANCE
         )
         tryCatchStatement.addCatch(new CatchStatement(
                 new Parameter(ClassHelper.make(GotoRecurHereException), 'ignore'),

http://git-wip-us.apache.org/repos/asf/groovy/blob/97e1d16c/src/test/org/codehaus/groovy/transform/SingletonASTTransformationTest.java
----------------------------------------------------------------------
diff --git a/src/test/org/codehaus/groovy/transform/SingletonASTTransformationTest.java b/src/test/org/codehaus/groovy/transform/SingletonASTTransformationTest.java
index 29f43f5..c9c748e 100644
--- a/src/test/org/codehaus/groovy/transform/SingletonASTTransformationTest.java
+++ b/src/test/org/codehaus/groovy/transform/SingletonASTTransformationTest.java
@@ -36,7 +36,7 @@ public class SingletonASTTransformationTest {
         try {
             ASTNode[] badInput = new ASTNode[]{
                     new ConstantExpression("sample"),
-                    new EmptyExpression()
+                    EmptyExpression.INSTANCE
             };
             new SingletonASTTransformation().visit(badInput, null);
             Assert.fail("Contract Failure");

http://git-wip-us.apache.org/repos/asf/groovy/blob/97e1d16c/src/test/org/codehaus/groovy/transform/tailrec/StatementReplacerTest.groovy
----------------------------------------------------------------------
diff --git a/src/test/org/codehaus/groovy/transform/tailrec/StatementReplacerTest.groovy b/src/test/org/codehaus/groovy/transform/tailrec/StatementReplacerTest.groovy
index 6abbebe..2377ed0 100644
--- a/src/test/org/codehaus/groovy/transform/tailrec/StatementReplacerTest.groovy
+++ b/src/test/org/codehaus/groovy/transform/tailrec/StatementReplacerTest.groovy
@@ -118,7 +118,7 @@ class StatementReplacerTest {
     public void replaceIfBlock() {
         def toReplace = aReturnStatement("old")
         def replacement = aReturnStatement("new")
-        def ifStatement = new IfStatement(aBooleanExpression(true), toReplace, new EmptyStatement())
+        def ifStatement = new IfStatement(aBooleanExpression(true), toReplace, EmptyStatement.INSTANCE)
 
         replacements[toReplace] = replacement
         replacer.replaceIn(ifStatement)
@@ -130,7 +130,7 @@ class StatementReplacerTest {
     public void replaceElseBlock() {
         def toReplace = aReturnStatement("old")
         def replacement = aReturnStatement("new")
-        def ifStatement = new IfStatement(aBooleanExpression(true), new EmptyStatement(),
toReplace)
+        def ifStatement = new IfStatement(aBooleanExpression(true), EmptyStatement.INSTANCE,
toReplace)
 
         replacements[toReplace] = replacement
         replacer.replaceIn(ifStatement)

http://git-wip-us.apache.org/repos/asf/groovy/blob/97e1d16c/src/test/org/codehaus/groovy/transform/tailrec/VariableExpressionReplacerTest.groovy
----------------------------------------------------------------------
diff --git a/src/test/org/codehaus/groovy/transform/tailrec/VariableExpressionReplacerTest.groovy
b/src/test/org/codehaus/groovy/transform/tailrec/VariableExpressionReplacerTest.groovy
index 06cfdfe..bb7bd0f 100644
--- a/src/test/org/codehaus/groovy/transform/tailrec/VariableExpressionReplacerTest.groovy
+++ b/src/test/org/codehaus/groovy/transform/tailrec/VariableExpressionReplacerTest.groovy
@@ -182,7 +182,7 @@ class VariableExpressionReplacerTest {
 
 
     def anEmptyStatement() {
-        new EmptyStatement()
+        EmptyStatement.INSTANCE
     }
 
     def aConstant(value) {

http://git-wip-us.apache.org/repos/asf/groovy/blob/97e1d16c/subprojects/groovy-templates/src/main/groovy/groovy/text/markup/MarkupBuilderCodeTransformer.java
----------------------------------------------------------------------
diff --git a/subprojects/groovy-templates/src/main/groovy/groovy/text/markup/MarkupBuilderCodeTransformer.java
b/subprojects/groovy-templates/src/main/groovy/groovy/text/markup/MarkupBuilderCodeTransformer.java
index b7537b6..69f2bb4 100644
--- a/subprojects/groovy-templates/src/main/groovy/groovy/text/markup/MarkupBuilderCodeTransformer.java
+++ b/subprojects/groovy-templates/src/main/groovy/groovy/text/markup/MarkupBuilderCodeTransformer.java
@@ -166,8 +166,7 @@ class MarkupBuilderCodeTransformer extends ClassCodeExpressionTransformer
{
                 //  ...
                 // }
                 Map<String,ClassNode> modelTypes = extractModelTypesFromClosureExpression((ClosureExpression)right);
-                Expression result = new EmptyExpression();
-                result.setSourcePosition(bin);
+                Expression result = EmptyExpression.INSTANCE;
                 classNode.putNodeMetaData(MarkupTemplateEngine.MODELTYPES_ASTKEY, modelTypes);
                 return result;
             }


Mime
View raw message