1
1
/*
2
- * Copyright 2002-2013 the original author or authors.
2
+ * Copyright 2002-2014 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
@@ -57,6 +57,11 @@ abstract class SerializableTypeWrapper {
57
57
private static final Class <?>[] SUPPORTED_SERIALIZABLE_TYPES = {
58
58
GenericArrayType .class , ParameterizedType .class , TypeVariable .class , WildcardType .class };
59
59
60
+ private static final Method EQUALS_METHOD = ReflectionUtils .findMethod (Object .class ,
61
+ "equals" , Object .class );
62
+
63
+ private static final Method GET_TYPE_PROVIDER_METHOD = ReflectionUtils .findMethod (
64
+ SerializableTypeProxy .class , "getTypeProvider" );
60
65
61
66
/**
62
67
* Return a {@link Serializable} variant of {@link Field#getGenericType()}.
@@ -134,7 +139,8 @@ static Type forTypeProvider(final TypeProvider provider) {
134
139
for (Class <?> type : SUPPORTED_SERIALIZABLE_TYPES ) {
135
140
if (type .isAssignableFrom (provider .getType ().getClass ())) {
136
141
ClassLoader classLoader = provider .getClass ().getClassLoader ();
137
- Class <?>[] interfaces = new Class <?>[] { type , Serializable .class };
142
+ Class <?>[] interfaces = new Class <?>[] { type ,
143
+ SerializableTypeProxy .class , Serializable .class };
138
144
InvocationHandler handler = new TypeProxyInvocationHandler (provider );
139
145
return (Type ) Proxy .newProxyInstance (classLoader , interfaces , handler );
140
146
}
@@ -143,6 +149,19 @@ static Type forTypeProvider(final TypeProvider provider) {
143
149
}
144
150
145
151
152
+ /**
153
+ * Additional interface implemented by the type proxy.
154
+ */
155
+ static interface SerializableTypeProxy {
156
+
157
+ /**
158
+ * Return the underlying type provider.
159
+ */
160
+ TypeProvider getTypeProvider ();
161
+
162
+ }
163
+
164
+
146
165
/**
147
166
* A {@link Serializable} interface providing access to a {@link Type}.
148
167
*/
@@ -190,6 +209,17 @@ public TypeProxyInvocationHandler(TypeProvider provider) {
190
209
191
210
@ Override
192
211
public Object invoke (Object proxy , Method method , Object [] args ) throws Throwable {
212
+ if (GET_TYPE_PROVIDER_METHOD .equals (method )) {
213
+ return this .provider ;
214
+ }
215
+ if (EQUALS_METHOD .equals (method )) {
216
+ Object other = args [0 ];
217
+ // Unwrap proxies for speed
218
+ while (other instanceof SerializableTypeProxy ) {
219
+ other = ((SerializableTypeProxy ) other ).getTypeProvider ().getType ();
220
+ }
221
+ return this .provider .getType ().equals (other );
222
+ }
193
223
if (Type .class .equals (method .getReturnType ()) && args == null ) {
194
224
return forTypeProvider (new MethodInvokeTypeProvider (this .provider , method , -1 ));
195
225
}
0 commit comments