You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@groovy.apache.org by su...@apache.org on 2019/10/11 13:54:29 UTC
[groovy] 16/23: minor fix-ups
This is an automated email from the ASF dual-hosted git repository.
sunlan pushed a commit to branch GROOVY_3_0_X
in repository https://gitbox.apache.org/repos/asf/groovy.git
commit 6a29c1f43d3e2e90f2cb1a653cf7646eec2032c7
Author: Eric Milles <er...@thomsonreuters.com>
AuthorDate: Mon Oct 7 13:21:20 2019 -0500
minor fix-ups
(cherry picked from commit b68bc43e72e57c2d285fbc34416e5eeed2ebd265)
---
.../codehaus/groovy/control/CompilationUnit.java | 37 +++++++++++-----------
.../java/org/codehaus/groovy/syntax/Types.java | 15 ++-------
.../{Groovy8046Bug.groovy => Groovy8046.groovy} | 36 +++++++++++++--------
3 files changed, 43 insertions(+), 45 deletions(-)
diff --git a/src/main/java/org/codehaus/groovy/control/CompilationUnit.java b/src/main/java/org/codehaus/groovy/control/CompilationUnit.java
index 1aa5221..14847e7 100644
--- a/src/main/java/org/codehaus/groovy/control/CompilationUnit.java
+++ b/src/main/java/org/codehaus/groovy/control/CompilationUnit.java
@@ -19,13 +19,12 @@
package org.codehaus.groovy.control;
import groovy.lang.GroovyClassLoader;
-import groovy.lang.GroovyRuntimeException;
import groovy.transform.CompilationUnitAware;
import org.codehaus.groovy.GroovyBugError;
-import org.codehaus.groovy.ast.ASTNode;
import org.codehaus.groovy.ast.ClassHelper;
import org.codehaus.groovy.ast.ClassNode;
import org.codehaus.groovy.ast.CompileUnit;
+import org.codehaus.groovy.ast.GroovyClassVisitor;
import org.codehaus.groovy.ast.InnerClassNode;
import org.codehaus.groovy.ast.ModuleNode;
import org.codehaus.groovy.classgen.AsmClassGenerator;
@@ -44,6 +43,7 @@ import org.codehaus.groovy.control.io.ReaderSource;
import org.codehaus.groovy.control.messages.ExceptionMessage;
import org.codehaus.groovy.control.messages.Message;
import org.codehaus.groovy.control.messages.SimpleMessage;
+import org.codehaus.groovy.syntax.RuntimeParserException;
import org.codehaus.groovy.syntax.SyntaxException;
import org.codehaus.groovy.tools.GroovyClass;
import org.codehaus.groovy.transform.ASTTransformationVisitor;
@@ -792,24 +792,23 @@ public class CompilationUnit extends ProcessingUnit {
//
// Run the Verifier on the outer class
//
+ GroovyClassVisitor visitor = verifier;
try {
- verifier.visitClass(classNode);
- } catch (GroovyRuntimeException rpe) {
- ASTNode node = rpe.getNode();
- getErrorCollector().addError(
- new SyntaxException(rpe.getMessage(), node.getLineNumber(), node.getColumnNumber(), node.getLastLineNumber(), node.getLastColumnNumber()),
- source
- );
+ visitor.visitClass(classNode);
+ } catch (RuntimeParserException rpe) {
+ getErrorCollector().addError(new SyntaxException(rpe.getMessage(), rpe.getNode()), source);
}
- LabelVerifier lv = new LabelVerifier(source);
- lv.visitClass(classNode);
+ visitor = new LabelVerifier(source);
+ visitor.visitClass(classNode);
- ClassCompletionVerifier completionVerifier = new ClassCompletionVerifier(source);
- completionVerifier.visitClass(classNode);
+ visitor = new ClassCompletionVerifier(source);
+ visitor.visitClass(classNode);
- ExtendedVerifier xverifier = new ExtendedVerifier(source);
- xverifier.visitClass(classNode);
+ visitor = new ExtendedVerifier(source);
+ visitor.visitClass(classNode);
+
+ visitor = null;
// because the class may be generated even if a error was found
// and that class may have an invalid format we fail here if needed
@@ -818,28 +817,28 @@ public class CompilationUnit extends ProcessingUnit {
//
// Prep the generator machinery
//
- ClassVisitor visitor = createClassVisitor();
+ ClassVisitor classVisitor = createClassVisitor();
String sourceName = (source == null ? classNode.getModule().getDescription() : source.getName());
// only show the file name and its extension like javac does in its stacktraces rather than the full path
// also takes care of both \ and / depending on the host compiling environment
if (sourceName != null)
sourceName = sourceName.substring(Math.max(sourceName.lastIndexOf('\\'), sourceName.lastIndexOf('/')) + 1);
- AsmClassGenerator generator = new AsmClassGenerator(source, context, visitor, sourceName);
+ AsmClassGenerator generator = new AsmClassGenerator(source, context, classVisitor, sourceName);
//
// Run the generation and create the class (if required)
//
generator.visitClass(classNode);
- byte[] bytes = ((ClassWriter) visitor).toByteArray();
+ byte[] bytes = ((ClassWriter) classVisitor).toByteArray();
generatedClasses.add(new GroovyClass(classNode.getName(), bytes));
//
// Handle any callback that's been set
//
if (CompilationUnit.this.classgenCallback != null) {
- classgenCallback.call(visitor, classNode);
+ classgenCallback.call(classVisitor, classNode);
}
//
diff --git a/src/main/java/org/codehaus/groovy/syntax/Types.java b/src/main/java/org/codehaus/groovy/syntax/Types.java
index 91a98d1..26e12b9 100644
--- a/src/main/java/org/codehaus/groovy/syntax/Types.java
+++ b/src/main/java/org/codehaus/groovy/syntax/Types.java
@@ -20,7 +20,6 @@ package org.codehaus.groovy.syntax;
import org.codehaus.groovy.GroovyBugError;
-import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
@@ -368,6 +367,9 @@ public class Types {
//---------------------------------------------------------------------------
// TYPE HIERARCHIES
+ public static boolean isAssignment(int type) {
+ return ofType(type, ASSIGNMENT_OPERATOR);
+ }
/**
* Given two types, returns true if the second describes the first.
@@ -1367,15 +1369,4 @@ public class Types {
addDescription(RIGHT_OF_MATCHED_CONTAINER, "<right of matched container>");
addDescription(SWITCH_ENTRIES, "<valid in a switch body>");
}
-
- private static final int[] ASSIGNMENT_TYPES;
-
- static {
- ASSIGNMENT_TYPES = new int[]{EQUAL, BITWISE_AND_EQUAL, BITWISE_OR_EQUAL, BITWISE_XOR_EQUAL, PLUS_EQUAL, MINUS_EQUAL, MULTIPLY_EQUAL, DIVIDE_EQUAL, INTDIV_EQUAL, MOD_EQUAL, POWER_EQUAL, LEFT_SHIFT_EQUAL, RIGHT_SHIFT_EQUAL, RIGHT_SHIFT_UNSIGNED_EQUAL, ELVIS_EQUAL};
- Arrays.sort(ASSIGNMENT_TYPES);
- }
-
- public static boolean isAssignment(int type) {
- return Arrays.binarySearch(ASSIGNMENT_TYPES, type) >= 0;
- }
}
diff --git a/src/test/groovy/bugs/Groovy8046Bug.groovy b/src/test/groovy/bugs/Groovy8046.groovy
similarity index 65%
rename from src/test/groovy/bugs/Groovy8046Bug.groovy
rename to src/test/groovy/bugs/Groovy8046.groovy
index 3b81814..ed7c653 100644
--- a/src/test/groovy/bugs/Groovy8046Bug.groovy
+++ b/src/test/groovy/bugs/Groovy8046.groovy
@@ -18,38 +18,46 @@
*/
package groovy.bugs
-import gls.CompilableTestSupport
+import groovy.transform.CompileStatic
+import org.junit.Test
-class Groovy8046Bug extends CompilableTestSupport {
+import static groovy.test.GroovyAssert.shouldFail
+
+@CompileStatic
+final class Groovy8046 {
+
+ @Test
void testFieldShouldNotHavePrimitiveVoidType() {
- def message = shouldNotCompile """
+ def err = shouldFail '''
class MyClass {
void field
}
- """
- assert message.contains("The field 'field' has invalid type void") ||
- message.contains("void is not allowed here")
+ '''
+
+ assert err =~ /The field 'field' has invalid type void|void is not allowed here/
}
+ @Test
void testParameterShouldNotHavePrimitiveVoidType() {
- def message = shouldNotCompile """
+ def err = shouldFail '''
class MyClass {
int foo(void param) {}
}
- """
- assert message.contains("The parameter 'param' in method 'int foo(void)' has invalid type void") ||
- message.contains("void is not allowed here")
+ '''
+
+ assert err =~ /The parameter 'param' in method 'int foo\(void\)' has invalid type void|void is not allowed here/
}
+ @Test
void testLocalVariableShouldNotHavePrimitiveVoidType() {
- def message = shouldNotCompile """
+ def err = shouldFail '''
class MyClass {
def foo() {
void bar = null
}
}
- """
- assert message.contains("The variable 'bar' has invalid type void") ||
- message.contains("void is not allowed here")
+ '''
+
+ assert err =~ /The variable 'bar' has invalid type void|void is not allowed here/
}
}