/** * Get the SortDefinition's property value for the given object. * @param obj the object to get the property value for * @return the property value */ @Nullable private Object getPropertyValue(Object obj) { // If a nested property cannot be read, simply return null // (similar to JSTL EL). If the property doesn't exist in the // first place, let the exception through. try { this.beanWrapper.setWrappedInstance(obj); return this.beanWrapper.getPropertyValue(this.sortDefinition.getProperty()); } catch (BeansException ex) { logger.debug("PropertyComparator could not access property - treating as null for sorting", ex); return null; } }
public void test(BeanWrapperImpl wrapper, Errors errors) { Matcher m = requiredPattern.matcher( (CharSequence)wrapper.getPropertyValue(propertyName)); if(!m.matches()) { errors.rejectValue(propertyName, null, errorMessage); } }
/** * This will be used to resolve expressions in step-scoped beans. */ @Override public Object resolveContextualObject(String key) { StepContext context = getContext(); // TODO: support for attributes as well maybe (setters not exposed yet // so not urgent). return new BeanWrapperImpl(context).getPropertyValue(key); }
/** * This will be used to resolve expressions in job-scoped beans. */ @Override public Object resolveContextualObject(String key) { JobContext context = getContext(); // TODO: support for attributes as well maybe (setters not exposed yet // so not urgent). return new BeanWrapperImpl(context).getPropertyValue(key); }
protected Map<String, Object> buildParametersDescription( InitializrMetadata metadata) { Map<String, Object> result = new LinkedHashMap<>(); BeanWrapperImpl wrapper = new BeanWrapperImpl(metadata); for (PropertyDescriptor descriptor : wrapper.getPropertyDescriptors()) { Object value = wrapper.getPropertyValue(descriptor.getName()); BeanWrapperImpl nested = new BeanWrapperImpl(value); if (nested.isReadableProperty("description") && nested.isReadableProperty("id")) { result.put((String) nested.getPropertyValue("id"), nested.getPropertyValue("description")); } } result.put("applicationName", "application name"); result.put("baseDir", "base directory to create in the archive"); return result; }
@Test(expected = InvalidPropertyException.class) public void getPropertyValueAutoGrowListNotParameterized() { wrapper.getPropertyValue("listNotParameterized[0]"); }
@Test(expected = NullValueInNestedPathException.class) public void getPropertyValueNullValueInNestedPathNoDefaultConstructor() { wrapper.getPropertyValue("nestedNoConstructor.prop"); }
/** * Get the SortDefinition's property value for the given object. * @param obj the object to get the property value for * @return the property value */ @Nullable private Object getPropertyValue(Object obj) { // If a nested property cannot be read, simply return null // (similar to JSTL EL). If the property doesn't exist in the // first place, let the exception through. try { this.beanWrapper.setWrappedInstance(obj); return this.beanWrapper.getPropertyValue(this.sortDefinition.getProperty()); } catch (BeansException ex) { logger.debug("PropertyComparator could not access property - treating as null for sorting", ex); return null; } }
@Override public Object getProperty(String path) { int index = PropertyAccessorUtils.getFirstNestedPropertySeparatorIndex(path); BeanWrapperImpl wrapper = createBeanWrapper(path, index); String key = path.substring(index + 1); return wrapper.getPropertyValue(key); }
@Test public void getPropertyValueNullValueInNestedPath() { assertNull(wrapper.getPropertyValue("nested.prop")); }
private Object getPropertyValue(Object bean, String nestedName) { BeanWrapperImpl wrapper = new BeanWrapperImpl(bean); wrapper.setAutoGrowNestedPaths(true); Object nestedValue = wrapper.getPropertyValue(nestedName); if (nestedValue == null) { try { nestedValue = wrapper.getPropertyType(nestedName).newInstance(); wrapper.setPropertyValue(nestedName, nestedValue); } catch (InstantiationException | IllegalAccessException e) { ReflectionUtils.handleReflectionException(e); } } return nestedValue; }
@Test public void getPropertyValueAutoGrowMultiDimensionalList() { assertNotNull(wrapper.getPropertyValue("multiList[0][0]")); assertEquals(1, bean.getMultiList().get(0).size()); assertThat(bean.getMultiList().get(0).get(0), instanceOf(Bean.class)); }
@Test public void getPropertyValueAutoGrowListBySeveralElements() { assertNotNull(wrapper.getPropertyValue("list[4]")); assertEquals(5, bean.getList().size()); assertThat(bean.getList().get(0), instanceOf(Bean.class)); assertThat(bean.getList().get(1), instanceOf(Bean.class)); assertThat(bean.getList().get(2), instanceOf(Bean.class)); assertThat(bean.getList().get(3), instanceOf(Bean.class)); assertThat(bean.getList().get(4), instanceOf(Bean.class)); assertNotNull(wrapper.getPropertyValue("list[0]")); assertNotNull(wrapper.getPropertyValue("list[1]")); assertNotNull(wrapper.getPropertyValue("list[2]")); assertNotNull(wrapper.getPropertyValue("list[3]")); }
/** * Render the home page with the specified template. * @param model the model data */ protected void renderHome(Map<String, Object> model) { InitializrMetadata metadata = this.metadataProvider.get(); model.put("serviceUrl", generateAppUrl()); BeanWrapperImpl wrapper = new BeanWrapperImpl(metadata); for (PropertyDescriptor descriptor : wrapper.getPropertyDescriptors()) { if ("types".equals(descriptor.getName())) { model.put("types", removeTypes(metadata.getTypes())); } else { model.put(descriptor.getName(), wrapper.getPropertyValue(descriptor.getName())); } } // Google analytics support model.put("trackingCode", metadata.getConfiguration().getEnv().getGoogleAnalyticsTrackingCode()); }
@Test public void getPropertyValueAutoGrowList() { assertNotNull(wrapper.getPropertyValue("list[0]")); assertEquals(1, bean.getList().size()); assertThat(bean.getList().get(0), instanceOf(Bean.class)); }
@Test public void getPropertyValueAutoGrowArrayBySeveralElements() { assertNotNull(wrapper.getPropertyValue("array[4]")); assertEquals(5, bean.getArray().length); assertThat(bean.getArray()[0], instanceOf(Bean.class)); assertThat(bean.getArray()[1], instanceOf(Bean.class)); assertThat(bean.getArray()[2], instanceOf(Bean.class)); assertThat(bean.getArray()[3], instanceOf(Bean.class)); assertThat(bean.getArray()[4], instanceOf(Bean.class)); assertNotNull(wrapper.getPropertyValue("array[0]")); assertNotNull(wrapper.getPropertyValue("array[1]")); assertNotNull(wrapper.getPropertyValue("array[2]")); assertNotNull(wrapper.getPropertyValue("array[3]")); }
@Test public void getPropertyValueAutoGrowMultiDimensionalArray() { assertNotNull(wrapper.getPropertyValue("multiArray[0][0]")); assertEquals(1, bean.getMultiArray()[0].length); assertThat(bean.getMultiArray()[0][0], instanceOf(Bean.class)); }
@Test public void getPropertyValueAutoGrowArray() { assertNotNull(wrapper.getPropertyValue("array[0]")); assertEquals(1, bean.getArray().length); assertThat(bean.getArray()[0], instanceOf(Bean.class)); }
public void acceptRepresentation(Representation entity) throws ResourceException { if (appCtx == null) { throw new ResourceException(404); } // copy op? Form form = getRequest().getEntityAsForm(); beanPath = form.getFirstValue("beanPath"); String newVal = form.getFirstValue("newVal"); if(newVal!=null) { int i = beanPath.indexOf("."); String beanName = i<0?beanPath:beanPath.substring(0,i); Object namedBean = appCtx.getBean(beanName); BeanWrapperImpl bwrap = new BeanWrapperImpl(namedBean); String propPath = beanPath.substring(i+1); Object coercedVal = bwrap.convertIfNecessary(newVal, bwrap.getPropertyValue(propPath).getClass()); bwrap.setPropertyValue(propPath, coercedVal); } Reference ref = getRequest().getResourceRef(); ref.setPath(getBeansRefPath()); ref.addSegment(beanPath); getResponse().redirectSeeOther(ref); }
@Test public void getPropertyValueAutoGrowListFailsAgainstLimit() { wrapper.setAutoGrowCollectionLimit(2); try { assertNotNull(wrapper.getPropertyValue("list[4]")); fail("Should have thrown InvalidPropertyException"); } catch (InvalidPropertyException ex) { // expected assertTrue(ex.getRootCause() instanceof IndexOutOfBoundsException); } }