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 2022/04/30 20:08:24 UTC
[groovy] 01/02: GROOVY-10607: stubgen: write non-static imports if Java references found
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
commit a7e1cee0950b9c74ec0325b4b1f979cdf492c8d1
Author: Eric Milles <er...@thomsonreuters.com>
AuthorDate: Fri Apr 29 15:06:42 2022 -0500
GROOVY-10607: stubgen: write non-static imports if Java references found
---
.../tools/javac/JavaAwareResolveVisitor.java | 15 +++---
.../groovy/tools/javac/JavaStubGenerator.java | 15 ++++++
.../groovy/tools/stubgenerator/Groovy10607.groovy | 54 ++++++++++++++++++++++
3 files changed, 77 insertions(+), 7 deletions(-)
diff --git a/src/main/java/org/codehaus/groovy/tools/javac/JavaAwareResolveVisitor.java b/src/main/java/org/codehaus/groovy/tools/javac/JavaAwareResolveVisitor.java
index 4fda6a5ac9..5cd7ea96eb 100644
--- a/src/main/java/org/codehaus/groovy/tools/javac/JavaAwareResolveVisitor.java
+++ b/src/main/java/org/codehaus/groovy/tools/javac/JavaAwareResolveVisitor.java
@@ -29,26 +29,27 @@ import static org.apache.groovy.ast.tools.ConstructorNodeUtils.getFirstIfSpecial
public class JavaAwareResolveVisitor extends ResolveVisitor {
- public JavaAwareResolveVisitor(CompilationUnit cu) {
+ public JavaAwareResolveVisitor(final CompilationUnit cu) {
super(cu);
}
@Override
- public void visitConstructor(ConstructorNode node) {
+ public void visitConstructor(final ConstructorNode node) {
super.visitConstructor(node);
Statement code = node.getCode();
Expression cce = getFirstIfSpecialConstructorCall(code);
- if (cce == null) return;
- cce.visit(this);
+ if (cce != null)
+ cce.visit(this);
}
@Override
- protected void visitClassCodeContainer(Statement code) {
+ protected void visitClassCodeContainer(final Statement stmt) {
// do nothing here, leave it to the normal resolving
}
@Override
- public void addError(String msg, ASTNode expr) {
- // do nothing here, leave it to the normal resolving
+ public void addError(final String error, final ASTNode node) {
+ if (error.startsWith("unable to resolve")) // GROOVY-10607
+ getSourceUnit().getAST().putNodeMetaData("require.imports", Boolean.TRUE);
}
}
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 be10047194..67c58b1601 100644
--- a/src/main/java/org/codehaus/groovy/tools/javac/JavaStubGenerator.java
+++ b/src/main/java/org/codehaus/groovy/tools/javac/JavaStubGenerator.java
@@ -1047,6 +1047,21 @@ public class JavaStubGenerator {
}
out.println();
+
+ // non-static imports required if any unresolved nodes encountered -- Java type(s)?
+ if (!Boolean.TRUE.equals(currentModule.getNodeMetaData("require.imports"))) return;
+
+ for (ImportNode i : currentModule.getImports()) {
+ if (i.getType().hasPackageName() && (i.getAlias() == null
+ || i.getAlias().equals(i.getType().getNameWithoutPackage())))
+ out.println("import " + i.getType().getName().replace('$', '.') + ";");
+ }
+
+ for (ImportNode si : currentModule.getStarImports()) {
+ out.println("import " + si.getPackageName() + "*;");
+ }
+
+ out.println();
}
public void clean() {
diff --git a/src/test/org/codehaus/groovy/tools/stubgenerator/Groovy10607.groovy b/src/test/org/codehaus/groovy/tools/stubgenerator/Groovy10607.groovy
new file mode 100644
index 0000000000..7ab59b6232
--- /dev/null
+++ b/src/test/org/codehaus/groovy/tools/stubgenerator/Groovy10607.groovy
@@ -0,0 +1,54 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.codehaus.groovy.tools.stubgenerator
+
+final class Groovy10607 extends StringSourcesStubTestCase {
+
+ @Override
+ Map<String, String> provideSources() {
+ [
+ 'p/Bar.java': '''
+ package p;
+ public class Bar {
+ }
+ ''',
+ 'q/Foo.groovy': '''
+ package q
+ import p.*
+ class Foo {
+ def baz(Bar b) {
+ }
+ }
+ ''',
+ 'Main.java': '''
+ public class Main {
+ public static void main(String[] args) {
+ new q.Foo();
+ }
+ }
+ ''',
+ ]
+ }
+
+ @Override
+ void verifyStubs() {
+ String stub = stubJavaSourceFor('q.Foo')
+ assert stub.contains('import p.*;')
+ }
+}