public OgnlContext createContext(Object target) { OgnlContext result = (OgnlContext)Ognl.createDefaultContext(target, _ognlResolver); if (_typeConverter != null) Ognl.setTypeConverter(result, _typeConverter); return result; }
public OgnlContext createContext(Object target) { OgnlContext result = (OgnlContext)Ognl.createDefaultContext(target, _ognlResolver); if (_typeConverter != null) Ognl.setTypeConverter(result, _typeConverter); return result; }
@SuppressWarnings("unchecked") private OgnlExpression(List<String> expressions, Object... targets) { this.targets = targets.length != 0 ? targets : NO_ROOT; this.expressions = expressions; this.context = Ognl.createDefaultContext(null); Ognl.setTypeConverter(this.context, new OgnlTypeConverter()); }
/** * Sets the object's properties using the default type converter. * * @param props the properties being set * @param o the object * @param context the action context * @param throwPropertyExceptions boolean which tells whether it should throw exceptions for * problems setting the properties */ public void setProperties(Map<String, ?> props, Object o, Map<String, Object> context, boolean throwPropertyExceptions) throws ReflectionException{ if (props == null) { return; } Ognl.setTypeConverter(context, getTypeConverterFromContext(context)); Object oldRoot = Ognl.getRoot(context); Ognl.setRoot(context, o); for (Map.Entry<String, ?> entry : props.entrySet()) { String expression = entry.getKey(); internalSetProperty(expression, entry.getValue(), o, context, throwPropertyExceptions); } Ognl.setRoot(context, oldRoot); }
@Test @SuppressWarnings("unchecked") public void shouldInvokePrimitiveConverter() throws OgnlException { when(converters.to(int.class)).thenReturn(converter); when(converter.convert("2", int.class, bundle)).thenReturn(2); Map<?,?> context = Ognl.createDefaultContext(myCat); Ognl.setTypeConverter(context, adapter); Ognl.setValue("length", context, myCat, "2"); assertThat(myCat.length, is(equalTo(2))); }
@SuppressWarnings("unchecked") @Test public void shouldInvokeCustomTypeConverter() throws OgnlException { when(converters.to(Tail.class)).thenReturn(converter); when(converter.convert("15", Tail.class, bundle)).thenReturn(new Tail(15)); Map<?,?> context = Ognl.createDefaultContext(myCat); Ognl.setTypeConverter(context, adapter); Ognl.setValue("tail", context, myCat, "15"); assertThat(myCat.tail.length, is(equalTo(15))); }
Ognl.setTypeConverter(contextFrom, conv); final Map contextTo = createDefaultContext(to, null); Ognl.setTypeConverter(contextTo, conv);
@Test public void shouldNotInstantiateIfLastTerm() throws OgnlException, NoSuchMethodException { final TypeConverter typeConverter = mock(TypeConverter.class); final House house = new House(); final Mouse tom = new Mouse(); Method method = House.class.getDeclaredMethod("setMouse", Mouse.class); when(typeConverter.convertValue(context, house, method, "mouse", "22", Mouse.class)).thenReturn(tom); Ognl.setTypeConverter(context, typeConverter); Ognl.setValue("mouse", context, house, "22"); assertThat(house.getMouse(), is(equalTo(tom))); }
/** * Sets the named property to the supplied value on the Object. * * @param name the name of the property to be set * @param value the value to set into the named property * @param o the object upon which to set the property * @param context the context which may include the TypeConverter * @param throwPropertyExceptions boolean which tells whether it should throw exceptions for * problems setting the property */ public void setProperty(String name, Object value, Object o, Map<String, Object> context, boolean throwPropertyExceptions) { Ognl.setTypeConverter(context, getTypeConverterFromContext(context)); Object oldRoot = Ognl.getRoot(context); Ognl.setRoot(context, o); internalSetProperty(name, value, o, context, throwPropertyExceptions); Ognl.setRoot(context, oldRoot); }
public void startContext(String name, Type type, Object root, ResourceBundle bundle) { OgnlContext context = createOgnlContext(root); context.setTraceEvaluations(true); context.put("rootType", type); context.put("removal", removal); context.put("nullHandler", nullHandler()); context.put(ResourceBundle.class, bundle); context.put("proxifier", proxifier); Ognl.setTypeConverter(context, createAdapter(bundle)); contexts.put(name, context); }
@Test(expected=IllegalArgumentException.class) public void shouldThrowExceptionIfNoConverterIsFound() throws Throwable { when(converters.to(Tail.class)).thenReturn(null); Map<?,?> context = Ognl.createDefaultContext(myCat); Ognl.setTypeConverter(context, adapter); try { Ognl.setValue("tail", context, myCat, "15"); } catch (OgnlException e) { throw e.getCause(); } } }
public Object getValue(Object context) throws EvaluationException { try { Map evaluationContext = Ognl.addDefaultContext(context, getVariables(context)); Ognl.setTypeConverter(evaluationContext, createTypeConverter()); return Ognl.getValue(expression, evaluationContext, context, expectedResultType); } catch (NoSuchPropertyException e) { throw new PropertyNotFoundException(context.getClass(), getExpressionString(), e); } catch (OgnlException e) { if (e.getReason() instanceof ValueCoercionException) { throw (ValueCoercionException) e.getReason(); } else { throw new EvaluationException(context.getClass(), getExpressionString(), "An OgnlException occurred getting the value for expression '" + getExpressionString() + "' on context [" + context.getClass() + "]", causeFor(e)); } } }
public void setValue(Object context, Object value) { try { Map evaluationContext = Ognl.addDefaultContext(context, getVariables(context)); Ognl.setTypeConverter(evaluationContext, createTypeConverter()); Ognl.setValue(expression, evaluationContext, context, value); } catch (NoSuchPropertyException e) { throw new PropertyNotFoundException(context.getClass(), getExpressionString(), e); } catch (OgnlException e) { if (e.getReason() instanceof ValueCoercionException) { throw (ValueCoercionException) e.getReason(); } else { throw new EvaluationException(context.getClass(), getExpressionString(), "An OgnlException occurred setting the value of expression '" + getExpressionString() + "' on context [" + context.getClass() + "] to [" + value + "]", causeFor(e)); } } }
@Before public void setup() throws Exception { MockitoAnnotations.initMocks(this); AbstractOgnlTestSupport.configOgnl(converters); this.house = new House(); this.context = (OgnlContext) Ognl.createDefaultContext(house); context.setTraceEvaluations(true); context.put("nullHandler", new GenericNullHandler(removal)); // OgnlRuntime.setPropertyAccessor(Set.class, new SetAccessor()); // OgnlRuntime.setPropertyAccessor(Map.class, new MapAccessor()); this.bundle = ResourceBundle.getBundle("messages"); Ognl.setTypeConverter(context, new VRaptorConvertersAdapter(converters, bundle)); when(converters.to(String.class)).thenReturn(new StringConverter()); when(converters.to(Long.class)).thenReturn(new LongConverter()); context.put("proxifier", new JavassistProxifier(new ReflectionInstanceCreator())); }
@Before public void setup() throws Exception { MockitoAnnotations.initMocks(this); AbstractOgnlTestSupport.configOgnl(converters); removal = new EmptyElementsRemoval(); bundle = ResourceBundle.getBundle("messages"); when(container.instanceFor(Converters.class)).thenReturn(converters); when(converters.to(Long.class)).thenReturn(new LongConverter()); when(converters.to(String.class)).thenReturn(new StringConverter()); when(container.instanceFor(EmptyElementsRemoval.class)).thenReturn(removal); this.myCat = new Cat(); this.context = (OgnlContext) Ognl.createDefaultContext(myCat); context.setTraceEvaluations(true); context.put(Container.class, container); context.put("removal", removal); context.put("nullHandler", new GenericNullHandler(removal)); // OgnlRuntime.setPropertyAccessor(Set.class, new SetAccessor()); // OgnlRuntime.setPropertyAccessor(Map.class, new MapAccessor()); Ognl.setTypeConverter(context, new VRaptorConvertersAdapter(converters, bundle)); context.put("proxifier", new JavassistProxifier(new ReflectionInstanceCreator())); }