Skip to content

Commit 0ae3af0

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

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
@@ -22,13 +22,12 @@ public interface ImportContext {
2222
/**
2323
* Add fqcn to the import list. Returns fqcn as needed in source code.
2424
* Attempts to handle fqcn with array and generics references.
25-
*
25+
* <p>
2626
* e.g.
2727
* java.util.Collection<org.marvel.Hulk> imports java.util.Collection and returns Collection
2828
* org.marvel.Hulk[] imports org.marvel.Hulk and returns Hulk
2929
*
3030
*
31-
* @param fqcn
3231
* @return import string
3332
*/
3433
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
@@ -18,7 +18,6 @@
1818
package org.hibernate.tool.internal.export.java;
1919

2020
import java.util.HashMap;
21-
import java.util.Iterator;
2221
import java.util.Map;
2322
import java.util.Set;
2423
import java.util.TreeSet;
@@ -57,13 +56,12 @@ public ImportContextImpl(String basePackage) {
5756
/**
5857
* Add fqcn to the import list. Returns fqcn as needed in source code.
5958
* Attempts to handle fqcn with array and generics references.
60-
*
59+
* <p>
6160
* e.g.
6261
* java.util.Collection<org.marvel.Hulk> imports java.util.Collection and returns Collection
6362
* org.marvel.Hulk[] imports org.marvel.Hulk and returns Hulk
6463
*
6564
*
66-
* @param fqcn
6765
* @return import string
6866
*/
6967
public String importType(String fqcn) {
@@ -88,14 +86,9 @@ public String importType(String fqcn) {
8886
String simpleName = StringHelper.unqualify(fqcn);
8987
if(simpleNames.containsKey(simpleName)) {
9088
String existingFqcn = (String) simpleNames.get(simpleName);
91-
if(existingFqcn.equals(pureFqcn)) {
92-
canBeSimple = true;
93-
} else {
94-
canBeSimple = false;
95-
}
89+
canBeSimple = existingFqcn.equals(pureFqcn);
9690
} else {
97-
canBeSimple = true;
98-
simpleNames.put(simpleName, pureFqcn);
91+
simpleNames.put(simpleName, pureFqcn);
9992
imports.add( pureFqcn );
10093
}
10194

@@ -127,38 +120,32 @@ public String staticImport(String fqcn, String member) {
127120
}
128121

129122
private boolean inDefaultPackage(String className) {
130-
return className.indexOf( "." ) < 0;
123+
return !className.contains(".");
131124
}
132125

133126
private boolean isPrimitive(String className) {
134127
return PRIMITIVES.containsKey( className );
135128
}
136129

137130
private boolean inSamePackage(String className) {
138-
String other = StringHelper.qualifier( className );
139-
return other == basePackage
140-
|| (other != null && other.equals( basePackage ) );
131+
return StringHelper.qualifier( className ).equals(basePackage);
141132
}
142133

143134
private boolean inJavaLang(String className) {
144135
return "java.lang".equals( StringHelper.qualifier( className ) );
145136
}
146137

147138
public String generateImports() {
148-
StringBuffer buf = new StringBuffer();
149-
150-
for ( Iterator<String> imps = imports.iterator(); imps.hasNext(); ) {
151-
String next = imps.next();
152-
if(isPrimitive(next) || inDefaultPackage(next) || inJavaLang(next) || inSamePackage(next)) {
153-
// dont add automatically "imported" stuff
154-
} else {
155-
if(staticImports.contains(next)) {
156-
buf.append("import static " + next + ";\r\n");
157-
} else {
158-
buf.append("import " + next + ";\r\n");
159-
}
160-
}
161-
}
139+
StringBuilder buf = new StringBuilder();
140+
for (String next : imports) {
141+
if (!(isPrimitive(next) || inDefaultPackage(next) || inJavaLang(next) || inSamePackage(next))) {
142+
if (staticImports.contains(next)) {
143+
buf.append("import static ").append(next).append(";\r\n");
144+
} else {
145+
buf.append("import ").append(next).append(";\r\n");
146+
}
147+
}
148+
}
162149

163150
if(buf.indexOf( "$" )>=0) {
164151
return buf.toString();

0 commit comments

Comments
 (0)