Skip to content

Commit

Permalink
make generic key works (#135)
Browse files Browse the repository at this point in the history
  • Loading branch information
freddyboucher authored and nmorel committed Dec 26, 2017
1 parent c2c3b12 commit 2f43303
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 9 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@

*.iml

target/
target/
gwt-unitCache/
war/
Original file line number Diff line number Diff line change
Expand Up @@ -385,7 +385,7 @@ protected final JSerializerType getJsonSerializerFromType( JType type, boolean s
* @return the {@link JSerializerType}.
* @throws com.github.nmorel.gwtjackson.rebind.exception.UnsupportedTypeException if any.
*/
protected final JSerializerType getKeySerializerFromType( JType type ) throws UnsupportedTypeException {
protected final JSerializerType getKeySerializerFromType( JType type ) throws UnsupportedTypeException, UnableToCompleteException {
return getKeySerializerFromType( type, false, false );
}

Expand All @@ -399,7 +399,7 @@ protected final JSerializerType getKeySerializerFromType( JType type ) throws Un
* @throws com.github.nmorel.gwtjackson.rebind.exception.UnsupportedTypeException if any.
*/
protected final JSerializerType getKeySerializerFromType( JType type, boolean subtype, boolean useDefault ) throws
UnsupportedTypeException {
UnsupportedTypeException, UnableToCompleteException {
JSerializerType.Builder builder = new JSerializerType.Builder().type( type );
if ( null != type.isWildcard() ) {
// For wildcard type, we use the base type to find the serializer.
Expand Down Expand Up @@ -429,7 +429,36 @@ protected final JSerializerType getKeySerializerFromType( JType type, boolean su

Optional<MapperInstance> keySerializer = configuration.getKeySerializer( type );
if ( keySerializer.isPresent() ) {
builder.instance( methodCallCode( keySerializer.get() ) );
// The type is configured in AbstractConfiguration.
if ( null != type.isParameterized() || null != type.isGenericType() ) {
JClassType[] typeArgs;
if ( null != type.isGenericType() ) {
typeArgs = type.isGenericType().asParameterizedByWildcards().getTypeArgs();
} else {
typeArgs = type.isParameterized().getTypeArgs();
}

ImmutableList.Builder<JSerializerType> parametersSerializerBuilder = ImmutableList.builder();
for ( int i = 0; i < typeArgs.length; i++ ) {
JSerializerType parameterSerializerType;
if ( keySerializer.get().getParameters().length <= i ) {
break;
}
if ( MapperType.KEY_SERIALIZER == keySerializer.get().getParameters()[i] ) {
parameterSerializerType = getKeySerializerFromType( typeArgs[i] );
} else {
parameterSerializerType = getJsonSerializerFromType( typeArgs[i], subtype );
}
parametersSerializerBuilder.add( parameterSerializerType );
}
ImmutableList<JSerializerType> parametersSerializer = parametersSerializerBuilder.build();
builder.parameters( parametersSerializer );
builder.instance( methodCallCodeWithJMapperTypeParameters( keySerializer.get(), parametersSerializer ) );

} else {
// The serializer has no parameters.
builder.instance( methodCallCode( keySerializer.get() ) );
}
return builder.build();
}

Expand Down Expand Up @@ -662,7 +691,7 @@ protected final JDeserializerType getJsonDeserializerFromType( JType type, boole
* @return the {@link JDeserializerType}.
* @throws com.github.nmorel.gwtjackson.rebind.exception.UnsupportedTypeException if any.
*/
protected final JDeserializerType getKeyDeserializerFromType( JType type ) throws UnsupportedTypeException {
protected final JDeserializerType getKeyDeserializerFromType( JType type ) throws UnsupportedTypeException, UnableToCompleteException {
return getKeyDeserializerFromType( type, false, false );
}

Expand All @@ -676,7 +705,7 @@ protected final JDeserializerType getKeyDeserializerFromType( JType type ) throw
* @throws com.github.nmorel.gwtjackson.rebind.exception.UnsupportedTypeException if any.
*/
protected final JDeserializerType getKeyDeserializerFromType( JType type, boolean subtype, boolean useDefault ) throws
UnsupportedTypeException {
UnsupportedTypeException, UnableToCompleteException {
JDeserializerType.Builder builder = new JDeserializerType.Builder().type( type );
if ( null != type.isWildcard() ) {
// For wildcard type, we use the base type to find the deserializer.
Expand Down Expand Up @@ -712,7 +741,33 @@ protected final JDeserializerType getKeyDeserializerFromType( JType type, boolea

Optional<MapperInstance> keyDeserializer = configuration.getKeyDeserializer( type );
if ( keyDeserializer.isPresent() ) {
builder.instance( methodCallCode( keyDeserializer.get() ) );
// The type is configured in AbstractConfiguration.
if ( null != type.isParameterized() || null != type.isGenericType() ) {
JClassType[] typeArgs;
if ( null != type.isGenericType() ) {
typeArgs = type.isGenericType().asParameterizedByWildcards().getTypeArgs();
} else {
typeArgs = type.isParameterized().getTypeArgs();
}

ImmutableList.Builder<JDeserializerType> parametersDeserializerBuilder = ImmutableList.builder();
for ( int i = 0; i < typeArgs.length; i++ ) {
JDeserializerType parameterDeserializerType;
if ( MapperType.KEY_DESERIALIZER == keyDeserializer.get().getParameters()[i] ) {
parameterDeserializerType = getKeyDeserializerFromType( typeArgs[i] );
} else {
parameterDeserializerType = getJsonDeserializerFromType( typeArgs[i], subtype );
}
parametersDeserializerBuilder.add( parameterDeserializerType );
}
ImmutableList<JDeserializerType> parametersDeserializer = parametersDeserializerBuilder.build();
builder.parameters( parametersDeserializer );
builder.instance( methodCallCodeWithJMapperTypeParameters( keyDeserializer.get(), parametersDeserializer ) );

} else {
// The deserializer has no parameters.
builder.instance( methodCallCode( keyDeserializer.get() ) );
}
return builder.build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -451,8 +451,8 @@ private MapperType[] getParameters( JType mappedType, JAbstractMethod method, bo
*/
private MapperInstance getKeyInstance( JType mappedType, JClassType classType, boolean isSerializers ) {
int nbParam = 0;
if ( !isSerializers && typeOracle.isEnumSupertype( mappedType ) ) {
nbParam = 1;
if ( null != mappedType.isGenericType() && (!isSerializers || !typeOracle.isEnumSupertype( mappedType )) ) {
nbParam = mappedType.isGenericType().getTypeParameters().length;
}

// we first look at static method
Expand Down

0 comments on commit 2f43303

Please sign in to comment.