-
Notifications
You must be signed in to change notification settings - Fork 4.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix Gson.getDelegateAdapter
not working properly for JsonAdapter
#2435
Changes from 2 commits
9e77d39
1513897
e96d323
3e6d3e4
3417cf2
f5a27be
cc4cc45
bdaf608
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -88,6 +88,19 @@ static String checkInstantiable(Class<?> c) { | |
return null; | ||
} | ||
|
||
private static <T> T useInstanceCreator(InstanceCreator<T> instanceCreator, Type type, Class<?> rawType) { | ||
T instance = instanceCreator.createInstance(type); | ||
if (instance == null) { | ||
throw new RuntimeException("InstanceCreator " + instanceCreator + " returned null for type " + type); | ||
} | ||
|
||
if (!rawType.isInstance(instance)) { | ||
throw new ClassCastException("InstanceCreator " + instanceCreator + " created instance of wrong type;" | ||
+ " expected " + rawType.getName() + " but got instance of unrelated type " + instance.getClass().getName()); | ||
} | ||
return instance; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added this for sanity, and to avoid some checks in |
||
} | ||
|
||
public <T> ObjectConstructor<T> get(TypeToken<T> typeToken) { | ||
final Type type = typeToken.getType(); | ||
final Class<? super T> rawType = typeToken.getRawType(); | ||
|
@@ -99,7 +112,7 @@ public <T> ObjectConstructor<T> get(TypeToken<T> typeToken) { | |
if (typeCreator != null) { | ||
return new ObjectConstructor<T>() { | ||
@Override public T construct() { | ||
return typeCreator.createInstance(type); | ||
return useInstanceCreator(typeCreator, type, rawType); | ||
} | ||
}; | ||
} | ||
|
@@ -111,7 +124,7 @@ public <T> ObjectConstructor<T> get(TypeToken<T> typeToken) { | |
if (rawTypeCreator != null) { | ||
return new ObjectConstructor<T>() { | ||
@Override public T construct() { | ||
return rawTypeCreator.createInstance(type); | ||
return useInstanceCreator(rawTypeCreator, type, rawType); | ||
} | ||
}; | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The section starting here is new; the changes above only remove one redundant leading space after the
*
(unfortunately the GitHub diff does not indicate that very well).