Skip to content

Commit

Permalink
final touches to make Collections work as Lists
Browse files Browse the repository at this point in the history
  • Loading branch information
vasvir committed Aug 13, 2014
1 parent 1d4f1e0 commit c77005c
Showing 1 changed file with 22 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import java.lang.reflect.Constructor;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
Expand Down Expand Up @@ -67,6 +68,7 @@ public class JsonEncoderDecoderInstanceLocator {
public final JClassType MAP_TYPE;
public final JClassType SET_TYPE;
public final JClassType LIST_TYPE;
public final JClassType COLLECTION_TYPE;

public final HashMap<JType, String> builtInEncoderDecoders = new HashMap<JType, String>();
public final JsonSerializerGenerators customGenerators = new JsonSerializerGenerators();
Expand All @@ -85,6 +87,7 @@ public JsonEncoderDecoderInstanceLocator(GeneratorContext context, TreeLogger lo
this.MAP_TYPE = find(Map.class);
this.SET_TYPE = find(Set.class);
this.LIST_TYPE = find(List.class);
this.COLLECTION_TYPE = find(Collection.class);

builtInEncoderDecoders.put(JPrimitiveType.BOOLEAN, JSON_ENCODER_DECODER_CLASS + ".BOOLEAN");
builtInEncoderDecoders.put(JPrimitiveType.BYTE, JSON_ENCODER_DECODER_CLASS + ".BYTE");
Expand Down Expand Up @@ -231,6 +234,11 @@ else if (encoderDecoder != null) {
if (encoderDecoder != null) {
return listMethod + "(" + expression + ", " + encoderDecoder + ")";
}

encoderDecoder = isCollectionEncoderDecoder(clazz, types, style);
if (encoderDecoder != null) {
return listMethod + "(" + expression + ", " + encoderDecoder + ")";
}
}

encoderDecoder = isArrayEncoderDecoder(type, style);
Expand Down Expand Up @@ -350,6 +358,19 @@ protected String isListEncoderDecoder( JClassType clazz,
return null;
}

protected String isCollectionEncoderDecoder(JClassType clazz, JClassType[] types,
Style style) throws UnableToCompleteException {
if (clazz.isAssignableTo(COLLECTION_TYPE)) {
if (types.length != 1) {
error("Collection must define one and only one type parameter");
}
String encoderDecoder = getNestedEncoderDecoder(types[0], style);
debug("type encoder for: " + types[0] + " is " + encoderDecoder);
return encoderDecoder;
}
return null;
}

protected JClassType[] getTypes(JType type) throws UnableToCompleteException {
JParameterizedType parameterizedType = type.isParameterized();
if (parameterizedType == null || parameterizedType.getTypeArgs() == null) {
Expand All @@ -360,7 +381,7 @@ protected JClassType[] getTypes(JType type) throws UnableToCompleteException {
}
boolean isCollectionType(JClassType clazz) {
return clazz != null
&& (clazz.isAssignableTo(SET_TYPE) || clazz.isAssignableTo(LIST_TYPE) || clazz.isAssignableTo(MAP_TYPE));
&& (clazz.isAssignableTo(SET_TYPE) || clazz.isAssignableTo(LIST_TYPE) || clazz.isAssignableTo(MAP_TYPE) || clazz.isAssignableTo(COLLECTION_TYPE));
}

protected void error(String msg) throws UnableToCompleteException {
Expand Down

0 comments on commit c77005c

Please sign in to comment.