|
32 | 32 |
|
33 | 33 | package org.scijava.util; |
34 | 34 |
|
35 | | -import com.googlecode.gentyref.GenericTypeReflector; |
36 | | - |
37 | 35 | import java.lang.reflect.Field; |
38 | 36 | import java.lang.reflect.Method; |
39 | 37 | import java.lang.reflect.Type; |
40 | 38 | import java.util.List; |
41 | 39 |
|
42 | | -/** |
43 | | - * Useful methods for working with {@link Type} objects, particularly generic |
44 | | - * types. |
45 | | - * <p> |
46 | | - * This class leans heavily on the excellent <a |
47 | | - * href="https://code.google.com/p/gentyref/">gentyref</a> library, and exists |
48 | | - * mainly to keep the gentyref dependency encapsulated within SciJava Common. |
49 | | - * </p> |
50 | | - * |
51 | | - * @author Curtis Rueden |
52 | | - * @see ClassUtils For utility methods specific to {@link Class} objects. |
53 | | - * @see ConversionUtils For utility methods that convert between {@link Type}s. |
54 | | - */ |
| 40 | +import org.scijava.util.Types; |
| 41 | + |
| 42 | +/** @deprecated Use {@link Types} instead. */ |
| 43 | +@Deprecated |
55 | 44 | public final class GenericUtils { |
56 | 45 |
|
57 | 46 | private GenericUtils() { |
58 | 47 | // prevent instantiation of utility class |
59 | 48 | } |
60 | 49 |
|
61 | | - /** |
62 | | - * Gets the sole raw class corresponding to the given type, or null if none. |
63 | | - */ |
| 50 | + /** @deprecated Use {@link Types#raw} instead. */ |
| 51 | + @Deprecated |
64 | 52 | public static Class<?> getClass(final Type type) { |
65 | | - if (type == null) return null; |
66 | | - if (type instanceof Class) return (Class<?>) type; |
67 | | - final List<Class<?>> c = getClasses(type); |
68 | | - if (c == null || c.size() != 1) return null; |
69 | | - return c.get(0); |
| 53 | + final List<Class<?>> bounds = Types.raws(type); |
| 54 | + return bounds != null && bounds.size() == 1 ? bounds.get(0) : null; |
70 | 55 | } |
71 | 56 |
|
72 | | - /** |
73 | | - * Gets all raw classes corresponding to the given type. |
74 | | - * <p> |
75 | | - * For example, a type parameter {@code A extends Number & Iterable} will |
76 | | - * return both {@link Number} and {@link Iterable} as its raw classes. |
77 | | - * </p> |
78 | | - */ |
| 57 | + /** @deprecated Use {@link Types#raws} instead. */ |
| 58 | + @Deprecated |
79 | 59 | public static List<Class<?>> getClasses(final Type type) { |
80 | | - if (type == null) return null; |
81 | | - return GenericTypeReflector.getUpperBoundClassAndInterfaces(type); |
| 60 | + return Types.raws(type); |
82 | 61 | } |
83 | 62 |
|
84 | | - /** |
85 | | - * Gets the component type of the given array type, or null if not an array. |
86 | | - */ |
| 63 | + /** @deprecated Use {@link Types#component} instead. */ |
| 64 | + @Deprecated |
87 | 65 | public static Type getComponentType(final Type type) { |
88 | | - return GenericTypeReflector.getArrayComponentType(type); |
| 66 | + return Types.component(type); |
89 | 67 | } |
90 | 68 |
|
91 | 69 | /** |
92 | | - * Gets the sole component class of the given array type, or null if none. |
| 70 | + * @deprecated Use {@link Types#component} and {@link Types#raw} instead. |
93 | 71 | */ |
| 72 | + @Deprecated |
94 | 73 | public static Class<?> getComponentClass(final Type type) { |
95 | | - return getClass(getComponentType(type)); |
| 74 | + return Types.raw(Types.component(type)); |
96 | 75 | } |
97 | 76 |
|
98 | | - /** |
99 | | - * Returns the "safe" generic type of the given field, as viewed from the |
100 | | - * given type. This may be narrower than what {@link Field#getGenericType()} |
101 | | - * returns, if the field is declared in a superclass, or {@code type} has a |
102 | | - * type parameter that is used in the type of the field. |
103 | | - * <p> |
104 | | - * For example, suppose we have the following three classes: |
105 | | - * </p> |
106 | | - * |
107 | | - * <pre> |
108 | | - * public class Thing<T> { |
109 | | - * public T thing; |
110 | | - * } |
111 | | - * |
112 | | - * public class NumberThing<N extends Number> extends Thing<N> { } |
113 | | - * |
114 | | - * public class IntegerThing extends NumberThing<Integer> { } |
115 | | - * </pre> |
116 | | - * |
117 | | - * Then this method operates as follows: |
118 | | - * |
119 | | - * <pre> |
120 | | - * field = ClassUtils.getField(Thing.class, "thing"); |
121 | | - * |
122 | | - * field.getType(); // Object |
123 | | - * field.getGenericType(); // T |
124 | | - * |
125 | | - * GenericUtils.getFieldType(field, Thing.class); // T |
126 | | - * GenericUtils.getFieldType(field, NumberThing.class); // N extends Number |
127 | | - * GenericUtils.getFieldType(field, IntegerThing.class); // Integer |
128 | | - * </pre> |
129 | | - */ |
| 77 | + /** @deprecated Use {@link Types#type(Field, Class)} instead. */ |
| 78 | + @Deprecated |
130 | 79 | public static Type getFieldType(final Field field, final Class<?> type) { |
131 | | - final Type wildType = GenericTypeReflector.addWildcardParameters(type); |
132 | | - return GenericTypeReflector.getExactFieldType(field, wildType); |
| 80 | + return Types.type(field, type); |
133 | 81 | } |
134 | 82 |
|
135 | 83 | /** |
136 | | - * Returns the "safe" class(es) of the given field, as viewed from the |
137 | | - * specified type. This may be narrower than what {@link Field#getType()} |
138 | | - * returns, if the field is declared in a superclass, or {@code type} has a |
139 | | - * type parameter that is used in the type of the field. |
140 | | - * <p> |
141 | | - * For example, suppose we have the following three classes: |
142 | | - * </p> |
143 | | - * |
144 | | - * <pre> |
145 | | - * |
146 | | - * public class Thing<T> { |
147 | | - * |
148 | | - * public T thing; |
149 | | - * } |
150 | | - * |
151 | | - * public class NumberThing<N extends Number> extends Thing<N> {} |
152 | | - * |
153 | | - * public class IntegerThing extends NumberThing<Integer> {} |
154 | | - * </pre> |
155 | | - * |
156 | | - * Then this method operates as follows: |
157 | | - * |
158 | | - * <pre> |
159 | | - * field = ClassUtils.getField(Thing.class, "thing"); |
160 | | - * |
161 | | - * field.getType(); // Object |
162 | | - * |
163 | | - * ClassUtils.getTypes(field, Thing.class).get(0); // Object |
164 | | - * ClassUtils.getTypes(field, NumberThing.class).get(0); // Number |
165 | | - * ClassUtils.getTypes(field, IntegerThing.class).get(0); // Integer |
166 | | - * </pre> |
167 | | - * <p> |
168 | | - * In cases of complex generics which take the intersection of multiple types |
169 | | - * using the {@code &} operator, there may be multiple types returned by this |
170 | | - * method. For example: |
171 | | - * </p> |
172 | | - * |
173 | | - * <pre> |
174 | | - * public class ComplexThing<T extends Serializable & Cloneable> extends Thing<T> {} |
175 | | - * |
176 | | - * ClassUtils.getTypes(field, ComplexThing.class); // Serializable, Cloneable |
177 | | - * </pre> |
178 | | - * |
179 | | - * @see #getFieldType(Field, Class) |
180 | | - * @see #getClasses(Type) |
| 84 | + * @deprecated Use {@link Types#type(Field, Class)} and {@link Types#raws} |
| 85 | + * instead. |
181 | 86 | */ |
| 87 | + @Deprecated |
182 | 88 | public static List<Class<?>> getFieldClasses(final Field field, |
183 | 89 | final Class<?> type) |
184 | 90 | { |
185 | | - final Type genericType = getFieldType(field, type); |
186 | | - return getClasses(genericType); |
| 91 | + return Types.raws(Types.type(field, type)); |
187 | 92 | } |
188 | 93 |
|
189 | | - /** |
190 | | - * As {@link #getFieldType(Field, Class)}, but with respect to the return |
191 | | - * type of the given {@link Method} rather than a {@link Field}. |
192 | | - */ |
| 94 | + /** @deprecated Use {@link Types#returnType} instead. */ |
| 95 | + @Deprecated |
193 | 96 | public static Type getMethodReturnType(final Method method, |
194 | 97 | final Class<?> type) |
195 | 98 | { |
196 | | - final Type wildType = GenericTypeReflector.addWildcardParameters(type); |
197 | | - return GenericTypeReflector.getExactReturnType(method, wildType); |
| 99 | + return Types.returnType(method, type); |
198 | 100 | } |
199 | 101 |
|
200 | 102 | /** |
201 | | - * As {@link #getFieldClasses(Field, Class)}, but with respect to the return |
202 | | - * type of the given {@link Method} rather than a {@link Field}. |
203 | | - * |
204 | | - * @see #getMethodReturnType(Method, Class) |
205 | | - * @see #getClasses(Type) |
| 103 | + * @deprecated Use {@link Types#returnType} and {@link Types#raws} instead. |
206 | 104 | */ |
207 | | - public static List<Class<?>> |
208 | | - getMethodReturnClasses(final Method method, final Class<?> type) |
| 105 | + @Deprecated |
| 106 | + public static List<Class<?>> getMethodReturnClasses(final Method method, |
| 107 | + final Class<?> type) |
209 | 108 | { |
210 | | - final Type genericType = getMethodReturnType(method, type); |
211 | | - return getClasses(genericType); |
| 109 | + return Types.raws(Types.returnType(method, type)); |
212 | 110 | } |
213 | 111 |
|
214 | | - /** |
215 | | - * Gets the given type's {@code n}th type parameter of the specified class. |
216 | | - * <p> |
217 | | - * For example, with class {@code StringList implements List<String>}, |
218 | | - * {@code getTypeParameter(StringList.class, Collection.class, 0)} returns |
219 | | - * {@code String}. |
220 | | - * </p> |
221 | | - */ |
| 112 | + /** @deprecated Use {@link Types#param} instead. */ |
| 113 | + @Deprecated |
222 | 114 | public static Type getTypeParameter(final Type type, final Class<?> c, |
223 | 115 | final int paramNo) |
224 | 116 | { |
225 | | - return GenericTypeReflector.getTypeParameter(type, |
226 | | - c.getTypeParameters()[paramNo]); |
| 117 | + return Types.param(type, c, paramNo); |
227 | 118 | } |
228 | 119 |
|
229 | 120 | } |
0 commit comments