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 16:42:59 UTC

[groovy] branch GROOVY_2_5_X updated: improve `getText()` for `ASTNode` and `BytecodeExpression`

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


The following commit(s) were added to refs/heads/GROOVY_2_5_X by this push:
     new 6a52be93b2 improve `getText()` for `ASTNode` and `BytecodeExpression`
6a52be93b2 is described below

commit 6a52be93b203a2bf964d77bbf38b36e3ebca5be6
Author: Eric Milles <er...@thomsonreuters.com>
AuthorDate: Tue Nov 15 09:50:51 2022 -0600

    improve `getText()` for `ASTNode` and `BytecodeExpression`
    
    2_5_X backport
---
 src/main/java/org/codehaus/groovy/ast/ASTNode.java | 32 ++++++++++++----------
 .../groovy/classgen/BytecodeExpression.java        | 28 +++++++++++--------
 2 files changed, 34 insertions(+), 26 deletions(-)

diff --git a/src/main/java/org/codehaus/groovy/ast/ASTNode.java b/src/main/java/org/codehaus/groovy/ast/ASTNode.java
index e171f793e8..6827851d67 100644
--- a/src/main/java/org/codehaus/groovy/ast/ASTNode.java
+++ b/src/main/java/org/codehaus/groovy/ast/ASTNode.java
@@ -51,19 +51,21 @@ public class ASTNode {
     private int lastColumnNumber = -1;
     private ListHashMap metaDataMap = null;
 
-    public void visit(GroovyCodeVisitor visitor) {
+    public void visit(final GroovyCodeVisitor visitor) {
         throw new RuntimeException("No visit() method implemented for class: " + getClass().getName());
     }
 
     public String getText() {
-        return "<not implemented yet for class: " + getClass().getName() + ">";
+        Class<?> nodeType = getClass();
+        if (nodeType.isAnonymousClass()) nodeType = nodeType.getSuperclass();
+        return "<not implemented yet for class: " + nodeType.getName() + ">";
     }
 
     public int getLineNumber() {
         return lineNumber;
     }
 
-    public void setLineNumber(int lineNumber) {
+    public void setLineNumber(final int lineNumber) {
         this.lineNumber = lineNumber;
     }
 
@@ -71,7 +73,7 @@ public class ASTNode {
         return columnNumber;
     }
 
-    public void setColumnNumber(int columnNumber) {
+    public void setColumnNumber(final int columnNumber) {
         this.columnNumber = columnNumber;
     }
 
@@ -79,7 +81,7 @@ public class ASTNode {
         return lastLineNumber;
     }
 
-    public void setLastLineNumber(int lastLineNumber) {
+    public void setLastLineNumber(final int lastLineNumber) {
         this.lastLineNumber = lastLineNumber;
     }
 
@@ -87,7 +89,7 @@ public class ASTNode {
         return lastColumnNumber;
     }
 
-    public void setLastColumnNumber(int lastColumnNumber) {
+    public void setLastColumnNumber(final int lastColumnNumber) {
         this.lastColumnNumber = lastColumnNumber;
     }
 
@@ -99,7 +101,7 @@ public class ASTNode {
      *
      * @param node - the node used to configure the position information
      */
-    public void setSourcePosition(ASTNode node) {
+    public void setSourcePosition(final ASTNode node) {
         this.lineNumber = node.getLineNumber();
         this.columnNumber = node.getColumnNumber();
         this.lastLineNumber = node.getLastLineNumber();
@@ -112,7 +114,7 @@ public class ASTNode {
      * @param key - the meta data key
      * @return the node meta data value for this key
      */
-    public <T> T getNodeMetaData(Object key) {
+    public <T> T getNodeMetaData(final Object key) {
         if (metaDataMap == null) {
             return (T) null;
         }
@@ -123,7 +125,7 @@ public class ASTNode {
      * Copies all node meta data from the other node to this one
      * @param other - the other node
      */
-    public void copyNodeMetaData(ASTNode other) {
+    public void copyNodeMetaData(final ASTNode other) {
         if (other.metaDataMap == null) {
             return;
         }
@@ -141,8 +143,8 @@ public class ASTNode {
      * @throws GroovyBugError if key is null or there is already meta
      *                        data under that key
      */
-    public void setNodeMetaData(Object key, Object value) {
-        Object old = putNodeMetaData(key,value);
+    public void setNodeMetaData(final Object key, final Object value) {
+        Object old = putNodeMetaData(key, value);
         if (old != null) throw new GroovyBugError("Tried to replace existing meta data on " + this + ".");
     }
 
@@ -154,7 +156,7 @@ public class ASTNode {
      * @return the old node meta data value for this key
      * @throws GroovyBugError if key is null
      */
-    public Object putNodeMetaData(Object key, Object value) {
+    public Object putNodeMetaData(final Object key, final Object value) {
         if (key == null) throw new GroovyBugError("Tried to set meta data with null key on " + this + ".");
         if (metaDataMap == null) {
             metaDataMap = new ListHashMap();
@@ -168,8 +170,8 @@ public class ASTNode {
      * @param key - the meta data key
      * @throws GroovyBugError if the key is null
      */
-    public void removeNodeMetaData(Object key) {
-        if (key==null) throw new GroovyBugError("Tried to remove meta data with null key "+this+".");
+    public void removeNodeMetaData(final Object key) {
+        if (key == null) throw new GroovyBugError("Tried to remove meta data with null key " + this + ".");
         if (metaDataMap == null) {
             return;
         }
@@ -181,7 +183,7 @@ public class ASTNode {
      * @return the node metadata. Always not null.
      */
     public Map<?,?> getNodeMetaData() {
-        if (metaDataMap==null) {
+        if (metaDataMap == null) {
             return Collections.emptyMap();
         }
         return Collections.unmodifiableMap(metaDataMap);
diff --git a/src/main/java/org/codehaus/groovy/classgen/BytecodeExpression.java b/src/main/java/org/codehaus/groovy/classgen/BytecodeExpression.java
index 670e4841c4..802fb9451f 100644
--- a/src/main/java/org/codehaus/groovy/classgen/BytecodeExpression.java
+++ b/src/main/java/org/codehaus/groovy/classgen/BytecodeExpression.java
@@ -25,29 +25,35 @@ import org.codehaus.groovy.ast.expr.ExpressionTransformer;
 import org.objectweb.asm.MethodVisitor;
 
 /**
- * Represents some custom bytecode generation by the compiler
+ * Represents some custom bytecode generation by the compiler.
  */
 public abstract class BytecodeExpression extends Expression {
+
     public static final BytecodeExpression NOP = new BytecodeExpression() {
-        public void visit(MethodVisitor visitor) {
-            //do nothing             
+        public void visit(final MethodVisitor visitor) {
+            // do nothing
         }
     };
 
     public BytecodeExpression() {
     }
-    
-    public BytecodeExpression(ClassNode type) {
-        super.setType(type);
+
+    public BytecodeExpression(final ClassNode type) {
+        setType(type);
     }
-    
-    public void visit(GroovyCodeVisitor visitor) {
-        visitor.visitBytecodeExpression(this);
+
+    @Override
+    public String getText() {
+        return "<bytecode sequence>";
     }
 
-    public abstract void visit(MethodVisitor mv);
+    public abstract void visit(MethodVisitor visitor);
+
+    public void visit(final GroovyCodeVisitor visitor) {
+        visitor.visitBytecodeExpression(this);
+    }
 
-    public Expression transformExpression(ExpressionTransformer transformer) {
+    public Expression transformExpression(final ExpressionTransformer transformer) {
         return this;
     }
 }