Skip to content

Commit

Permalink
fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
azum committed Oct 11, 2017
1 parent eadf2be commit 40dfcb6
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 30 deletions.
Expand Up @@ -200,15 +200,10 @@ private Class<?> getCollectionReturnComponentType(Method method) throws ClassNot
ParameterizedType t2 = (ParameterizedType)t1;
Type[] actualTypeArgs = t2.getActualTypeArguments();
if (actualTypeArgs != null && actualTypeArgs.length == 1) {
try {
return Class.forName(actualTypeArgs[0].getTypeName());
} catch (ClassNotFoundException e) {
throw e;
}
return Class.forName(actualTypeArgs[0].getTypeName());
}
}
throw new ClassNotFoundException(
"could not determine generic return type for method "+method.toString());
throw new ClassNotFoundException("could not determine generic return type for method " + method.toString());
}

private Tuple2<Class<?>, Class<?>> getMapReturnComponentTypes(Method method) throws ClassNotFoundException {
Expand All @@ -221,26 +216,23 @@ private Tuple2<Class<?>, Class<?>> getMapReturnComponentTypes(Method method) thr
ParameterizedType t2 = (ParameterizedType)t1;
Type[] actualTypeArgs = t2.getActualTypeArguments();
if (actualTypeArgs != null && actualTypeArgs.length == 2) {
try {
return new Tuple2<>(
Class.forName(actualTypeArgs[0].getTypeName()),
Class.forName(actualTypeArgs[1].getTypeName()));
} catch (ClassNotFoundException e) {
throw e;
}
return new Tuple2<>(
Class.forName(actualTypeArgs[0].getTypeName()),
Class.forName(actualTypeArgs[1].getTypeName()));
}
}
throw new ClassNotFoundException(
"could not determine generic return type for method "+method.toString());
throw new ClassNotFoundException("could not determine generic return type for method " + method.toString());
}

private Object fromObjValue(Object v, Class<?> type) {
if (null == v) {
if (Boolean.TYPE.equals(type)) { // expected boolean (not Boolean)
return Boolean.TRUE.equals(v); // force true/false (also when v == null)
} else if (null == v) {
return null;
} else if (Boolean.TYPE.equals(type)) { // expected boolean
return Boolean.TRUE.equals((Boolean)v); // force true/false (also when v == null)
} else if (JsonType.isNullablePrimitive(v)) {
if (Double.class.equals(type) || double.class.equals(type)) {
if (Boolean.class.equals(type)) {
return Boolean.TRUE.equals(v);
} else if (Double.class.equals(type) || double.class.equals(type)) {
return ((Number)v).doubleValue();
} else if (Float.class.equals(type) || float.class.equals(type)) {
return ((Number)v).floatValue();
Expand All @@ -255,8 +247,7 @@ private Object fromObjValue(Object v, Class<?> type) {
Obj o = (Obj)v;
WrapType wrapType = type.getAnnotation(WrapType.class);
if (wrapType != null) {
Function<Obj, Object> builder = createBuilder(wrapType, type);
return builder.apply(o);
return createBuilder(wrapType, type).apply(o);
} else if (WrapperItemFactory.class.isAssignableFrom(type)) {
try {
Method factoryMethod = findWrapMethod(type);
Expand All @@ -267,26 +258,26 @@ private Object fromObjValue(Object v, Class<?> type) {
} else if (ObjWrapper.class.isAssignableFrom(type)) { // sub object
return WF.wrap(o, type);
} else {
throw new IllegalArgumentException("cannot convert value:"+v+" to type:"+type.getName());
throw new IllegalArgumentException("cannot convert value:" + v + " to type:" + type.getName());
}
} else {
throw new IllegalArgumentException("cannot convert value:"+v+" to type:"+type.getName());
throw new IllegalArgumentException("cannot convert value:" + v + " to type:" + type.getName());
}
}

private static final Map<Class<?>, Function<Obj, Object>> BUILDER_CACHE = new HashMap<>();
private static final Map<Class<?>, Function<Obj, Object>> BUILDERS_CACHE = new HashMap<>();
private Function<Obj, Object> createBuilder(WrapType wrapType, Class<?> type) {
Function<Obj, Object> builder = BUILDER_CACHE.get(type);
Function<Obj, Object> builder = BUILDERS_CACHE.get(type);
if (builder == null) {
String field = wrapType.field();
Map<String, Class<?>> typeMap = new HashMap<>();
String[] values = wrapType.values();
Class<?>[] types = wrapType.types();
for (int i = 0; i < values.length; i++) {
typeMap.put(values[i], types[i]);
builder = (o) -> WF.wrap(o, typeMap.get(o.fetch(field)));
BUILDER_CACHE.put(type, builder);
}
builder = (o) -> WF.wrap(o, typeMap.get(o.fetch(field)));
BUILDERS_CACHE.put(type, builder);
}
return builder;
}
Expand Down
35 changes: 33 additions & 2 deletions zen-core/src/test/java/com/nominanuda/zen/obj/ObjWrapperTest.java
Expand Up @@ -17,8 +17,10 @@

import static com.nominanuda.zen.obj.wrap.Wrap.WF;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import java.util.List;

Expand Down Expand Up @@ -64,7 +66,7 @@ public void testObjWrapper() {
assertTrue(bo2 instanceof BizObject2);
}

public interface BizObject extends ObjWrapper, TypeChooser {
interface BizObject extends ObjWrapper, TypeChooser {
void foo(String s);
String foo();
String chain1();
Expand All @@ -77,8 +79,37 @@ default String overridden() {
List<BizObject2> subObjects();
}

public interface BizObject2 extends ObjWrapper, Obj, TypeChooser {
interface BizObject2 extends ObjWrapper, Obj, TypeChooser {

}


@Test
public void testPrimitiveWrapping() {
PrimitiveObject o = WF.wrap(PrimitiveObject.class);
assertFalse(o.booleanNull());
assertNull(o.getBooleanNull());
assertNull(o.getIntegerNull());
assertNull(o.getDoubleNull());
assertNull(o.getFloatNull());
try {
o.intNull();
o.doubleNull();
o.floatNull();
fail("didn't throw exception as expected"); // should not arrive here
} catch (Exception e) {
assertTrue(e instanceof NullPointerException);
}
}

interface PrimitiveObject extends ObjWrapper {
boolean booleanNull();
Boolean getBooleanNull();
int intNull();
Integer getIntegerNull();
float floatNull();
Float getFloatNull();
double doubleNull();
Double getDoubleNull();
}
}

0 comments on commit 40dfcb6

Please sign in to comment.