groovy-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From kee...@apache.org
Subject incubator-groovy git commit: GROOVY-7509: Problem With Stub Generator And Static Import Aliases (closes #76)
Date Fri, 21 Aug 2015 14:29:35 GMT
Repository: incubator-groovy
Updated Branches:
  refs/heads/GROOVY_2_4_X 66583a853 -> 23011a235


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/23011a23
Tree: http://git-wip-us.apache.org/repos/asf/incubator-groovy/tree/23011a23
Diff: http://git-wip-us.apache.org/repos/asf/incubator-groovy/diff/23011a23

Branch: refs/heads/GROOVY_2_4_X
Commit: 23011a2352e0140f3720bb5b71d83b88738dd360
Parents: 66583a8
Author: Shil Sinha <shil.sinha@gmail.com>
Authored: Wed Jul 29 11:58:36 2015 -0400
Committer: Keegan Witt <keeganwitt@gmail.com>
Committed: Fri Aug 21 10:29:15 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/23011a23/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 f01ef0f..358326a 100644
--- a/src/main/org/codehaus/groovy/tools/javac/JavaStubGenerator.java
+++ b/src/main/org/codehaus/groovy/tools/javac/JavaStubGenerator.java
@@ -867,9 +867,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
@@ -919,7 +925,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/23011a23/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


Mime
View raw message