PojoUtils { public static Expression createExpression(Class<?> inputType, String expr, Class<?> returnType) { return createExpression(inputType, expr, returnType, null); } 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 (expected = RuntimeException.class) public void testPrivateFieldExpression() { final Class<?> testPojoClass = TestPojo.class; createExpression(testPojoClass, "privateIntField", int.class); }
@Test public void testBasicExpression() { TestPojo testObj = new TestPojo(1); Class<?> testObjClass = testObj.getClass(); assertEquals(testObj.getIntVal(), createExpression(testObjClass, "intField", int.class).execute(testObj)); assertEquals(testObj.getIntVal(), createExpression(testObjClass, "intVal", int.class).execute(testObj)); }
@Test public void testNestedPOJOExpression() { Expression expression = createExpression(fqcn, "innerObj.boolVal", boolean.class); assertTrue((Boolean)expression.execute(testObj)); expression = createExpression(fqcn, "{$.innerObj.boolVal}", boolean.class); assertTrue((Boolean)expression.execute(testObj)); expression = createExpression(fqcn, "$.innerObj.boolVal", boolean.class); assertTrue((Boolean)expression.execute(testObj)); expression = createExpression(fqcn, "!{$.innerObj.boolVal}", boolean.class); assertFalse((Boolean)expression.execute(testObj)); expression = createExpression(fqcn, "!{$}.innerObj.boolVal", boolean.class); assertFalse((Boolean)expression.execute(testObj)); }
@Test public void testComplexPOJOExpression() { Expression expression = createExpression(fqcn, "{$}.innerObj.boolVal && ({$}.innerObj.intVal == 11)", boolean.class); assertTrue((Boolean)expression.execute(testObj)); expression = createExpression(fqcn, "valueOf({$.innerObj.privateIntVal}) + substring({$.innerObj.stringVal}, 2) + {$.innerObj.privateStringVal}.length()", String.class, new String[]{"org.apache.commons.lang3.StringUtils.*", "java.lang.String.valueOf"}); assertEquals("11llo5", expression.execute(testObj)); expression = createExpression(fqcn, "round(pow({$.innerObj.privateFloatVal}, {$.innerObj.privateDoubleVal}))", long.class, new String[] {"java.lang.Math.*"}); assertEquals(46162L, expression.execute(testObj)); expression = createExpression(fqcn, "{$.innerObj.privateFloatVal} > 0 && {$.innerObj.privateStringVal}.length() > 0", boolean.class); assertEquals(true, expression.execute(testObj)); }
@Test public void testExpressionSerialization() { Expression expression = createExpression(fqcn, "{$.innerObj.boolVal}", boolean.class); assertTrue((Boolean)expression.execute(testObj)); }
@Test public void testCustomImports() { Expression expression = createExpression(fqcn, "concat({$.innerObj.stringVal}, {$.innerObj.privateStringVal})", String.class, new String[] {TestImports.class.getName() + ".concat"}); assertEquals("hello hello", expression.execute(testObj)); }