/** * Specify a target object to apply the property path to. * Alternatively, specify a target bean name. * @param targetObject a target object, for example a bean reference * or an inner bean * @see #setTargetBeanName */ public void setTargetObject(Object targetObject) { this.targetBeanWrapper = PropertyAccessorFactory.forBeanPropertyAccess(targetObject); }
/** * Create a new BeanPropertySqlParameterSource for the given bean. * @param object the bean instance to wrap */ public BeanPropertySqlParameterSource(Object object) { this.beanWrapper = PropertyAccessorFactory.forBeanPropertyAccess(object); }
/** * Specify a target object to apply the property path to. * Alternatively, specify a target bean name. * @param targetObject a target object, for example a bean reference * or an inner bean * @see #setTargetBeanName */ public void setTargetObject(Object targetObject) { this.targetBeanWrapper = PropertyAccessorFactory.forBeanPropertyAccess(targetObject); }
/** * Create a new {@link BeanWrapper} for the underlying target object. * @see #getTarget() */ protected BeanWrapper createBeanWrapper() { if (this.target == null) { throw new IllegalStateException("Cannot access properties on null bean instance '" + getObjectName() + "'"); } return PropertyAccessorFactory.forBeanPropertyAccess(this.target); }
private void writeMapEntry(TagWriter tagWriter, @Nullable String valueProperty, @Nullable String labelProperty, Map.Entry<?, ?> entry, int itemIndex) throws JspException { Object mapKey = entry.getKey(); Object mapValue = entry.getValue(); BeanWrapper mapKeyWrapper = PropertyAccessorFactory.forBeanPropertyAccess(mapKey); BeanWrapper mapValueWrapper = PropertyAccessorFactory.forBeanPropertyAccess(mapValue); Object renderValue = (valueProperty != null ? mapKeyWrapper.getPropertyValue(valueProperty) : mapKey.toString()); Object renderLabel = (labelProperty != null ? mapValueWrapper.getPropertyValue(labelProperty) : mapValue.toString()); writeElementTag(tagWriter, mapKey, renderValue, renderLabel, itemIndex); }
/** * Create a new {@link BeanWrapper} for the underlying target object. * @see #getTarget() */ protected BeanWrapper createBeanWrapper() { if (this.target == null) { throw new IllegalStateException("Cannot access properties on null bean instance '" + getObjectName() + "'"); } return PropertyAccessorFactory.forBeanPropertyAccess(this.target); }
/** * Render the inner '{@code option}' tags using the supplied * {@link Map} as the source. * @see #renderOption(TagWriter, Object, Object, Object) */ private void renderFromMap(TagWriter tagWriter) throws JspException { Map<?, ?> optionMap = (Map<?, ?>) this.optionSource; for (Map.Entry<?, ?> entry : optionMap.entrySet()) { Object mapKey = entry.getKey(); Object mapValue = entry.getValue(); Object renderValue = (this.valueProperty != null ? PropertyAccessorFactory.forBeanPropertyAccess(mapKey).getPropertyValue(this.valueProperty) : mapKey); Object renderLabel = (this.labelProperty != null ? PropertyAccessorFactory.forBeanPropertyAccess(mapValue).getPropertyValue(this.labelProperty) : mapValue); renderOption(tagWriter, mapKey, renderValue, renderLabel); } }
@Override @Nullable public Object getObject() throws BeansException { BeanWrapper target = this.targetBeanWrapper; if (target != null) { if (logger.isWarnEnabled() && this.targetBeanName != null && this.beanFactory instanceof ConfigurableBeanFactory && ((ConfigurableBeanFactory) this.beanFactory).isCurrentlyInCreation(this.targetBeanName)) { logger.warn("Target bean '" + this.targetBeanName + "' is still in creation due to a circular " + "reference - obtained value for property '" + this.propertyPath + "' may be outdated!"); } } else { // Fetch prototype target bean... Assert.state(this.beanFactory != null, "No BeanFactory available"); Assert.state(this.targetBeanName != null, "No target bean name specified"); Object bean = this.beanFactory.getBean(this.targetBeanName); target = PropertyAccessorFactory.forBeanPropertyAccess(bean); } Assert.state(this.propertyPath != null, "No property path specified"); return target.getPropertyValue(this.propertyPath); }
private void writeObjectEntry(TagWriter tagWriter, @Nullable String valueProperty, @Nullable String labelProperty, Object item, int itemIndex) throws JspException { BeanWrapper wrapper = PropertyAccessorFactory.forBeanPropertyAccess(item); Object renderValue; if (valueProperty != null) { renderValue = wrapper.getPropertyValue(valueProperty); } else if (item instanceof Enum) { renderValue = ((Enum<?>) item).name(); } else { renderValue = item; } Object renderLabel = (labelProperty != null ? wrapper.getPropertyValue(labelProperty) : item); writeElementTag(tagWriter, item, renderValue, renderLabel, itemIndex); }
this.targetBeanWrapper = PropertyAccessorFactory.forBeanPropertyAccess(bean); this.resultType = this.targetBeanWrapper.getPropertyType(this.propertyPath);
/** * Copy the properties of the supplied {@link Annotation} to the supplied target bean. * Any properties defined in {@code excludedProperties} will not be copied. * <p>A specified value resolver may resolve placeholders in property values, for example. * @param ann the annotation to copy from * @param bean the bean instance to copy to * @param valueResolver a resolve to post-process String property values (may be {@code null}) * @param excludedProperties the names of excluded properties, if any * @see org.springframework.beans.BeanWrapper */ public static void copyPropertiesToBean(Annotation ann, Object bean, @Nullable StringValueResolver valueResolver, String... excludedProperties) { Set<String> excluded = new HashSet<>(Arrays.asList(excludedProperties)); Method[] annotationProperties = ann.annotationType().getDeclaredMethods(); BeanWrapper bw = PropertyAccessorFactory.forBeanPropertyAccess(bean); for (Method annotationProperty : annotationProperties) { String propertyName = annotationProperty.getName(); if (!excluded.contains(propertyName) && bw.isWritableProperty(propertyName)) { Object value = ReflectionUtils.invokeMethod(annotationProperty, ann); if (valueResolver != null && value instanceof String) { value = valueResolver.resolveStringValue((String) value); } bw.setPropertyValue(propertyName, value); } } }
private void writeMapEntry(TagWriter tagWriter, @Nullable String valueProperty, @Nullable String labelProperty, Map.Entry<?, ?> entry, int itemIndex) throws JspException { Object mapKey = entry.getKey(); Object mapValue = entry.getValue(); BeanWrapper mapKeyWrapper = PropertyAccessorFactory.forBeanPropertyAccess(mapKey); BeanWrapper mapValueWrapper = PropertyAccessorFactory.forBeanPropertyAccess(mapValue); Object renderValue = (valueProperty != null ? mapKeyWrapper.getPropertyValue(valueProperty) : mapKey.toString()); Object renderLabel = (labelProperty != null ? mapValueWrapper.getPropertyValue(labelProperty) : mapValue.toString()); writeElementTag(tagWriter, mapKey, renderValue, renderLabel, itemIndex); }
@Override public ActivationSpec createActivationSpec(ResourceAdapter adapter, JmsActivationSpecConfig config) { Class<?> activationSpecClassToUse = this.activationSpecClass; if (activationSpecClassToUse == null) { activationSpecClassToUse = determineActivationSpecClass(adapter); if (activationSpecClassToUse == null) { throw new IllegalStateException("Property 'activationSpecClass' is required"); } } ActivationSpec spec = (ActivationSpec) BeanUtils.instantiateClass(activationSpecClassToUse); BeanWrapper bw = PropertyAccessorFactory.forBeanPropertyAccess(spec); if (this.defaultProperties != null) { bw.setPropertyValues(this.defaultProperties); } populateActivationSpecProperties(bw, config); return spec; }
/** * Render the inner '{@code option}' tags using the supplied {@link Collection} of * objects as the source. The value of the {@link #valueProperty} field is used * when rendering the '{@code value}' of the '{@code option}' and the value of the * {@link #labelProperty} property is used when rendering the label. */ private void doRenderFromCollection(Collection<?> optionCollection, TagWriter tagWriter) throws JspException { for (Object item : optionCollection) { BeanWrapper wrapper = PropertyAccessorFactory.forBeanPropertyAccess(item); Object value; if (this.valueProperty != null) { value = wrapper.getPropertyValue(this.valueProperty); } else if (item instanceof Enum) { value = ((Enum<?>) item).name(); } else { value = item; } Object label = (this.labelProperty != null ? wrapper.getPropertyValue(this.labelProperty) : item); renderOption(tagWriter, item, value, label); } }
@Override protected DefinitionsFactory createDefinitionsFactory(ApplicationContext applicationContext, LocaleResolver resolver) { if (definitionsFactoryClass != null) { DefinitionsFactory factory = BeanUtils.instantiateClass(definitionsFactoryClass); if (factory instanceof ApplicationContextAware) { ((ApplicationContextAware) factory).setApplicationContext(applicationContext); } BeanWrapper bw = PropertyAccessorFactory.forBeanPropertyAccess(factory); if (bw.isWritableProperty("localeResolver")) { bw.setPropertyValue("localeResolver", resolver); } if (bw.isWritableProperty("definitionDAO")) { bw.setPropertyValue("definitionDAO", createLocaleDefinitionDao(applicationContext, resolver)); } return factory; } else { return super.createDefinitionsFactory(applicationContext, resolver); } }
if (!pvs.isEmpty()) { try { BeanWrapper bw = PropertyAccessorFactory.forBeanPropertyAccess(this); ResourceLoader resourceLoader = new ServletContextResourceLoader(filterConfig.getServletContext()); Environment env = this.environment;
@Test public void testUntypedPropertyWithMapAtRuntime() { class Holder<D> { private final D data; public Holder(D data) { this.data = data; } public D getData() { return this.data; } } Map<String, Object> data = new HashMap<>(); data.put("x", "y"); Holder<Map<String, Object>> context = new Holder<>(data); BeanWrapper bw = PropertyAccessorFactory.forBeanPropertyAccess(context); assertEquals("y", bw.getPropertyValue("data['x']")); bw.setPropertyValue("data['message']", "it works!"); assertEquals("it works!", data.get("message")); }
/** * Map config parameters onto bean properties of this servlet, and * invoke subclass initialization. * @throws ServletException if bean properties are invalid (or required * properties are missing), or if subclass initialization fails. */ @Override public final void init() throws ServletException { // Set bean properties from init parameters. PropertyValues pvs = new ServletConfigPropertyValues(getServletConfig(), this.requiredProperties); if (!pvs.isEmpty()) { try { BeanWrapper bw = PropertyAccessorFactory.forBeanPropertyAccess(this); ResourceLoader resourceLoader = new ServletContextResourceLoader(getServletContext()); bw.registerCustomEditor(Resource.class, new ResourceEditor(resourceLoader, getEnvironment())); initBeanWrapper(bw); bw.setPropertyValues(pvs, true); } catch (BeansException ex) { if (logger.isErrorEnabled()) { logger.error("Failed to set bean properties on servlet '" + getServletName() + "'", ex); } throw ex; } } // Let subclasses do whatever initialization they like. initServletBean(); }
/** * This implementation applies the passed-in job data map as bean property * values, and delegates to {@code executeInternal} afterwards. * @see #executeInternal */ @Override public final void execute(JobExecutionContext context) throws JobExecutionException { try { BeanWrapper bw = PropertyAccessorFactory.forBeanPropertyAccess(this); MutablePropertyValues pvs = new MutablePropertyValues(); pvs.addPropertyValues(context.getScheduler().getContext()); pvs.addPropertyValues(context.getMergedJobDataMap()); bw.setPropertyValues(pvs, true); } catch (SchedulerException ex) { throw new JobExecutionException(ex); } executeInternal(context); }
BeanWrapper bw = PropertyAccessorFactory.forBeanPropertyAccess(job); MutablePropertyValues pvs = new MutablePropertyValues(); if (this.schedulerContext != null) {