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/08/21 19:34:32 UTC
[groovy] 01/01: reuse getType for field in visitVariableExpression
and simplify its flow
This is an automated email from the ASF dual-hosted git repository.
emilles pushed a commit to branch GROOVY-9699
in repository https://gitbox.apache.org/repos/asf/groovy.git
commit 48e2ea93542e51859c5a796f5ce62b08e141a83b
Author: Eric Milles <er...@thomsonreuters.com>
AuthorDate: Fri Aug 21 10:53:13 2020 -0500
reuse getType for field in visitVariableExpression and simplify its flow
---
.../transform/stc/StaticTypeCheckingVisitor.java | 41 +++++---------
.../groovy/typecheckers/RegexCheckerTest.groovy | 64 +++++++++++-----------
2 files changed, 46 insertions(+), 59 deletions(-)
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 a2e814d..409f4fb 100644
--- a/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java
+++ b/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java
@@ -189,8 +189,6 @@ import static org.codehaus.groovy.ast.tools.GeneralUtils.isOrImplements;
import static org.codehaus.groovy.ast.tools.GeneralUtils.localVarX;
import static org.codehaus.groovy.ast.tools.GeneralUtils.thisPropX;
import static org.codehaus.groovy.ast.tools.GeneralUtils.varX;
-import static org.codehaus.groovy.ast.tools.GenericsUtils.findActualTypeByGenericsPlaceholderName;
-import static org.codehaus.groovy.ast.tools.GenericsUtils.makeDeclaringAndActualGenericsTypeMapOfExactType;
import static org.codehaus.groovy.ast.tools.GenericsUtils.toGenericTypesString;
import static org.codehaus.groovy.ast.tools.WideningCategories.isBigDecCategory;
import static org.codehaus.groovy.ast.tools.WideningCategories.isBigIntCategory;
@@ -552,7 +550,10 @@ public class StaticTypeCheckingVisitor extends ClassCodeVisitorSupport {
final Variable accessedVariable = vexp.getAccessedVariable();
final TypeCheckingContext.EnclosingClosure enclosingClosure = typeCheckingContext.getEnclosingClosure();
- VariableExpression localVariableExpression = null;
+ if (accessedVariable == null) {
+ return;
+ }
+
if (accessedVariable instanceof DynamicVariable) {
// a dynamic variable is either a closure property, a class member referenced from a closure, or an undeclared variable
@@ -601,15 +602,7 @@ public class StaticTypeCheckingVisitor extends ClassCodeVisitorSupport {
if (inferredType != null && !inferredType.equals(OBJECT_TYPE)) {
vexp.putNodeMetaData(INFERRED_RETURN_TYPE, inferredType);
} else {
- // GROOVY-7691
- FieldNode fieldNode = (FieldNode) accessedVariable;
- ClassNode fieldType = findActualTypeByGenericsPlaceholderName(
- fieldNode.getOriginType().getUnresolvedName(),
- makeDeclaringAndActualGenericsTypeMapOfExactType(fieldNode.getDeclaringClass(), typeCheckingContext.getEnclosingClassNode())
- );
- if (fieldType != null) {
- storeType(vexp, fieldType);
- }
+ storeType(vexp, getType(vexp));
}
}
} else if (accessedVariable instanceof PropertyNode) {
@@ -623,25 +616,21 @@ public class StaticTypeCheckingVisitor extends ClassCodeVisitorSupport {
SetterInfo setterInfo = removeSetterInfo(leftExpression);
if (setterInfo != null) {
Expression rightExpression = enclosingBinaryExpression.getRightExpression();
- if (!ensureValidSetter(vexp, leftExpression, rightExpression, setterInfo)) {
- return;
- }
+ ensureValidSetter(vexp, leftExpression, rightExpression, setterInfo);
}
}
}
- } else if (accessedVariable instanceof Parameter) {
- if (enclosingClosure == null) {
- localVariableExpression = new ParameterVariableExpression((Parameter) accessedVariable);
- }
- } else if (accessedVariable instanceof VariableExpression) {
- if (enclosingClosure == null) {
- localVariableExpression = (VariableExpression) accessedVariable;
+ } else if (enclosingClosure == null) {
+ VariableExpression localVariable;
+ if (accessedVariable instanceof Parameter) {
+ Parameter parameter = (Parameter) accessedVariable;
+ localVariable = new ParameterVariableExpression(parameter);
+ } else {
+ localVariable = vexp;
}
- }
- if (localVariableExpression != null) {
- ClassNode inferredType = localVariableExpression.getNodeMetaData(INFERRED_TYPE);
- inferredType = getInferredTypeFromTempInfo(localVariableExpression, inferredType);
+ ClassNode inferredType = localVariable.getNodeMetaData(INFERRED_TYPE);
+ inferredType = getInferredTypeFromTempInfo(localVariable, inferredType);
if (inferredType != null && !inferredType.equals(OBJECT_TYPE)) {
vexp.putNodeMetaData(INFERRED_RETURN_TYPE, inferredType);
}
diff --git a/subprojects/groovy-typecheckers/src/test/groovy/groovy/typecheckers/RegexCheckerTest.groovy b/subprojects/groovy-typecheckers/src/test/groovy/groovy/typecheckers/RegexCheckerTest.groovy
index 138b1bd..ec2c66f 100644
--- a/subprojects/groovy-typecheckers/src/test/groovy/groovy/typecheckers/RegexCheckerTest.groovy
+++ b/subprojects/groovy-typecheckers/src/test/groovy/groovy/typecheckers/RegexCheckerTest.groovy
@@ -18,27 +18,25 @@
*/
package groovy.typecheckers
-import groovy.transform.TypeChecked
import org.codehaus.groovy.control.CompilerConfiguration
-import org.codehaus.groovy.control.customizers.ASTTransformationCustomizer
-import org.codehaus.groovy.control.customizers.ImportCustomizer
+import org.codehaus.groovy.control.customizers.*
import org.junit.BeforeClass
import org.junit.Test
import static groovy.test.GroovyAssert.assertScript
import static groovy.test.GroovyAssert.shouldFail
-class RegexCheckerTest {
+final class RegexCheckerTest {
+
private static GroovyShell shell
@BeforeClass
- static void setup() {
- def cc = new CompilerConfiguration()
- def customizer = new ASTTransformationCustomizer(TypeChecked)
- customizer.annotationParameters = [extensions: 'groovy.typecheckers.RegexChecker']
- cc.addCompilationCustomizers(customizer)
- cc.addCompilationCustomizers(new ImportCustomizer().addStarImports("java.util.regex"))
- shell = new GroovyShell(cc)
+ static void setUp() {
+ shell = new GroovyShell(new CompilerConfiguration().tap {
+ def customizer = new ASTTransformationCustomizer(groovy.transform.TypeChecked)
+ customizer.annotationParameters = [extensions: 'groovy.typecheckers.RegexChecker']
+ addCompilationCustomizers(customizer, new ImportCustomizer().addStarImports('java.util.regex'))
+ })
}
@Test
@@ -244,34 +242,34 @@ class RegexCheckerTest {
@Test
void testBadRegexGroupCount() {
- shouldFailWithBadGroupCount'''
- def m = 'foobaz' =~ /(...)(...)/
- assert m.find()
- assert m.group(3)
+ def shouldFailWithBadGroupCount = { String script ->
+ def err = shouldFail(shell, script)
+ assert err =~ /No group 3|Invalid group count 3 for regex with 2 groups/
+ }
+
+ shouldFailWithBadGroupCount '''
+ def m = 'foobaz' =~ /(...)(...)/
+ assert m.find()
+ assert m.group(3)
'''
- shouldFailWithBadGroupCount'''
- def p = ~'(...)(...)'
- Matcher m = p.matcher('barfoo')
- assert m.find()
- assert m.group(3)
+ shouldFailWithBadGroupCount '''
+ def p = ~'(...)(...)'
+ Matcher m = p.matcher('barfoo')
+ assert m.find()
+ assert m.group(3)
'''
- shouldFailWithBadGroupCount'''
- Pattern p = Pattern.compile('(...)(...)')
- Matcher m = p.matcher('foobar')
- assert m.find()
- assert m.group(3)
+ shouldFailWithBadGroupCount '''
+ Pattern p = Pattern.compile('(...)(...)')
+ Matcher m = p.matcher('foobar')
+ assert m.find()
+ assert m.group(3)
'''
- shouldFailWithBadGroupCount'''
- def m = 'barbaz' =~ /(...)(...)/
- assert m[0][3]
+ shouldFailWithBadGroupCount '''
+ def m = 'barbaz' =~ /(...)(...)/
+ assert m[0][3]
'''
}
- private void shouldFailWithBadGroupCount(String script) {
- def err = shouldFail(shell, script)
- assert err.message.contains(/Invalid group count 3 for regex with 2 groups/)
- }
-
@Test
void testGoodRegexGroupCount() {
assertScript shell, '''