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 2020/12/07 17:23:02 UTC
[groovy] branch master updated: prevent non-final parameter
warnings for script methods
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 c5468d9 prevent non-final parameter warnings for script methods
c5468d9 is described below
commit c5468d9628a6c2d681fa1e4c4db4909fec787a4b
Author: Eric Milles <er...@thomsonreuters.com>
AuthorDate: Mon Dec 7 11:22:49 2020 -0600
prevent non-final parameter warnings for script methods
---
src/main/java/org/codehaus/groovy/ast/ModuleNode.java | 16 +++++++++++-----
.../groovy/console/ui/AstNodeToScriptAdapterTest.groovy | 7 +++----
.../groovy/console/ui/ScriptToTreeNodeAdapterTest.groovy | 6 +++---
3 files changed, 17 insertions(+), 12 deletions(-)
diff --git a/src/main/java/org/codehaus/groovy/ast/ModuleNode.java b/src/main/java/org/codehaus/groovy/ast/ModuleNode.java
index 988aebb..fedec5a 100644
--- a/src/main/java/org/codehaus/groovy/ast/ModuleNode.java
+++ b/src/main/java/org/codehaus/groovy/ast/ModuleNode.java
@@ -47,6 +47,7 @@ import static org.codehaus.groovy.ast.tools.GeneralUtils.params;
import static org.codehaus.groovy.ast.tools.GeneralUtils.stmt;
import static org.codehaus.groovy.ast.tools.GeneralUtils.varX;
import static org.objectweb.asm.Opcodes.ACC_ABSTRACT;
+import static org.objectweb.asm.Opcodes.ACC_FINAL;
import static org.objectweb.asm.Opcodes.ACC_INTERFACE;
import static org.objectweb.asm.Opcodes.ACC_PUBLIC;
import static org.objectweb.asm.Opcodes.ACC_STATIC;
@@ -329,6 +330,12 @@ public class ModuleNode extends ASTNode {
}
}
+ private static Parameter[] finalParam(final ClassNode type, final String name) {
+ Parameter parameter = param(type, name);
+ parameter.setModifiers(ACC_FINAL);
+ return params(parameter);
+ }
+
protected ClassNode createStatementsClass() {
ClassNode classNode = getScriptClassDummy();
if (classNode.getName().endsWith("package-info")) {
@@ -342,12 +349,11 @@ public class ModuleNode extends ASTNode {
"main",
ACC_PUBLIC | ACC_STATIC,
ClassHelper.VOID_TYPE,
- params(param(ClassHelper.STRING_TYPE.makeArray(), "args")),
+ finalParam(ClassHelper.STRING_TYPE.makeArray(), "args"),
ClassNode.EMPTY_ARRAY,
- // InvokerHelper.runScript(scriptClass, args)
stmt(
callX(
- classX(ClassHelper.make(InvokerHelper.class)),
+ ClassHelper.make(InvokerHelper.class),
"runScript",
args(classX(classNode), varX("args"))
)
@@ -377,7 +383,7 @@ public class ModuleNode extends ASTNode {
classNode.addConstructor(
ACC_PUBLIC,
- params(param(ClassHelper.make(Binding.class), "context")),
+ finalParam(ClassHelper.make(Binding.class), "context"),
ClassNode.EMPTY_ARRAY,
stmt);
@@ -397,7 +403,7 @@ public class ModuleNode extends ASTNode {
private MethodNode handleMainMethodIfPresent(final List<MethodNode> methods) {
boolean found = false;
MethodNode result = null;
- for (Iterator<MethodNode> iter = methods.iterator(); iter.hasNext();) {
+ for (Iterator<MethodNode> iter = methods.iterator(); iter.hasNext(); ) {
MethodNode node = iter.next();
if (node.getName().equals("main")) {
if (node.isStatic() && node.getParameters().length == 1) {
diff --git a/subprojects/groovy-console/src/test/groovy/groovy/console/ui/AstNodeToScriptAdapterTest.groovy b/subprojects/groovy-console/src/test/groovy/groovy/console/ui/AstNodeToScriptAdapterTest.groovy
index 323a9cf..99cdcdc 100644
--- a/subprojects/groovy-console/src/test/groovy/groovy/console/ui/AstNodeToScriptAdapterTest.groovy
+++ b/subprojects/groovy-console/src/test/groovy/groovy/console/ui/AstNodeToScriptAdapterTest.groovy
@@ -31,25 +31,24 @@ import static org.codehaus.groovy.ast.tools.GeneralUtils.constX
import static org.codehaus.groovy.ast.tools.GeneralUtils.stmt
/**
- * Unit test for ScriptToTreeNodeAdapter.
+ * Unit test for {@link AstNodeToScriptAdapter}.
*
* The assertions in this test case often assert against the toString() representation of
* an object. Normally, this is bad form. However, the class under test is meant to display
* toString() forms in a user interface. So in this case it is appropriate.
*/
-class AstNodeToScriptAdapterTest extends GroovyTestCase {
+final class AstNodeToScriptAdapterTest extends GroovyTestCase {
private static String compileToScript(String script, CompilePhase phase = CompilePhase.SEMANTIC_ANALYSIS) {
new AstNodeToScriptAdapter().compileToScript(script, phase.phaseNumber)
}
void testScript() {
-
String result = compileToScript('true')
assert result =~ /public class script[0-9]* extends groovy\.lang\.Script \{/
assert result =~ /public script[0-9]*\(\) \{\s*\}/
- assert result =~ /public script[0-9]*\(groovy.lang.Binding context\) \{\s*super\(context\)\s*\}/
+ assert result =~ /public script[0-9]*\(final groovy.lang.Binding context\) \{\s*super\(context\)\s*\}/
assert result =~ /public java.lang.Object run\(\) \{\s*true\s*\}/
}
diff --git a/subprojects/groovy-console/src/test/groovy/groovy/console/ui/ScriptToTreeNodeAdapterTest.groovy b/subprojects/groovy-console/src/test/groovy/groovy/console/ui/ScriptToTreeNodeAdapterTest.groovy
index 538d974..7110106 100644
--- a/subprojects/groovy-console/src/test/groovy/groovy/console/ui/ScriptToTreeNodeAdapterTest.groovy
+++ b/subprojects/groovy-console/src/test/groovy/groovy/console/ui/ScriptToTreeNodeAdapterTest.groovy
@@ -26,13 +26,13 @@ import org.codehaus.groovy.control.Phases
import javax.swing.tree.TreeNode
/**
- * Unit test for ScriptToTreeNodeAdapter.
+ * Unit test for {@link ScriptToTreeNodeAdapter}.
*
* The assertions in this test case often assert against the toString() representation of
* an object. Normally, this is bad form. However, the class under test is meant to display
* toString() forms in a user interface. So in this case it is appropriate.
*/
-class ScriptToTreeNodeAdapterTest extends GroovyTestCase {
+final class ScriptToTreeNodeAdapterTest extends GroovyTestCase {
private final classLoader = new GroovyClassLoader()
@@ -285,7 +285,7 @@ class ScriptToTreeNodeAdapterTest extends GroovyTestCase {
eq('Methods'),
eq('MethodNode - main'),
startsWith('ExpressionStatement'), //notice, there is only one ExpressionStatement
- startsWith('MethodCall'),
+ startsWith('StaticMethodCallExpression'),
]
)
}