Mercurial > hg > graal-compiler
comparison graal/com.oracle.truffle.dsl.processor/src/com/oracle/truffle/dsl/processor/java/transform/OrganizedImports.java @ 18757:0ec5f5a2e720
Truffle-DSL: disable automatic static imports.
author | Christian Humer <christian.humer@gmail.com> |
---|---|
date | Mon, 29 Dec 2014 23:38:39 +0100 |
parents | c22714b214d0 |
children |
comparison
equal
deleted
inserted
replaced
18756:c22714b214d0 | 18757:0ec5f5a2e720 |
---|---|
34 import com.oracle.truffle.dsl.processor.java.model.*; | 34 import com.oracle.truffle.dsl.processor.java.model.*; |
35 | 35 |
36 public final class OrganizedImports { | 36 public final class OrganizedImports { |
37 | 37 |
38 private final Map<String, String> classImportUsage = new HashMap<>(); | 38 private final Map<String, String> classImportUsage = new HashMap<>(); |
39 private final Map<String, String> staticImportUsage = new HashMap<>(); | |
40 private final Map<String, Set<String>> autoImportCache = new HashMap<>(); | 39 private final Map<String, Set<String>> autoImportCache = new HashMap<>(); |
41 | 40 |
42 private final CodeTypeElement topLevelClass; | 41 private final CodeTypeElement topLevelClass; |
43 | 42 |
44 private OrganizedImports(CodeTypeElement topLevelClass) { | 43 private OrganizedImports(CodeTypeElement topLevelClass) { |
88 public String createStaticMethodReference(Element enclosedElement, TypeMirror type, String methodName) { | 87 public String createStaticMethodReference(Element enclosedElement, TypeMirror type, String methodName) { |
89 return createStaticReference(enclosedElement, type, methodName); | 88 return createStaticReference(enclosedElement, type, methodName); |
90 } | 89 } |
91 | 90 |
92 private String createStaticReference(Element enclosedElement, TypeMirror type, String name) { | 91 private String createStaticReference(Element enclosedElement, TypeMirror type, String name) { |
93 String qualifiedName = staticImportUsage.get(name); | 92 // ambiguous import |
94 if (qualifiedName == null) { | 93 return createTypeReference(enclosedElement, type) + "." + name; |
95 // ambiguous import | |
96 return createTypeReference(enclosedElement, type) + "." + name; | |
97 } else { | |
98 // import declared and not ambiguous | |
99 return name; | |
100 } | |
101 } | 94 } |
102 | 95 |
103 private String createWildcardName(Element enclosedElement, WildcardType type) { | 96 private String createWildcardName(Element enclosedElement, WildcardType type) { |
104 StringBuilder b = new StringBuilder(); | 97 StringBuilder b = new StringBuilder(); |
105 if (type.getExtendsBound() != null) { | 98 if (type.getExtendsBound() != null) { |
148 | 141 |
149 public Set<CodeImport> generateImports() { | 142 public Set<CodeImport> generateImports() { |
150 Set<CodeImport> imports = new HashSet<>(); | 143 Set<CodeImport> imports = new HashSet<>(); |
151 | 144 |
152 imports.addAll(generateImports(classImportUsage)); | 145 imports.addAll(generateImports(classImportUsage)); |
153 imports.addAll(generateStaticImports(staticImportUsage)); | |
154 | 146 |
155 return imports; | 147 return imports; |
156 } | 148 } |
157 | 149 |
158 private boolean needsImport(Element enclosed, TypeMirror importType) { | 150 private boolean needsImport(Element enclosed, TypeMirror importType) { |
214 if (!superTypes.contains(declaredType)) { | 206 if (!superTypes.contains(declaredType)) { |
215 autoImportedTypes.add(getQualifiedName(declaredType)); | 207 autoImportedTypes.add(getQualifiedName(declaredType)); |
216 } | 208 } |
217 } | 209 } |
218 } | 210 } |
219 } | |
220 | |
221 private Set<CodeImport> generateStaticImports(Map<String, String> toGenerate) { | |
222 Set<String> autoImportedStaticTypes = new HashSet<>(); | |
223 | |
224 // if type is declared inside a super type of this class -> no import | |
225 autoImportedStaticTypes.add(getQualifiedName(topLevelClass)); | |
226 autoImportedStaticTypes.addAll(getQualifiedSuperTypeNames(topLevelClass)); | |
227 | |
228 TreeSet<CodeImport> importObjects = new TreeSet<>(); | |
229 for (String symbol : toGenerate.keySet()) { | |
230 String qualifiedName = toGenerate.get(symbol); | |
231 if (qualifiedName == null) { | |
232 // ambiguous | |
233 continue; | |
234 } | |
235 // not not import | |
236 if (autoImportedStaticTypes.contains(qualifiedName)) { | |
237 continue; | |
238 } | |
239 | |
240 importObjects.add(new CodeImport(qualifiedName, symbol, true)); | |
241 } | |
242 | |
243 return importObjects; | |
244 } | 211 } |
245 | 212 |
246 private abstract static class TypeReferenceVisitor extends CodeElementScanner<Void, Void> { | 213 private abstract static class TypeReferenceVisitor extends CodeElementScanner<Void, Void> { |
247 | 214 |
248 @Override | 215 @Override |
412 | 379 |
413 private class ImportTypeReferenceVisitor extends TypeReferenceVisitor { | 380 private class ImportTypeReferenceVisitor extends TypeReferenceVisitor { |
414 | 381 |
415 @Override | 382 @Override |
416 public void visitStaticFieldReference(Element enclosedType, TypeMirror type, String elementName) { | 383 public void visitStaticFieldReference(Element enclosedType, TypeMirror type, String elementName) { |
417 registerSymbol(staticImportUsage, ElementUtils.getQualifiedName(type), elementName); | 384 visitTypeReference(enclosedType, type); |
418 } | 385 } |
419 | 386 |
420 @Override | 387 @Override |
421 public void visitStaticMethodReference(Element enclosedType, TypeMirror type, String elementName) { | 388 public void visitStaticMethodReference(Element enclosedType, TypeMirror type, String elementName) { |
422 registerSymbol(staticImportUsage, ElementUtils.getQualifiedName(type), elementName); | 389 visitTypeReference(enclosedType, type); |
423 } | 390 } |
424 | 391 |
425 @Override | 392 @Override |
426 public void visitTypeReference(Element enclosedType, TypeMirror type) { | 393 public void visitTypeReference(Element enclosedType, TypeMirror type) { |
427 if (type != null) { | 394 if (type != null) { |