List of usage examples for java.beans PropertyDescriptor getName
public String getName()
From source file:com.wavemaker.runtime.data.util.QueryHandler.java
private Object cloneObject(Object oldObj, Object newObj, Class cls) { PropertyDescriptor[] beanProps; try {//from w w w. java 2s .c o m beanProps = Introspector.getBeanInfo(cls).getPropertyDescriptors(); for (PropertyDescriptor propertyDescriptor : beanProps) { if (propertyDescriptor.getName().equals("class")) { continue; } Method getter = propertyDescriptor.getReadMethod(); Method setter = propertyDescriptor.getWriteMethod(); Object val = getter.invoke(oldObj); setter.invoke(newObj, val); } } catch (Exception ex) { throw new WMRuntimeException(ex); } return newObj; }
From source file:com.dexcoder.dal.spring.mapper.JdbcRowMapper.java
/** * Initialize the mapping metadata for the given class. * @param mappedClass the mapped class/*from www . j ava 2s . c om*/ */ protected void initialize(Class<T> mappedClass) { this.mappedClass = mappedClass; this.mappedFields = new HashMap<String, PropertyDescriptor>(); this.mappedProperties = new HashSet<String>(); PropertyDescriptor[] pds = BeanUtils.getPropertyDescriptors(mappedClass); for (PropertyDescriptor pd : pds) { if (pd.getWriteMethod() != null) { Method readMethod = pd.getReadMethod(); if (readMethod != null) { Column aColumn = readMethod.getAnnotation(Column.class); if (aColumn != null) { String name = NameUtils.getLegalName(aColumn.value()); this.mappedFields.put(lowerCaseName(name), pd); } } this.mappedFields.put(lowerCaseName(pd.getName()), pd); String underscoredName = underscoreName(pd.getName()); if (!lowerCaseName(pd.getName()).equals(underscoredName)) { this.mappedFields.put(underscoredName, pd); } this.mappedProperties.add(pd.getName()); } } }
From source file:com.mx.core.dao.BeanPropRowMap.java
/** * Extract the values for all columns in the current row. * <p>Utilizes public setters and result set metadata. * @see java.sql.ResultSetMetaData// ww w .jav a 2s.c o m */ public T mapRow(ResultSet rs, int rowNumber) throws SQLException { Assert.state(this.mappedClass != null, "Mapped class was not specified"); T mappedObject = BeanUtils.instantiate(this.mappedClass); BeanWrapper bw = PropertyAccessorFactory.forBeanPropertyAccess(mappedObject); initBeanWrapper(bw); ResultSetMetaData rsmd = rs.getMetaData(); int columnCount = rsmd.getColumnCount(); Set<String> populatedProperties = (isCheckFullyPopulated() ? new HashSet<String>() : null); for (int index = 1; index <= columnCount; index++) { String column = JdbcUtils.lookupColumnName(rsmd, index); PropertyDescriptor pd = this.mappedFields.get(column.replaceAll(" ", "").toLowerCase()); if (pd != null) { try { Object value = getColumnValue(rs, index, pd); if (logger.isDebugEnabled() && rowNumber == 0) { //logger.debug("Mapping column '" + column + "' to property '" + // pd.getName() + "' of type " + pd.getPropertyType()); } try { bw.setPropertyValue(pd.getName(), value); } catch (TypeMismatchException e) { if (value == null && primitivesDefaultedForNullValue) { logger.debug("Intercepted TypeMismatchException for row " + rowNumber + " and column '" + column + "' with value " + value + " when setting property '" + pd.getName() + "' of type " + pd.getPropertyType() + " on object: " + mappedObject); } else { throw e; } } if (populatedProperties != null) { populatedProperties.add(pd.getName()); } } catch (NotWritablePropertyException ex) { throw new DataRetrievalFailureException( "Unable to map column " + column + " to property " + pd.getName(), ex); } } } if (populatedProperties != null && !populatedProperties.equals(this.mappedProperties)) { throw new InvalidDataAccessApiUsageException("Given ResultSet does not contain all fields " + "necessary to populate object of class [" + this.mappedClass + "]: " + this.mappedProperties); } return mappedObject; }
From source file:org.dataconservancy.packaging.tool.impl.AnnotationDrivenPackageStateSerializerTest.java
private XStream configure(XStream x) { x.processAnnotations(PackageState.class); /* APPLICATION_VERSION */ x.alias(StreamId.APPLICATION_VERSION.name(), AbstractSerializationTest.TestObjects.applicationVersion.getClass()); PropertyDescriptor pd = SerializationAnnotationUtil.getStreamDescriptors(PackageState.class) .get(StreamId.APPLICATION_VERSION); x.registerLocalConverter(PackageState.class, pd.getName(), new ApplicationVersionConverter()); /* DOMAIN_PROFILE_LIST */ x.alias(StreamId.DOMAIN_PROFILE_LIST.name(), AbstractSerializationTest.TestObjects.domainProfileUris.getClass()); pd = SerializationAnnotationUtil.getStreamDescriptors(PackageState.class).get(StreamId.DOMAIN_PROFILE_LIST); x.registerLocalConverter(PackageState.class, pd.getName(), new DomainProfileUriListConverter()); /* PACKAGE_METADATA */ x.alias(StreamId.PACKAGE_METADATA.name(), AbstractSerializationTest.TestObjects.packageMetadata.getClass()); pd = SerializationAnnotationUtil.getStreamDescriptors(PackageState.class).get(StreamId.PACKAGE_METADATA); x.registerLocalConverter(PackageState.class, pd.getName(), new PackageMetadataConverter()); /* PACKAGE_NAME */ x.alias(StreamId.PACKAGE_NAME.name(), AbstractSerializationTest.TestObjects.packageName.getClass()); pd = SerializationAnnotationUtil.getStreamDescriptors(PackageState.class).get(StreamId.PACKAGE_NAME); x.registerLocalConverter(PackageState.class, pd.getName(), new PackageNameConverter()); /* USER_SPECIFIED_PROPERTIES */ x.alias(StreamId.USER_SPECIFIED_PROPERTIES.name(), AbstractSerializationTest.TestObjects.userProperties.getClass()); pd = SerializationAnnotationUtil.getStreamDescriptors(PackageState.class) .get(StreamId.USER_SPECIFIED_PROPERTIES); x.registerLocalConverter(PackageState.class, pd.getName(), new UserPropertyConverter()); return x;/* w w w . j av a 2 s. c o m*/ }
From source file:de.xwic.appkit.core.transport.xml.XmlBeanSerializer.java
/** * @param doc//from w w w .j ava2 s . c o m * @param string * @param query * @throws IntrospectionException * @throws InvocationTargetException * @throws IllegalAccessException * @throws IllegalArgumentException */ public void serializeBean(Element elm, String elementName, Object bean) throws TransportException { Element root = elm.addElement(elementName); root.addAttribute("type", bean.getClass().getName()); try { BeanInfo beanInfo = Introspector.getBeanInfo(bean.getClass()); for (PropertyDescriptor pd : beanInfo.getPropertyDescriptors()) { if (!"class".equals(pd.getName())) { Element pElm = root.addElement(pd.getName()); Method mRead = pd.getReadMethod(); if (mRead == null) { log.debug(String.format("No '%s' property on '%s'.", pd.getName(), bean.getClass())); continue; } Object value = mRead.invoke(bean, NO_ARGS); boolean addType = true; addType = value != null && !value.getClass().equals(pd.getPropertyType()) && !pd.getPropertyType().isPrimitive(); addValue(pElm, value, addType); } } } catch (Exception e) { throw new TransportException("Error serializing bean: " + e, e); } }
From source file:grails.util.GrailsClassUtils.java
/** * Retrieves all the properties of the given class which are assignable to the given type * * @param clazz The class to retrieve the properties from * @param propertySuperType The type of the properties you wish to retrieve * @return An array of PropertyDescriptor instances *///from ww w . ja v a2 s . c om public static PropertyDescriptor[] getPropertiesAssignableToType(Class<?> clazz, Class<?> propertySuperType) { if (clazz == null || propertySuperType == null) return new PropertyDescriptor[0]; Set<PropertyDescriptor> properties = new HashSet<PropertyDescriptor>(); PropertyDescriptor descriptor = null; try { PropertyDescriptor[] descriptors = BeanUtils.getPropertyDescriptors(clazz); for (int i = 0; i < descriptors.length; i++) { descriptor = descriptors[i]; Class<?> currentPropertyType = descriptor.getPropertyType(); if (propertySuperType.isAssignableFrom(descriptor.getPropertyType())) { properties.add(descriptor); } } } catch (Exception e) { if (descriptor == null) { LOG.error(String.format("Got exception while checking property descriptors for class %s", clazz.getName()), e); } else { LOG.error(String.format( "Got exception while checking PropertyDescriptor.propertyType for field %s.%s", clazz.getName(), descriptor.getName()), e); } return new PropertyDescriptor[0]; } return properties.toArray(new PropertyDescriptor[properties.size()]); }
From source file:grails.util.GrailsClassUtils.java
/** * Retrieves all the properties of the given class for the given type * * @param clazz The class to retrieve the properties from * @param propertyType The type of the properties you wish to retrieve * * @return An array of PropertyDescriptor instances *//*from w w w .j av a2 s . co m*/ public static PropertyDescriptor[] getPropertiesOfType(Class<?> clazz, Class<?> propertyType) { if (clazz == null || propertyType == null) { return new PropertyDescriptor[0]; } Set<PropertyDescriptor> properties = new HashSet<PropertyDescriptor>(); PropertyDescriptor descriptor = null; try { PropertyDescriptor[] descriptors = BeanUtils.getPropertyDescriptors(clazz); for (int i = 0; i < descriptors.length; i++) { descriptor = descriptors[i]; Class<?> currentPropertyType = descriptor.getPropertyType(); if (isTypeInstanceOfPropertyType(propertyType, currentPropertyType)) { properties.add(descriptor); } } } catch (Exception e) { if (descriptor == null) { LOG.error(String.format("Got exception while checking property descriptors for class %s", clazz.getName()), e); } else { LOG.error(String.format( "Got exception while checking PropertyDescriptor.propertyType for field %s.%s", clazz.getName(), descriptor.getName()), e); } // if there are any errors in instantiating just return null for the moment return new PropertyDescriptor[0]; } return properties.toArray(new PropertyDescriptor[properties.size()]); }
From source file:de.xwic.appkit.core.transport.xml.XmlBeanSerializer.java
/** * Deserializes an element into a bean./*www. j a v a 2s. c o m*/ * * @param elm * @param context * @return * @throws TransportException */ public void deserializeBean(Object bean, Element elm, Map<EntityKey, Integer> context, boolean forceLoadCollection) throws TransportException { // now read the properties try { BeanInfo bi = Introspector.getBeanInfo(bean.getClass()); for (PropertyDescriptor pd : bi.getPropertyDescriptors()) { Method mWrite = pd.getWriteMethod(); if (!"class".equals(pd.getName()) && mWrite != null && !skipPropertyNames.contains(pd.getName())) { Element elmProp = elm.element(pd.getName()); if (elmProp != null) { Object value = readValue(context, elmProp, pd, forceLoadCollection); mWrite.invoke(bean, new Object[] { value }); } else { log.warn("The property " + pd.getName() + " is not specified in the xml document. The bean may not be restored completly"); } } } } catch (Exception e) { throw new TransportException("Error deserializing bean: " + e, e); } }