/
TypeVariableBindings.java
93 lines (79 loc) · 3.68 KB
/
TypeVariableBindings.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
/*
* Hibernate Validator, declare and validate application constraints
*
* License: Apache License, Version 2.0
* See the license.txt file in the root directory or <http://www.apache.org/licenses/LICENSE-2.0>.
*/
package org.hibernate.validator.internal.util;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.lang.reflect.TypeVariable;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
/**
* @author Gunnar Morling
*
*/
public class TypeVariableBindings {
private TypeVariableBindings() {
}
/**
* Returns the bindings for all the type variables from the given type's hierarchy. The returned map will contain an
* entry for each type in the given type's class hierarchy (including interfaces). Each entry is a map from the
* given type's type variables to the corresponding type variables of the type represented by that entry.
*/
public static Map<Class<?>, Map<TypeVariable<?>, TypeVariable<?>>> getTypeVariableBindings(Class<?> type) {
Map<Class<?>, Map<TypeVariable<?>, TypeVariable<?>>> allBindings = new HashMap<>();
Map<TypeVariable<?>, TypeVariable<?>> currentBindings = new HashMap<>();
TypeVariable<?>[] subTypeParameters = type.getTypeParameters();
for ( TypeVariable<?> typeVariable : subTypeParameters ) {
currentBindings.put( typeVariable, typeVariable );
}
allBindings.put( type, currentBindings );
collectTypeBindings( type, allBindings, currentBindings );
allBindings.put( Object.class, Collections.emptyMap() );
return CollectionHelper.toImmutableMap( allBindings );
}
private static void collectTypeBindings(Class<?> subType, Map<Class<?>, Map<TypeVariable<?>, TypeVariable<?>>> allBindings, Map<TypeVariable<?>, TypeVariable<?>> bindings) {
processGenericSuperType( allBindings, bindings, subType.getGenericSuperclass() );
for ( Type genericInterface : subType.getGenericInterfaces() ) {
processGenericSuperType( allBindings, bindings, genericInterface );
}
}
private static void processGenericSuperType(Map<Class<?>, Map<TypeVariable<?>, TypeVariable<?>>> allBindings,
Map<TypeVariable<?>, TypeVariable<?>> bindings, Type genericSuperType) {
if ( genericSuperType == null ) {
return;
}
else if ( genericSuperType instanceof ParameterizedType ) {
Map<TypeVariable<?>, TypeVariable<?>> newBindings = new HashMap<>();
Type[] typeArguments = ( (ParameterizedType) genericSuperType ).getActualTypeArguments();
TypeVariable<?>[] typeParameters = ( (Class<?>) ( (ParameterizedType) genericSuperType ).getRawType() ).getTypeParameters();
for ( int i = 0; i < typeArguments.length; i++ ) {
Type typeArgument = typeArguments[i];
TypeVariable<?> typeParameter = typeParameters[i];
boolean typeParameterFoundInSubType = false;
for ( Entry<TypeVariable<?>, TypeVariable<?>> subTypeParameter : bindings.entrySet() ) {
if ( typeArgument.equals( subTypeParameter.getValue() ) ) {
newBindings.put( subTypeParameter.getKey(), typeParameter );
typeParameterFoundInSubType = true;
}
}
if ( !typeParameterFoundInSubType ) {
newBindings.put( typeParameter, typeParameter );
}
}
allBindings.put( ( (Class<?>) ( (ParameterizedType) genericSuperType ).getRawType() ), newBindings );
collectTypeBindings( ( (Class<?>) ( (ParameterizedType) genericSuperType ).getRawType() ), allBindings, newBindings );
}
else if ( genericSuperType instanceof Class ) {
allBindings.put( (Class<?>) genericSuperType, Collections.emptyMap() );
collectTypeBindings( (Class<?>) genericSuperType, allBindings, new HashMap<>() );
}
else {
throw new IllegalArgumentException( "Unexpected type: " + genericSuperType );
}
}
}