From 7a98df59f6521179da2213c74eaf05e969550cad Mon Sep 17 00:00:00 2001 From: Eric Milles Date: Mon, 13 May 2024 14:39:36 -0500 Subject: [PATCH] GROOVY-11374 --- .../groovy/ast/expr/MethodCallExpression.java | 43 +++++++++++++++++-- .../groovy/ast/expr/MethodCallExpression.java | 43 +++++++++++++++++-- .../groovy/ast/expr/MethodCallExpression.java | 43 +++++++++++++++++-- 3 files changed, 120 insertions(+), 9 deletions(-) diff --git a/base/org.codehaus.groovy30/src/org/codehaus/groovy/ast/expr/MethodCallExpression.java b/base/org.codehaus.groovy30/src/org/codehaus/groovy/ast/expr/MethodCallExpression.java index cca1469bf0..4c957a1804 100644 --- a/base/org.codehaus.groovy30/src/org/codehaus/groovy/ast/expr/MethodCallExpression.java +++ b/base/org.codehaus.groovy30/src/org/codehaus/groovy/ast/expr/MethodCallExpression.java @@ -28,6 +28,8 @@ import java.util.Collections; import java.util.List; +import static org.codehaus.groovy.tools.Utilities.isJavaIdentifier; + /** * A method call on an object or class. */ @@ -43,7 +45,6 @@ public class MethodCallExpression extends Expression implements MethodCall { // type spec for generics private GenericsType[] genericsTypes; - private boolean usesGenerics; private MethodNode target; @@ -132,12 +133,49 @@ public ASTNode getReceiver() { } public String getText() { + /* GRECLIPSE edit -- GROOVY-11374 String object = objectExpression.getText(); String meth = method.getText(); String args = arguments.getText(); String spread = spreadSafe ? "*" : ""; String dereference = safe ? "?" : ""; return object + spread + dereference + "." + meth + args; + */ + StringBuilder builder = new StringBuilder( 64 ); + builder.append(getObjectExpression().getText()); + if (isSpreadSafe()) builder.append('*'); + if (isSafe()) builder.append('?'); + builder.append('.'); + + if (isUsingGenerics()) { + builder.append('<'); + boolean first = true; + for (GenericsType t : getGenericsTypes()) { + if (!first) builder.append(", "); + else first = false; + builder.append(t); + } + builder.append('>'); + } + + Expression method = getMethod(); + if (method instanceof GStringExpression) { + builder.append('"').append(method.getText()).append('"'); + } else if (!(method instanceof ConstantExpression)) { + builder.append('(').append(method.getText()).append(')'); + } else { + Object value = ((ConstantExpression) method).getValue(); + if (!(value instanceof String) || !isJavaIdentifier((String) value)) { + builder.append("'").append(value).append("'"); + } else { + builder.append((String) value); + } + } + + builder.append(getArguments().getText()); + + return builder.toString(); + // GRECLIPSE end } /** @@ -178,12 +216,11 @@ public GenericsType[] getGenericsTypes() { } public void setGenericsTypes(GenericsType[] genericsTypes) { - usesGenerics = usesGenerics || genericsTypes != null; this.genericsTypes = genericsTypes; } public boolean isUsingGenerics() { - return usesGenerics; + return (genericsTypes != null && genericsTypes.length > 0); } /** diff --git a/base/org.codehaus.groovy40/src/org/codehaus/groovy/ast/expr/MethodCallExpression.java b/base/org.codehaus.groovy40/src/org/codehaus/groovy/ast/expr/MethodCallExpression.java index bf1d181c29..1dd101214c 100644 --- a/base/org.codehaus.groovy40/src/org/codehaus/groovy/ast/expr/MethodCallExpression.java +++ b/base/org.codehaus.groovy40/src/org/codehaus/groovy/ast/expr/MethodCallExpression.java @@ -28,6 +28,8 @@ import java.util.Collections; import java.util.List; +import static org.codehaus.groovy.tools.Utilities.isJavaIdentifier; + /** * A method call on an object or class. */ @@ -43,7 +45,6 @@ public class MethodCallExpression extends Expression implements MethodCall { // type spec for generics private GenericsType[] genericsTypes; - private boolean usesGenerics; private MethodNode target; @@ -138,12 +139,49 @@ public ASTNode getReceiver() { @Override public String getText() { + /* GRECLIPSE edit -- GROOVY-11374 String object = objectExpression.getText(); String meth = method.getText(); String args = arguments.getText(); String spread = spreadSafe ? "*" : ""; String dereference = safe ? "?" : ""; return object + spread + dereference + "." + meth + args; + */ + StringBuilder builder = new StringBuilder( 64 ); + builder.append(getObjectExpression().getText()); + if (isSpreadSafe()) builder.append('*'); + if (isSafe()) builder.append('?'); + builder.append('.'); + + if (isUsingGenerics()) { + builder.append('<'); + boolean first = true; + for (GenericsType t : getGenericsTypes()) { + if (!first) builder.append(", "); + else first = false; + builder.append(t); + } + builder.append('>'); + } + + Expression method = getMethod(); + if (method instanceof GStringExpression) { + builder.append('"').append(method.getText()).append('"'); + } else if (!(method instanceof ConstantExpression)) { + builder.append('(').append(method.getText()).append(')'); + } else { + Object value = ((ConstantExpression) method).getValue(); + if (!(value instanceof String) || !isJavaIdentifier((String) value)) { + builder.append("'").append(value).append("'"); + } else { + builder.append((String) value); + } + } + + builder.append(getArguments().getText()); + + return builder.toString(); + // GRECLIPSE end } /** @@ -184,12 +222,11 @@ public GenericsType[] getGenericsTypes() { } public void setGenericsTypes(GenericsType[] genericsTypes) { - usesGenerics = usesGenerics || genericsTypes != null; this.genericsTypes = genericsTypes; } public boolean isUsingGenerics() { - return usesGenerics; + return (genericsTypes != null && genericsTypes.length > 0); } /** diff --git a/base/org.codehaus.groovy50/src/org/codehaus/groovy/ast/expr/MethodCallExpression.java b/base/org.codehaus.groovy50/src/org/codehaus/groovy/ast/expr/MethodCallExpression.java index bf1d181c29..1dd101214c 100644 --- a/base/org.codehaus.groovy50/src/org/codehaus/groovy/ast/expr/MethodCallExpression.java +++ b/base/org.codehaus.groovy50/src/org/codehaus/groovy/ast/expr/MethodCallExpression.java @@ -28,6 +28,8 @@ import java.util.Collections; import java.util.List; +import static org.codehaus.groovy.tools.Utilities.isJavaIdentifier; + /** * A method call on an object or class. */ @@ -43,7 +45,6 @@ public class MethodCallExpression extends Expression implements MethodCall { // type spec for generics private GenericsType[] genericsTypes; - private boolean usesGenerics; private MethodNode target; @@ -138,12 +139,49 @@ public ASTNode getReceiver() { @Override public String getText() { + /* GRECLIPSE edit -- GROOVY-11374 String object = objectExpression.getText(); String meth = method.getText(); String args = arguments.getText(); String spread = spreadSafe ? "*" : ""; String dereference = safe ? "?" : ""; return object + spread + dereference + "." + meth + args; + */ + StringBuilder builder = new StringBuilder( 64 ); + builder.append(getObjectExpression().getText()); + if (isSpreadSafe()) builder.append('*'); + if (isSafe()) builder.append('?'); + builder.append('.'); + + if (isUsingGenerics()) { + builder.append('<'); + boolean first = true; + for (GenericsType t : getGenericsTypes()) { + if (!first) builder.append(", "); + else first = false; + builder.append(t); + } + builder.append('>'); + } + + Expression method = getMethod(); + if (method instanceof GStringExpression) { + builder.append('"').append(method.getText()).append('"'); + } else if (!(method instanceof ConstantExpression)) { + builder.append('(').append(method.getText()).append(')'); + } else { + Object value = ((ConstantExpression) method).getValue(); + if (!(value instanceof String) || !isJavaIdentifier((String) value)) { + builder.append("'").append(value).append("'"); + } else { + builder.append((String) value); + } + } + + builder.append(getArguments().getText()); + + return builder.toString(); + // GRECLIPSE end } /** @@ -184,12 +222,11 @@ public GenericsType[] getGenericsTypes() { } public void setGenericsTypes(GenericsType[] genericsTypes) { - usesGenerics = usesGenerics || genericsTypes != null; this.genericsTypes = genericsTypes; } public boolean isUsingGenerics() { - return usesGenerics; + return (genericsTypes != null && genericsTypes.length > 0); } /**