You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@groovy.apache.org by em...@apache.org on 2022/11/15 15:43:41 UTC

[groovy] branch GROOVY_2_5_X updated (24cd3a14a7 -> 9d48af314c)

This is an automated email from the ASF dual-hosted git repository.

emilles pushed a change to branch GROOVY_2_5_X
in repository https://gitbox.apache.org/repos/asf/groovy.git


    from 24cd3a14a7 STC: fewer calls to `chooseBestMethod`
     new 9cb9c41c72 GROOVY-9739: prevent typecast expression with null type
     new 9d48af314c `CastExpression`: indicate coercion in `getText()` and `toString()`

The 2 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:
 .../codehaus/groovy/ast/expr/CastExpression.java   | 74 ++++++++++++----------
 1 file changed, 40 insertions(+), 34 deletions(-)


[groovy] 01/02: GROOVY-9739: prevent typecast expression with null type

Posted by em...@apache.org.
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

commit 9cb9c41c728dd5443317b3db3224ebe13caa4ba0
Author: Eric Milles <er...@thomsonreuters.com>
AuthorDate: Tue Nov 15 09:09:53 2022 -0600

    GROOVY-9739: prevent typecast expression with null type
    
    2_5_X backport
---
 src/main/java/org/codehaus/groovy/ast/expr/CastExpression.java | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/src/main/java/org/codehaus/groovy/ast/expr/CastExpression.java b/src/main/java/org/codehaus/groovy/ast/expr/CastExpression.java
index c1e6c88a2d..0c0747e0d2 100644
--- a/src/main/java/org/codehaus/groovy/ast/expr/CastExpression.java
+++ b/src/main/java/org/codehaus/groovy/ast/expr/CastExpression.java
@@ -102,8 +102,8 @@ public class CastExpression extends Expression {
         return expression;
     }
     
-    public void setType(ClassNode t) {
-        super.setType(t);
+    @Override
+    public void setType(final ClassNode type) {
+        super.setType(java.util.Objects.requireNonNull(type)); // GROOVY-9739
     }
-
 }


[groovy] 02/02: `CastExpression`: indicate coercion in `getText()` and `toString()`

Posted by em...@apache.org.
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

commit 9d48af314cb4144203a6d82a78c320251160b191
Author: Eric Milles <er...@thomsonreuters.com>
AuthorDate: Tue Nov 15 08:44:51 2022 -0600

    `CastExpression`: indicate coercion in `getText()` and `toString()`
    
    2_5_X backport
---
 .../codehaus/groovy/ast/expr/CastExpression.java   | 68 ++++++++++++----------
 1 file changed, 37 insertions(+), 31 deletions(-)

diff --git a/src/main/java/org/codehaus/groovy/ast/expr/CastExpression.java b/src/main/java/org/codehaus/groovy/ast/expr/CastExpression.java
index 0c0747e0d2..a640c0de3e 100644
--- a/src/main/java/org/codehaus/groovy/ast/expr/CastExpression.java
+++ b/src/main/java/org/codehaus/groovy/ast/expr/CastExpression.java
@@ -22,32 +22,37 @@ import org.codehaus.groovy.ast.ClassNode;
 import org.codehaus.groovy.ast.GroovyCodeVisitor;
 
 /**
- * Represents a type cast expression
+ * Represents a typecast expression.
  */
 public class CastExpression extends Expression {
-    
+
     private final Expression expression;
-    private boolean ignoreAutoboxing=false;
-    private boolean coerce = false;
-    private boolean strict = false;
+    private final boolean ignoreAutoboxing;
+
+    private boolean coerce;
+    private boolean strict;
 
-    public static CastExpression asExpression(ClassNode type, Expression expression) {
+    public static CastExpression asExpression(final ClassNode type, final Expression expression) {
         CastExpression answer = new CastExpression(type, expression);
         answer.setCoerce(true);
         return answer;
     }
 
-    public CastExpression(ClassNode type, Expression expression) {
-        this(type,expression,false);
+    public CastExpression(final ClassNode type, final Expression expression) {
+        this(type, expression, false);
     }
 
-    public CastExpression(ClassNode type, Expression expression, boolean ignoreAutoboxing) {
-        super.setType(type);
+    public CastExpression(final ClassNode type, final Expression expression, final boolean ignoreAutoboxing) {
+        this.setType(type);
         this.expression = expression;
         this.ignoreAutoboxing = ignoreAutoboxing;
     }
-    
-    public boolean isIgnoringAutoboxing(){
+
+    public Expression getExpression() {
+        return expression;
+    }
+
+    public boolean isIgnoringAutoboxing() {
         return ignoreAutoboxing;
     }
 
@@ -55,53 +60,54 @@ public class CastExpression extends Expression {
         return coerce;
     }
 
-    public void setCoerce(boolean coerce) {
+    public void setCoerce(final boolean coerce) {
         this.coerce = coerce;
     }
 
     /**
-     * If strict mode is true, then when the compiler generates a cast, it will disable
-     * Groovy casts and rely on a strict cast (CHECKCAST)
-     * @return true if strict mode is enable
+     * If strict mode is true, then when the compiler generates a cast, it will
+     * disable Groovy casts and rely on a strict cast (CHECKCAST).
      */
     public boolean isStrict() {
         return strict;
     }
 
     /**
-     * If strict mode is true, then when the compiler generates a cast, it will disable
-     * Groovy casts and rely on a strict cast (CHECKCAST)
-     * @param strict strict mode
+     * If strict mode is true, then when the compiler generates a cast, it will
+     * disable Groovy casts and rely on a strict cast (CHECKCAST).
      */
     public void setStrict(final boolean strict) {
         this.strict = strict;
     }
 
+    @Override
     public String toString() {
-        return super.toString() +"[(" + getType().getName() + ") " + expression + "]";
+        return super.toString() + "[" + getText() + "]";
     }
 
-    public void visit(GroovyCodeVisitor visitor) {
+    @Override
+    public void visit(final GroovyCodeVisitor visitor) {
         visitor.visitCastExpression(this);
     }
 
-    public Expression transformExpression(ExpressionTransformer transformer) {
-        CastExpression ret =  new CastExpression(getType(), transformer.transform(expression));
-        ret.setSourcePosition(this);
+    @Override
+    public Expression transformExpression(final ExpressionTransformer transformer) {
+        CastExpression ret = new CastExpression(getType(), transformer.transform(expression), isIgnoringAutoboxing());
         ret.setCoerce(this.isCoerce());
         ret.setStrict(this.isStrict());
+        ret.setSourcePosition(this);
         ret.copyNodeMetaData(this);
         return ret;
     }
-    
+
+    @Override
     public String getText() {
-        return "(" + getType() + ") " + expression.getText();
+        if (isCoerce()) {
+            return expression.getText() + " as " + getType().toString(false);
+        }
+        return "(" + getType().toString(false) + ") " + expression.getText();
     }
- 
-    public Expression getExpression() {
-        return expression;
-    }
-    
+
     @Override
     public void setType(final ClassNode type) {
         super.setType(java.util.Objects.requireNonNull(type)); // GROOVY-9739