List of usage examples for org.apache.commons.beanutils PropertyUtils getIndexedProperty
public static Object getIndexedProperty(Object bean, String name) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException
Return the value of the specified indexed property of the specified bean, with no type conversions.
For more details see PropertyUtilsBean
.
From source file:jp.co.opentone.bsol.framework.core.util.PropertyGetUtil.java
public static Object getNestedProperty(Object data, String name) { try {//from www . ja v a 2 s . c om if (!name.contains(".")) { return PropertyUtils.getProperty(data, name); } if (name.contains("[")) { String[] names = name.split("\\.", 2); Object obj = null; try { obj = PropertyUtils.getIndexedProperty(data, names[0]); } catch (NullPointerException npe) { // ???null?? obj = null; } if (obj == null) { return null; } return getNestedProperty(obj, names[1]); } try { return PropertyUtils.getNestedProperty(data, name); } catch (NestedNullException nne) { // ?????null?????? // null? return null; } } catch (Exception e) { throw new ApplicationFatalRuntimeException("invalid property : " + name, e); } }
From source file:com.rolex.explore.beanutils.service.BeanUtilsSpecificService.java
public void exploreBeanUtil() { SampleBean bean = new SampleBean(); String property1 = "name"; String property2 = "currentAddress.city"; String property3 = "previousAddresses[0].city"; String property4 = "previousAddresses[3].city"; String property5 = "vehicleLicenseModel(R60)"; Place place1 = new Place("Sentosa", "Singapore"); Place place2 = new Place("Colombo", "Sri Lanka"); List<Place> places = new ArrayList<Place>(); places.add(place1);//from w w w . j a v a 2s.com places.add(place2); String property6 = "yearlyPlacesVisited(2000)"; String property7 = "placesVisited"; String property8 = "placesVisited[0]"; TourismAward award = new TourismAward("World Award Committee", "USA"); String property9 = "yearlyPlacesVisited(2000)[0].tourismAwards[0]"; try { PropertyUtils.setSimpleProperty(bean, property1, "Rolex Rolex"); String value1 = (String) PropertyUtils.getSimpleProperty(bean, property1); System.out.println("###Reverse1: " + value1); PropertyUtils.setNestedProperty(bean, property2, "Hoffman Estates"); String value2 = (String) PropertyUtils.getNestedProperty(bean, property2); System.out.println("###Reverse2: " + value2); PropertyUtils.setNestedProperty(bean, property3, "Schaumburg"); String value3 = (String) PropertyUtils.getNestedProperty(bean, property3); System.out.println("###Reverse3: " + value3); PropertyUtils.setNestedProperty(bean, property4, "Des Plaines"); String value4 = (String) PropertyUtils.getNestedProperty(bean, property4); System.out.println("###Reverse4: " + value4); Address[] arrayValue1 = (Address[]) PropertyUtils.getSimpleProperty(bean, "previousAddresses"); System.out.println("###ReverseArray: " + Arrays.toString(arrayValue1)); PropertyUtils.setMappedProperty(bean, property5, "Sonata"); String value5 = (String) PropertyUtils.getMappedProperty(bean, property5); System.out.println("###Reverse5: " + value5); PropertyUtils.setMappedProperty(bean, property6, places); List<Place> value6 = (List<Place>) PropertyUtils.getMappedProperty(bean, property6); System.out.println("###Reverse6: " + value6.get(0)); PropertyUtils.setSimpleProperty(bean, property7, places); List<Place> value7 = (List<Place>) PropertyUtils.getSimpleProperty(bean, property7); System.out.println("###Reverse7: " + value7.get(0)); PropertyUtils.setIndexedProperty(bean, property8, place2); Place value8 = (Place) PropertyUtils.getIndexedProperty(bean, property8); System.out.println("###Reverse8: " + value8); PropertyUtils.setNestedProperty(bean, property9, award); TourismAward value9 = (TourismAward) PropertyUtils.getNestedProperty(bean, property9); System.out.println("###Reverse8: " + value8); } catch (IllegalAccessException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (InvocationTargetException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (NoSuchMethodException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
From source file:org.displaytag.util.LookupUtil.java
/** * <p>/*from www .ja v a 2 s . co m*/ * Returns the value of a property in the given bean. * </p> * <p> * This method is a modificated version from commons-beanutils PropertyUtils.getProperty(). It allows intermediate * nulls in expression without throwing exception (es. it doesn't throw an exception for the property * <code>object.date.time</code> if <code>date</code> is null) * </p> * @param bean javabean * @param name name of the property to read from the javabean * @return Object * @throws ObjectLookupException for errors while retrieving a property in the bean */ public static Object getBeanProperty(Object bean, String name) throws ObjectLookupException { if (log.isDebugEnabled()) { log.debug("getProperty [" + name + "] on bean " + bean); } if (bean == null) { throw new IllegalArgumentException("No bean specified"); } if (name == null) { throw new IllegalArgumentException("No name specified"); } Object evalBean = bean; String evalName = name; try { int indexOfINDEXEDDELIM; int indexOfMAPPEDDELIM; int indexOfMAPPEDDELIM2; int indexOfNESTEDDELIM; while (true) { indexOfNESTEDDELIM = evalName.indexOf(PropertyUtils.NESTED_DELIM); indexOfMAPPEDDELIM = evalName.indexOf(PropertyUtils.MAPPED_DELIM); indexOfMAPPEDDELIM2 = evalName.indexOf(PropertyUtils.MAPPED_DELIM2); if (indexOfMAPPEDDELIM2 >= 0 && indexOfMAPPEDDELIM >= 0 && (indexOfNESTEDDELIM < 0 || indexOfNESTEDDELIM > indexOfMAPPEDDELIM)) { indexOfNESTEDDELIM = evalName.indexOf(PropertyUtils.NESTED_DELIM, indexOfMAPPEDDELIM2); } else { indexOfNESTEDDELIM = evalName.indexOf(PropertyUtils.NESTED_DELIM); } if (indexOfNESTEDDELIM < 0) { break; } String next = evalName.substring(0, indexOfNESTEDDELIM); indexOfINDEXEDDELIM = next.indexOf(PropertyUtils.INDEXED_DELIM); indexOfMAPPEDDELIM = next.indexOf(PropertyUtils.MAPPED_DELIM); if (evalBean instanceof Map) { evalBean = ((Map) evalBean).get(next); } else if (indexOfMAPPEDDELIM >= 0) { evalBean = PropertyUtils.getMappedProperty(evalBean, next); } else if (indexOfINDEXEDDELIM >= 0) { evalBean = PropertyUtils.getIndexedProperty(evalBean, next); } else { evalBean = PropertyUtils.getSimpleProperty(evalBean, next); } if (evalBean == null) { log.debug("Null property value for '" + evalName.substring(0, indexOfNESTEDDELIM) + "'"); return null; } evalName = evalName.substring(indexOfNESTEDDELIM + 1); } indexOfINDEXEDDELIM = evalName.indexOf(PropertyUtils.INDEXED_DELIM); indexOfMAPPEDDELIM = evalName.indexOf(PropertyUtils.MAPPED_DELIM); if (evalBean instanceof Map) { evalBean = ((Map) evalBean).get(evalName); } else if (indexOfMAPPEDDELIM >= 0) { evalBean = PropertyUtils.getMappedProperty(evalBean, evalName); } else if (indexOfINDEXEDDELIM >= 0) { evalBean = PropertyUtils.getIndexedProperty(evalBean, evalName); } else { evalBean = PropertyUtils.getSimpleProperty(evalBean, evalName); } } catch (IllegalAccessException e) { throw new ObjectLookupException(LookupUtil.class, evalBean, evalName, e); } catch (InvocationTargetException e) { throw new ObjectLookupException(LookupUtil.class, evalBean, evalName, e); } catch (NoSuchMethodException e) { throw new ObjectLookupException(LookupUtil.class, evalBean, evalName, e); } return evalBean; }
From source file:org.kuali.rice.krad.util.ObjectUtils.java
/** * This method safely extracts either simple values OR nested values. For example, if the bo is SubAccount, and the * fieldName is/* www. ja va 2 s . com*/ * a21SubAccount.subAccountTypeCode, this thing makes sure it gets the value off the very end attribute, no matter * how deeply * nested it is. The code would be slightly simpler if this was done recursively, but this is safer, and consumes a * constant * amount of memory, no matter how deeply nested it goes. * * @param bo * @param fieldName * @return The field value if it exists. If it doesnt, and the name is invalid, and */ public static Object getNestedValue(Object bo, String fieldName) { if (bo == null) { throw new IllegalArgumentException("The bo passed in was null."); } if (StringUtils.isBlank(fieldName)) { throw new IllegalArgumentException("The fieldName passed in was blank."); } // okay, this section of code is to handle sub-object values, like // SubAccount.a21SubAccount.subAccountTypeCode. it basically walks // through the period-delimited list of names, and ends up with the // final value. String[] fieldNameParts = fieldName.split("\\."); Object currentObject = null; Object priorObject = bo; for (int i = 0; i < fieldNameParts.length; i++) { String fieldNamePart = fieldNameParts[i]; try { if (fieldNamePart.indexOf("]") > 0) { currentObject = PropertyUtils.getIndexedProperty(priorObject, fieldNamePart); } else { currentObject = PropertyUtils.getSimpleProperty(priorObject, fieldNamePart); } } catch (IllegalAccessException e) { throw new RuntimeException("Caller does not have access to the property accessor method.", e); } catch (InvocationTargetException e) { throw new RuntimeException("Property accessor method threw an exception.", e); } catch (NoSuchMethodException e) { throw new RuntimeException("The accessor method requested for this property cannot be found.", e); } // materialize the proxy, if it is a proxy if (ProxyHelper.isProxy(currentObject)) { currentObject = ProxyHelper.getRealObject(currentObject); } // if a node or the leaf is null, then we're done, there's no need to // continue accessing null things if (currentObject == null) { return currentObject; } priorObject = currentObject; } return currentObject; }
From source file:org.openmobster.core.mobileObject.TestBeanSyntax.java
private Object initializeIndexedProperty(Object parentObject, String property, PropertyDescriptor propertyMetaData) throws Exception { Object element = null;/*from www . j a v a2 s . co m*/ //Find the Class of the elementType Class elementType = null; if (!propertyMetaData.getPropertyType().isArray()) { ParameterizedType returnType = (ParameterizedType) propertyMetaData.getReadMethod() .getGenericReturnType(); Type[] actualTypes = returnType.getActualTypeArguments(); for (Type actualType : actualTypes) { elementType = (Class) actualType; } } else { elementType = propertyMetaData.getPropertyType().getComponentType(); } //An IndexedProperty Object indexedProperty = PropertyUtils.getProperty(parentObject, propertyMetaData.getName()); //Initialize the IndexedProperty (An Array or Collection) if (indexedProperty == null) { if (propertyMetaData.getPropertyType().isArray()) { //TODO: Remove hardcoded array size PropertyUtils.setProperty(parentObject, propertyMetaData.getName(), Array.newInstance(elementType, 1)); } else { //Handle Collection Construction PropertyUtils.setProperty(parentObject, propertyMetaData.getName(), new ArrayList()); } indexedProperty = PropertyUtils.getProperty(parentObject, propertyMetaData.getName()); } //Check to see if the index specified by the field requires creation of new //element try { element = PropertyUtils.getIndexedProperty(parentObject, property); } catch (IndexOutOfBoundsException iae) { Object newlyInitialized = elementType.newInstance(); if (!propertyMetaData.getPropertyType().isArray()) { ((Collection) indexedProperty).add(newlyInitialized); } else { //TODO: Remove hardcoded array index Array.set(indexedProperty, 0, newlyInitialized); } element = newlyInitialized; } return element; }
From source file:org.openmobster.core.mobileObject.xml.MobileObjectSerializer.java
private Object initializeIndexedProperty(Object parentObject, String property, PropertyDescriptor propertyMetaData, List<ArrayMetaData> objectMetaData, String propertyPath) throws Exception { Object element = null;//w w w .j a v a2 s. c om //ArrayUri String arrayUri = null; Integer arrayIndex = 0; if (propertyPath.endsWith("]")) { int lastIndex = propertyPath.lastIndexOf('['); arrayUri = propertyPath.substring(0, lastIndex); arrayIndex = Integer.parseInt(propertyPath.substring(lastIndex + 1, propertyPath.length() - 1).trim()); } ArrayMetaData arrayMetaData = null; for (ArrayMetaData local : objectMetaData) { if (local.arrayUri.equals(arrayUri)) { arrayMetaData = local; break; } } //Find the Class of the elementType String elementTypeName = arrayMetaData.arrayClass; Class elementType = null; if (elementTypeName != null && elementTypeName.trim().length() > 0 && !elementTypeName.equals("null")) { elementType = Thread.currentThread().getContextClassLoader().loadClass(arrayMetaData.arrayClass); } else { //Figure out the element type from the Property Information //This happens when a brand new object is created on the device and is being synced //with the backend //The MobileObject Framework on the device does not know about any Class level information //of the remote bean //The Limitation of this is that: // //* Indexed Properties if Collections must be Parameterized with Concrete Types //* Indexed Properties if Arrays must be Arrays of Concrete Types if (!propertyMetaData.getPropertyType().isArray()) { ParameterizedType returnType = (ParameterizedType) propertyMetaData.getReadMethod() .getGenericReturnType(); Type[] actualTypes = returnType.getActualTypeArguments(); for (Type actualType : actualTypes) { elementType = (Class) actualType; } } else { elementType = propertyMetaData.getPropertyType().getComponentType(); } } //An IndexedProperty Object indexedProperty = PropertyUtils.getProperty(parentObject, propertyMetaData.getName()); //Initialize the IndexedProperty (An Array or Collection) if (propertyMetaData.getPropertyType().isArray()) { int arraySize = arrayMetaData.arrayLength; if (indexedProperty == null) { //Initialize the Array with Size from Object Meta Data PropertyUtils.setProperty(parentObject, propertyMetaData.getName(), Array.newInstance(elementType, arraySize)); } else { //Make sure the Array Size matches int actualSize = Array.getLength(indexedProperty); if (actualSize != arraySize) { //Re-set the existing Array PropertyUtils.setProperty(parentObject, propertyMetaData.getName(), Array.newInstance(elementType, arraySize)); } } } else { if (indexedProperty == null) { //Handle Collection Construction PropertyUtils.setProperty(parentObject, propertyMetaData.getName(), new ArrayList()); } } //Check to see if the index specified by the field requires creation of new //element indexedProperty = PropertyUtils.getProperty(parentObject, propertyMetaData.getName()); if (!propertyMetaData.getPropertyType().isArray()) { try { element = PropertyUtils.getIndexedProperty(parentObject, property); } catch (IndexOutOfBoundsException iae) { Object newlyInitialized = elementType.newInstance(); ((Collection) indexedProperty).add(newlyInitialized); element = newlyInitialized; } } else { element = PropertyUtils.getIndexedProperty(parentObject, property); if (element == null) { Object newlyInitialized = elementType.newInstance(); Array.set(indexedProperty, arrayIndex, newlyInitialized); element = newlyInitialized; } } return element; }