Java Code Examples for org.apache.commons.beanutils.PropertyUtils#getPropertyDescriptor()
The following examples show how to use
org.apache.commons.beanutils.PropertyUtils#getPropertyDescriptor() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DataDictionaryTypeServiceBase.java From rice with Educational Community License v2.0 | 6 votes |
protected List<RemotableAttributeError> validateDataDictionaryAttribute(KimTypeAttribute attr, String key, String value) { try { // create an object of the proper type per the component Object componentObject = Class.forName( attr.getKimAttribute().getComponentName() ).newInstance(); if ( attr.getKimAttribute().getAttributeName() != null ) { // get the bean utils descriptor for accessing the attribute on that object PropertyDescriptor propertyDescriptor = PropertyUtils.getPropertyDescriptor(componentObject, attr.getKimAttribute().getAttributeName()); if ( propertyDescriptor != null ) { // set the value on the object so that it can be checked Object attributeValue = KRADUtils.hydrateAttributeValue(propertyDescriptor.getPropertyType(), value); if (attributeValue == null) { attributeValue = value; // not a super-awesome fallback strategy, but... } propertyDescriptor.getWriteMethod().invoke( componentObject, attributeValue); return validateDataDictionaryAttribute(attr.getKimTypeId(), attr.getKimAttribute().getComponentName(), componentObject, propertyDescriptor); } } } catch (Exception e) { throw new KimTypeAttributeValidationException(e); } return Collections.emptyList(); }
Example 2
Source File: AttributeValidatingTypeServiceBase.java From rice with Educational Community License v2.0 | 6 votes |
/** * <p>Validates the attribute value for the given {@link TypeAttributeDefinition} having a componentName.</p> * <p>This implementation instantiates a component object using reflection on the class name specified in the * {@link TypeAttributeDefinition}s componentName, gets a {@link PropertyDescriptor} for the attribute of the * component object, hydrates the attribute's value from it's String form, sets that value on the component object, * and then delegates to * {@link #validatePrimitiveAttributeFromDescriptor(AttributeValidatingTypeServiceBase.TypeAttributeDefinition, String, Object, java.beans.PropertyDescriptor)}. * </p> * * @param typeAttributeDefinition * @param attributeName * @param value * @return */ protected List<RemotableAttributeError> validateDataDictionaryAttribute(TypeAttributeDefinition typeAttributeDefinition, String attributeName, String value) { try { // create an object of the proper type per the component Object componentObject = Class.forName( typeAttributeDefinition.getComponentName() ).newInstance(); if ( attributeName != null ) { // get the bean utils descriptor for accessing the attribute on that object PropertyDescriptor propertyDescriptor = PropertyUtils.getPropertyDescriptor(componentObject, attributeName); if ( propertyDescriptor != null ) { // set the value on the object so that it can be checked Object attributeValue = getAttributeValue(propertyDescriptor, value); propertyDescriptor.getWriteMethod().invoke( componentObject, attributeValue); return validatePrimitiveAttributeFromDescriptor(typeAttributeDefinition, typeAttributeDefinition.getComponentName(), componentObject, propertyDescriptor); } } } catch (Exception e) { throw new TypeAttributeValidationException(e); } return Collections.emptyList(); }
Example 3
Source File: TestDataGenerator.java From kfs with GNU Affero General Public License v3.0 | 6 votes |
/** * If the actual transaction implementation has a "setUniversityFiscalYear" method, use that to set the * fiscal year to the value of TestUtils.getFiscalYearForTesting() * @param transaction transaction to try to set fiscal year on */ protected void setFiscalYear(Transaction transaction) { try { final PropertyDescriptor propertyDescriptor = PropertyUtils.getPropertyDescriptor(transaction, KFSPropertyConstants.UNIVERSITY_FISCAL_YEAR); if (propertyDescriptor.getReadMethod() != null) { PropertyUtils.setProperty(transaction, KFSPropertyConstants.UNIVERSITY_FISCAL_YEAR, TestUtils.getFiscalYearForTesting()); } } catch (IllegalAccessException iae) { LOG.info("Could test universityFiscalYear property on fixture of transaction type: "+transaction.getClass().getName(), iae); } catch (InvocationTargetException ite) { LOG.info("Could test universityFiscalYear property on fixture of transaction type: "+transaction.getClass().getName(), ite); } catch (NoSuchMethodException nsme) { LOG.info("Could test universityFiscalYear property on fixture of transaction type: "+transaction.getClass().getName(), nsme); } }
Example 4
Source File: JdbcEntitySerDeserHelper.java From eagle with Apache License 2.0 | 5 votes |
/** * * @param obj * @param fieldName * @return * @throws IllegalAccessException * @throws NoSuchMethodException * @throws InvocationTargetException */ public static PropertyDescriptor getPropertyDescriptor(Object obj,String fieldName) throws IllegalAccessException, NoSuchMethodException, InvocationTargetException { String key = obj.getClass().getName()+"."+fieldName; PropertyDescriptor propertyDescriptor = _propertyDescriptorCache.get(key); if(propertyDescriptor ==null){ propertyDescriptor = PropertyUtils.getPropertyDescriptor(obj, fieldName); _propertyDescriptorCache.put(key,propertyDescriptor); } return propertyDescriptor; }
Example 5
Source File: Jira340TestCase.java From rice with Educational Community License v2.0 | 5 votes |
public void testGenericReadPropertyUtils() throws Throwable { assertEquals(String.class, PropertyUtils.getPropertyType(new ReadNamedBean(), "name")); final PropertyDescriptor d = PropertyUtils.getPropertyDescriptor(new ReadNamedBean(), "name"); assertEquals(String.class, d.getPropertyType()); assertEquals(String.class, d.getReadMethod().getReturnType()); }
Example 6
Source File: UniqueFieldValidatorImpl.java From cm_ext with Apache License 2.0 | 5 votes |
/** * @return return the value of the property */ @VisibleForTesting Object propertyValue(Object obj, String property) { try { PropertyDescriptor desc = PropertyUtils.getPropertyDescriptor(obj, property); return desc.getReadMethod().invoke(obj, new Object[]{}); } catch( Exception e) { throw new ValidationException(e); } }
Example 7
Source File: IndexDefinition.java From eagle with Apache License 2.0 | 5 votes |
private byte[][] generateIndexValues(TaggedLogAPIEntity entity) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException { final byte[][] result = new byte[columns.length][]; for (int i = 0; i < columns.length; ++i) { final IndexColumn column = columns[i]; final String columnName = column.getColumnName(); if (column.isTag) { final Map<String, String> tags = entity.getTags(); if (tags == null || tags.get(columnName) == null) { result[i] = EMPTY_VALUE; } else { result[i] = tags.get(columnName).getBytes(UTF_8_CHARSET); } } else { PropertyDescriptor pd = column.getPropertyDescriptor(); if (pd == null) { pd = PropertyUtils.getPropertyDescriptor(entity, columnName); column.setPropertyDescriptor(pd); } final Object value = pd.getReadMethod().invoke(entity); if (value == null) { result[i] = EMPTY_VALUE; } else { final Qualifier q = column.getQualifier(); result[i] = q.getSerDeser().serialize(value); } } if (result[i].length > MAX_INDEX_VALUE_BYTE_LENGTH) { throw new IllegalArgumentException("Index field value exceeded the max length: " + MAX_INDEX_VALUE_BYTE_LENGTH + ", actual length: " + result[i].length); } } return result; }
Example 8
Source File: EntitySerDeserializer.java From eagle with Apache License 2.0 | 5 votes |
public Map<String, byte[]> writeValue(TaggedLogAPIEntity entity, EntityDefinition ed) throws Exception { Map<String, byte[]> qualifierValues = new HashMap<String, byte[]>(); // iterate all modified qualifiers for (String fieldName : entity.modifiedQualifiers()) { PropertyDescriptor pd = PropertyUtils.getPropertyDescriptor(entity, fieldName); Object obj = pd.getReadMethod().invoke(entity); Qualifier q = ed.getDisplayNameMap().get(fieldName); EntitySerDeser<Object> ser = q.getSerDeser(); byte[] value = ser.serialize(obj); qualifierValues.put(q.getQualifierName(), value); } return qualifierValues; }
Example 9
Source File: Jira340TestCase.java From rice with Educational Community License v2.0 | 5 votes |
public void testGenericWriteOverloadNonInheritPropertyUtils() throws Throwable { //can't really assert a specific type b/c it's non-deterministic - just want to make sure things don't blow up assertNotNull(PropertyUtils.getPropertyType(new WriteOverloadNamedBeanNonInherit(), "name")); final PropertyDescriptor d = PropertyUtils.getPropertyDescriptor(new WriteOverloadNamedBeanNonInherit(), "name"); assertNotNull(d.getPropertyType()); assertNotNull(d.getWriteMethod().getParameterTypes()[0]); }
Example 10
Source File: OrderTest.java From dhis2-core with BSD 3-Clause "New" or "Revised" License | 5 votes |
@Before public void setUp() throws Exception { object1 = new TestObject(); object2 = new TestObject(); PropertyDescriptor propertyDescriptor = PropertyUtils.getPropertyDescriptor( object1, "value" ); valueProperty = new Property( String.class, propertyDescriptor.getReadMethod(), propertyDescriptor.getWriteMethod() ); valueProperty.setName( "value" ); orderAsc = new Order( valueProperty, Direction.ASCENDING ); orderDesc = new Order( valueProperty, Direction.DESCENDING ); }
Example 11
Source File: Bean.java From olca-app with Mozilla Public License 2.0 | 5 votes |
public static Method findGetter(Object bean, String property) throws Exception { PropertyDescriptor descriptor = PropertyUtils.getPropertyDescriptor( bean, property); if (descriptor != null) return PropertyUtils.getReadMethod(descriptor); return null; }
Example 12
Source File: Bean.java From olca-app with Mozilla Public License 2.0 | 5 votes |
public static Method findSetter(Object bean, String property) throws Exception { PropertyDescriptor descriptor = PropertyUtils.getPropertyDescriptor( bean, property); if (descriptor != null) return PropertyUtils.getWriteMethod(descriptor); return null; }
Example 13
Source File: EntitySerDeserializer.java From Eagle with Apache License 2.0 | 5 votes |
@SuppressWarnings("unchecked") public <T> T readValue(Map<String, byte[]> qualifierValues, EntityDefinition ed) throws Exception{ Class<? extends TaggedLogAPIEntity> clazz = ed.getEntityClass(); if(clazz == null){ throw new NullPointerException("Entity class of service "+ed.getService()+" is null"); } TaggedLogAPIEntity obj = clazz.newInstance(); Map<String, Qualifier> map = ed.getQualifierNameMap(); for(Map.Entry<String, byte[]> entry : qualifierValues.entrySet()){ Qualifier q = map.get(entry.getKey()); if(q == null){ // if it's not pre-defined qualifier, it must be tag unless it's a bug if(obj.getTags() == null){ obj.setTags(new HashMap<String, String>()); } obj.getTags().put(entry.getKey(), new StringSerDeser().deserialize(entry.getValue())); continue; } // TODO performance loss compared with new operator // parse different types of qualifiers String fieldName = q.getDisplayName(); PropertyDescriptor pd = PropertyUtils.getPropertyDescriptor(obj, fieldName); if(entry.getValue() != null){ Object args = q.getSerDeser().deserialize(entry.getValue()); pd.getWriteMethod().invoke(obj, args); // if (logger.isDebugEnabled()) { // logger.debug(entry.getKey() + ":" + args + " is deserialized"); // } } } return (T)obj; }
Example 14
Source File: Jira340TestCase.java From rice with Educational Community License v2.0 | 5 votes |
public void testGenericWriteOverloadInheritPropertyUtils() throws Throwable { //can't really assert a specific type b/c it's non-deterministic - just want to make sure things don't blow up assertNotNull(PropertyUtils.getPropertyType(new WriteOverloadNamedBeanInherit(), "name")); final PropertyDescriptor d = PropertyUtils.getPropertyDescriptor(new WriteOverloadNamedBeanInherit(), "name"); assertNotNull(d.getPropertyType()); assertNotNull(d.getWriteMethod().getParameterTypes()[0]); }
Example 15
Source File: HudsonTestCase.java From jenkins-test-harness with MIT License | 5 votes |
/** * Asserts that two JavaBeans are equal as far as the given list of properties are concerned. * * <p> * This method takes two objects that have properties (getXyz, isXyz, or just the public xyz field), * and makes sure that the property values for each given property are equals (by using {@link #assertEquals(Object, Object)}) * * <p> * Property values can be null on both objects, and that is OK, but passing in a property that doesn't * exist will fail an assertion. * * <p> * This method is very convenient for comparing a large number of properties on two objects, * for example to verify that the configuration is identical after a config screen roundtrip. * * @param lhs * One of the two objects to be compared. * @param rhs * The other object to be compared * @param properties * ','-separated list of property names that are compared. * @since 1.297 */ public void assertEqualBeans(Object lhs, Object rhs, String properties) throws Exception { assertNotNull("lhs is null",lhs); assertNotNull("rhs is null",rhs); for (String p : properties.split(",")) { PropertyDescriptor pd = PropertyUtils.getPropertyDescriptor(lhs, p); Object lp,rp; if(pd==null) { // field? try { Field f = lhs.getClass().getField(p); lp = f.get(lhs); rp = f.get(rhs); } catch (NoSuchFieldException e) { assertNotNull("No such property "+p+" on "+lhs.getClass(),pd); return; } } else { lp = PropertyUtils.getProperty(lhs, p); rp = PropertyUtils.getProperty(rhs, p); } if (lp!=null && rp!=null && lp.getClass().isArray() && rp.getClass().isArray()) { // deep array equality comparison int m = Array.getLength(lp); int n = Array.getLength(rp); assertEquals("Array length is different for property "+p, m,n); for (int i=0; i<m; i++) assertEquals(p+"["+i+"] is different", Array.get(lp,i),Array.get(rp,i)); return; } assertEquals("Property "+p+" is different",lp,rp); } }
Example 16
Source File: JenkinsRule.java From jenkins-test-harness with MIT License | 5 votes |
/** * Asserts that two JavaBeans are equal as far as the given list of properties are concerned. * * <p> * This method takes two objects that have properties (getXyz, isXyz, or just the public xyz field), * and makes sure that the property values for each given property are equals (by using {@link org.junit.Assert#assertThat(Object, org.hamcrest.Matcher)}) * * <p> * Property values can be null on both objects, and that is OK, but passing in a property that doesn't * exist will fail an assertion. * * <p> * This method is very convenient for comparing a large number of properties on two objects, * for example to verify that the configuration is identical after a config screen roundtrip. * * @param lhs * One of the two objects to be compared. * @param rhs * The other object to be compared * @param properties * ','-separated list of property names that are compared. * @since 1.297 */ public void assertEqualBeans(Object lhs, Object rhs, String properties) throws Exception { assertThat("LHS", lhs, notNullValue()); assertThat("RHS", rhs, notNullValue()); for (String p : properties.split(",")) { PropertyDescriptor pd = PropertyUtils.getPropertyDescriptor(lhs, p); Object lp,rp; if(pd==null) { // field? try { Field f = lhs.getClass().getField(p); lp = f.get(lhs); rp = f.get(rhs); } catch (NoSuchFieldException e) { assertThat("No such property " + p + " on " + lhs.getClass(), pd, notNullValue()); return; } } else { lp = PropertyUtils.getProperty(lhs, p); rp = PropertyUtils.getProperty(rhs, p); } if (lp!=null && rp!=null && lp.getClass().isArray() && rp.getClass().isArray()) { // deep array equality comparison int m = Array.getLength(lp); int n = Array.getLength(rp); assertThat("Array length is different for property " + p, n, is(m)); for (int i=0; i<m; i++) assertThat(p + "[" + i + "] is different", Array.get(rp, i), is(Array.get(lp,i))); return; } assertThat("Property " + p + " is different", rp, is(lp)); } }
Example 17
Source File: DefaultFieldFilterServiceTest.java From dhis2-core with BSD 3-Clause "New" or "Revised" License | 5 votes |
private static Property addProperty( Map<String, Property> propertyMap, Object bean, String property ) throws Exception { PropertyDescriptor pd = PropertyUtils.getPropertyDescriptor( bean, property ); Property p = new Property( pd.getPropertyType(), pd.getReadMethod(), pd.getWriteMethod() ); p.setName( pd.getName() ); p.setReadable( true ); propertyMap.put( pd.getName(), p ); return p; }
Example 18
Source File: CustomerInvoiceWriteoffLookupResultLookupableHelperServiceImpl.java From kfs with GNU Affero General Public License v3.0 | 4 votes |
/** * @param element * @param attributeName * @return Column * * KRAD Conversion: setup up the results column in the display results set. * * No use of data dictionary. */ protected Column setupResultsColumn(BusinessObject element, String attributeName, BusinessObjectRestrictions businessObjectRestrictions) { Column col = new Column(); col.setPropertyName(attributeName); String columnTitle = getDataDictionaryService().getAttributeLabel(element.getClass(), attributeName); if (StringUtils.isBlank(columnTitle)) { columnTitle = getDataDictionaryService().getCollectionLabel(element.getClass(), attributeName); } col.setColumnTitle(columnTitle); col.setMaxLength(getDataDictionaryService().getAttributeMaxLength(element.getClass(), attributeName)); try { Class formatterClass = getDataDictionaryService().getAttributeFormatter(element.getClass(), attributeName); Formatter formatter = null; if (formatterClass != null) { formatter = (Formatter) formatterClass.newInstance(); col.setFormatter(formatter); } // pick off result column from result list, do formatting String propValue = KFSConstants.EMPTY_STRING; Object prop = ObjectUtils.getPropertyValue(element, attributeName); // set comparator and formatter based on property type Class propClass = null; PropertyDescriptor propDescriptor = PropertyUtils.getPropertyDescriptor(element, col.getPropertyName()); if (propDescriptor != null) { propClass = propDescriptor.getPropertyType(); } // formatters if (prop != null) { propValue = getContractsGrantsReportHelperService().formatByType(prop, formatter); } // comparator col.setComparator(CellComparatorHelper.getAppropriateComparatorForPropertyClass(propClass)); col.setValueComparator(CellComparatorHelper.getAppropriateValueComparatorForPropertyClass(propClass)); propValue = super.maskValueIfNecessary(element.getClass(), col.getPropertyName(), propValue, businessObjectRestrictions); col.setPropertyValue(propValue); if (StringUtils.isNotBlank(propValue)) { col.setColumnAnchor(getInquiryUrl(element, col.getPropertyName())); } } catch (InstantiationException ie) { throw new RuntimeException("Unable to get new instance of formatter class for property " + col.getPropertyName(), ie); } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException ex) { throw new RuntimeException("Cannot access PropertyType for property " + "'" + col.getPropertyName() + "' " + " on an instance of '" + element.getClass().getName() + "'.", ex); } return col; }
Example 19
Source File: GenerateDunningLettersLookupableHelperServiceImpl.java From kfs with GNU Affero General Public License v3.0 | 4 votes |
/** * @param element * @param attributeName * @return Column */ protected Column setupResultsColumn(BusinessObject element, String attributeName, BusinessObjectRestrictions businessObjectRestrictions) { Column col = new Column(); col.setPropertyName(attributeName); String columnTitle = getDataDictionaryService().getAttributeLabel(element.getClass(), attributeName); if (StringUtils.isBlank(columnTitle)) { columnTitle = getDataDictionaryService().getCollectionLabel(element.getClass(), attributeName); } col.setColumnTitle(columnTitle); col.setMaxLength(getDataDictionaryService().getAttributeMaxLength(element.getClass(), attributeName)); try { Class formatterClass = getDataDictionaryService().getAttributeFormatter(element.getClass(), attributeName); Formatter formatter = null; if (ObjectUtils.isNotNull(formatterClass)) { formatter = (Formatter) formatterClass.newInstance(); col.setFormatter(formatter); } // Pick off result column from result list, do formatting String propValue = KFSConstants.EMPTY_STRING; Object prop = ObjectUtils.getPropertyValue(element, attributeName); // Set comparator and formatter based on property type Class propClass = null; PropertyDescriptor propDescriptor = PropertyUtils.getPropertyDescriptor(element, col.getPropertyName()); if (ObjectUtils.isNotNull(propDescriptor)) { propClass = propDescriptor.getPropertyType(); } // Formatters if (ObjectUtils.isNotNull(prop)) { propValue = getContractsGrantsReportHelperService().formatByType(prop, formatter); } // Comparator col.setComparator(CellComparatorHelper.getAppropriateComparatorForPropertyClass(propClass)); col.setValueComparator(CellComparatorHelper.getAppropriateValueComparatorForPropertyClass(propClass)); propValue = super.maskValueIfNecessary(element.getClass(), col.getPropertyName(), propValue, businessObjectRestrictions); col.setPropertyValue(propValue); if (StringUtils.isNotBlank(propValue)) { col.setColumnAnchor(getInquiryUrl(element, col.getPropertyName())); } } catch (InstantiationException ie) { throw new RuntimeException("Unable to get new instance of formatter class for property " + col.getPropertyName(), ie); } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException ex) { throw new RuntimeException("Cannot access PropertyType for property " + "'" + col.getPropertyName() + "' " + " on an instance of '" + element.getClass().getName() + "'.", ex); } return col; }
Example 20
Source File: ContractsGrantsInvoiceLookupableHelperServiceImpl.java From kfs with GNU Affero General Public License v3.0 | 4 votes |
/** * @param element * @param attributeName * @return Column */ protected Column setupResultsColumn(BusinessObject element, String attributeName, BusinessObjectRestrictions businessObjectRestrictions) { Column col = new Column(); col.setPropertyName(attributeName); String columnTitle = getDataDictionaryService().getAttributeLabel(element.getClass(), attributeName); if (StringUtils.isBlank(columnTitle)) { columnTitle = getDataDictionaryService().getCollectionLabel(element.getClass(), attributeName); } col.setColumnTitle(columnTitle); col.setMaxLength(getDataDictionaryService().getAttributeMaxLength(element.getClass(), attributeName)); try { Class formatterClass = getDataDictionaryService().getAttributeFormatter(element.getClass(), attributeName); Formatter formatter = null; if (formatterClass != null) { formatter = (Formatter) formatterClass.newInstance(); col.setFormatter(formatter); } // Pick off result column from result list, do formatting String propValue = KFSConstants.EMPTY_STRING; Object prop = ObjectUtils.getPropertyValue(element, attributeName); // Set comparator and formatter based on property type Class propClass = null; PropertyDescriptor propDescriptor = PropertyUtils.getPropertyDescriptor(element, col.getPropertyName()); if (propDescriptor != null) { propClass = propDescriptor.getPropertyType(); } // Formatters if (prop != null) { propValue = getContractsGrantsReportHelperService().formatByType(prop, formatter); } // Comparator col.setComparator(CellComparatorHelper.getAppropriateComparatorForPropertyClass(propClass)); col.setValueComparator(CellComparatorHelper.getAppropriateValueComparatorForPropertyClass(propClass)); propValue = super.maskValueIfNecessary(element.getClass(), col.getPropertyName(), propValue, businessObjectRestrictions); col.setPropertyValue(propValue); if (StringUtils.isNotBlank(propValue)) { col.setColumnAnchor(getInquiryUrl(element, col.getPropertyName())); } } catch (InstantiationException ie) { throw new RuntimeException("Unable to get new instance of formatter class for property " + col.getPropertyName(), ie); } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException ex) { throw new RuntimeException("Cannot access PropertyType for property " + "'" + col.getPropertyName() + "' " + " on an instance of '" + element.getClass().getName() + "'.", ex); } return col; }