This is an automated email from the ASF dual-hosted git repository.
emilles 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 7c3e525 minor refactor
7c3e525 is described below
commit 7c3e525948ba6c8ee7e7dbb55b0d90e14eb0616a
Author: Eric Milles <[email protected]>
AuthorDate: Thu Oct 10 10:36:41 2019 -0500
minor refactor
---
.../org/codehaus/groovy/antlr/SourceBuffer.java | 79 ++++++++-----
src/main/java/org/codehaus/groovy/ast/ASTNode.java | 38 +++---
.../org/codehaus/groovy/ast/AnnotatedNode.java | 107 ++++++++---------
.../groovy/ast/ClassCodeVisitorSupport.java | 130 ++++++++++++---------
.../java/org/codehaus/groovy/ast/FieldNode.java | 14 ++-
5 files changed, 203 insertions(+), 165 deletions(-)
diff --git a/src/main/java/org/codehaus/groovy/antlr/SourceBuffer.java
b/src/main/java/org/codehaus/groovy/antlr/SourceBuffer.java
index 6498499..9a1f366 100644
--- a/src/main/java/org/codehaus/groovy/antlr/SourceBuffer.java
+++ b/src/main/java/org/codehaus/groovy/antlr/SourceBuffer.java
@@ -26,28 +26,28 @@ import java.util.List;
* held within itself.
*/
public class SourceBuffer {
- private final List<StringBuilder> lines;
- private StringBuilder current;
+ private final List<StringBuilder> lines = new ArrayList<>();
+ private StringBuilder current = new StringBuilder();
public SourceBuffer() {
- lines = new ArrayList<StringBuilder>();
- //lines.add(new StringBuilder()); // dummy row for position [0] in the
List
-
- current = new StringBuilder();
lines.add(current);
}
/**
- * Obtains a snippet of the source code within the bounds specified
- * @param start (inclusive line/ inclusive column)
- * @param end (inclusive line / exclusive column)
+ * Obtains a snippet of the source code within the bounds specified.
+ *
+ * @param start (inclusive line / inclusive column)
+ * @param end (inclusive line / exclusive column)
* @return specified snippet of source code as a String, or null if no
source available
*/
public String getSnippet(LineColumn start, LineColumn end) {
// preconditions
- if (start == null || end == null) { return null; } // no text to return
- if (start.equals(end)) { return null; } // no text to return
- if (lines.size() == 1 && current.length() == 0) { return null; } //
buffer hasn't been filled yet
+ if (start == null || end == null || start.equals(end)) {
+ return null; // no text to return
+ }
+ if (lines.size() == 1 && current.length() == 0) {
+ return null; // buffer hasn't been filled yet
+ }
// working variables
int startLine = start.getLine();
@@ -56,24 +56,46 @@ public class SourceBuffer {
int endColumn = end.getColumn();
// reset any out of bounds requests
- if (startLine < 1) { startLine = 1;}
- if (endLine < 1) { endLine = 1;}
- if (startColumn < 1) { startColumn = 1;}
- if (endColumn < 1) { endColumn = 1;}
- if (startLine > lines.size()) { startLine = lines.size(); }
- if (endLine > lines.size()) { endLine = lines.size(); }
+ if (startLine < 1) {
+ startLine = 1;
+ }
+ if (endLine < 1) {
+ endLine = 1;
+ }
+ if (startColumn < 1) {
+ startColumn = 1;
+ }
+ if (endColumn < 1) {
+ endColumn = 1;
+ }
+ if (startLine > lines.size()) {
+ startLine = lines.size();
+ }
+ if (endLine > lines.size()) {
+ endLine = lines.size();
+ }
// obtain the snippet from the buffer within specified bounds
StringBuilder snippet = new StringBuilder();
- for (int i = startLine - 1; i < endLine;i++) {
- String line = ((StringBuilder)lines.get(i)).toString();
+ for (int i = startLine - 1; i < endLine; i += 1) {
+ String line = lines.get(i).toString();
if (startLine == endLine) {
// reset any out of bounds requests (again)
- if (startColumn > line.length()) { startColumn =
line.length();}
- if (startColumn < 1) { startColumn = 1;}
- if (endColumn > line.length()) { endColumn = line.length() +
1;}
- if (endColumn < 1) { endColumn = 1;}
- if (endColumn < startColumn) { endColumn = startColumn;}
+ if (startColumn > line.length()) {
+ startColumn = line.length();
+ }
+ if (startColumn < 1) {
+ startColumn = 1;
+ }
+ if (endColumn > line.length()) {
+ endColumn = line.length() + 1;
+ }
+ if (endColumn < 1) {
+ endColumn = 1;
+ }
+ if (endColumn < startColumn) {
+ endColumn = startColumn;
+ }
line = line.substring(startColumn - 1, endColumn - 1);
} else {
@@ -84,7 +106,7 @@ public class SourceBuffer {
}
if (i == endLine - 1) {
if (endColumn - 1 < line.length()) {
- line = line.substring(0,endColumn - 1);
+ line = line.substring(0, endColumn - 1);
}
}
}
@@ -94,12 +116,11 @@ public class SourceBuffer {
}
/**
- * Writes the specified character into the buffer
- * @param c
+ * Writes the specified character into the buffer.
*/
public void write(int c) {
if (c != -1) {
- current.append((char)c);
+ current.append((char) c);
}
if (c == '\n') {
current = new StringBuilder();
diff --git a/src/main/java/org/codehaus/groovy/ast/ASTNode.java
b/src/main/java/org/codehaus/groovy/ast/ASTNode.java
index 93124f5..39b9f8c 100644
--- a/src/main/java/org/codehaus/groovy/ast/ASTNode.java
+++ b/src/main/java/org/codehaus/groovy/ast/ASTNode.java
@@ -18,10 +18,8 @@
*/
package org.codehaus.groovy.ast;
-import org.codehaus.groovy.util.ListHashMap;
-
+import java.util.Arrays;
import java.util.Map;
-import java.util.Objects;
/**
* Base class for any AST node. This class supports basic information used in
all nodes of the AST:
@@ -29,15 +27,15 @@ import java.util.Objects;
* <li> line and column number information. Usually a node represents a certain
* area in a text file determined by a starting position and an ending
position.
* For nodes that do not represent this, this information will be -1. A node
can
- * also be configured in its line/col information using another node through
+ * also be configured in its line/col information using another node through
* setSourcePosition(otherNode).</li>
- * <li> every node can store meta data. A phase operation or transform can use
- * this to transport arbitrary information to another phase operation or
+ * <li> every node can store meta data. A phase operation or transform can use
+ * this to transport arbitrary information to another phase operation or
* transform. The only requirement is that the other phase operation or
transform
- * runs after the part storing the information. If the information transport
is
- * done it is strongly recommended to remove that meta data.</li>
+ * runs after the part storing the information. If the information transport is
+ * done it is strongly recommended to remove that meta data.</li>
* <li> a text representation of this node trough getText(). This was in the
- * past used for assertion messages. Since the usage of power asserts this
+ * past used for assertion messages. Since the usage of power asserts this
* method will not be called for this purpose anymore and might be removed in
* future versions of Groovy</li>
* </ul>
@@ -48,7 +46,8 @@ public class ASTNode implements NodeMetaDataHandler {
private int columnNumber = -1;
private int lastLineNumber = -1;
private int lastColumnNumber = -1;
- private Map metaDataMap = null;
+
+ private Map<?, ?> metaDataMap;
public void visit(GroovyCodeVisitor visitor) {
throw new RuntimeException("No visit() method implemented for class: "
+ getClass().getName());
@@ -89,20 +88,20 @@ public class ASTNode implements NodeMetaDataHandler {
public void setLastColumnNumber(int lastColumnNumber) {
this.lastColumnNumber = lastColumnNumber;
}
-
+
/**
* Sets the source position using another ASTNode.
* The sourcePosition consists of a line/column pair for
* the start and a line/column pair for the end of the
- * expression or statement
- *
+ * expression or statement
+ *
* @param node - the node used to configure the position information
*/
public void setSourcePosition(ASTNode node) {
+ this.lineNumber = node.getLineNumber();
this.columnNumber = node.getColumnNumber();
this.lastLineNumber = node.getLastLineNumber();
this.lastColumnNumber = node.getLastColumnNumber();
- this.lineNumber = node.getLineNumber();
}
/**
@@ -114,8 +113,8 @@ public class ASTNode implements NodeMetaDataHandler {
}
@Override
- public ListHashMap getMetaDataMap() {
- return (ListHashMap) metaDataMap;
+ public Map<?, ?> getMetaDataMap() {
+ return metaDataMap;
}
@Override
@@ -124,12 +123,7 @@ public class ASTNode implements NodeMetaDataHandler {
}
@Override
- public boolean equals(Object o) {
- return this == o;
- }
-
- @Override
public int hashCode() {
- return Objects.hash(lineNumber, columnNumber, lastLineNumber,
lastColumnNumber);
+ return Arrays.hashCode(new int[] {lineNumber, columnNumber,
lastLineNumber, lastColumnNumber});
}
}
diff --git a/src/main/java/org/codehaus/groovy/ast/AnnotatedNode.java
b/src/main/java/org/codehaus/groovy/ast/AnnotatedNode.java
index a4264bf..10c12db 100644
--- a/src/main/java/org/codehaus/groovy/ast/AnnotatedNode.java
+++ b/src/main/java/org/codehaus/groovy/ast/AnnotatedNode.java
@@ -30,96 +30,89 @@ import java.util.List;
*/
public class AnnotatedNode extends ASTNode implements
GroovydocHolder<AnnotatedNode> {
private List<AnnotationNode> annotations = Collections.emptyList();
+ private ClassNode declaringClass;
private boolean synthetic;
- ClassNode declaringClass;
- private boolean hasNoRealSourcePositionFlag;
-
- public AnnotatedNode() {
- }
public List<AnnotationNode> getAnnotations() {
return annotations;
}
public List<AnnotationNode> getAnnotations(ClassNode type) {
- List<AnnotationNode> ret = new
ArrayList<AnnotationNode>(annotations.size());
- for (AnnotationNode node: annotations) {
- if (type.equals(node.getClassNode())) ret.add(node);
+ List<AnnotationNode> ret = new ArrayList<>(annotations.size());
+ for (AnnotationNode node : annotations) {
+ if (type.equals(node.getClassNode())) {
+ ret.add(node);
+ }
}
return ret;
}
- public void addAnnotation(AnnotationNode value) {
- checkInit();
- annotations.add(value);
- }
-
- private void checkInit() {
- if (annotations == Collections.EMPTY_LIST)
- annotations = new ArrayList<AnnotationNode>(3);
+ public void addAnnotation(AnnotationNode annotation) {
+ if (annotation != null) {
+ if (annotations == Collections.EMPTY_LIST) {
+ annotations = new ArrayList<>(3);
+ }
+ annotations.add(annotation);
+ }
}
public void addAnnotations(List<AnnotationNode> annotations) {
- for (AnnotationNode node : annotations) {
- addAnnotation(node);
+ for (AnnotationNode annotation : annotations) {
+ addAnnotation(annotation);
}
}
- /**
- * returns true if this node is added by the compiler.
- * <b>NOTE</b>:
- * This method has nothing to do with the synthetic flag
- * for fields, methods or classes.
- * @return true if this node is added by the compiler
- */
- public boolean isSynthetic() {
- return synthetic;
+ public /*@Nullable*/ ClassNode getDeclaringClass() {
+ return declaringClass;
}
- /**
- * sets this node as a node added by the compiler.
- * <b>NOTE</b>:
- * This method has nothing to do with the synthetic flag
- * for fields, methods or classes.
- * @param synthetic - if true this node is marked as
- * added by the compiler
- */
- public void setSynthetic(boolean synthetic) {
- this.synthetic = synthetic;
+ public void setDeclaringClass(ClassNode declaringClass) {
+ this.declaringClass = declaringClass;
}
- public ClassNode getDeclaringClass() {
- return declaringClass;
+ @Override
+ public Groovydoc getGroovydoc() {
+ Groovydoc groovydoc = getNodeMetaData(DOC_COMMENT);
+ return (groovydoc != null ? groovydoc : Groovydoc.EMPTY_GROOVYDOC);
}
- /**
- * @param declaringClass - The declaringClass to set.
- */
- public void setDeclaringClass(ClassNode declaringClass) {
- this.declaringClass = declaringClass;
+ @Override
+ public AnnotatedNode getInstance() {
+ return this;
}
/**
- * Currently only ever returns true for default constructors
- * added by the compiler. See GROOVY-4161.
+ * Returns true for default constructors added by the compiler.
+ * <p>
+ * See GROOVY-4161
*/
public boolean hasNoRealSourcePosition() {
- return hasNoRealSourcePositionFlag;
+ return
Boolean.TRUE.equals(getNodeMetaData("org.codehaus.groovy.ast.AnnotatedNode.hasNoRealSourcePosition"));
}
- public void setHasNoRealSourcePosition(boolean value) {
- this.hasNoRealSourcePositionFlag = value;
+ public void setHasNoRealSourcePosition(boolean hasNoRealSourcePosition) {
+ if (hasNoRealSourcePosition) {
+
putNodeMetaData("org.codehaus.groovy.ast.AnnotatedNode.hasNoRealSourcePosition",
Boolean.TRUE);
+ } else {
+
removeNodeMetaData("org.codehaus.groovy.ast.AnnotatedNode.hasNoRealSourcePosition");
+ }
}
- @Override
- public Groovydoc getGroovydoc() {
- Groovydoc groovydoc = this.<Groovydoc>getNodeMetaData(DOC_COMMENT);
-
- return null == groovydoc ? Groovydoc.EMPTY_GROOVYDOC : groovydoc;
+ /**
+ * Indicates if this node was added by the compiler.
+ * <p>
+ * <b>Note</b>: This method has nothing to do with the synthetic flag for
classes, fields, methods or properties.
+ */
+ public boolean isSynthetic() {
+ return synthetic;
}
- @Override
- public AnnotatedNode getInstance() {
- return this;
+ /**
+ * Sets this node as a node added by the compiler.
+ * <p>
+ * <b>Note</b>: This method has nothing to do with the synthetic flag for
classes, fields, methods or properties.
+ */
+ public void setSynthetic(boolean synthetic) {
+ this.synthetic = synthetic;
}
}
diff --git a/src/main/java/org/codehaus/groovy/ast/ClassCodeVisitorSupport.java
b/src/main/java/org/codehaus/groovy/ast/ClassCodeVisitorSupport.java
index 378d2be..15dfa9e 100644
--- a/src/main/java/org/codehaus/groovy/ast/ClassCodeVisitorSupport.java
+++ b/src/main/java/org/codehaus/groovy/ast/ClassCodeVisitorSupport.java
@@ -42,7 +42,6 @@ import
org.codehaus.groovy.control.messages.SyntaxErrorMessage;
import org.codehaus.groovy.syntax.SyntaxException;
import org.codehaus.groovy.transform.ErrorCollecting;
-import java.util.List;
import java.util.Map;
public abstract class ClassCodeVisitorSupport extends CodeVisitorSupport
implements ErrorCollecting, GroovyClassVisitor {
@@ -54,10 +53,14 @@ public abstract class ClassCodeVisitorSupport extends
CodeVisitorSupport impleme
node.visitContents(this);
visitObjectInitializerStatements(node);
}
-
- protected void visitObjectInitializerStatements(ClassNode node) {
- for (Statement element : node.getObjectInitializerStatements()) {
- element.visit(this);
+
+ public void visitAnnotations(AnnotatedNode node) {
+ for (AnnotationNode annotation : node.getAnnotations()) {
+ // skip built-in properties
+ if (annotation.isBuiltIn()) continue;
+ for (Map.Entry<String, Expression> member :
annotation.getMembers().entrySet()) {
+ member.getValue().visit(this);
+ }
}
}
@@ -89,31 +92,14 @@ public abstract class ClassCodeVisitorSupport extends
CodeVisitorSupport impleme
}
}
- public void visitAnnotations(AnnotatedNode node) {
- List<AnnotationNode> annotations = node.getAnnotations();
- if (annotations.isEmpty()) return;
- for (AnnotationNode an : annotations) {
- // skip built-in properties
- if (an.isBuiltIn()) continue;
- for (Map.Entry<String, Expression> member :
an.getMembers().entrySet()) {
- member.getValue().visit(this);
- }
- }
- }
-
- public void visitBlockStatement(BlockStatement block) {
- visitStatement(block);
- super.visitBlockStatement(block);
- }
-
- protected void visitClassCodeContainer(Statement code) {
- if (code != null) code.visit(this);
+ @Override
+ public void visitConstructor(ConstructorNode node) {
+ visitConstructorOrMethod(node, true);
}
@Override
- public void visitDeclarationExpression(DeclarationExpression expression) {
- visitAnnotations(expression);
- super.visitDeclarationExpression(expression);
+ public void visitMethod(MethodNode node) {
+ visitConstructorOrMethod(node, false);
}
protected void visitConstructorOrMethod(MethodNode node, boolean
isConstructor) {
@@ -124,20 +110,14 @@ public abstract class ClassCodeVisitorSupport extends
CodeVisitorSupport impleme
}
}
- public void visitConstructor(ConstructorNode node) {
- visitConstructorOrMethod(node, true);
- }
-
- public void visitMethod(MethodNode node) {
- visitConstructorOrMethod(node, false);
- }
-
+ @Override
public void visitField(FieldNode node) {
visitAnnotations(node);
Expression init = node.getInitialExpression();
if (init != null) init.visit(this);
}
+ @Override
public void visitProperty(PropertyNode node) {
visitAnnotations(node);
Statement statement = node.getGetterBlock();
@@ -150,90 +130,132 @@ public abstract class ClassCodeVisitorSupport extends
CodeVisitorSupport impleme
if (init != null) init.visit(this);
}
- public void addError(String msg, ASTNode expr) {
- SourceUnit source = getSourceUnit();
- source.getErrorCollector().addErrorAndContinue(
- new SyntaxErrorMessage(new SyntaxException(msg + '\n',
expr.getLineNumber(), expr.getColumnNumber(), expr.getLastLineNumber(),
expr.getLastColumnNumber()), source)
- );
+ protected void visitClassCodeContainer(Statement code) {
+ if (code != null) code.visit(this);
}
- protected abstract SourceUnit getSourceUnit();
+ protected void visitObjectInitializerStatements(ClassNode node) {
+ for (Statement statement : node.getObjectInitializerStatements()) {
+ statement.visit(this);
+ }
+ }
- protected void visitStatement(Statement statement) {
+ @Override
+ public void visitDeclarationExpression(DeclarationExpression expression) {
+ visitAnnotations(expression);
+ super.visitDeclarationExpression(expression);
}
+
//--------------------------------------------------------------------------
+
+ @Override
public void visitAssertStatement(AssertStatement statement) {
visitStatement(statement);
super.visitAssertStatement(statement);
}
+ @Override
+ public void visitBlockStatement(BlockStatement statement) {
+ visitStatement(statement);
+ super.visitBlockStatement(statement);
+ }
+
+ @Override
public void visitBreakStatement(BreakStatement statement) {
visitStatement(statement);
super.visitBreakStatement(statement);
}
+ @Override
public void visitCaseStatement(CaseStatement statement) {
visitStatement(statement);
super.visitCaseStatement(statement);
}
+ @Override
public void visitCatchStatement(CatchStatement statement) {
visitStatement(statement);
super.visitCatchStatement(statement);
}
+ @Override
public void visitContinueStatement(ContinueStatement statement) {
visitStatement(statement);
super.visitContinueStatement(statement);
}
- public void visitDoWhileLoop(DoWhileStatement loop) {
- visitStatement(loop);
- super.visitDoWhileLoop(loop);
+ @Override
+ public void visitDoWhileLoop(DoWhileStatement statement) {
+ visitStatement(statement);
+ super.visitDoWhileLoop(statement);
}
+ @Override
public void visitExpressionStatement(ExpressionStatement statement) {
visitStatement(statement);
super.visitExpressionStatement(statement);
}
- public void visitForLoop(ForStatement forLoop) {
- visitStatement(forLoop);
- super.visitForLoop(forLoop);
+ @Override
+ public void visitForLoop(ForStatement statement) {
+ visitStatement(statement);
+ super.visitForLoop(statement);
}
- public void visitIfElse(IfStatement ifElse) {
- visitStatement(ifElse);
- super.visitIfElse(ifElse);
+ @Override
+ public void visitIfElse(IfStatement statement) {
+ visitStatement(statement);
+ super.visitIfElse(statement);
}
+ @Override
public void visitReturnStatement(ReturnStatement statement) {
visitStatement(statement);
super.visitReturnStatement(statement);
}
+ @Override
public void visitSwitch(SwitchStatement statement) {
visitStatement(statement);
super.visitSwitch(statement);
}
+ @Override
public void visitSynchronizedStatement(SynchronizedStatement statement) {
visitStatement(statement);
super.visitSynchronizedStatement(statement);
}
+ @Override
public void visitThrowStatement(ThrowStatement statement) {
visitStatement(statement);
super.visitThrowStatement(statement);
}
+ @Override
public void visitTryCatchFinally(TryCatchStatement statement) {
visitStatement(statement);
super.visitTryCatchFinally(statement);
}
- public void visitWhileLoop(WhileStatement loop) {
- visitStatement(loop);
- super.visitWhileLoop(loop);
+ @Override
+ public void visitWhileLoop(WhileStatement statement) {
+ visitStatement(statement);
+ super.visitWhileLoop(statement);
+ }
+
+
//--------------------------------------------------------------------------
+
+ protected void visitStatement(Statement statement) {
+ }
+
+ protected abstract SourceUnit getSourceUnit();
+
+ @Override
+ public void addError(String error, ASTNode node) {
+ SourceUnit source = getSourceUnit();
+ source.getErrorCollector().addErrorAndContinue(
+ new SyntaxErrorMessage(new SyntaxException(error + '\n',
node.getLineNumber(), node.getColumnNumber(), node.getLastLineNumber(),
node.getLastColumnNumber()), source)
+ );
}
}
diff --git a/src/main/java/org/codehaus/groovy/ast/FieldNode.java
b/src/main/java/org/codehaus/groovy/ast/FieldNode.java
index cae3dcf..cc266e5 100644
--- a/src/main/java/org/codehaus/groovy/ast/FieldNode.java
+++ b/src/main/java/org/codehaus/groovy/ast/FieldNode.java
@@ -51,18 +51,22 @@ public class FieldNode extends AnnotatedNode implements
Opcodes, Variable {
this.initialValueExpression = initialValueExpression;
}
+ @Override
public Expression getInitialExpression() {
return initialValueExpression;
}
+ @Override
public int getModifiers() {
return modifiers;
}
+ @Override
public String getName() {
return name;
}
+ @Override
public ClassNode getType() {
return type;
}
@@ -85,6 +89,7 @@ public class FieldNode extends AnnotatedNode implements
Opcodes, Variable {
this.holder = holder;
}
+ @Override
public boolean isDynamicTyped() {
return dynamicTyped;
}
@@ -150,10 +155,12 @@ public class FieldNode extends AnnotatedNode implements
Opcodes, Variable {
this.owner = owner;
}
+ @Override
public boolean hasInitialExpression() {
return initialValueExpression != null;
}
+ @Override
public boolean isInStaticContext() {
return isStatic();
}
@@ -169,7 +176,7 @@ public class FieldNode extends AnnotatedNode implements
Opcodes, Variable {
/**
* @deprecated
*/
- @Deprecated
+ @Deprecated @Override
public boolean isClosureSharedVariable() {
return false;
}
@@ -177,10 +184,11 @@ public class FieldNode extends AnnotatedNode implements
Opcodes, Variable {
/**
* @deprecated
*/
- @Deprecated
+ @Deprecated @Override
public void setClosureSharedVariable(boolean inClosure) {
}
+ @Override
public ClassNode getOriginType() {
return originType;
}
@@ -190,7 +198,7 @@ public class FieldNode extends AnnotatedNode implements
Opcodes, Variable {
}
public void rename(String name) {
- declaringClass.renameField(this.name, name);
+ getDeclaringClass().renameField(this.name, name);
this.name = name;
}
}