public Object setValue(Object ctx, Object elCtx, VariableResolverFactory variableFactory, Object value) { try { return method.invoke(ctx, convert(value, targetType)); } catch (Exception e) { throw new RuntimeException("error binding property", e); } }
private static Object handleTypeCoercion(Class type, Object value) { if (type != null && value != null && value.getClass() != type) { if (!canConvert(type, value.getClass())) { throw new RuntimeException("cannot assign " + value.getClass().getName() + " to type: " + type.getName()); } try { return convert(value, type); } catch (Exception e) { throw new RuntimeException("cannot convert value of " + value.getClass().getName() + " to: " + type.getName()); } } return value; }
public void execute(BootstrapContext context) { DataConversion.addConversionHandler(Queue.class, new ConversionHandler() { @Override public Object convertFrom(Object in) { if (in instanceof Collection) { return new LinkedList((Collection) in); } return null; } @Override public boolean canConvertFrom(Class cls) { return Collection.class.isAssignableFrom(cls); } }); } }
public static Member getFieldOrWriteAccessor(Class clazz, String property, Class type) { for (Field f : clazz.getFields()) { if (property.equals(f.getName()) && (type == null || canConvert(f.getType(), type))) { return f; } } return getSetter(clazz, property, type); }
public static Method getSetter(Class clazz, String property, Class type) { String simple = "set" + property; property = ReflectionUtil.getSetter(property); for (Method meth : clazz.getMethods()) { if ((meth.getModifiers() & PUBLIC) != 0 && meth.getParameterTypes().length == 1 && (property.equals(meth.getName()) || simple.equals(meth.getName())) && (type == null || canConvert(meth.getParameterTypes()[0], type))) { return meth; } } return null; }
private Object executeOverrideTarget(Method o, Object ctx, Object value) { try { return o.invoke(ctx, convert(value, targetType)); } catch (Exception e2) { throw new RuntimeException("unable to invoke method", e2); } } }
public void setValue(Object value) { if (knownType != null && value != null && value.getClass() != knownType) { if (!canConvert(knownType, value.getClass())) { throw new RuntimeException("cannot assign " + value.getClass().getName() + " to type: " + knownType.getName()); } try { value = convert(value, knownType); } catch (Exception e) { throw new RuntimeException("cannot convert value of " + value.getClass().getName() + " to: " + knownType.getName()); } } //noinspection unchecked variableMap.put(name, value); }
public static Member getFieldOrWriteAccessor(Class clazz, String property, Class type) { for (Field f : clazz.getFields()) { if (property.equals(f.getName()) && (type == null || canConvert(f.getType(), type))) { return f; } } return getSetter(clazz, property, type); }
public void execute(BootstrapContext context) { DataConversion.addConversionHandler(Queue.class, new ConversionHandler() { @Override public Object convertFrom(Object in) { if (in instanceof Collection) { return new LinkedList((Collection) in); } return null; } @Override public boolean canConvertFrom(Class cls) { return Collection.class.isAssignableFrom(cls); } }); } }
public Object setValue(Object ctx, Object elCtx, VariableResolverFactory variableFactory, Object value) { try { return method.invoke(ctx, convert(value, targetType)); } catch (Exception e) { throw new RuntimeException("error binding property", e); } }
public void setValue(Object value) { if (knownType != null && value != null && value.getClass() != knownType) { Class t = value.getClass(); if (!canConvert(knownType, t)) { throw new RuntimeException("cannot assign " + value.getClass().getName() + " to type: " + knownType.getName()); } try { value = convert(value, knownType); } catch (Exception e) { throw new RuntimeException("cannot convert value of " + value.getClass().getName() + " to: " + knownType.getName()); } } //noinspection unchecked entry.setValue(value); }
public static Method getSetter(Class clazz, String property, Class type) { String simple = "set" + property; property = ReflectionUtil.getSetter(property); for (Method meth : clazz.getMethods()) { if ((meth.getModifiers() & PUBLIC) != 0 && meth.getParameterTypes().length == 1 && (property.equals(meth.getName()) || simple.equals(meth.getName())) && (type == null || canConvert(meth.getParameterTypes()[0], type))) { return meth; } } return null; }
public void testDataConverterStrictMode() throws Exception { OptimizerFactory.setDefaultOptimizer("ASM"); DataConversion.addConversionHandler(Date.class, new MVELDateCoercion()); ParserContext ctx = new ParserContext(); ctx.addImport("Cheese", Cheese.class); ctx.setStrongTyping(true); ctx.setStrictTypeEnforcement(true); Locale.setDefault(Locale.US); Cheese expectedCheese = new Cheese(); expectedCheese.setUseBy(new SimpleDateFormat("dd-MMM-yyyy").parse("10-Jul-1974")); ExpressionCompiler compiler = new ExpressionCompiler("c = new Cheese(); c.useBy = '10-Jul-1974'; return c", ctx); Cheese actualCheese = (Cheese) executeExpression(compiler.compile(), createTestMap()); assertEquals(expectedCheese.getUseBy(), actualCheese.getUseBy()); }
private Object executeOverrideTarget(Method o, Object ctx, Object value) { try { return o.invoke(ctx, convert(value, targetType)); } catch (Exception e2) { throw new RuntimeException("unable to invoke method", e2); } } }
private static Object handleTypeCoercion(Class type, Object value) { if (type != null && value != null && value.getClass() != type) { if (!canConvert(type, value.getClass())) { throw new RuntimeException("cannot assign " + value.getClass().getName() + " to type: " + type.getName()); } try { return convert(value, type); } catch (Exception e) { throw new RuntimeException("cannot convert value of " + value.getClass().getName() + " to: " + type.getName()); } } return value; }
public static Member getFieldOrWriteAccessor(Class clazz, String property, Class type) { for (Field f : clazz.getFields()) { if (property.equals(f.getName()) && (type == null || canConvert(f.getType(), type))) { return f; } } return getSetter(clazz, property, type); }
public static void setProperty(final Object i, final Field f, final Object v) { try { f.setAccessible(true); f.set(i, DataConversion.convert(v, f.getType())); } catch (Exception e) { throw new RuntimeException("could not set field (inst=" + i + "; field=" + f + "; val=" + v + ")", e); } }
public void setValue(Object value) { if (knownType != null && value != null && value.getClass() != knownType) { if (!canConvert(knownType, value.getClass())) { throw new RuntimeException("cannot assign " + value.getClass().getName() + " to type: " + knownType.getName()); } try { value = convert(value, knownType); } catch (Exception e) { throw new RuntimeException("cannot convert value of " + value.getClass().getName() + " to: " + knownType.getName()); } } //noinspection unchecked variableMap.put(name, value); }
public static Method getSetter(Class clazz, String property, Class type) { property = ReflectionUtil.getSetter(property); for (Method meth : clazz.getMethods()) { if ((meth.getModifiers() & PUBLIC) != 0 && meth.getParameterTypes().length == 1 && property.equals(meth.getName()) && (type == null || canConvert(meth.getParameterTypes()[0], type))) { return meth; } } return null; }
public static void setProperty(final Object i, final Field f, final Object v) { try { f.setAccessible(true); f.set(i, DataConversion.convert(v, f.getType())); } catch (Exception e) { throw new RuntimeException("could not set field (inst=" + i + "; field=" + f + "; val=" + v + ")", e); } }