Skip to content

Commit d8d5d10

Browse files
committed
HBX-3195: Improve the implementation of ImportContext and ImportContextImpl
Signed-off-by: Koen Aers <[email protected]>
1 parent 9d48df4 commit d8d5d10

File tree

2 files changed

+16
-30
lines changed

2 files changed

+16
-30
lines changed

orm/src/main/java/org/hibernate/tool/internal/export/java/ImportContext.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,12 @@ public interface ImportContext {
55
/**
66
* Add fqcn to the import list. Returns fqcn as needed in source code.
77
* Attempts to handle fqcn with array and generics references.
8-
*
8+
* <p>
99
* e.g.
1010
* java.util.Collection<org.marvel.Hulk> imports java.util.Collection and returns Collection
1111
* org.marvel.Hulk[] imports org.marvel.Hulk and returns Hulk
1212
*
1313
*
14-
* @param fqcn
1514
* @return import string
1615
*/
1716
public abstract String importType(String fqcn);

orm/src/main/java/org/hibernate/tool/internal/export/java/ImportContextImpl.java

Lines changed: 15 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package org.hibernate.tool.internal.export.java;
22

33
import java.util.HashMap;
4-
import java.util.Iterator;
54
import java.util.Map;
65
import java.util.Set;
76
import java.util.TreeSet;
@@ -40,13 +39,12 @@ public ImportContextImpl(String basePackage) {
4039
/**
4140
* Add fqcn to the import list. Returns fqcn as needed in source code.
4241
* Attempts to handle fqcn with array and generics references.
43-
*
42+
* <p>
4443
* e.g.
4544
* java.util.Collection<org.marvel.Hulk> imports java.util.Collection and returns Collection
4645
* org.marvel.Hulk[] imports org.marvel.Hulk and returns Hulk
4746
*
4847
*
49-
* @param fqcn
5048
* @return import string
5149
*/
5250
public String importType(String fqcn) {
@@ -71,14 +69,9 @@ public String importType(String fqcn) {
7169
String simpleName = StringHelper.unqualify(fqcn);
7270
if(simpleNames.containsKey(simpleName)) {
7371
String existingFqcn = (String) simpleNames.get(simpleName);
74-
if(existingFqcn.equals(pureFqcn)) {
75-
canBeSimple = true;
76-
} else {
77-
canBeSimple = false;
78-
}
72+
canBeSimple = existingFqcn.equals(pureFqcn);
7973
} else {
80-
canBeSimple = true;
81-
simpleNames.put(simpleName, pureFqcn);
74+
simpleNames.put(simpleName, pureFqcn);
8275
imports.add( pureFqcn );
8376
}
8477

@@ -110,38 +103,32 @@ public String staticImport(String fqcn, String member) {
110103
}
111104

112105
private boolean inDefaultPackage(String className) {
113-
return className.indexOf( "." ) < 0;
106+
return !className.contains(".");
114107
}
115108

116109
private boolean isPrimitive(String className) {
117110
return PRIMITIVES.containsKey( className );
118111
}
119112

120113
private boolean inSamePackage(String className) {
121-
String other = StringHelper.qualifier( className );
122-
return other == basePackage
123-
|| (other != null && other.equals( basePackage ) );
114+
return StringHelper.qualifier( className ).equals(basePackage);
124115
}
125116

126117
private boolean inJavaLang(String className) {
127118
return "java.lang".equals( StringHelper.qualifier( className ) );
128119
}
129120

130121
public String generateImports() {
131-
StringBuffer buf = new StringBuffer();
132-
133-
for ( Iterator<String> imps = imports.iterator(); imps.hasNext(); ) {
134-
String next = imps.next();
135-
if(isPrimitive(next) || inDefaultPackage(next) || inJavaLang(next) || inSamePackage(next)) {
136-
// dont add automatically "imported" stuff
137-
} else {
138-
if(staticImports.contains(next)) {
139-
buf.append("import static " + next + ";\r\n");
140-
} else {
141-
buf.append("import " + next + ";\r\n");
142-
}
143-
}
144-
}
122+
StringBuilder buf = new StringBuilder();
123+
for (String next : imports) {
124+
if (!(isPrimitive(next) || inDefaultPackage(next) || inJavaLang(next) || inSamePackage(next))) {
125+
if (staticImports.contains(next)) {
126+
buf.append("import static ").append(next).append(";\r\n");
127+
} else {
128+
buf.append("import ").append(next).append(";\r\n");
129+
}
130+
}
131+
}
145132

146133
if(buf.indexOf( "$" )>=0) {
147134
return buf.toString();

0 commit comments

Comments
 (0)