MCPcopy Create free account
hub / github.com/apache/tomcat / getGenericType

Method getGenericType

java/org/apache/tomcat/websocket/Util.java:175–243  ·  view source on GitHub ↗
(Class<T> type, Class<? extends T> clazz)

Source from the content-addressed store, hash-verified

173
174
175 private static <T> TypeResult getGenericType(Class<T> type, Class<? extends T> clazz) {
176
177 // Look to see if this class implements the interface of interest
178
179 // Get all the interfaces
180 Type[] interfaces = clazz.getGenericInterfaces();
181 for (Type iface : interfaces) {
182 // Only need to check interfaces that use generics
183 if (iface instanceof ParameterizedType pi) {
184 // Look for the interface of interest
185 if (pi.getRawType() instanceof Class) {
186 if (type.isAssignableFrom((Class<?>) pi.getRawType())) {
187 return getTypeParameter(clazz, pi.getActualTypeArguments()[0]);
188 }
189 }
190 }
191 }
192
193 // Interface not found on this class. Look at the superclass.
194 @SuppressWarnings("unchecked")
195 Class<? extends T> superClazz = (Class<? extends T>) clazz.getSuperclass();
196 if (superClazz == null) {
197 // Finished looking up the class hierarchy without finding anything
198 throw new IllegalStateException();
199 }
200
201 TypeResult superClassTypeResult = getGenericType(type, superClazz);
202 int dimension = superClassTypeResult.getDimension();
203 if (superClassTypeResult.getIndex() == -1 && dimension == 0) {
204 // Superclass implements interface and defines explicit type for
205 // the interface of interest
206 return superClassTypeResult;
207 }
208
209 if (superClassTypeResult.getIndex() > -1) {
210 // Superclass implements interface and defines unknown type for
211 // the interface of interest
212 // Map that unknown type to the generic types defined in this class
213 ParameterizedType superClassType = (ParameterizedType) clazz.getGenericSuperclass();
214 TypeResult result =
215 getTypeParameter(clazz, superClassType.getActualTypeArguments()[superClassTypeResult.getIndex()]);
216 result.incrementDimension(superClassTypeResult.getDimension());
217 if (result.getClazz() != null && result.getDimension() > 0) {
218 superClassTypeResult = result;
219 } else {
220 return result;
221 }
222 }
223
224 if (superClassTypeResult.getDimension() > 0) {
225 StringBuilder className = new StringBuilder();
226 className.append("[".repeat(Math.max(0, dimension)));
227 className.append('L');
228 className.append(superClassTypeResult.getClazz().getCanonicalName());
229 className.append(';');
230
231 Class<?> arrayClazz;
232 try {

Callers 3

getMessageTypeMethod · 0.95
getDecoderTypeMethod · 0.95
getEncoderTypeMethod · 0.95

Calls 11

getTypeParameterMethod · 0.95
getDimensionMethod · 0.95
getIndexMethod · 0.95
incrementDimensionMethod · 0.95
getClazzMethod · 0.95
getCanonicalNameMethod · 0.80
forNameMethod · 0.80
toStringMethod · 0.65
isAssignableFromMethod · 0.45
appendMethod · 0.45
maxMethod · 0.45

Tested by

no test coverage detected