You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@groovy.apache.org by ke...@apache.org on 2015/08/21 16:28:03 UTC

incubator-groovy git commit: GROOVY-7509: Problem With Stub Generator And Static Import Aliases (closes #76)

Repository: incubator-groovy
Updated Branches:
  refs/heads/master 3619eb189 -> b9fa1f327


GROOVY-7509: Problem With Stub Generator And Static Import Aliases (closes #76)


Project: http://git-wip-us.apache.org/repos/asf/incubator-groovy/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-groovy/commit/b9fa1f32
Tree: http://git-wip-us.apache.org/repos/asf/incubator-groovy/tree/b9fa1f32
Diff: http://git-wip-us.apache.org/repos/asf/incubator-groovy/diff/b9fa1f32

Branch: refs/heads/master
Commit: b9fa1f327db3e8b70f90fe6692d5e43287bee553
Parents: 3619eb1
Author: Shil Sinha <sh...@gmail.com>
Authored: Wed Jul 29 11:58:36 2015 -0400
Committer: Keegan Witt <ke...@gmail.com>
Committed: Fri Aug 21 10:27:53 2015 -0400

----------------------------------------------------------------------
 .../groovy/tools/javac/JavaStubGenerator.java   | 11 +++-
 .../stubgenerator/ImportStaticAliasTest.groovy  | 64 ++++++++++++++++++++
 2 files changed, 73 insertions(+), 2 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-groovy/blob/b9fa1f32/src/main/org/codehaus/groovy/tools/javac/JavaStubGenerator.java
----------------------------------------------------------------------
diff --git a/src/main/org/codehaus/groovy/tools/javac/JavaStubGenerator.java b/src/main/org/codehaus/groovy/tools/javac/JavaStubGenerator.java
index a8e8b2e..66bc312 100644
--- a/src/main/org/codehaus/groovy/tools/javac/JavaStubGenerator.java
+++ b/src/main/org/codehaus/groovy/tools/javac/JavaStubGenerator.java
@@ -878,9 +878,15 @@ public class JavaStubGenerator {
                 val = constValue.toString();
             else
                 val = "\"" + escapeSpecialChars(constValue.toString()) + "\"";
-        } else if (memberValue instanceof PropertyExpression || memberValue instanceof VariableExpression) {
+        } else if (memberValue instanceof PropertyExpression) {
             // assume must be static class field or enum value or class that Java can resolve
             val = ((Expression) memberValue).getText();
+        } else if (memberValue instanceof VariableExpression) {
+            val = ((Expression) memberValue).getText();
+            //check for an alias
+            ImportNode alias = currentModule.getStaticImports().get(val);
+            if (alias != null)
+                val = alias.getClassName() + "." + alias.getFieldName();
         } else if (memberValue instanceof ClosureExpression) {
             // annotation closure; replaced with this specific class literal to cover the
             // case where annotation type uses Class<? extends Closure> for the closure's type
@@ -930,7 +936,8 @@ public class JavaStubGenerator {
         imports.addAll(Arrays.asList(ResolveVisitor.DEFAULT_IMPORTS));
 
         for (Map.Entry<String, ImportNode> entry : moduleNode.getStaticImports().entrySet()) {
-            imports.add("static "+entry.getValue().getType().getName()+"."+entry.getKey());
+            if (entry.getKey().equals(entry.getValue().getFieldName()))
+                imports.add("static "+entry.getValue().getType().getName()+"."+entry.getKey());
         }
 
         for (Map.Entry<String, ImportNode> entry : moduleNode.getStaticStarImports().entrySet()) {

http://git-wip-us.apache.org/repos/asf/incubator-groovy/blob/b9fa1f32/src/test/org/codehaus/groovy/tools/stubgenerator/ImportStaticAliasTest.groovy
----------------------------------------------------------------------
diff --git a/src/test/org/codehaus/groovy/tools/stubgenerator/ImportStaticAliasTest.groovy b/src/test/org/codehaus/groovy/tools/stubgenerator/ImportStaticAliasTest.groovy
new file mode 100644
index 0000000..54ca46d
--- /dev/null
+++ b/src/test/org/codehaus/groovy/tools/stubgenerator/ImportStaticAliasTest.groovy
@@ -0,0 +1,64 @@
+/*
+ *  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
+
+class ImportStaticAliasTest extends StringSourcesStubTestCase {
+
+    Map<String, String> provideSources() {
+        [
+                'Constants.java': '''
+                    package test;
+                    public class Constants {
+                        public static final String C1 = "c1";
+                    }
+                ''',
+
+                'MyAnnotation.java': '''
+                    package test;
+                    public @interface MyAnnotation {
+                        String value();
+                    }
+                ''',
+
+                'Test.groovy': '''
+                    package test
+                    import static test.Constants.C1 as C2
+                    @MyAnnotation(C2)
+                    class Test {
+                        def test
+                        Test(test) {
+                            this.test = test
+                        }
+                    }
+                ''',
+
+                'SomeJavaClass.java': '''
+                    package test;
+                    public class SomeJavaClass {
+                        Test test;
+                    }
+                '''
+        ]
+    }
+
+    void verifyStubs() {
+        // Stubs should not include import statements for aliased static imports
+        assert !stubJavaSourceFor('test.Test').contains('import static test.Constants.C1')
+    }
+}
\ No newline at end of file