List of usage examples for org.apache.commons.beanutils PropertyUtils isReadable
public static boolean isReadable(Object bean, String name)
Return true
if the specified property name identifies a readable property on the specified bean; otherwise, return false
.
For more details see PropertyUtilsBean
.
From source file:com.ponysdk.core.export.util.PropertyUtil.java
public static String getProperty(final Object bean, final String property) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException { Object propertyValue = NA;//from ww w. j a va 2 s.c o m if (property != null) { final String[] tokens = property.split("\\."); propertyValue = getPropertyValue(bean, tokens[0]); for (int i = 1; i < tokens.length; i++) { if (tokens[i].equals("toString")) { propertyValue = propertyValue.toString(); } else if (PropertyUtils.isReadable(propertyValue, tokens[i])) { propertyValue = PropertyUtils.getProperty(propertyValue, tokens[i]); } else { if (propertyValue instanceof List<?>) { final List<?> propertyList = (List<?>) propertyValue; final List<Object> values = new ArrayList<>(); for (final Object object : propertyList) { values.add(getPropertyValue(object, tokens[i])); } if (values.isEmpty()) propertyValue = NA; else propertyValue = values; } else if (propertyValue instanceof Map<?, ?>) { final Map<?, ?> propertyMap = (Map<?, ?>) propertyValue; final List<Object> values = new ArrayList<>(); for (final Object object : propertyMap.values()) { values.add(getPropertyValue(object, tokens[i])); } propertyValue = values; } else { propertyValue = NA; } } } } return String.valueOf(propertyValue == null ? NA : propertyValue); }
From source file:es.pode.adminusuarios.negocio.servicios.AltaGrupoException.java
/** * Finds the root cause of the parent exception * by traveling up the exception tree/*from w ww . j a v a 2s.c o m*/ */ private static Throwable findRootCause(Throwable th) { if (th != null) { // Lets reflectively get any JMX or EJB exception causes. try { Throwable targetException = null; // java.lang.reflect.InvocationTargetException // or javax.management.ReflectionException String exceptionProperty = "targetException"; if (PropertyUtils.isReadable(th, exceptionProperty)) { targetException = (Throwable) PropertyUtils.getProperty(th, exceptionProperty); } else { exceptionProperty = "causedByException"; //javax.ejb.EJBException if (PropertyUtils.isReadable(th, exceptionProperty)) { targetException = (Throwable) PropertyUtils.getProperty(th, exceptionProperty); } } if (targetException != null) { th = targetException; } } catch (Exception ex) { // just print the exception and continue ex.printStackTrace(); } if (th.getCause() != null) { th = th.getCause(); th = findRootCause(th); } } return th; }
From source file:com.reizes.shiva.etl.core.transformer.ModelToMySqlDumpTransformer.java
@Override public Object doProcess(Object input) throws Exception { String[] output = new String[columns.length]; for (int i = 0; i < columns.length; i++) { String name = StringUtil.camelize(columns[i]); if (PropertyUtils.isReadable(input, name)) { Object data = PropertyUtils.getSimpleProperty(input, name); if (data != null) { if (data instanceof Date) { output[i] = String.format("%1$tY-%1$tm-%1$td %1$tH:%1$tM:%1$tS", data); } else if (data instanceof Boolean) { output[i] = (Boolean) data ? "1" : "0"; } else { output[i] = "\"" + StringUtils.replaceEach(data.toString().trim(), new String[] { "\"", "\\", "\t" }, new String[] { "\\\"", "\\\\", "\\t" }) + "\""; }// w ww. j a va 2 s.com } else { output[i] = "NULL"; } } } String str = StringUtil.join(output, '\t') + '\n'; return str; }
From source file:ca.sqlpower.architect.TestUtils.java
/** * Sets all the settable properties on the given target object * which are not in the given ignore set. * /* w w w. ja v a2 s . c o m*/ * @param target The object to change the properties of * @param propertiesToIgnore The properties of target not to modify or read * @return A Map describing the new values of all the non-ignored, readable * properties in target. */ public static Map<String, Object> setAllInterestingProperties(Object target, Set<String> propertiesToIgnore) throws Exception { PropertyDescriptor props[] = PropertyUtils.getPropertyDescriptors(target); for (int i = 0; i < props.length; i++) { Object oldVal = null; if (PropertyUtils.isReadable(target, props[i].getName()) && props[i].getReadMethod() != null && !propertiesToIgnore.contains(props[i].getName())) { oldVal = PropertyUtils.getProperty(target, props[i].getName()); } if (PropertyUtils.isWriteable(target, props[i].getName()) && props[i].getWriteMethod() != null && !propertiesToIgnore.contains(props[i].getName())) { NewValueMaker valueMaker = new ArchitectValueMaker(new SPObjectRoot()); Object newVal = valueMaker.makeNewValue(props[i].getPropertyType(), oldVal, props[i].getName()); System.out.println("Changing property \"" + props[i].getName() + "\" to \"" + newVal + "\""); PropertyUtils.setProperty(target, props[i].getName(), newVal); } } // read them all back at the end in case there were dependencies between properties return ca.sqlpower.testutil.TestUtils.getAllInterestingProperties(target, propertiesToIgnore); }
From source file:com.roadmap.common.util.ObjectUtil.java
/** * Return true if the specified property name identifies a readable * property on the specified bean; otherwise, return false. * * @param Object object to be examined//from w w w.ja v a 2 s.com * @param String property name to be evaluated * * @return boolean */ @SuppressWarnings("rawtypes") public static boolean isReadable(Object vo, String prop) { boolean retVal = false; try { if (vo instanceof Map) { retVal = vo != null && ((Map) vo).containsValue(prop); } else { retVal = PropertyUtils.isReadable(vo, prop); } } catch (Exception e) { //No need to handle } return retVal; }
From source file:com.algoTrader.service.ib.IBMarketDataServiceException.java
/** * Finds the root cause of the parent exception * by traveling up the exception tree/*from www . j av a 2s. c om*/ */ private static Throwable findRootCause(Throwable th) { if (th != null) { // Reflectively get any exception causes. try { Throwable targetException = null; // java.lang.reflect.InvocationTargetException String exceptionProperty = "targetException"; if (PropertyUtils.isReadable(th, exceptionProperty)) { targetException = (Throwable) PropertyUtils.getProperty(th, exceptionProperty); } else { exceptionProperty = "causedByException"; //javax.ejb.EJBException if (PropertyUtils.isReadable(th, exceptionProperty)) { targetException = (Throwable) PropertyUtils.getProperty(th, exceptionProperty); } } if (targetException != null) { th = targetException; } } catch (Exception ex) { // just print the exception and continue ex.printStackTrace(); } if (th.getCause() != null) { th = th.getCause(); th = findRootCause(th); } } return th; }
From source file:com.brsanthu.dataexporter.BeanColumnBuilder.java
public Column[] build(Object bean) { if (bean == null) { throw new IllegalArgumentException("Parameter bean cannot be null"); }/*from w w w .j a v a2s.com*/ //Read each getters Field[] fields = bean.getClass().getFields(); for (Field field : fields) { if (PropertyUtils.isReadable(bean, field.getName())) { //To still work on } } return null; }
From source file:com.prashsoft.javakiva.KivaUtil.java
public static Object getBeanProperty(Object bean, String name) { Object beanProperty = null;//from w ww .j a v a 2 s .co m try { beanProperty = PropertyUtils.isReadable(bean, name) ? PropertyUtils.getProperty(bean, name) : null; } catch (IllegalAccessException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } catch (NoSuchMethodException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } return beanProperty; }
From source file:com.googlecode.psiprobe.tools.logging.DefaultAccessor.java
protected Object getProperty(Object o, String name, Object defaultValue) { try {//from www .j av a 2 s . c o m return PropertyUtils.isReadable(o, name) ? PropertyUtils.getProperty(o, name) : defaultValue; } catch (Exception e) { log.debug("Could not access property \"" + name + "\" of object " + o, e); return defaultValue; } }
From source file:com.roadmap.common.util.ObjectUtil.java
/** * Return true if the specified property name identifies a readable * property on the specified bean; otherwise, return false. * * @param Object object to be examined/*w w w .java2 s . c o m*/ * @param String property name to be evaluated * * @return boolean */ @SuppressWarnings("rawtypes") public static boolean isReadable(Object vo, String[] props) { boolean retVal = false; try { for (String string : props) { retVal = PropertyUtils.isReadable(vo, string); if (!retVal) break; } } catch (Exception e) { //No need to handle } return retVal; }