1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.mybatis.generator.api.dom.java;
17
18 public class JavaDomUtils {
19
20
21
22
23
24
25
26
27
28 public static String calculateTypeName(CompilationUnit compilationUnit, FullyQualifiedJavaType fqjt) {
29 if(compilationUnit == null
30 || typeDoesNotRequireImport(fqjt)
31 || typeIsInSamePackage(compilationUnit, fqjt)
32 || typeIsAlreadyImported(compilationUnit, fqjt)) {
33 return fqjt.getShortName();
34 } else {
35 return fqjt.getFullyQualifiedName();
36 }
37 }
38
39 private static boolean typeDoesNotRequireImport(FullyQualifiedJavaType fullyQualifiedJavaType) {
40 return fullyQualifiedJavaType.isPrimitive()
41 || !fullyQualifiedJavaType.isExplicitlyImported();
42 }
43
44 private static boolean typeIsInSamePackage(CompilationUnit compilationUnit, FullyQualifiedJavaType fullyQualifiedJavaType) {
45 return fullyQualifiedJavaType.getPackageName().equals(compilationUnit.getType().getPackageName());
46 }
47
48 private static boolean typeIsAlreadyImported(CompilationUnit compilationUnit, FullyQualifiedJavaType fullyQualifiedJavaType) {
49 FullyQualifiedJavaType nonGenericType = new FullyQualifiedJavaType(fullyQualifiedJavaType.getFullyQualifiedNameWithoutTypeParameters());
50 return compilationUnit.getImportedTypes().contains(nonGenericType);
51 }
52 }