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 2020/03/14 06:31:08 UTC
[groovy] branch GROOVY_3_0_X updated (601ec54 -> 18abc55)
This is an automated email from the ASF dual-hosted git repository.
sunlan pushed a change to branch GROOVY_3_0_X
in repository https://gitbox.apache.org/repos/asf/groovy.git.
from 601ec54 move misplaced test
new 770bb90 GROOVY-9454: add test case
new 17381bc GROOVY-9447: Add a parse tree viewer to groovy console
new 2b79aff GROOVY-9448: Missing throws clause of constructor declaration in java stubs
new 18abc55 Minor refactoring: extract class `CstInspector`
The 4 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails. The revisions
listed as "add" were already present in the repository and have only
been added to this reference.
Summary of changes:
.../groovy/tools/javac/JavaStubGenerator.java | 25 ++-
.../transform/stc/TypeInferenceSTCTest.groovy | 197 +++++++++++----------
.../main/groovy/groovy/console/ui/Console.groovy | 31 +++-
.../groovy/groovy/console/ui/ConsoleActions.groovy | 9 +-
.../groovy/console/ui/view/BasicMenuBar.groovy | 1 +
.../groovy/console/ui/view/MacOSXMenuBar.groovy | 1 +
6 files changed, 163 insertions(+), 101 deletions(-)
[groovy] 03/04: GROOVY-9448: Missing throws clause of constructor
declaration in java stubs
Posted by su...@apache.org.
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 2b79affcd6f82b57f811af69fb8964541324be5e
Author: Daniel Sun <su...@apache.org>
AuthorDate: Sat Mar 7 21:54:11 2020 +0800
GROOVY-9448: Missing throws clause of constructor declaration in java stubs
(cherry picked from commit 40149b6ccde186f5a04173e4b5fe361cb9cbcd8c)
---
.../groovy/tools/javac/JavaStubGenerator.java | 25 ++++++++++++++--------
1 file changed, 16 insertions(+), 9 deletions(-)
diff --git a/src/main/java/org/codehaus/groovy/tools/javac/JavaStubGenerator.java b/src/main/java/org/codehaus/groovy/tools/javac/JavaStubGenerator.java
index 7e73515..46d3cff 100644
--- a/src/main/java/org/codehaus/groovy/tools/javac/JavaStubGenerator.java
+++ b/src/main/java/org/codehaus/groovy/tools/javac/JavaStubGenerator.java
@@ -544,6 +544,9 @@ public class JavaStubGenerator {
printParams(out, constructorNode);
+ ClassNode[] exceptions = constructorNode.getExceptions();
+ printExceptions(out, exceptions);
+
ConstructorCallExpression constrCall = getFirstIfSpecialConstructorCall(constructorNode.getCode());
if (constrCall == null) {
out.println(" {}");
@@ -695,15 +698,7 @@ public class JavaStubGenerator {
printParams(out, methodNode);
ClassNode[] exceptions = methodNode.getExceptions();
- for (int i = 0; i < exceptions.length; i++) {
- ClassNode exception = exceptions[i];
- if (i == 0) {
- out.print("throws ");
- } else {
- out.print(", ");
- }
- printType(out, exception);
- }
+ printExceptions(out, exceptions);
if (Traits.isTrait(clazz)) {
out.println(";");
@@ -740,6 +735,18 @@ public class JavaStubGenerator {
}
}
+ private void printExceptions(PrintWriter out, ClassNode[] exceptions) {
+ for (int i = 0; i < exceptions.length; i++) {
+ ClassNode exception = exceptions[i];
+ if (i == 0) {
+ out.print("throws ");
+ } else {
+ out.print(", ");
+ }
+ printType(out, exception);
+ }
+ }
+
private static boolean isAbstract(final MethodNode methodNode) {
if (isDefaultTraitImpl(methodNode)) {
return false;
[groovy] 02/04: GROOVY-9447: Add a parse tree viewer to groovy
console
Posted by su...@apache.org.
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 17381bc67fbe53efa4bf35594b7eb2482f403ca4
Author: Daniel Sun <su...@apache.org>
AuthorDate: Sat Mar 7 20:23:38 2020 +0800
GROOVY-9447: Add a parse tree viewer to groovy console
(cherry picked from commit 675dd0560b363b0200947d3e7bf6ced363645c0e)
---
.../src/main/groovy/groovy/console/ui/Console.groovy | 20 ++++++++++++++++++--
.../groovy/groovy/console/ui/ConsoleActions.groovy | 9 ++++++++-
.../groovy/console/ui/view/BasicMenuBar.groovy | 1 +
.../groovy/console/ui/view/MacOSXMenuBar.groovy | 1 +
4 files changed, 28 insertions(+), 3 deletions(-)
diff --git a/subprojects/groovy-console/src/main/groovy/groovy/console/ui/Console.groovy b/subprojects/groovy-console/src/main/groovy/groovy/console/ui/Console.groovy
index 0ce2c43..f0d5351 100644
--- a/subprojects/groovy-console/src/main/groovy/groovy/console/ui/Console.groovy
+++ b/subprojects/groovy-console/src/main/groovy/groovy/console/ui/Console.groovy
@@ -27,10 +27,15 @@ import groovy.swing.SwingBuilder
import groovy.transform.CompileStatic
import groovy.transform.ThreadInterrupt
import groovy.ui.GroovyMain
+import org.antlr.v4.gui.TestRig
+import org.antlr.v4.runtime.CharStream
+import org.antlr.v4.runtime.CharStreams
+import org.antlr.v4.runtime.CommonTokenStream
+import org.apache.groovy.antlr.LexerFrame
import org.apache.groovy.io.StringBuilderWriter
import org.apache.groovy.parser.antlr4.GroovyLangLexer
+import org.apache.groovy.parser.antlr4.GroovyLangParser
import org.apache.groovy.util.SystemUtil
-import org.codehaus.groovy.antlr.LexerFrame
import org.codehaus.groovy.control.CompilerConfiguration
import org.codehaus.groovy.control.ErrorCollector
import org.codehaus.groovy.control.MultipleCompilationErrorsException
@@ -71,7 +76,6 @@ import javax.swing.text.SimpleAttributeSet
import javax.swing.text.Style
import javax.swing.text.StyleConstants
import javax.swing.text.html.HTML
-
import java.awt.BorderLayout
import java.awt.Component
import java.awt.Dimension
@@ -1043,6 +1047,18 @@ class Console implements CaretListener, HyperlinkListener, ComponentListener, Fo
new AstBrowser(inputArea, rootElement, shell.getClassLoader(), config).run({ inputArea.getText() })
}
+ void inspectCst(EventObject evt = null) {
+ String text = this.inputEditor.textEditor.text
+
+ CharStream charStream = CharStreams.fromReader(new StringReader(text))
+ GroovyLangLexer lexer = new GroovyLangLexer(charStream)
+ CommonTokenStream tokens = new CommonTokenStream(lexer)
+ GroovyLangParser parser = new GroovyLangParser(tokens)
+
+ def tr = new TestRig(new String[] { 'Groovy', 'compilationUnit', '-gui' })
+ tr.process(lexer, GroovyLangParser.class, parser, charStream)
+ }
+
void inspectTokens(EventObject evt = null) {
def content = inputArea.getText()
def lf = (config.pluginFactory instanceof org.codehaus.groovy.antlr.AntlrParserPluginFactory
diff --git a/subprojects/groovy-console/src/main/groovy/groovy/console/ui/ConsoleActions.groovy b/subprojects/groovy-console/src/main/groovy/groovy/console/ui/ConsoleActions.groovy
index 2b93fe8..57a3f19 100644
--- a/subprojects/groovy-console/src/main/groovy/groovy/console/ui/ConsoleActions.groovy
+++ b/subprojects/groovy-console/src/main/groovy/groovy/console/ui/ConsoleActions.groovy
@@ -257,12 +257,19 @@ inspectVariablesAction = action(
)
inspectAstAction = action(
- name: 'Inspect Ast',
+ name: 'Inspect AST',
closure: controller.&inspectAst,
mnemonic: 'A',
accelerator: shortcut('T'),
)
+inspectCstAction = action(
+ name: 'Inspect CST',
+ closure: controller.&inspectCst,
+ mnemonic: 'C',
+ accelerator: KeyStroke.getKeyStroke(KeyEvent.VK_T, InputEvent.CTRL_DOWN_MASK + InputEvent.ALT_DOWN_MASK)
+)
+
inspectTokensAction = action(
name: 'Inspect Tokens',
closure: controller.&inspectTokens,
diff --git a/subprojects/groovy-console/src/main/groovy/groovy/console/ui/view/BasicMenuBar.groovy b/subprojects/groovy-console/src/main/groovy/groovy/console/ui/view/BasicMenuBar.groovy
index 84328a5..4760c66 100644
--- a/subprojects/groovy-console/src/main/groovy/groovy/console/ui/view/BasicMenuBar.groovy
+++ b/subprojects/groovy-console/src/main/groovy/groovy/console/ui/view/BasicMenuBar.groovy
@@ -94,6 +94,7 @@ menuBar {
menuItem(inspectLastAction)
menuItem(inspectVariablesAction)
menuItem(inspectAstAction)
+ menuItem(inspectCstAction)
menuItem(inspectTokensAction)
}
diff --git a/subprojects/groovy-console/src/main/groovy/groovy/console/ui/view/MacOSXMenuBar.groovy b/subprojects/groovy-console/src/main/groovy/groovy/console/ui/view/MacOSXMenuBar.groovy
index b85b4ee..58a78a7 100644
--- a/subprojects/groovy-console/src/main/groovy/groovy/console/ui/view/MacOSXMenuBar.groovy
+++ b/subprojects/groovy-console/src/main/groovy/groovy/console/ui/view/MacOSXMenuBar.groovy
@@ -145,6 +145,7 @@ menuBar {
menuItem(inspectLastAction, icon:null)
menuItem(inspectVariablesAction, icon:null)
menuItem(inspectAstAction, icon:null)
+ menuItem(inspectCstAction, icon:null)
menuItem(inspectTokensAction, icon:null)
}
}
[groovy] 04/04: Minor refactoring: extract class `CstInspector`
Posted by su...@apache.org.
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 18abc55b966ae07fd3e66f65375ec47bc35c7373
Author: Daniel Sun <su...@apache.org>
AuthorDate: Sat Mar 7 21:56:50 2020 +0800
Minor refactoring: extract class `CstInspector`
(cherry picked from commit 8a7e716b2518268db2767f2b62181a7f00f9ac9e)
---
.../main/groovy/groovy/console/ui/Console.groovy | 25 ++++++++++++++++------
1 file changed, 18 insertions(+), 7 deletions(-)
diff --git a/subprojects/groovy-console/src/main/groovy/groovy/console/ui/Console.groovy b/subprojects/groovy-console/src/main/groovy/groovy/console/ui/Console.groovy
index f0d5351..dc8cf38 100644
--- a/subprojects/groovy-console/src/main/groovy/groovy/console/ui/Console.groovy
+++ b/subprojects/groovy-console/src/main/groovy/groovy/console/ui/Console.groovy
@@ -1047,16 +1047,27 @@ class Console implements CaretListener, HyperlinkListener, ComponentListener, Fo
new AstBrowser(inputArea, rootElement, shell.getClassLoader(), config).run({ inputArea.getText() })
}
+ @CompileStatic
+ private static class CstInspector extends TestRig {
+ CstInspector() throws Exception {
+ super(new String[] { 'Groovy', 'compilationUnit', '-gui' })
+ }
+
+ void inspectParseTree(String text) {
+ CharStream charStream = CharStreams.fromReader(new StringReader(text))
+ GroovyLangLexer lexer = new GroovyLangLexer(charStream)
+ CommonTokenStream tokens = new CommonTokenStream(lexer)
+ GroovyLangParser parser = new GroovyLangParser(tokens)
+ process(lexer, GroovyLangParser.class, parser, charStream)
+ }
+ }
+
+ @CompileStatic
void inspectCst(EventObject evt = null) {
String text = this.inputEditor.textEditor.text
- CharStream charStream = CharStreams.fromReader(new StringReader(text))
- GroovyLangLexer lexer = new GroovyLangLexer(charStream)
- CommonTokenStream tokens = new CommonTokenStream(lexer)
- GroovyLangParser parser = new GroovyLangParser(tokens)
-
- def tr = new TestRig(new String[] { 'Groovy', 'compilationUnit', '-gui' })
- tr.process(lexer, GroovyLangParser.class, parser, charStream)
+ def gtr = new CstInspector()
+ gtr.inspectParseTree(text)
}
void inspectTokens(EventObject evt = null) {
[groovy] 01/04: GROOVY-9454: add test case
Posted by su...@apache.org.
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 770bb90c203a16f3b7e2a3e889f911761ece222c
Author: Eric Milles <er...@thomsonreuters.com>
AuthorDate: Mon Mar 9 12:29:06 2020 -0500
GROOVY-9454: add test case
(cherry picked from commit ea4f69abbb4540b3542c81b2c6496fb8852fe8f4)
---
.../transform/stc/TypeInferenceSTCTest.groovy | 197 +++++++++++----------
1 file changed, 108 insertions(+), 89 deletions(-)
diff --git a/src/test/groovy/transform/stc/TypeInferenceSTCTest.groovy b/src/test/groovy/transform/stc/TypeInferenceSTCTest.groovy
index f570378..2562166 100644
--- a/src/test/groovy/transform/stc/TypeInferenceSTCTest.groovy
+++ b/src/test/groovy/transform/stc/TypeInferenceSTCTest.groovy
@@ -18,15 +18,16 @@
*/
package groovy.transform.stc
+import groovy.test.NotYetImplemented
+import org.codehaus.groovy.ast.ClassHelper
+import org.codehaus.groovy.ast.ClassNode
import org.codehaus.groovy.ast.MethodNode
-import org.codehaus.groovy.control.customizers.CompilationCustomizer
+import org.codehaus.groovy.ast.tools.WideningCategories
+import org.codehaus.groovy.classgen.GeneratorContext
import org.codehaus.groovy.control.CompilePhase
import org.codehaus.groovy.control.SourceUnit
-import org.codehaus.groovy.classgen.GeneratorContext
-import org.codehaus.groovy.ast.ClassNode
-import org.codehaus.groovy.ast.ClassHelper
+import org.codehaus.groovy.control.customizers.CompilationCustomizer
import org.codehaus.groovy.transform.stc.StaticTypesMarker
-import org.codehaus.groovy.ast.tools.WideningCategories
/**
* Unit tests for static type checking : type inference.
@@ -34,10 +35,10 @@ import org.codehaus.groovy.ast.tools.WideningCategories
class TypeInferenceSTCTest extends StaticTypeCheckingTestCase {
void testStringToInteger() {
- assertScript """
- def name = "123" // we want type inference
- name.toInteger() // toInteger() is defined by DGM
- """
+ assertScript '''
+ def name = "123" // we want type inference
+ name.toInteger() // toInteger() is defined by DGM
+ '''
}
void testGStringMethods() {
@@ -77,47 +78,46 @@ class TypeInferenceSTCTest extends StaticTypeCheckingTestCase {
def g = new Greeter()
g.greeting("Guillaume")
-
'''
}
void testInstanceOf() {
- assertScript """
- Object o
- if (o instanceof String) o.toUpperCase()
- """
+ assertScript '''
+ Object o
+ if (o instanceof String) o.toUpperCase()
+ '''
}
void testEmbeddedInstanceOf() {
- assertScript """
- Object o
- if (o instanceof Object) {
- if (o instanceof String) {
- o.toUpperCase()
+ assertScript '''
+ Object o
+ if (o instanceof Object) {
+ if (o instanceof String) {
+ o.toUpperCase()
+ }
}
- }
- """
+ '''
}
void testEmbeddedInstanceOf2() {
- assertScript """
- Object o
- if (o instanceof String) {
- if (true) {
- o.toUpperCase()
+ assertScript '''
+ Object o
+ if (o instanceof String) {
+ if (true) {
+ o.toUpperCase()
+ }
}
- }
- """
+ '''
}
void testEmbeddedInstanceOf3() {
shouldFailWithMessages '''
- Object o
- if (o instanceof String) {
- if (o instanceof Object) { // causes the inferred type of 'o' to be overwritten
- o.toUpperCase()
+ Object o
+ if (o instanceof String) {
+ if (o instanceof Object) { // causes the inferred type of 'o' to be overwritten
+ o.toUpperCase()
+ }
}
- }
''', 'Cannot find matching method java.lang.Object#toUpperCase()'
}
@@ -137,11 +137,38 @@ class TypeInferenceSTCTest extends StaticTypeCheckingTestCase {
if (o instanceof String) {
o.toUpperCase()
} else {
- o.toUpperCase() // ensure that type information is lost in else()
+ o.toUpperCase() // ensure that type information is lost in else
}
''', 'Cannot find matching method java.lang.Object#toUpperCase()'
}
+ @NotYetImplemented // GROOVY-9454
+ void testInstanceOfOnGenericProperty() {
+ assertScript '''
+ interface Face {
+ }
+ class Impl implements Face {
+ String something
+ }
+ class Task<R extends Face> implements java.util.concurrent.Callable<String> {
+ R request
+
+ @Override
+ String call() {
+ if (request instanceof Impl) {
+ def thing = request.something // No such property: something for class: R
+ def lower = thing.toLowerCase()
+ } else {
+ // ...
+ return null
+ }
+ }
+ }
+ def task = new Task<Impl>(request: new Impl(something: 'Hello World'))
+ assert task.call() == 'hello world'
+ '''
+ }
+
void testMultipleInstanceOf() {
assertScript '''
class A {
@@ -153,7 +180,6 @@ class TypeInferenceSTCTest extends StaticTypeCheckingTestCase {
void foo2() { println 'ok 2' }
}
-
def o = new A()
if (o instanceof A) {
@@ -167,7 +193,6 @@ class TypeInferenceSTCTest extends StaticTypeCheckingTestCase {
if (o instanceof A || o instanceof B) {
o.foo()
}
-
'''
}
@@ -176,16 +201,11 @@ class TypeInferenceSTCTest extends StaticTypeCheckingTestCase {
class A {
int foo() { 1 }
}
-
class B {
int foo2() { 2 }
}
-
-
def o = new A()
-
int result = o instanceof A?o.foo():(o instanceof B?o.foo2():3)
-
'''
}
@@ -374,7 +394,6 @@ class TypeInferenceSTCTest extends StaticTypeCheckingTestCase {
'''
}
-
void testCallMethodInWithContextAndShadowing() {
// make sure that the method which is found in 'with' is actually the one from class A
// which returns a String
@@ -390,6 +409,7 @@ class TypeInferenceSTCTest extends StaticTypeCheckingTestCase {
method().toUpperCase()
}
'''
+
// check that if we switch signatures, it fails
shouldFailWithMessages '''
class A {
@@ -454,8 +474,6 @@ class TypeInferenceSTCTest extends StaticTypeCheckingTestCase {
}
'''
assert method.code.statements[0].expression.leftExpression.getNodeMetaData(StaticTypesMarker.DECLARATION_INFERRED_TYPE) == ClassHelper.make(HashSet)
-
-
}
void testChooseMethodWithTypeInference() {
@@ -481,7 +499,7 @@ class TypeInferenceSTCTest extends StaticTypeCheckingTestCase {
keys*.toUpperCase()
values*.toUpperCase()
'''
-
+
shouldFailWithMessages '''
List values = [x:1,y:2,z:3]*.value
values*.toUpperCase()
@@ -574,7 +592,8 @@ class TypeInferenceSTCTest extends StaticTypeCheckingTestCase {
}
void testSwitchCaseAnalysis() {
- assertScript '''import org.codehaus.groovy.ast.tools.WideningCategories.LowestUpperBoundClassNode as LUB
+ assertScript '''
+ import org.codehaus.groovy.ast.tools.WideningCategories.LowestUpperBoundClassNode as LUB
def method(int x) {
def returnValue= new Date()
@@ -600,30 +619,30 @@ class TypeInferenceSTCTest extends StaticTypeCheckingTestCase {
void testGroovy6215() {
assertScript '''
- def processNumber(int x) {
- def value = getValueForNumber(x)
- value
- }
+ def processNumber(int x) {
+ def value = getValueForNumber(x)
+ value
+ }
- def getValueForNumber(int x) {
- def valueToReturn
- switch(x) {
- case 1:
- valueToReturn = 'One'
- break
- case 2:
- valueToReturn = []
- valueToReturn << 'Two'
- break
- }
- valueToReturn
+ def getValueForNumber(int x) {
+ def valueToReturn
+ switch(x) {
+ case 1:
+ valueToReturn = 'One'
+ break
+ case 2:
+ valueToReturn = []
+ valueToReturn << 'Two'
+ break
}
- def v1 = getValueForNumber(1)
- def v2 = getValueForNumber(2)
- def v3 = getValueForNumber(3)
- assert v1 == 'One'
- assert v2 == ['Two']
- assert v3 == null
+ valueToReturn
+ }
+ def v1 = getValueForNumber(1)
+ def v2 = getValueForNumber(2)
+ def v3 = getValueForNumber(3)
+ assert v1 == 'One'
+ assert v2 == ['Two']
+ assert v3 == null
'''
}
@@ -678,29 +697,29 @@ class TypeInferenceSTCTest extends StaticTypeCheckingTestCase {
// GROOVY-6522
void testInferenceWithImplicitClosureCoercion() {
assertScript '''
-interface CustomCallable<T> {
- T call()
-}
+ interface CustomCallable<T> {
+ T call()
+ }
-class Thing {
- static <T> T customType(CustomCallable<T> callable) {
- callable.call()
- }
+ class Thing {
+ static <T> T customType(CustomCallable<T> callable) {
+ callable.call()
+ }
- @ASTTest(phase=INSTRUCTION_SELECTION,value={
- lookup('test').each {
- def call = it.expression
- def irt = call.getNodeMetaData(INFERRED_TYPE)
- assert irt == LIST_TYPE
- }
- })
- static void run() {
- test: customType { [] } // return type is not inferred - fails compile
- }
-}
+ @ASTTest(phase=INSTRUCTION_SELECTION,value={
+ lookup('test').each {
+ def call = it.expression
+ def irt = call.getNodeMetaData(INFERRED_TYPE)
+ assert irt == LIST_TYPE
+ }
+ })
+ static void run() {
+ test: customType { [] } // return type is not inferred - fails compile
+ }
+ }
-Thing.run()
-'''
+ Thing.run()
+ '''
}
void testInferenceWithImplicitClosureCoercionAndArrayReturn() {
@@ -809,6 +828,6 @@ Thing.run()
}
meth()
-'''
+ '''
}
}