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/12/23 22:39:36 UTC
[groovy] branch master updated: GROOVY-10417: MethodNode toString() could add quotes when name contains spaces (or other non-identifier characters)
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 ada187b GROOVY-10417: MethodNode toString() could add quotes when name contains spaces (or other non-identifier characters)
ada187b is described below
commit ada187bcde382650f550729a2a81c5308adc08cc
Author: Paul King <pa...@asert.com.au>
AuthorDate: Tue Dec 21 22:04:01 2021 +1000
GROOVY-10417: MethodNode toString() could add quotes when name contains spaces (or other non-identifier characters)
---
.../org/apache/groovy/ast/tools/MethodNodeUtils.java | 16 +++++++++++++++-
src/main/java/org/codehaus/groovy/ast/MethodNode.java | 16 +++++++++++++---
2 files changed, 28 insertions(+), 4 deletions(-)
diff --git a/src/main/java/org/apache/groovy/ast/tools/MethodNodeUtils.java b/src/main/java/org/apache/groovy/ast/tools/MethodNodeUtils.java
index 00b639d..2f3029e 100644
--- a/src/main/java/org/apache/groovy/ast/tools/MethodNodeUtils.java
+++ b/src/main/java/org/apache/groovy/ast/tools/MethodNodeUtils.java
@@ -59,12 +59,26 @@ public class MethodNodeUtils {
* @return the method node's descriptor
*/
public static String methodDescriptor(final MethodNode mNode) {
+ return methodDescriptor(mNode, false);
+ }
+
+ /**
+ * Return the method node's descriptor which includes its return type,
+ * name and parameter types without generics.
+ *
+ * @param mNode the method node
+ * @param pretty whether to quote a name with spaces
+ * @return the method node's descriptor
+ */
+ public static String methodDescriptor(final MethodNode mNode, final boolean pretty) {
Parameter[] parameters = mNode.getParameters();
int nParameters = parameters == null ? 0 : parameters.length;
+ String name = mNode.getName();
+ String prettyName = name.contains(" ") && pretty ? "\"" + name + "\"" : name;
StringBuilder sb = new StringBuilder(mNode.getName().length() * 2 + nParameters * 10);
sb.append(mNode.getReturnType().getName());
sb.append(' ');
- sb.append(mNode.getName());
+ sb.append(prettyName);
sb.append('(');
for (int i = 0; i < nParameters; i += 1) {
if (i > 0) {
diff --git a/src/main/java/org/codehaus/groovy/ast/MethodNode.java b/src/main/java/org/codehaus/groovy/ast/MethodNode.java
index 5a9b4c2..82b1a1a 100644
--- a/src/main/java/org/codehaus/groovy/ast/MethodNode.java
+++ b/src/main/java/org/codehaus/groovy/ast/MethodNode.java
@@ -78,11 +78,20 @@ public class MethodNode extends AnnotatedNode {
*/
public String getTypeDescriptor() {
if (typeDescriptor == null) {
- typeDescriptor = methodDescriptor(this);
+ typeDescriptor = methodDescriptor(this, false);
}
return typeDescriptor;
}
+ /**
+ * The type descriptor for a method node is a string containing the name of the method, its return type,
+ * and its parameter types in a canonical form. For simplicity, we use the format of a Java declaration
+ * without parameter names or generics.
+ */
+ public String getTypeDescriptor(final boolean pretty) {
+ return methodDescriptor(this, pretty);
+ }
+
private void invalidateCachedData() {
typeDescriptor = null;
}
@@ -282,12 +291,13 @@ public class MethodNode extends AnnotatedNode {
@Override
public String getText() {
int mask = this instanceof ConstructorNode ? Modifier.constructorModifiers() : Modifier.methodModifiers();
+ String prettyName = getName().contains(" ") ? "\"" + getName() + "\"" : getName();
return new StringBuilder(AstToTextHelper.getModifiersText(getModifiers() & mask))
.append(' ')
.append(toGenericTypesString(getGenericsTypes()))
.append(AstToTextHelper.getClassText(getReturnType()))
.append(' ')
- .append(getName())
+ .append(prettyName)
.append('(')
.append(AstToTextHelper.getParametersText(getParameters()))
.append(')')
@@ -299,6 +309,6 @@ public class MethodNode extends AnnotatedNode {
@Override
public String toString() {
ClassNode declaringClass = getDeclaringClass();
- return super.toString() + "[" + getTypeDescriptor() + (declaringClass == null ? "" : " from " + formatTypeName(declaringClass)) + "]";
+ return super.toString() + "[" + getTypeDescriptor(true) + (declaringClass == null ? "" : " from " + formatTypeName(declaringClass)) + "]";
}
}