|
5 | 5 | import com.google.common.base.Optional; |
6 | 6 | import com.google.common.collect.FluentIterable; |
7 | 7 | import com.google.common.collect.ImmutableList; |
| 8 | +import com.google.common.collect.ImmutableMap; |
8 | 9 | import com.google.common.collect.Sets; |
9 | 10 |
|
10 | 11 | import java.util.*; |
|
17 | 18 | */ |
18 | 19 | public class TypeHelper { |
19 | 20 | private static ImmutableList<String> PARSED_TYPES_DELIMITERS = ImmutableList.of(",", "<", ">"); |
| 21 | + private static ImmutableMap<String, String> TYPE_DESCRIPTION_ALIASES = ImmutableMap.of( |
| 22 | + Integer.class.getCanonicalName(), "int", |
| 23 | + Iterable.class.getCanonicalName(), "LIST", |
| 24 | + List.class.getCanonicalName(), "LIST", |
| 25 | + Map.class.getCanonicalName(), "MAP"); |
20 | 26 | private static Pattern guavaOptionalPattern = Pattern.compile("\\Q" + Optional.class.getName() + "<\\E(.+)>"); |
21 | 27 | private static Pattern java8OptionalPattern = Pattern.compile("\\Qjava.util.Optional<\\E(.+)>"); |
22 | 28 | private static Set<String> RAW_TYPES_STR = Sets.newHashSet("byte", "short", "int", "long", "float", "double", "boolean", "char"); |
@@ -50,8 +56,43 @@ public String apply(ParsedType param) { |
50 | 56 | } |
51 | 57 | } |
52 | 58 |
|
53 | | - static String getTypeExpressionFor(String type) { |
54 | | - StringTokenizer tokenizer = new StringTokenizer(type+"\n", Joiner.on("").join(PARSED_TYPES_DELIMITERS), true); |
| 59 | + static String toTypeDescription(ParsedType parsedType) { |
| 60 | + boolean aliasedType = TYPE_DESCRIPTION_ALIASES.containsKey(parsedType.className); |
| 61 | + String type; |
| 62 | + if(aliasedType) { |
| 63 | + type = TYPE_DESCRIPTION_ALIASES.get(parsedType.className); |
| 64 | + } else { |
| 65 | + boolean primitive = parsedType.className.startsWith("java.lang"); |
| 66 | + type = parsedType.className.substring(parsedType.className.lastIndexOf('.') + 1); |
| 67 | + if (primitive) { |
| 68 | + type = type.toLowerCase(); |
| 69 | + } |
| 70 | + if ("DateTime".equals(type) || "DateMidnight".equals(type)) { |
| 71 | + type = "Date"; |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + if(parsedType.parameters.isEmpty()) { // We're on a raw type |
| 76 | + return type; |
| 77 | + } else { // We're on a parameterized type |
| 78 | + String parametersTypeDescriptions = FluentIterable.from(parsedType.parameters) |
| 79 | + .transform(new Function<ParsedType, String>() { |
| 80 | + @Override |
| 81 | + public String apply(ParsedType param) { |
| 82 | + return toTypeDescription(param); |
| 83 | + } |
| 84 | + }).join(Joiner.on(", ")); |
| 85 | + |
| 86 | + if(aliasedType) { |
| 87 | + return String.format("%s[%s]", type, parametersTypeDescriptions); |
| 88 | + } else { |
| 89 | + return String.format("%s<%s>", type, parametersTypeDescriptions); |
| 90 | + } |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + static ParsedType parseParameterizedType(String parameterizedType) { |
| 95 | + StringTokenizer tokenizer = new StringTokenizer(parameterizedType+"\n", Joiner.on("").join(PARSED_TYPES_DELIMITERS), true); |
55 | 96 | String[] tokens = new String[tokenizer.countTokens()]; |
56 | 97 | ParsedType rootParsedType = null; |
57 | 98 | ParsedType currentParsedType = null; |
@@ -87,8 +128,15 @@ static String getTypeExpressionFor(String type) { |
87 | 128 | } |
88 | 129 | } |
89 | 130 | } |
| 131 | + return rootParsedType; |
| 132 | + } |
90 | 133 |
|
91 | | - return getTypeExpressionFor(rootParsedType); |
| 134 | + static String toTypeDescription(String type) { |
| 135 | + return toTypeDescription(parseParameterizedType(type)); |
| 136 | + } |
| 137 | + |
| 138 | + static String getTypeExpressionFor(String type) { |
| 139 | + return getTypeExpressionFor(parseParameterizedType(type)); |
92 | 140 | } |
93 | 141 |
|
94 | 142 | static boolean isParameterizedType(String type) { |
|
0 commit comments