-
Notifications
You must be signed in to change notification settings - Fork 1
/
Converters.java
164 lines (148 loc) · 6.26 KB
/
Converters.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/* -*- mode: Java; c-basic-offset: 2; indent-tabs-mode: nil; coding: utf-8-unix -*-
*
* Copyright © 2018–2019 microBean.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
* implied. See the License for the specific language governing
* permissions and limitations under the License.
*/
package org.microbean.microprofile.config;
import java.lang.reflect.AnnotatedElement;
import java.lang.reflect.GenericDeclaration;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.lang.reflect.TypeVariable;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import javax.annotation.Priority;
import org.eclipse.microprofile.config.spi.Converter;
final class Converters {
private Converters() {
super();
}
static final Type getConversionType(final Converter<?> converter) {
final Type returnValue;
if (converter instanceof PrioritizedConverter) {
returnValue = ((PrioritizedConverter<?>)converter).getConversionType();
} else {
returnValue = getConversionType(Objects.requireNonNull(converter).getClass());
}
return returnValue;
}
static final int getPriority(final Converter<?> converter) {
int returnValue = 100; // per specification by default
if (converter != null) {
if (converter instanceof PrioritizedConverter) {
returnValue = ((PrioritizedConverter)converter).getPriority();
} else {
final Integer priorityInteger = getPriority(converter.getClass());
if (priorityInteger != null) {
returnValue = priorityInteger.intValue();
}
}
}
return returnValue;
}
private static final Integer getPriority(final AnnotatedElement annotatedElement) {
Integer returnValue = null;
if (annotatedElement != null) {
final Priority priority = annotatedElement.getAnnotation(Priority.class);
if (priority != null) {
returnValue = Integer.valueOf(priority.value());
}
}
return returnValue;
}
static final Type getConversionType(final Type type) {
return getConversionType(type, null, null);
}
private static final Type getConversionType(final Type type, Set<Type> seen, Map<TypeVariable<?>, Type> reifiedTypes) {
Type returnValue = null;
if (type != null) {
if (seen == null) {
seen = new HashSet<>();
}
if (reifiedTypes == null) {
reifiedTypes = new HashMap<>();
}
if (!seen.contains(type)) {
seen.add(type);
if (type instanceof Class) {
final Class<?> c = (Class<?>)type;
if (Converter.class.isAssignableFrom(c)) {
final Type[] genericInterfaces = c.getGenericInterfaces();
assert genericInterfaces != null;
for (final Type genericInterface : genericInterfaces) {
returnValue = getConversionType(genericInterface, seen, reifiedTypes); // XXX recursive call
if (returnValue != null) {
break;
}
}
if (returnValue == null) {
returnValue = getConversionType(c.getSuperclass(), seen, reifiedTypes); // XXX recursive call
}
}
} else if (type instanceof ParameterizedType) {
final ParameterizedType pt = (ParameterizedType)type;
final Type rawType = pt.getRawType();
assert rawType instanceof GenericDeclaration;
final TypeVariable<?>[] typeParameters = ((GenericDeclaration)rawType).getTypeParameters();
assert typeParameters != null;
final Type[] actualTypeArguments = pt.getActualTypeArguments();
assert actualTypeArguments != null;
assert actualTypeArguments.length == typeParameters.length;
if (actualTypeArguments.length > 0) {
for (int i = 0; i < actualTypeArguments.length; i++) {
Type actualTypeArgument = actualTypeArguments[i];
if (actualTypeArgument instanceof Class ||
actualTypeArgument instanceof ParameterizedType) {
reifiedTypes.put(typeParameters[i], actualTypeArgument);
} else if (actualTypeArgument instanceof TypeVariable) {
final Type reifiedType = reifiedTypes.get((TypeVariable)actualTypeArgument);
if (reifiedType == null) {
reifiedTypes.put((TypeVariable)actualTypeArgument, Object.class);
actualTypeArgument = Object.class;
} else {
actualTypeArgument = reifiedType;
}
assert actualTypeArgument != null;
assert (actualTypeArgument instanceof ParameterizedType || actualTypeArgument instanceof Class) : "Unexpected actualTypeArgument: " + actualTypeArgument;
reifiedTypes.put(typeParameters[i], actualTypeArgument);
}
}
}
if (Converter.class.equals(rawType)) {
assert actualTypeArguments.length == 1;
final Type typeArgument = actualTypeArguments[0];
if (typeArgument instanceof Class ||
typeArgument instanceof ParameterizedType) {
returnValue = typeArgument;
} else if (typeArgument instanceof TypeVariable) {
final TypeVariable<?> typeVariable = (TypeVariable<?>)typeArgument;
returnValue = reifiedTypes.get(typeVariable);
assert returnValue instanceof ParameterizedType || returnValue instanceof Class : "Unexpected returnValue: " + returnValue;
} else {
throw new IllegalArgumentException("Unhandled conversion type: " + typeArgument);
}
} else {
returnValue = getConversionType(rawType, seen, reifiedTypes); // XXX recursive call
}
} else {
throw new IllegalArgumentException("Unhandled type: " + type);
}
}
}
return returnValue;
}
}