BUG 428 remove importing unnecessary imports 48/5448/18
authorJozef Gloncak <jgloncak@cisco.com>
Mon, 28 Apr 2014 11:41:40 +0000 (13:41 +0200)
committerJozef Gloncak <jgloncak@cisco.com>
Mon, 30 Jun 2014 10:08:39 +0000 (12:08 +0200)
List of imports is created from all GeneratedTypes which were used while
generating interface or class.

Fully qualified name is always used for classes which belongs to
java.lang.* package

Change-Id: I14dcf1bd852ef596c6001c5bf2ffeac9a65d962b
Signed-off-by: Jozef Gloncak <jgloncak@cisco.com>
code-generator/binding-java-api-generator/src/main/java/org/opendaylight/yangtools/sal/java/api/generator/BaseTemplate.xtend
code-generator/binding-java-api-generator/src/main/java/org/opendaylight/yangtools/sal/java/api/generator/BuilderTemplate.xtend
code-generator/binding-java-api-generator/src/main/java/org/opendaylight/yangtools/sal/java/api/generator/GeneratorUtil.java
code-generator/binding-java-api-generator/src/main/java/org/opendaylight/yangtools/sal/java/api/generator/InterfaceTemplate.xtend
code-generator/binding-java-api-generator/src/test/java/org/opendaylight/yangtools/sal/java/api/generator/test/ClassCodeGeneratorTest.java
code-generator/binding-java-api-generator/src/test/java/org/opendaylight/yangtools/sal/java/api/generator/test/CompilationTest.java
code-generator/binding-java-api-generator/src/test/resources/compilation/class-name-collision/class-name-collision [new file with mode: 0644]
code-generator/binding-java-api-generator/src/test/resources/compilation/class-name-collision/configuration [new file with mode: 0644]
code-generator/binding-java-api-generator/src/test/resources/compilation/class-name-collision/other-module [new file with mode: 0644]

index fc9535e0d2bd84386d3fd8545be3511bb22b6f06..ababc45775c1109d26845bd472cd22adb951b411 100644 (file)
@@ -22,6 +22,7 @@ import org.opendaylight.yangtools.sal.binding.model.api.Restrictions
 import org.opendaylight.yangtools.sal.binding.model.api.GeneratedTransferObject
 import java.util.Collection
 import java.util.Arrays
+import java.util.HashMap
 
 abstract class BaseTemplate {
 
@@ -34,7 +35,7 @@ abstract class BaseTemplate {
             throw new IllegalArgumentException("Generated type reference cannot be NULL!")
         }
         this.type = _type;
-        this.importMap = GeneratorUtil.createImports(type)
+        this.importMap = new HashMap<String,String>()
     }
 
     def packageDefinition() '''package «type.packageName»;'''
@@ -259,8 +260,8 @@ abstract class BaseTemplate {
     def protected generateToString(Collection<GeneratedProperty> properties) '''
         «IF !properties.empty»
             @Override
-            public String toString() {
-                StringBuilder builder = new StringBuilder("«type.name» [");
+            public «String.importedName» toString() {
+                «StringBuilder.importedName» builder = new «StringBuilder.importedName»("«type.name» [");
                 boolean first = true;
 
                 «FOR property : properties»
index 7d954465df34d83b3234563e28d66c7065d81462..7dd355230387a27ca245954b12a8ab9dd826e705 100644 (file)
@@ -406,7 +406,7 @@ class BuilderTemplate extends BaseTemplate {
 
     def private generateAugmentField(boolean init) '''
         «IF augmentField != null»
-            private final «Map.importedName»<Class<? extends «augmentField.returnType.importedName»>, «augmentField.returnType.importedName»> «augmentField.name»«IF init» = new «HashMap.importedName»<>()«ENDIF»;
+            private «Map.importedName»<«Class.importedName»<? extends «augmentField.returnType.importedName»>, «augmentField.returnType.importedName»> «augmentField.name» = new «HashMap.importedName»<>();
         «ENDIF»
     '''
 
@@ -426,7 +426,7 @@ class BuilderTemplate extends BaseTemplate {
         «ENDFOR»
         «IF augmentField != null»
 
-            public «type.name»«BUILDER» add«augmentField.name.toFirstUpper»(Class<? extends «augmentField.returnType.importedName»> augmentationType, «augmentField.returnType.importedName» augmentation) {
+            public «type.name»«BUILDER» add«augmentField.name.toFirstUpper»(«Class.importedName»<? extends «augmentField.returnType.importedName»> augmentationType, «augmentField.returnType.importedName» augmentation) {
                 this.«augmentField.name».put(augmentationType, augmentation);
                 return this;
             }
@@ -479,8 +479,8 @@ class BuilderTemplate extends BaseTemplate {
                     this.«augmentField.name» = «Collections.importedName».emptyMap();
                     break;
                 case 1:
-                    final «Map.importedName».Entry<Class<? extends «augmentField.returnType.importedName»>, «augmentField.returnType.importedName»> e = builder.«augmentField.name».entrySet().iterator().next();
-                    this.«augmentField.name» = «Collections.importedName».<Class<? extends «augmentField.returnType.importedName»>, «augmentField.returnType.importedName»>singletonMap(e.getKey(), e.getValue());
+                    final «Map.importedName».Entry<«Class.importedName»<? extends «augmentField.returnType.importedName»>, «augmentField.returnType.importedName»> e = builder.«augmentField.name».entrySet().iterator().next();
+                    this.«augmentField.name» = «Collections.importedName».<«Class.importedName»<? extends «augmentField.returnType.importedName»>, «augmentField.returnType.importedName»>singletonMap(e.getKey(), e.getValue());
                     break;
                 default :
                     this.«augmentField.name» = new «HashMap.importedName»<>(builder.«augmentField.name»);
@@ -535,7 +535,7 @@ class BuilderTemplate extends BaseTemplate {
 
             @SuppressWarnings("unchecked")
             «IF addOverride»@Override«ENDIF»
-            public <E extends «augmentField.returnType.importedName»> E get«augmentField.name.toFirstUpper»(Class<E> augmentationType) {
+            public <E extends «augmentField.returnType.importedName»> E get«augmentField.name.toFirstUpper»(«Class.importedName»<E> augmentationType) {
                 if (augmentationType == null) {
                     throw new IllegalArgumentException("Augmentation Type reference cannot be NULL!");
                 }
@@ -578,7 +578,7 @@ class BuilderTemplate extends BaseTemplate {
     def protected generateEquals() '''
         «IF !properties.empty || augmentField != null»
             @Override
-            public boolean equals(java.lang.Object obj) {
+            public boolean equals(«Object.importedName» obj) {
                 if (this == obj) {
                     return true;
                 }
@@ -621,8 +621,8 @@ class BuilderTemplate extends BaseTemplate {
     def override generateToString(Collection<GeneratedProperty> properties) '''
         «IF !(properties === null)»
             @Override
-            public String toString() {
-                StringBuilder builder = new StringBuilder("«type.name» [");
+            public «String.importedName» toString() {
+                «StringBuilder.importedName» builder = new «StringBuilder.importedName» ("«type.name» [");
                 boolean first = true;
 
                 «FOR property : properties»
index c2b97b090aeffe5235fbb132bb6157c34adf3865..dae2d33a769432539a76e4136a35bcbe376fce52 100644 (file)
@@ -14,7 +14,6 @@ import java.util.ArrayList;
 import java.util.LinkedHashMap;
 import java.util.List;
 import java.util.Map;
-
 import org.opendaylight.yangtools.binding.generator.util.TypeConstants;
 import org.opendaylight.yangtools.binding.generator.util.Types;
 import org.opendaylight.yangtools.sal.binding.model.api.AnnotationType;
@@ -258,15 +257,11 @@ public final class GeneratorUtil {
             }
         } else {
             builder = new StringBuilder();
-            if (typePackageName.startsWith("java.lang")) {
-                builder.append(type.getName());
-            } else {
                 if (!typePackageName.isEmpty()) {
                     builder.append(typePackageName + Constants.DOT + type.getName());
                 } else {
                     builder.append(type.getName());
                 }
-            }
             if (type.equals(Types.voidType())) {
                 return "void";
             }
index 329680808073322ee87d9b9ebd29b713bddec487..54c2eeb45d16a6f641eee1a0c48831c64de979dc 100644 (file)
@@ -87,7 +87,7 @@ class InterfaceTemplate extends BaseTemplate {
     def private generateAnnotations(List<AnnotationType> annotations) '''
         «IF annotations != null && !annotations.empty»
             «FOR annotation : annotations»
-                @«annotation.name»
+                @«annotation.importedName»
                 «IF annotation.parameters != null && !annotation.parameters.empty»
                 (
                 «FOR param : annotation.parameters SEPARATOR ","»
index 9a318715275a70362a5dbda35790181a6da37c19..63fb27cd2fef08151499e02cd6f61bf0773af07b 100644 (file)
@@ -7,14 +7,15 @@
  */
 package org.opendaylight.yangtools.sal.java.api.generator.test;
 
-import static org.junit.Assert.*;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
 
 import java.io.File;
 import java.net.URISyntaxException;
 import java.util.ArrayList;
 import java.util.List;
 import java.util.Set;
-
 import org.junit.BeforeClass;
 import org.junit.Ignore;
 import org.junit.Test;
@@ -83,7 +84,7 @@ public class ClassCodeGeneratorTest {
                     final String outputStr = clsGen.generate(genTO);
 
                     assertNotNull(outputStr);
-                    assertTrue(outputStr.contains("public CompositeKeyListKey(Byte _key1, String _key2)"));
+                    assertTrue(outputStr.contains("public CompositeKeyListKey(java.lang.Byte _key1, java.lang.String _key2)"));
 
                     assertEquals(2, propertyCount);
                     genTOsCount++;
index b9f73bb3b2b14ecbe1f9559a841744de024c42f4..2cd6d0ce746d7c7377b47631e38b92e9acb87523 100644 (file)
@@ -537,6 +537,18 @@ public class CompilationTest extends BaseCompilationTest {
         cleanUp(sourcesOutputDir, compiledOutputDir);
     }
 
+    @Test
+    public void classNamesColisionTest() throws Exception {
+        final File sourcesOutputDir = new File(GENERATOR_OUTPUT_PATH + FS + "class-name-collision");
+        assertTrue("Failed to create test file '" + sourcesOutputDir + "'", sourcesOutputDir.mkdir());
+        final File compiledOutputDir = new File(COMPILER_OUTPUT_PATH + FS + "class-name-collision");
+        assertTrue("Failed to create test file '" + compiledOutputDir + "'", compiledOutputDir.mkdir());
+
+        generateTestSources("/compilation/class-name-collision", sourcesOutputDir);
+        testCompilation(sourcesOutputDir, compiledOutputDir);
+        cleanUp(sourcesOutputDir, compiledOutputDir);
+    }
+
     private void generateTestSources(String resourceDirPath, File sourcesOutputDir) throws Exception {
         final List<File> sourceFiles = getSourceFiles(resourceDirPath);
         final Set<Module> modulesToBuild = parser.parseYangModels(sourceFiles);
diff --git a/code-generator/binding-java-api-generator/src/test/resources/compilation/class-name-collision/class-name-collision b/code-generator/binding-java-api-generator/src/test/resources/compilation/class-name-collision/class-name-collision
new file mode 100644 (file)
index 0000000..6325820
--- /dev/null
@@ -0,0 +1,31 @@
+/*
+ * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+module class-name-collision {
+    yang-version 1;
+    namespace "urn:class:name:collision";
+    prefix "bar";
+
+    revision "2014-04-28" {
+
+    }
+
+    list string {
+    }
+
+    list string-builder {
+    }
+
+    list class {
+    }
+
+    list set {
+    }
+
+    list map {
+    }
+}
diff --git a/code-generator/binding-java-api-generator/src/test/resources/compilation/class-name-collision/configuration b/code-generator/binding-java-api-generator/src/test/resources/compilation/class-name-collision/configuration
new file mode 100644 (file)
index 0000000..10058ab
--- /dev/null
@@ -0,0 +1,27 @@
+/*
+ * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+module configuration {
+    yang-version 1;
+    namespace "urn:configuration";
+    prefix "bar";
+
+    revision "2014-04-30" {
+    }
+
+    container configuration {
+        list class {
+            key "name";
+            leaf name {
+                type string;
+            }
+            leaf value {
+                type string;
+            }
+       }
+    }
+}
diff --git a/code-generator/binding-java-api-generator/src/test/resources/compilation/class-name-collision/other-module b/code-generator/binding-java-api-generator/src/test/resources/compilation/class-name-collision/other-module
new file mode 100644 (file)
index 0000000..46964be
--- /dev/null
@@ -0,0 +1,21 @@
+/*
+ * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+module other-module {
+    yang-version 1;
+    namespace "urn:other-module";
+    prefix "bar";
+
+    revision "2014-04-29" {
+    }
+
+    list foo {
+    }
+
+    list boo {
+    }
+}