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:43 UTC
[groovy] 02/02: `CastExpression`: indicate coercion in `getText()` and `toString()`
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