List of usage examples for org.apache.commons.beanutils PropertyUtils getReadMethod
public static Method getReadMethod(PropertyDescriptor descriptor)
Return an accessible property getter method for this property, if there is one; otherwise return null
.
For more details see PropertyUtilsBean
.
From source file:com.usefullc.platform.common.utils.ArrayUtils.java
/** * ??//from ww w .j a v a 2s . c o m * * @param objList * @param propName * @return */ @SuppressWarnings("unchecked") public static <T1, T2> List<T2> objListToPrimitiveList(List<T1> objList, String propName) { Assert.notEmpty(objList); List<T2> destList = new ArrayList<T2>(); try { for (T1 obj : objList) { PropertyDescriptor propDes = PropertyUtils.getPropertyDescriptor(obj, propName); Method m = PropertyUtils.getReadMethod(propDes); Object resultValue = m.invoke(obj); if (resultValue != null) { destList.add((T2) resultValue); } } } catch (Exception e) { throw new RuntimeException(e); } return destList; }
From source file:com.timesoft.kaitoo.ws.hibernate.AbstractPojo.java
/** * DOCUMENT ME!//from www . ja v a 2 s . c o m * * @return DOCUMENT ME! */ public String toString() { PropertyDescriptor[] pd = PropertyUtils.getPropertyDescriptors(this); StringBuffer buffer = new StringBuffer(); if (((List) callStack.get()).contains(this)) { buffer.append("Cyclic Reference!!!"); } else { ((List) callStack.get()).add(this); for (int index = 0; index < pd.length; ++index) { if ((null != PropertyUtils.getReadMethod(pd[index])) && (pd[index].getPropertyType() != Class.class)) { if (buffer.length() > 0) { buffer.append(", "); } String prop_name = pd[index].getName(); buffer.append(prop_name).append("="); try { buffer.append(PropertyUtils.getProperty(this, prop_name)); } catch (Exception e) { buffer.append(e.getMessage()); } } } ((List) callStack.get()).remove(this); } buffer.insert(0, " { ").insert(0, getClass().getName()).append(" }"); return buffer.toString(); }
From source file:com.hack23.cia.service.data.impl.util.LoadHelper.java
/** * Inits the proxy and collections.//from w ww . j a v a 2 s . c om * * @param obj * the obj * @param properties * the properties * @param dejaVu * the deja vu * @throws IllegalAccessException * the illegal access exception * @throws InvocationTargetException * the invocation target exception * @throws NoSuchMethodException * the no such method exception */ private static void initProxyAndCollections(final Object obj, final PropertyDescriptor[] properties, final Set<Object> dejaVu) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException { for (final PropertyDescriptor propertyDescriptor : properties) { if (PropertyUtils.getReadMethod(propertyDescriptor) != null) { final Object origProp = PropertyUtils.getProperty(obj, propertyDescriptor.getName()); if (origProp != null) { recursiveInitialize(origProp, dejaVu); } if (origProp instanceof Collection) { for (final Object item : (Collection<?>) origProp) { recursiveInitialize(item, dejaVu); } } } } }
From source file:com.timesoft.kaitoo.ws.hibernate.AbstractPojo.java
public String toValueString() { PropertyDescriptor[] pd = PropertyUtils.getPropertyDescriptors(this); StringBuffer buffer = new StringBuffer(); SimpleDateFormat simple = new SimpleDateFormat("dd-MM-yyyy", Locale.ENGLISH); if (((List) callStack.get()).contains(this)) { buffer.append("Cyclic Reference!!!"); } else {/*w ww.ja v a 2 s . c om*/ ((List) callStack.get()).add(this); for (int index = 0; index < pd.length; ++index) { if ((null != PropertyUtils.getReadMethod(pd[index])) && (pd[index].getPropertyType() != Class.class)) { if (buffer.length() > 0) { buffer.append(", "); } String prop_name = pd[index].getName(); try { if (null == PropertyUtils.getProperty(this, prop_name)) { buffer.append("\" \""); } else { if (pd[index].getPropertyType() == Calendar.class) { buffer.append("\"" + simple.format( ((Calendar) PropertyUtils.getProperty(this, prop_name)).getTime()) + "\""); } else if (pd[index].getPropertyType() == Date.class) { buffer.append( "\"" + simple.format(PropertyUtils.getProperty(this, prop_name) + "\"")); } else { buffer.append("\"" + PropertyUtils.getProperty(this, prop_name) + "\""); } } } catch (Exception e) { buffer.append(e.getMessage()); } } } ((List) callStack.get()).remove(this); } buffer.append(" \n"); return buffer.toString(); }
From source file:it.sample.parser.util.CommonsUtil.java
/** * Metodo di utilita' che copia i campi non nulli della classe sorgente in quelli della classe di destinazione * /*from ww w. java2s .co m*/ * @param source * @param destination */ public static <K, T> void copyNotNullProperties(K source, T destination) { PropertyDescriptor[] descriptors = PropertyUtils.getPropertyDescriptors(source); if (descriptors != null) { for (PropertyDescriptor descriptor : descriptors) { try { String propertyName = descriptor.getName(); Field field = getDeclaredField(propertyName, source.getClass()); if (field != null && field.getAnnotation(IgnoreField.class) == null) { boolean wasAccessible = field.isAccessible(); field.setAccessible(true); if (PropertyUtils.getReadMethod(descriptor) != null) { Object val = PropertyUtils.getSimpleProperty(source, propertyName); if (val != null && descriptor.getWriteMethod() != null) { PropertyUtils.setProperty(destination, propertyName, val); } } field.setAccessible(wasAccessible); } } catch (Exception e) { e.printStackTrace(); } } } }
From source file:edu.ku.brc.af.ui.forms.DataGetterForObj.java
@Override public Object getFieldValue(final Object dataObjArg, final String fieldName) { Object dataObj = dataObjArg;//from w ww.j a v a 2 s . c o m //System.out.println("["+fieldName+"]["+(dataObj != null ? dataObj.getClass().toString() : "N/A")+"]"); Object value = null; if (dataObj != null) { try { Iterator<?> iter = null; if (dataObj instanceof Set<?>) { iter = ((Set<?>) dataObj).iterator(); } else if (dataObj instanceof org.hibernate.collection.PersistentSet) { iter = ((org.hibernate.collection.PersistentSet) dataObj).iterator(); } if (iter != null) { while (iter.hasNext()) { Object obj = iter.next(); if (obj instanceof AttributeIFace) // Not scalable (needs interface) { AttributeIFace asg = (AttributeIFace) obj; //log.debug("["+asg.getDefinition().getFieldName()+"]["+fieldName+"]"); if (asg.getDefinition().getFieldName().equals(fieldName)) { if (asg.getDefinition().getDataType() == AttributeIFace.FieldType.StringType .getType()) { return asg.getStrValue(); //} else if (asg.getDefinition().getDataType() == AttributeIFace.FieldType.MemoType.getType()) //{ // return asg.getStrValue(); } else if (asg.getDefinition().getDataType() == AttributeIFace.FieldType.IntegerType .getType()) { return asg.getDblValue().intValue(); } else if (asg.getDefinition().getDataType() == AttributeIFace.FieldType.FloatType .getType()) { return asg.getDblValue().floatValue(); } else if (asg.getDefinition().getDataType() == AttributeIFace.FieldType.DoubleType .getType()) { return asg.getDblValue(); } else if (asg.getDefinition().getDataType() == AttributeIFace.FieldType.BooleanType .getType()) { return new Boolean(asg.getDblValue() != 0.0); } } } else if (obj instanceof FormDataObjIFace) { dataObj = obj; break; } else { return null; } } } //log.debug(fieldName); if (fieldName.startsWith("@get")) { try { String methodName = fieldName.substring(1, fieldName.length()).trim(); Method method = dataObj.getClass().getMethod(methodName, new Class<?>[] {}); if (method != null) { value = method.invoke(dataObj, new Object[] {}); } } catch (NoSuchMethodException ex) { ex.printStackTrace(); edu.ku.brc.af.core.UsageTracker.incrHandledUsageCount(); edu.ku.brc.exceptions.ExceptionTracker.getInstance().capture(DataGetterForObj.class, ex); } catch (IllegalAccessException ex) { ex.printStackTrace(); edu.ku.brc.af.core.UsageTracker.incrHandledUsageCount(); edu.ku.brc.exceptions.ExceptionTracker.getInstance().capture(DataGetterForObj.class, ex); } catch (InvocationTargetException ex) { ex.printStackTrace(); edu.ku.brc.af.core.UsageTracker.incrHandledUsageCount(); edu.ku.brc.exceptions.ExceptionTracker.getInstance().capture(DataGetterForObj.class, ex); } } else { PropertyDescriptor descr = PropertyUtils.getPropertyDescriptor(dataObj, fieldName.trim()); if (descr != null) { Method getter = PropertyUtils.getReadMethod(descr); if (getter != null) { value = getter.invoke(dataObj, (Object[]) null); if (value instanceof PersistentSet) { PersistentSet vSet = (PersistentSet) value; if (vSet.getSession() != null && !vSet.isDirty()) { int loadCode = dataObj instanceof FormDataObjIFace ? ((FormDataObjIFace) dataObj).shouldForceLoadChildSet(getter) : -1; if (loadCode != 0) { int max = loadCode == -1 ? vSet.size() : loadCode + 1; int i = 0; for (Object v : vSet) { if (v instanceof FormDataObjIFace) { ((FormDataObjIFace) v).forceLoad(); } if (++i == max) { break; } } } } } } } else if (showErrors) { log.error("We could not find a field named[" + fieldName.trim() + "] in data object [" + dataObj.getClass().toString() + "]"); } } } catch (Exception ex) { log.error(ex); if (!(ex instanceof org.hibernate.LazyInitializationException)) { edu.ku.brc.af.core.UsageTracker.incrHandledUsageCount(); edu.ku.brc.exceptions.ExceptionTracker.getInstance().capture(DataGetterForObj.class, ex); } } } return value; }
From source file:com.opengamma.language.object.ObjectFunctionProvider.java
protected MetaFunction getObjectValuesInstance(final ObjectInfo object, final String category) { // TODO: the string constants here should be at the top of the file final Class<?> clazz = object.getObjectClass(); final String name = "Expand" + object.getName(); final String description = "Expand the contents of " + object.getLabel(); final MetaParameter target = new MetaParameter(uncapitalize(object.getName()), JavaTypeInfo.builder(clazz).get()); target.setDescription(capitalize(object.getLabel()) + " to query"); final Map<String, Method> readers = new HashMap<String, Method>(); for (PropertyDescriptor prop : PropertyUtils.getPropertyDescriptors(clazz)) { AttributeInfo info = object.getInheritedAttribute(prop.getName()); if ((info == null) || !info.isReadable()) { continue; }// ww w .j a v a2s. c om final Method read = PropertyUtils.getReadMethod(prop); if (read != null) { readers.put(info.getLabel(), read); } } if (readers.isEmpty()) { return null; } else { return new ObjectValuesFunction(category, name, description, readers, target).getMetaFunction(); } }
From source file:com.opengamma.language.object.ObjectFunctionProvider.java
protected void loadGetAndSet(final ObjectInfo object, final Collection<MetaFunction> definitions, final String category) { if (object.getLabel() != null) { final Class<?> clazz = object.getObjectClass(); final Set<String> attributes = new HashSet<String>(object.getDirectAttributes()); for (PropertyDescriptor prop : PropertyUtils.getPropertyDescriptors(clazz)) { final AttributeInfo attribute = object.getDirectAttribute(prop.getName()); if (attribute != null) { if (attribute.isReadable()) { final Method read = PropertyUtils.getReadMethod(prop); if (read != null) { loadObjectGetter(object, attribute, read, definitions, category); attributes.remove(prop.getName()); }// ww w .j a v a 2 s. c o m } if (attribute.isWriteable()) { final Method write = PropertyUtils.getWriteMethod(prop); if (write != null) { loadObjectSetter(object, attribute, write, definitions, category); attributes.remove(prop.getName()); } } } } if (!attributes.isEmpty()) { for (String attribute : attributes) { throw new OpenGammaRuntimeException( "Attribute " + attribute + " is not exposed on object " + object); } } } }
From source file:net.firejack.platform.core.store.BaseStore.java
private <T extends Annotation> T getMethodAnnotation(Class<T> clazz, Object example, String name) { T annotation = null;// w ww.ja v a 2 s. co m try { PropertyDescriptor propertyDescriptor = PropertyUtils.getPropertyDescriptor(example, name); Method method = PropertyUtils.getReadMethod(propertyDescriptor); annotation = method.getAnnotation(clazz); } catch (Exception e) { logger.warn("Can't find " + clazz.getName() + " annotation.", e); } return annotation; }
From source file:nl.nn.adapterframework.configuration.AbstractSpringPoweredDigesterFactory.java
protected void checkAttribute(Object currObj, String beanName, String attributeName, String value, Map<String, String> attrs) throws Exception { PropertyDescriptor pd = PropertyUtils.getPropertyDescriptor(currObj, attributeName); if (pd != null) { Method rm = PropertyUtils.getReadMethod(pd); if (rm != null) { try { Object dv = rm.invoke(currObj, new Object[0]); if (currObj instanceof HasSpecialDefaultValues) { dv = ((HasSpecialDefaultValues) currObj).getSpecialDefaultValue(attributeName, dv, attrs); }//w w w . j a v a2 s . c o m if (dv != null) { if (dv instanceof String) { if (value.equals(dv)) { addSetToDefaultConfigWarning(currObj, beanName, attributeName, value); } } else { if (value.length() == 0) { addConfigWarning(currObj, beanName, "attribute [" + attributeName + "] with type [" + dv.getClass().getName() + "] has no value"); } else { if (dv instanceof Boolean) { if (Boolean.valueOf(value).equals(dv)) { addSetToDefaultConfigWarning(currObj, beanName, attributeName, value); } } else { if (dv instanceof Integer) { try { if (Integer.valueOf(value).equals(dv)) { addSetToDefaultConfigWarning(currObj, beanName, attributeName, value); } } catch (NumberFormatException e) { addConfigWarning(currObj, beanName, "attribute [" + attributeName + "] String [" + value + "] cannot be converted to Integer: " + e.getMessage()); } } else { if (dv instanceof Long) { try { if (Long.valueOf(value).equals(dv)) { addSetToDefaultConfigWarning(currObj, beanName, attributeName, value); } } catch (NumberFormatException e) { addConfigWarning(currObj, beanName, "attribute [" + attributeName + "] String [" + value + "] cannot be converted to Long: " + e.getMessage()); } } else { log.warn("Unknown returning type [" + rm.getReturnType() + "] for getter method [" + rm.getName() + "], object [" + getObjectName(currObj, beanName) + "]"); } } } } } } } catch (Throwable t) { log.warn("Error on getting default for object [" + getObjectName(currObj, beanName) + "] with method [" + rm.getName() + "]", t); } } } }