public static void main(String[] args) { bf = new ClassPathXmlApplicationContext("/Spring-beans.xml"); System.out.println(bf.getBean("empresa")); BeanWrapper company = new BeanWrapperImpl((Empresa) bf.getBean("empresa")); company.setPropertyValue("nome", "Target Trust"); // ou pode ser algo assim PropertyValue value = new PropertyValue("nome", "Some Company Inc."); company.setPropertyValue(value); BeanWrapper carlos = new BeanWrapperImpl((Funcionario) bf.getBean("gestor")); carlos.setPropertyValue("nome", "Carlos Santos"); company.setPropertyValue("gestor", carlos.getWrappedInstance()); Long sal = (Long) company.getPropertyValue("gestor.salario"); System.out.println("Salário: " + sal); System.out.println(company); System.out.println(company.getWrappedInstance()); // // verifica o tipo da propriedade salario // System.out.println(((BeanWrapperImpl) // company).getPropertyDescriptor("gestor.salarlosrio").getPropertyType()); // carlos.setPropertyValue("salario", 200L); // System.out.println(carlos.getPropertyValue("salario")); }
/** * Looks for a property of the reference instance with a given name and type. * * <p>If found its value is returned. We follow the Java bean conventions with augmentation for * groovy support and static fields/properties. We will therefore match, in this order: * * <ol> * <li>Standard public bean property (with getter or just public field, using normal * introspection) * <li>Public static property with getter method * <li>Public static field * </ol> * * @return property value or null if no property or static field was found */ protected Object getPropertyOrStaticPropertyOrFieldValue(String name, Class type) { BeanWrapper ref = getReference(); Object value = null; if (ref.isReadableProperty(name)) { value = ref.getPropertyValue(name); } else if (GrailsClassUtils.isPublicField(ref.getWrappedInstance(), name)) { value = GrailsClassUtils.getFieldValue(ref.getWrappedInstance(), name); } else { value = GrailsClassUtils.getStaticPropertyValue(clazz, name); } if ((value != null) && GrailsClassUtils.isGroovyAssignableFrom(type, value.getClass())) { return value; } return null; }
/** * Get the value of the named property, with support for static properties in both Java and Groovy * classes (which as of Groovy JSR 1.0 RC 01 only have getters in the metaClass) * * @param name * @param type * @return The property value or null */ public Object getPropertyValue(String name, Class type) { // Handle standard java beans normal or static properties BeanWrapper ref = getReference(); Object value = null; if (ref.isReadableProperty(name)) { value = ref.getPropertyValue(name); } else { // Groovy workaround Object inst = ref.getWrappedInstance(); if (inst instanceof GroovyObject) { final Map properties = DefaultGroovyMethods.getProperties(inst); if (properties.containsKey(name)) { value = properties.get(name); } } } if (value != null && (type.isAssignableFrom(value.getClass()) || GrailsClassUtils.isMatchBetweenPrimativeAndWrapperTypes(type, value.getClass()))) { return value; } else { return null; } }
protected Errors validateConstraint(Constraint constraint, Object value, Boolean shouldVeto) { BeanWrapper constrainedBean = new BeanWrapperImpl(new TestClass()); constrainedBean.setPropertyValue(constraint.getPropertyName(), value); Errors errors = new BindException( constrainedBean.getWrappedInstance(), constrainedBean.getWrappedClass().getName()); if (!(constraint instanceof VetoingConstraint) || shouldVeto == null) { constraint.validate(constrainedBean.getWrappedInstance(), value, errors); } else { boolean vetoed = ((VetoingConstraint) constraint) .validateWithVetoing(constrainedBean.getWrappedInstance(), value, errors); if (shouldVeto.booleanValue() && !vetoed) fail("Constraint should veto"); else if (!shouldVeto.booleanValue() && vetoed) fail("Constraint shouldn't veto"); } return errors; }
@Override public Map<String, Object> getActionParameters() { Class<?> wrappedClazz = beanWrapper.getWrappedInstance().getClass(); Field[] fields = wrappedClazz.getDeclaredFields(); Map<String, Object> params = new HashMap<String, Object>(); for (Field field : fields) { String fieldProperty = field.getName(); if (beanWrapper.isReadableProperty(fieldProperty)) { params.put(fieldProperty, beanWrapper.getPropertyValue(fieldProperty)); } } return params; }
private Class<?> getPropertyTypeForPath(String propertyName) { Class<?> type = bean.getPropertyType(propertyName); if (type == null) { // type not available via BeanWrapper - this happens with e.g. empty list indexes - so // find type by examining GrailsDomainClass Object target = bean.getWrappedInstance(); String path = propertyName.replaceAll("\\[.+?\\]", ""); if (path.indexOf(PATH_SEPARATOR) > -1) { // transform x.y.z into value of x.y and path z target = bean.getPropertyValue(StringUtils.substringBeforeLast(propertyName, ".")); path = StringUtils.substringAfterLast(path, "."); } type = getReferencedTypeForCollection(path, target); } return type; }
@SuppressWarnings("unchecked") public void afterPropertiesSet() { if (this.name == null) { this.name = this.beanName; } if (this.group == null) { this.group = Scheduler.DEFAULT_GROUP; } if (this.applicationContextJobDataKey != null) { if (this.applicationContext == null) { throw new IllegalStateException( "JobDetailBean needs to be set up in an ApplicationContext " + "to be able to handle an 'applicationContextJobDataKey'"); } getJobDataMap().put(this.applicationContextJobDataKey, this.applicationContext); } /* JobDetailImpl jdi = new JobDetailImpl(); jdi.setName(this.name); jdi.setGroup(this.group); jdi.setJobClass(this.jobClass); jdi.setJobDataMap(this.jobDataMap); this.jobDetail = jdi; */ Class jobDetailClass; try { jobDetailClass = getClass().getClassLoader().loadClass("org.quartz.impl.JobDetailImpl"); } catch (ClassNotFoundException ex) { jobDetailClass = JobDetail.class; } BeanWrapper bw = new BeanWrapperImpl(jobDetailClass); MutablePropertyValues pvs = new MutablePropertyValues(); pvs.add("name", this.name); pvs.add("group", this.group); pvs.add("jobClass", this.jobClass); pvs.add("jobDataMap", this.jobDataMap); bw.setPropertyValues(pvs); this.jobDetail = (JobDetail) bw.getWrappedInstance(); }
@SuppressWarnings("unchecked") private Object autoCreatePropertyIfPossible( BeanWrapper wrapper, String propertyName, Object propertyValue) { propertyName = PropertyAccessorUtils.canonicalPropertyName(propertyName); int currentKeyStart = propertyName.indexOf(PropertyAccessor.PROPERTY_KEY_PREFIX_CHAR); int currentKeyEnd = propertyName.indexOf(PropertyAccessor.PROPERTY_KEY_SUFFIX_CHAR); String propertyNameWithIndex = propertyName; if (currentKeyStart > -1) { propertyName = propertyName.substring(0, currentKeyStart); } Class<?> type = wrapper.getPropertyType(propertyName); Object val = wrapper.isReadableProperty(propertyName) ? wrapper.getPropertyValue(propertyName) : null; LOG.debug( "Checking if auto-create is possible for property [" + propertyName + "] and type [" + type + "]"); if (type != null && val == null && (isDomainClass(type) || isEmbedded(wrapper, propertyName))) { if (!shouldPropertyValueSkipAutoCreate(propertyValue) && isNullAndWritableProperty(wrapper, propertyName)) { if (isDomainClass(type)) { Object created = autoInstantiateDomainInstance(type); if (created != null) { val = created; wrapper.setPropertyValue(propertyName, created); } } else if (isEmbedded(wrapper, propertyName)) { Object created = autoInstantiateEmbeddedInstance(type); if (created != null) { val = created; wrapper.setPropertyValue(propertyName, created); } } } } else { final Object beanInstance = wrapper.getWrappedInstance(); if (type != null && Collection.class.isAssignableFrom(type)) { Collection<?> c = null; final Class<?> referencedType = getReferencedTypeForCollection(propertyName, beanInstance); if (isNullAndWritableProperty(wrapper, propertyName)) { c = decorateCollectionForDomainAssociation( GrailsClassUtils.createConcreteCollection(type), referencedType); } else { if (wrapper.isReadableProperty(propertyName)) { c = decorateCollectionForDomainAssociation( (Collection<?>) wrapper.getPropertyValue(propertyName), referencedType); } } if (wrapper.isWritableProperty(propertyName) && c != null) { wrapper.setPropertyValue(propertyName, c); } val = c; if (c != null && currentKeyStart > -1 && currentKeyEnd > -1) { String indexString = propertyNameWithIndex.substring(currentKeyStart + 1, currentKeyEnd); int index = Integer.parseInt(indexString); if (isDomainClass(referencedType)) { Object instance = findIndexedValue(c, index); if (instance != null) { val = instance; } else { instance = autoInstantiateDomainInstance(referencedType); if (instance != null) { val = instance; if (index == c.size()) { addAssociationToTarget(propertyName, beanInstance, instance); } else if (index > c.size()) { while (index > c.size()) { addAssociationToTarget( propertyName, beanInstance, autoInstantiateDomainInstance(referencedType)); } addAssociationToTarget(propertyName, beanInstance, instance); } } } } } } else if (type != null && Map.class.isAssignableFrom(type)) { Map<String, Object> map; if (isNullAndWritableProperty(wrapper, propertyName)) { map = new HashMap<String, Object>(); wrapper.setPropertyValue(propertyName, map); } else { map = (Map) wrapper.getPropertyValue(propertyName); } val = map; wrapper.setPropertyValue(propertyName, val); if (currentKeyStart > -1 && currentKeyEnd > -1) { String indexString = propertyNameWithIndex.substring(currentKeyStart + 1, currentKeyEnd); Class<?> referencedType = getReferencedTypeForCollection(propertyName, beanInstance); if (isDomainClass(referencedType)) { final Object domainInstance = autoInstantiateDomainInstance(referencedType); val = domainInstance; map.put(indexString, domainInstance); } } } } return val; }
public Object getWrappedInstance() { return beanWrapper.getWrappedInstance(); }
public Object getWrappedInstance() { return sourceObject.getWrappedInstance(); }