PojoUtils { public static <T, V> Setter<T, V> createSetter(Class<? extends T> pojoClass, String setterExpr, Class<? extends V> exprClass) { return createSetter(pojoClass, setterExpr, DEFAULT_EXP_OBJECT_PLACEHOLDER, DEFAULT_EXP_VAL_PLACEHOLDER, exprClass); } private  PojoUtils(); static GetterBoolean<T> createGetterBoolean(Class<? extends T> pojoClass, String getterExpr); @SuppressWarnings("unchecked") static GetterBoolean<T> createGetterBoolean(Class<? extends T> pojoClass, String getterExpr, String exprObjectPlaceholder); static GetterByte<T> createGetterByte(Class<? extends T> pojoClass, String getterExpr); @SuppressWarnings("unchecked") static GetterByte<T> createGetterByte(Class<? extends T> pojoClass, String getterExpr, String exprObjectPlaceholder); static GetterChar<T> createGetterChar(Class<? extends T> pojoClass, String getterExpr); @SuppressWarnings({ "unchecked"}) static GetterChar<T> createGetterChar(Class<? extends T> pojoClass, String getterExpr, String exprObjectPlaceholder); static GetterShort<T> createGetterShort(Class<? extends T> pojoClass, String getterExpr); @SuppressWarnings("unchecked") static GetterShort<T> createGetterShort(Class<? extends T> pojoClass, String getterExpr, String exprObjectPlaceholder); static GetterInt<T> createGetterInt(Class<? extends T> pojoClass, String getterExpr); @SuppressWarnings("unchecked") static GetterInt<T> createGetterInt(Class<? extends T> pojoClass, String getterExpr, String exprObjectPlaceholder); static GetterLong<T> createGetterLong(Class<? extends T> pojoClass, String getterExpr); @SuppressWarnings("unchecked") static GetterLong<T> createGetterLong(Class<? extends T> pojoClass, String getterExpr, String exprObjectPlaceholder); static GetterFloat<T> createGetterFloat(Class<? extends T> pojoClass, String getterExpr); @SuppressWarnings("unchecked") static GetterFloat<T> createGetterFloat(Class<? extends T> pojoClass, String getterExpr, String exprObjectPlaceholder); static GetterDouble<T> createGetterDouble(Class<? extends T> pojoClass, String getterExpr); @SuppressWarnings("unchecked") static GetterDouble<T> createGetterDouble(Class<? extends T> pojoClass, String getterExpr, String exprObjectPlaceholder); static Getter<T, V> createGetter(Class<? extends T> pojoClass, String getterExpr, Class<? extends V> exprClass); @SuppressWarnings("unchecked") static Getter<T, V> createGetter(Class<? extends T> pojoClass, String getterExpr, String exprObjectPlaceholder, Class<? extends V> exprClass); static Object constructGetter(Class<?> pojoClass, String getterExpr, Class<?> exprClass); static Object constructGetter(Class<?> pojoClass, String getterExpr, String exprObjectPlaceholder, Class<?> exprClass); static SetterBoolean<T> createSetterBoolean(Class<? extends T> pojoClass, String setterExpr); @SuppressWarnings("unchecked") static SetterBoolean<T> createSetterBoolean(Class<? extends T> pojoClass, String setterExpr, String exprObjectPlaceholder, String exprValuePlaceholder); static SetterByte<T> createSetterByte(Class<? extends T> pojoClass, String setterExpr); @SuppressWarnings("unchecked") static SetterByte<T> createSetterByte(Class<? extends T> pojoClass, String setterExpr, String exprObjectPlaceholder, String exprValuePlaceholder); static SetterChar<T> createSetterChar(Class<? extends T> pojoClass, String setterExpr); @SuppressWarnings("unchecked") static SetterChar<T> createSetterChar(Class<? extends T> pojoClass, String setterExpr, String exprObjectPlaceholder, String exprValuePlaceholder); static SetterShort<T> createSetterShort(Class<? extends T> pojoClass, String setterExpr); @SuppressWarnings("unchecked") static SetterShort<T> createSetterShort(Class<? extends T> pojoClass, String setterExpr, String exprObjectPlaceholder, String exprValuePlaceholder); static SetterInt<T> createSetterInt(Class<? extends T> pojoClass, String setterExpr); @SuppressWarnings("unchecked") static SetterInt<T> createSetterInt(Class<? extends T> pojoClass, String setterExpr, String exprObjectPlaceholder, String exprValuePlaceholder); static SetterLong<T> createSetterLong(Class<? extends T> pojoClass, String setterExpr); @SuppressWarnings("unchecked") static SetterLong<T> createSetterLong(Class<? extends T> pojoClass, String setterExpr, String exprObjectPlaceholder, String exprValuePlaceholder); static SetterFloat<T> createSetterFloat(Class<? extends T> pojoClass, String setterExpr); @SuppressWarnings("unchecked") static SetterFloat<T> createSetterFloat(Class<? extends T> pojoClass, String setterExpr, String exprObjectPlaceholder, String exprValuePlaceholder); static SetterDouble<T> createSetterDouble(Class<? extends T> pojoClass, String setterExpr); @SuppressWarnings("unchecked") static SetterDouble<T> createSetterDouble(Class<? extends T> pojoClass, String setterExpr, String exprObjectPlaceholder, String exprValuePlaceholder); static Setter<T, V> createSetter(Class<? extends T> pojoClass, String setterExpr, Class<? extends V> exprClass); @SuppressWarnings("unchecked") static Setter<T, V> createSetter(Class<? extends T> pojoClass, String setterExpr, String exprObjectPlaceholder, String exprValuePlaceholder, Class<? extends V> exprClass); static Object constructSetter(Class<?> pojoClass, String setterExpr, Class<?> exprClass); static Object constructSetter(Class<?> pojoClass, String setterExpr, String exprObjectPlaceholder, String exprValPlaceholder, Class<?> exprClass); static Expression createExpression(Class<?> inputType, String expr, Class<?> returnType); static Expression createExpression(Class<?> inputType, String expr, Class<?> returnType,
      String[] defaultImports); static Expression createExpression(Class<?> inputType, String expr, Class<?> returnType,
      String[] defaultImports, Expression.ExpressionParser parser); static final String DEFAULT_EXP_OBJECT_PLACEHOLDER; static final String DEFAULT_EXP_VAL_PLACEHOLDER; static final String DEFAULT_EXPRESSION_OBJ_PLACEHOLDER; }
@Test public void testSetterString() throws Exception { String string = innerObj.stringVal.concat("test"); createSetter(fqcn, "innerObj.stringVal", String.class).set(testObj, string); assertSame(string, innerObj.stringVal); createSetter(innerObjClass, "stringVal", String.class).set(innerObj, string = string.concat("more test")); assertEquals(string, innerObj.stringVal); createSetter(innerObjClass, "protectedStringVal", String.class).set(innerObj, string = string.concat("and more test")); assertSame(string, innerObj.protectedStringVal); createSetter(innerObjClass, "privateStringVal", String.class).set(innerObj, string = string.concat("and even more test")); assertSame(string, innerObj.getPrivateStringVal()); }
@Test public void testSetterObject() throws Exception { createSetter(fqcn, "innerObj.objVal", Object.class).set(testObj, fqcn); assertSame(fqcn, innerObj.objVal); createSetter(innerObjClass, "objVal", Object.class).set(innerObj, innerObjClass); assertSame(innerObjClass, innerObj.objVal); createSetter(innerObjClass, "protectedObjVal", Object.class).set(innerObj, innerObjClass); assertSame(innerObjClass, innerObj.protectedObjVal); createSetter(innerObjClass, "privateObjVal", Object.class).set(innerObj, innerObjClass); assertSame(innerObjClass, innerObj.getPrivateObjVal()); }
@Test (expected = IllegalArgumentException.class) public void testWrongSetterArgument() { final Class<?> testPojoClass = TestPojo.class; createSetter(testPojoClass, TestPojo.INT_FIELD_NAME, int.class); }