How can I refactor these wrapper methods to get rid of duplicated code?
The following two methods are used to wrap deserialization using Google Gson:
public static <T> T Deserialize(String jsonData, Type t) {
T obj = null;
try {
obj = new Gson().fromJson(jsonData, t);
} catch (Exception e) {
Log.e(DEBUG_TAG, e.getMessage());
}
return obj;
}
public static <T> T Deserialize(String jsonData, Class<T> toClass) {
T obj = null;
try {
obj = new Gson().fromJson(jsonData, toClass);
} catch (Exception e) {
Log.e(DEBUG_TAG, e.getMessage());
}
return obj;
}
They are开发者_如何学C almost identical, but I can't figure out a smart way to get rid of the duplicated code.
Any suggestions?
Class
implements the interface Type
, so it looks like only having the first method should be sufficient.
EDIT: actually it looks like these methods are implemented separately for a reason. At least read the javadoc to understand why before refactoring. Thanks to home for pointing this out.
Type
is an interface implemented by Class
, so you could get rid of the second method completely.
精彩评论