This is an automated email from the ASF dual-hosted git repository.
emilles pushed a commit to branch GROOVY_4_0_X
in repository https://gitbox.apache.org/repos/asf/groovy.git
The following commit(s) were added to refs/heads/GROOVY_4_0_X by this push:
new 0e8e7de529 minor items
0e8e7de529 is described below
commit 0e8e7de529f54c7a3d211f175bc9b90263d70e10
Author: Eric Milles <[email protected]>
AuthorDate: Sat Mar 4 09:49:28 2023 -0600
minor items
---
src/antlr/GroovyLexer.g4 | 31 ++++------
src/antlr/GroovyParser.g4 | 11 ++--
.../codehaus/groovy/ast/stmt/BlockStatement.java | 66 ++++++++++------------
.../org/codehaus/groovy/ast/stmt/IfStatement.java | 48 +++++++++++-----
.../groovy/classgen/AsmClassGenerator.java | 16 +++---
.../transform/stc/StaticTypeCheckingSupport.java | 20 ++++---
.../transform/stc/StaticTypeCheckingVisitor.java | 2 +
.../groovy/ast/stmt/ExpressionStatementTest.groovy | 20 +++----
...StatementTest.groovy => IfStatementTest.groovy} | 31 ++++++----
.../groovy/ast/stmt/ThrowStatementTest.groovy | 23 ++++----
10 files changed, 141 insertions(+), 127 deletions(-)
diff --git a/src/antlr/GroovyLexer.g4 b/src/antlr/GroovyLexer.g4
index 5e1965f552..f4b2f3f31a 100644
--- a/src/antlr/GroovyLexer.g4
+++ b/src/antlr/GroovyLexer.g4
@@ -38,24 +38,12 @@ options {
}
@header {
- import java.util.Deque;
- import java.util.ArrayDeque;
- import java.util.Map;
- import java.util.HashMap;
- import java.util.Set;
- import java.util.HashSet;
- import java.util.Collections;
- import java.util.Arrays;
- import java.util.logging.Logger;
- import java.util.logging.Level;
- import java.util.EmptyStackException;
+ import java.util.*;
import org.apache.groovy.util.Maps;
import static org.apache.groovy.parser.antlr4.SemanticPredicates.*;
}
@members {
- private static final Logger LOGGER =
Logger.getLogger(GroovyLexer.class.getName());
-
private boolean errorIgnored;
private long tokenIndex;
private int lastTokenType;
@@ -129,10 +117,12 @@ options {
return this.lastTokenType;
}
+ @SuppressWarnings("unused")
public int getLine() {
return line;
}
+ @SuppressWarnings("unused")
public int getColumn() {
return column;
}
@@ -223,15 +213,16 @@ options {
public int popMode() {
try {
return super.popMode();
- } catch (EmptyStackException ignored) { // raised when parens are
unmatched: too many ), ], or }
- if (LOGGER.isLoggable(Level.FINEST)) {
-
LOGGER.finest(org.codehaus.groovy.runtime.DefaultGroovyMethods.asString(ignored));
- }
+ } catch (EmptyStackException ignore) { // raised when parens are
unmatched: too many ), ], or }
}
return Integer.MIN_VALUE;
}
+ private void addComment(int type) {
+ String text = _input.getText(Interval.of(_tokenStartCharIndex,
getCharIndex() - 1));
+ }
+
private static boolean isJavaIdentifierStartAndNotIdentifierIgnorable(int
codePoint) {
return Character.isJavaIdentifierStart(codePoint) &&
!Character.isIdentifierIgnorable(codePoint);
}
@@ -983,17 +974,17 @@ WS : ([ \t]+ | LineEscape+) -> skip
;
// Inside (...) and [...] but not {...}, ignore newlines.
-NL : LineTerminator { this.ignoreTokenInsideParens(); }
+NL : LineTerminator { ignoreTokenInsideParens(); }
;
// Multiple-line comments (including groovydoc comments)
ML_COMMENT
- : '/*' .*? '*/' { this.ignoreMultiLineCommentConditionally(); } ->
type(NL)
+ : '/*' .*? '*/' { addComment(0);
ignoreMultiLineCommentConditionally(); } -> type(NL)
;
// Single-line comments
SL_COMMENT
- : '//' ~[\r\n\uFFFF]* { this.ignoreTokenInsideParens(); } ->
type(NL)
+ : '//' ~[\r\n\uFFFF]* { addComment(1); ignoreTokenInsideParens(); }
-> type(NL)
;
// Script-header comments.
diff --git a/src/antlr/GroovyParser.g4 b/src/antlr/GroovyParser.g4
index bffe4aa6d3..afb062a3d6 100644
--- a/src/antlr/GroovyParser.g4
+++ b/src/antlr/GroovyParser.g4
@@ -1215,15 +1215,14 @@ className
identifier
: Identifier
| CapitalizedIdentifier
- | VAR
- | IN
-// | DEF
- | TRAIT
| AS
- | YIELD
+ | IN
| PERMITS
- | SEALED
| RECORD
+ | SEALED
+ | TRAIT
+ | VAR
+ | YIELD
;
builtInType
diff --git a/src/main/java/org/codehaus/groovy/ast/stmt/BlockStatement.java
b/src/main/java/org/codehaus/groovy/ast/stmt/BlockStatement.java
index 0deb2941fa..9fd6e24465 100644
--- a/src/main/java/org/codehaus/groovy/ast/stmt/BlockStatement.java
+++ b/src/main/java/org/codehaus/groovy/ast/stmt/BlockStatement.java
@@ -24,49 +24,51 @@ import org.codehaus.groovy.ast.VariableScope;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
+import java.util.StringJoiner;
/**
* A list of statements and a scope.
*/
public class BlockStatement extends Statement {
- private List<Statement> statements = new ArrayList<Statement>();
+ private List<Statement> statements;
private VariableScope scope;
public BlockStatement() {
- this(new ArrayList<Statement>(), new VariableScope());
+ this(new ArrayList<>(), new VariableScope());
}
/**
* Creates a BlockStatement with a scope and children statements.
+ *
* @param statements
- * the statements. Do not pass null. If you do, no exception will
occur,
- * but a NullPointerException will eventually occur later. Also, a
reference
- * to the list is kept, so modifying the List later does effect this
class.
+ * the statements, which cannot be null or an exception occurs. No
reference
+ * to the array is held, so modifying the array later has no effect
on this
+ * class.
* @param scope
* the scope
*/
- public BlockStatement(List<Statement> statements, VariableScope scope) {
- this.statements = statements;
- this.scope = scope;
+ public BlockStatement(final Statement[] statements, final VariableScope
scope) {
+ this(new ArrayList<>(Arrays.asList(statements)), scope);
}
/**
* Creates a BlockStatement with a scope and children statements.
+ *
* @param statements
- * the statements, which cannot be null or an exception occurs. No
reference
- * to the array is held, so modifying the array later has no effect
on this
- * class.
+ * the statements. Do not pass null. If you do, no exception will
occur,
+ * but a NullPointerException will eventually occur later. Also, a
reference
+ * to the list is kept, so modifying the List later does effect this
class.
* @param scope
* the scope
*/
- public BlockStatement(Statement[] statements, VariableScope scope) {
- this.statements.addAll(Arrays.asList(statements));
+ public BlockStatement(final List<Statement> statements, final
VariableScope scope) {
+ this.statements = statements;
this.scope = scope;
}
@Override
- public void visit(GroovyCodeVisitor visitor) {
+ public void visit(final GroovyCodeVisitor visitor) {
visitor.visitBlockStatement(this);
}
@@ -74,46 +76,38 @@ public class BlockStatement extends Statement {
return statements;
}
- public void addStatement(Statement statement) {
+ public void addStatement(final Statement statement) {
statements.add(statement);
}
- public void addStatements(List<Statement> listOfStatements) {
+ public void addStatements(final List<Statement> listOfStatements) {
statements.addAll(listOfStatements);
}
- @Override
- public String toString() {
- return super.toString() + statements;
- }
-
@Override
public String getText() {
- StringBuilder buffer = new StringBuilder("{ ");
- boolean first = true;
+ StringJoiner text = new StringJoiner("; ", "{ ", " }");
for (Statement statement : statements) {
- if (first) {
- first = false;
- }
- else {
- buffer.append("; ");
- }
- buffer.append(statement.getText());
+ text.add(statement.getText());
}
- buffer.append(" }");
- return buffer.toString();
+ return text.toString();
}
@Override
- public boolean isEmpty() {
- return statements.isEmpty();
+ public String toString() {
+ return super.toString() + statements;
}
- public void setVariableScope(VariableScope scope) {
- this.scope = scope;
+ @Override
+ public boolean isEmpty() {
+ return statements.isEmpty();
}
public VariableScope getVariableScope() {
return scope;
}
+
+ public void setVariableScope(final VariableScope scope) {
+ this.scope = scope;
+ }
}
diff --git a/src/main/java/org/codehaus/groovy/ast/stmt/IfStatement.java
b/src/main/java/org/codehaus/groovy/ast/stmt/IfStatement.java
index 31bbbcd8cc..0a9301a314 100644
--- a/src/main/java/org/codehaus/groovy/ast/stmt/IfStatement.java
+++ b/src/main/java/org/codehaus/groovy/ast/stmt/IfStatement.java
@@ -22,7 +22,7 @@ import org.codehaus.groovy.ast.GroovyCodeVisitor;
import org.codehaus.groovy.ast.expr.BooleanExpression;
/**
- * Represents an if (condition) { ... } else { ... } statement in Groovy
+ * Represents an if (condition) { ... } else { ... } statement in Groovy.
*/
public class IfStatement extends Statement {
@@ -30,15 +30,28 @@ public class IfStatement extends Statement {
private Statement ifBlock;
private Statement elseBlock;
+ public IfStatement(final BooleanExpression booleanExpression, final
Statement ifBlock, final Statement elseBlock) {
+ setBooleanExpression(booleanExpression);
+ setIfBlock(ifBlock);
+ setElseBlock(elseBlock);
+ }
- public IfStatement(BooleanExpression booleanExpression, Statement ifBlock,
Statement elseBlock) {
+ public void setBooleanExpression(final BooleanExpression
booleanExpression) {
this.booleanExpression = booleanExpression;
- this.ifBlock = ifBlock;
- this.elseBlock = elseBlock;
}
+ public void setIfBlock(final Statement statement) {
+ ifBlock = statement;
+ }
+
+ public void setElseBlock(final Statement statement) {
+ elseBlock = statement;
+ }
+
+
//--------------------------------------------------------------------------
+
@Override
- public void visit(GroovyCodeVisitor visitor) {
+ public void visit(final GroovyCodeVisitor visitor) {
visitor.visitIfElse(this);
}
@@ -54,15 +67,22 @@ public class IfStatement extends Statement {
return elseBlock;
}
- public void setBooleanExpression(BooleanExpression booleanExpression) {
- this.booleanExpression = booleanExpression;
- }
-
- public void setIfBlock(Statement statement) {
- ifBlock = statement;
- }
+ @Override
+ public String getText() {
+ Statement thenStmt = getIfBlock(), elseStmt = getElseBlock();
- public void setElseBlock(Statement statement) {
- elseBlock = statement;
+ StringBuilder text = new StringBuilder(64);
+ text.append("if (");
+ text.append(getBooleanExpression().getText());
+ text.append(") ");
+ text.append(thenStmt.getText());
+ if (elseStmt != null && !elseStmt.isEmpty()) {
+ if (!(thenStmt instanceof BlockStatement)) {
+ text.append(';');
+ }
+ text.append(" else ");
+ text.append(elseStmt.getText());
+ }
+ return text.toString();
}
}
diff --git a/src/main/java/org/codehaus/groovy/classgen/AsmClassGenerator.java
b/src/main/java/org/codehaus/groovy/classgen/AsmClassGenerator.java
index 32993f3341..4cd15bb6e9 100644
--- a/src/main/java/org/codehaus/groovy/classgen/AsmClassGenerator.java
+++ b/src/main/java/org/codehaus/groovy/classgen/AsmClassGenerator.java
@@ -2385,14 +2385,14 @@ public class AsmClassGenerator extends ClassGenerator {
controller.getOperandStack().remove(1);
}
- public void onLineNumber(final ASTNode statement, final String message) {
- if (statement == null || statement instanceof BlockStatement) return;
-
- currentASTNode = statement;
- int line = statement.getLineNumber();
- if (!ASM_DEBUG && line == controller.getLineNumber()) return;
-
- controller.visitLineNumber(line);
+ public void onLineNumber(final ASTNode node, final String message) {
+ if (node != null && !(node instanceof BlockStatement)) {
+ currentASTNode = node;
+ int line = node.getLineNumber();
+ if (line != controller.getLineNumber() || ASM_DEBUG) {
+ controller.visitLineNumber(line);
+ }
+ }
}
public void throwException(final String message) {
diff --git
a/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingSupport.java
b/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingSupport.java
index 410a81e873..d18cba1940 100644
---
a/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingSupport.java
+++
b/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingSupport.java
@@ -1408,26 +1408,28 @@ public abstract class StaticTypeCheckingSupport {
if (UNKNOWN_PARAMETER_TYPE == argumentType) { // argument is null
return !isPrimitiveType(parameterType);
}
+ boolean isArrayParameter = parameterType.isArray();
if (!isAssignableTo(argumentType, parameterType)) {
- if (!lastArg || !parameterType.isArray()
+ if (!lastArg || !isArrayParameter
|| !isAssignableTo(argumentType,
parameterType.getComponentType())) {
return false; // incompatible assignment
}
}
- if (parameterType.isUsingGenerics() && argumentType.isUsingGenerics())
{
- GenericsType gt = GenericsUtils.buildWildcardType(parameterType);
- if (!gt.isCompatibleWith(argumentType)) {
- boolean samCoercion = isSAMType(parameterType) &&
argumentType.equals(CLOSURE_TYPE);
- if (!samCoercion) return false; // else assume parameters and
return checked earlier
- }
- } else if (parameterType.isArray() && argumentType.isArray()) {
+ if (isArrayParameter && argumentType.isArray()) {
// verify component type
return
typeCheckMethodArgumentWithGenerics(parameterType.getComponentType(),
argumentType.getComponentType(), lastArg);
- } else if (lastArg && parameterType.isArray()) {
+
+ } else if (isArrayParameter && lastArg) {
// verify component type, but if we reach that point, the only
possibility is that the argument is
// the last one of the call, so we're in the cast of a vargs call
// (otherwise, we face a type checker bug)
return
typeCheckMethodArgumentWithGenerics(parameterType.getComponentType(),
argumentType, lastArg);
+
+ } else if (parameterType.isUsingGenerics() &&
argumentType.isUsingGenerics()) {
+ if
(!GenericsUtils.buildWildcardType(parameterType).isCompatibleWith(argumentType))
{
+ boolean samCoercion = argumentType.equals(CLOSURE_TYPE) &&
isSAMType(parameterType);
+ if (!samCoercion) return false; // else assume parameters and
return checked earlier
+ }
}
return true;
}
diff --git
a/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java
b/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java
index 142ae2c866..0ab47e4671 100644
---
a/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java
+++
b/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java
@@ -2456,10 +2456,12 @@ public class StaticTypeCheckingVisitor extends
ClassCodeVisitorSupport {
if (name.equals(pn.getGetterNameOrDefault())) {
MethodNode node = new MethodNode(name, Opcodes.ACC_PUBLIC
| (pn.isStatic() ? Opcodes.ACC_STATIC : 0), pn.getType(),
Parameter.EMPTY_ARRAY, ClassNode.EMPTY_ARRAY, null);
node.setDeclaringClass(pn.getDeclaringClass());
+ node.setSynthetic(true);
return node;
} else if (name.equals(pn.getSetterNameOrDefault()) &&
!Modifier.isFinal(pn.getModifiers())) {
MethodNode node = new MethodNode(name, Opcodes.ACC_PUBLIC
| (pn.isStatic() ? Opcodes.ACC_STATIC : 0), VOID_TYPE, new Parameter[]{new
Parameter(pn.getType(), pn.getName())}, ClassNode.EMPTY_ARRAY, null);
node.setDeclaringClass(pn.getDeclaringClass());
+ node.setSynthetic(true);
return node;
}
}
diff --git
a/src/test/org/codehaus/groovy/ast/stmt/ExpressionStatementTest.groovy
b/src/test/org/codehaus/groovy/ast/stmt/ExpressionStatementTest.groovy
index 96b018c379..f1a80a4d56 100644
--- a/src/test/org/codehaus/groovy/ast/stmt/ExpressionStatementTest.groovy
+++ b/src/test/org/codehaus/groovy/ast/stmt/ExpressionStatementTest.groovy
@@ -18,19 +18,17 @@
*/
package org.codehaus.groovy.ast.stmt
-import groovy.test.GroovyTestCase
-import org.codehaus.groovy.ast.ClassNode
-import org.codehaus.groovy.ast.expr.ArgumentListExpression
-import org.codehaus.groovy.ast.expr.ConstructorCallExpression
+import org.codehaus.groovy.ast.ClassHelper
+import org.junit.Test
-class ExpressionStatementTest extends GroovyTestCase {
+import static org.codehaus.groovy.ast.tools.GeneralUtils.ctorX
+final class ExpressionStatementTest {
+
+ @Test
void testGetText() {
- assert new ExpressionStatement(
- new ConstructorCallExpression(
- new ClassNode(Object),
- new ArgumentListExpression())).
- text == 'new java.lang.Object()'
- }
+ def stmt = new ExpressionStatement(ctorX(ClassHelper.OBJECT_TYPE))
+ assert stmt.text == 'new java.lang.Object()'
+ }
}
diff --git
a/src/test/org/codehaus/groovy/ast/stmt/ExpressionStatementTest.groovy
b/src/test/org/codehaus/groovy/ast/stmt/IfStatementTest.groovy
similarity index 56%
copy from src/test/org/codehaus/groovy/ast/stmt/ExpressionStatementTest.groovy
copy to src/test/org/codehaus/groovy/ast/stmt/IfStatementTest.groovy
index 96b018c379..3574457e37 100644
--- a/src/test/org/codehaus/groovy/ast/stmt/ExpressionStatementTest.groovy
+++ b/src/test/org/codehaus/groovy/ast/stmt/IfStatementTest.groovy
@@ -18,19 +18,30 @@
*/
package org.codehaus.groovy.ast.stmt
-import groovy.test.GroovyTestCase
-import org.codehaus.groovy.ast.ClassNode
-import org.codehaus.groovy.ast.expr.ArgumentListExpression
-import org.codehaus.groovy.ast.expr.ConstructorCallExpression
+import org.junit.Test
-class ExpressionStatementTest extends GroovyTestCase {
+import static org.codehaus.groovy.ast.tools.GeneralUtils.*
+final class IfStatementTest {
+
+ @Test
void testGetText() {
- assert new ExpressionStatement(
- new ConstructorCallExpression(
- new ClassNode(Object),
- new ArgumentListExpression())).
- text == 'new java.lang.Object()'
+ def stmt = new IfStatement(boolX(constX(true)),
block(returnS(nullX())), null)
+
+ assert stmt.text == 'if (true) { return null }'
}
+ @Test
+ void testGetText2() {
+ def stmt = new IfStatement(boolX(constX(true)), block(),
block(returnS(nullX())))
+
+ assert stmt.text == 'if (true) { } else { return null }'
+ }
+
+ @Test
+ void testGetText3() {
+ def stmt = new IfStatement(boolX(varX('list')), returnS(varX('list')),
block(returnS(nullX())))
+
+ assert stmt.text == 'if (list) return list; else { return null }'
+ }
}
diff --git a/src/test/org/codehaus/groovy/ast/stmt/ThrowStatementTest.groovy
b/src/test/org/codehaus/groovy/ast/stmt/ThrowStatementTest.groovy
index f2f82a9253..d25eeb81f2 100644
--- a/src/test/org/codehaus/groovy/ast/stmt/ThrowStatementTest.groovy
+++ b/src/test/org/codehaus/groovy/ast/stmt/ThrowStatementTest.groovy
@@ -18,21 +18,18 @@
*/
package org.codehaus.groovy.ast.stmt
-import groovy.test.GroovyTestCase
-import org.codehaus.groovy.ast.ClassNode
-import org.codehaus.groovy.ast.expr.ArgumentListExpression
-import org.codehaus.groovy.ast.expr.ConstantExpression
-import org.codehaus.groovy.ast.expr.ConstructorCallExpression
+import org.codehaus.groovy.ast.ClassHelper
+import org.junit.Test
-class ThrowStatementTest extends GroovyTestCase {
+import static org.codehaus.groovy.ast.tools.GeneralUtils.constX
+import static org.codehaus.groovy.ast.tools.GeneralUtils.ctorX
+final class ThrowStatementTest {
+
+ @Test
void testGetText() {
- assert new ThrowStatement(
- new ConstructorCallExpression(
- new ClassNode(Exception),
- new ArgumentListExpression(
- new ConstantExpression('oops')))).
- text == 'throw new java.lang.Exception(oops)'
- }
+ def stmt = new ThrowStatement(ctorX(ClassHelper.THROWABLE_TYPE,
constX('oops')))
+ assert stmt.text == 'throw new java.lang.Throwable(oops)' // TODO:
quoted string
+ }
}