|
1 |
| |
|
2 |
| |
|
3 |
| |
|
4 |
| |
|
5 |
| |
|
6 |
| |
|
7 |
| |
|
8 |
| |
|
9 |
| |
|
10 |
| |
|
11 |
| |
|
12 |
| |
|
13 |
| |
|
14 |
| |
|
15 |
| |
|
16 |
| package org.rblasch.convert.type; |
|
17 |
| |
|
18 |
| import org.rblasch.convert.assertion.Assertion; |
|
19 |
| |
|
20 |
| import java.util.ArrayList; |
|
21 |
| import java.util.Arrays; |
|
22 |
| import java.util.Collections; |
|
23 |
| import java.util.HashMap; |
|
24 |
| import java.util.Iterator; |
|
25 |
| import java.util.List; |
|
26 |
| import java.util.Map; |
|
27 |
| |
|
28 |
| |
|
29 |
| |
|
30 |
| |
|
31 |
| public class Types { |
|
32 |
| |
|
33 |
| private static Map types = new HashMap(); |
|
34 |
| |
|
35 |
49772
| private static boolean isKnown(final Object key) {
|
|
36 |
49772
| return types.containsKey(key);
|
|
37 |
| } |
|
38 |
| |
|
39 |
280
| private static void makeKnown(final Object key, final Type t) {
|
|
40 |
280
| Type knownT = null;
|
|
41 |
280
| if (types.containsValue(t)) {
|
|
42 |
50
| for (final Iterator i = types.values().iterator(); i.hasNext();) {
|
|
43 |
1780
| final Type ti = (Type) i.next();
|
|
44 |
1780
| if (t.equals(ti)) {
|
|
45 |
51
| knownT = ti;
|
|
46 |
| } |
|
47 |
| } |
|
48 |
| } else { |
|
49 |
230
| knownT = t;
|
|
50 |
| } |
|
51 |
280
| Assertion.ensure(knownT != null, "Known type must not be null");
|
|
52 |
| |
|
53 |
280
| types.put(key, knownT);
|
|
54 |
| } |
|
55 |
| |
|
56 |
24746
| private static Type knownType(final Object key) {
|
|
57 |
24746
| if (isKnown(key)) {
|
|
58 |
24746
| return (Type) types.get(key);
|
|
59 |
| } else { |
|
60 |
0
| throw new UnknownTypeException(key + " is not a known type");
|
|
61 |
| } |
|
62 |
| } |
|
63 |
| |
|
64 |
2411
| public static Type findTypeByName(final String name) throws UnknownTypeException {
|
|
65 |
2411
| return findType(name);
|
|
66 |
| } |
|
67 |
| |
|
68 |
2078
| public static Type primitiveArrayType() {
|
|
69 |
2078
| return ArrayType.getInstance();
|
|
70 |
| } |
|
71 |
| |
|
72 |
8
| public static Type nullType() {
|
|
73 |
8
| return NullType.getInstance();
|
|
74 |
| } |
|
75 |
| |
|
76 |
0
| public static Type findPrimitiveType(final Class c) {
|
|
77 |
0
| return new PrimitiveType(c);
|
|
78 |
| } |
|
79 |
| |
|
80 |
14835
| public static Type findTypeByClass(final Class c) {
|
|
81 |
14835
| return findType(c);
|
|
82 |
| } |
|
83 |
| |
|
84 |
0
| public static Type findPTypeByName(final String t, final String p1) throws UnknownTypeException {
|
|
85 |
0
| return findPType(t, Collections.singletonList(p1));
|
|
86 |
| } |
|
87 |
| |
|
88 |
202
| public static Type findPTypeByClass(final Class c, final Class p1) throws UnknownTypeException {
|
|
89 |
202
| return findPType(c, Collections.singletonList(p1));
|
|
90 |
| } |
|
91 |
| |
|
92 |
4488
| public static Type findPTypeByType(final Type t, final Type p1) throws UnknownTypeException {
|
|
93 |
4488
| return findPType(t, Collections.singletonList(p1));
|
|
94 |
| } |
|
95 |
| |
|
96 |
2824
| public static ParameterizableType findPTypeByType(final Type t, final List parameters) throws UnknownTypeException {
|
|
97 |
2824
| return findPType(t, parameters);
|
|
98 |
| } |
|
99 |
| |
|
100 |
7514
| private static ParameterizableType findPType(final Object type, final List parameters) throws UnknownTypeException {
|
|
101 |
7514
| final List request = new ArrayList(2);
|
|
102 |
7514
| request.add(type);
|
|
103 |
7514
| request.addAll(parameters);
|
|
104 |
7514
| if (isKnown(request)) {
|
|
105 |
7381
| return (ParameterizableType) knownType(request);
|
|
106 |
| } else { |
|
107 |
133
| final ParameterizableType t = findPTypeInternal(type, parameters);
|
|
108 |
133
| makeKnown(request, t);
|
|
109 |
133
| return t;
|
|
110 |
| } |
|
111 |
| } |
|
112 |
| |
|
113 |
17512
| private static Type findType(final Object type) throws UnknownTypeException {
|
|
114 |
17512
| if (isKnown(type)) {
|
|
115 |
17365
| return knownType(type);
|
|
116 |
| } else { |
|
117 |
147
| final Type t = findTypeInternal(type);
|
|
118 |
147
| makeKnown(type, t);
|
|
119 |
147
| return t;
|
|
120 |
| } |
|
121 |
| } |
|
122 |
| |
|
123 |
147
| private static Type findTypeInternal(final Object t) throws UnknownTypeException {
|
|
124 |
147
| if (t instanceof Type) {
|
|
125 |
42
| return (Type) t;
|
|
126 |
105
| } else if (t instanceof String) {
|
|
127 |
22
| return findTypeInternal((String) t);
|
|
128 |
83
| } else if (t instanceof Class) {
|
|
129 |
83
| return findTypeInternal((Class) t);
|
|
130 |
| } else { |
|
131 |
0
| throw new UnknownTypeException("Don't know how to resolve type " + t + " (" + t.getClass() + ")");
|
|
132 |
| } |
|
133 |
| } |
|
134 |
| |
|
135 |
22
| private static Type findTypeInternal(final String name) throws UnknownTypeException {
|
|
136 |
22
| if ("any".equals(name)) {
|
|
137 |
0
| return AnyType.getInstance();
|
|
138 |
22
| } else if ("array".equals(name)) {
|
|
139 |
3
| return ArrayType.getInstance();
|
|
140 |
| } else { |
|
141 |
19
| try {
|
|
142 |
19
| return PrimitiveType.valueOf(name);
|
|
143 |
| } catch (final UnknownTypeException ex) { |
|
144 |
6
| try {
|
|
145 |
6
| return ClassType.valueOf(name);
|
|
146 |
| } catch (final UnknownTypeException ex2) { |
|
147 |
3
| try {
|
|
148 |
3
| return ParameterizableType.valueOf(name);
|
|
149 |
| } catch (final UnknownTypeException ex3) { |
|
150 |
0
| throw new UnknownTypeException(name + " is not a known type");
|
|
151 |
| } |
|
152 |
| } |
|
153 |
| } |
|
154 |
| } |
|
155 |
| } |
|
156 |
| |
|
157 |
83
| private static Type findTypeInternal(final Class c) throws UnknownTypeException {
|
|
158 |
83
| if (c.isPrimitive()) {
|
|
159 |
4
| return new PrimitiveType(c);
|
|
160 |
79
| } else if (c.isArray()) {
|
|
161 |
5
| return new ParameterizableType(ArrayType.getInstance(),
|
|
162 |
| Arrays.asList(new Type[]{findTypeByClass(c.getComponentType())})); |
|
163 |
| } else { |
|
164 |
74
| return new ClassType(c);
|
|
165 |
| } |
|
166 |
| } |
|
167 |
| |
|
168 |
133
| private static ParameterizableType findPTypeInternal(final Object t, final List parameters) throws UnknownTypeException {
|
|
169 |
133
| final List l = new ArrayList(parameters.size());
|
|
170 |
133
| for (final Iterator i = parameters.iterator(); i.hasNext();) {
|
|
171 |
133
| l.add(findType(i.next()));
|
|
172 |
| } |
|
173 |
133
| return new ParameterizableType(findType(t), l);
|
|
174 |
| } |
|
175 |
| } |