List of usage examples for java.beans Introspector getBeanInfo
public static BeanInfo getBeanInfo(Class<?> beanClass, Class<?> stopClass) throws IntrospectionException
From source file:com.esofthead.mycollab.common.interceptor.aspect.AuditLogUtil.java
static public String getChangeSet(Object oldObj, Object newObj, List<String> excludeFields, boolean isSelective) { Class cl = oldObj.getClass(); List<AuditChangeItem> changeItems = new ArrayList<>(); try {//w w w . j a va 2s . c o m BeanInfo beanInfo = Introspector.getBeanInfo(cl, Object.class); for (PropertyDescriptor propertyDescriptor : beanInfo.getPropertyDescriptors()) { String fieldName = propertyDescriptor.getName(); if (excludeFields.contains(fieldName)) { continue; } String oldProp = getValue(PropertyUtils.getProperty(oldObj, fieldName)); Object newPropVal; try { newPropVal = PropertyUtils.getProperty(newObj, fieldName); } catch (Exception e) { continue; } String newProp = getValue(newPropVal); if (!oldProp.equals(newProp)) { if (isSelective && newProp.equals("")) { } else { AuditChangeItem changeItem = new AuditChangeItem(); changeItem.setField(fieldName); changeItem.setNewvalue(newProp); changeItem.setOldvalue(oldProp); changeItems.add(changeItem); } } } } catch (Exception e) { LOG.error("There is error when convert changeset", e); return null; } return (changeItems.size() > 0) ? JsonDeSerializer.toJson(changeItems) : null; }
From source file:com.gisgraphy.rest.BeanToRestParameter.java
public static String toQueryString(Object object) { if (object == null) { throw new RestClientException("Can not get queryString for null object"); }//from ww w.j a v a 2 s. c o m StringBuilder sb = new StringBuilder(128); try { boolean first = true; String andValue = "&"; for (PropertyDescriptor thisPropertyDescriptor : Introspector .getBeanInfo(object.getClass(), Object.class).getPropertyDescriptors()) { Object property = PropertyUtils.getProperty(object, thisPropertyDescriptor.getName()); if (property != null) { sb.append(first ? "?" : andValue); sb.append(thisPropertyDescriptor.getName()); sb.append("="); sb.append(URLEncoder.encode(property.toString(), Constants.CHARSET)); first = false; } } } catch (Exception e) { throw new RestClientException("can not generate url for bean: " + e.getMessage(), e); } return sb.toString(); }
From source file:com.ginema.api.reflection.ReflectionUtils.java
public static Method getGetterMethod2(Class clazz, String name) throws IntrospectionException { PropertyDescriptor desc[] = Introspector.getBeanInfo(clazz, Object.class).getPropertyDescriptors(); for (PropertyDescriptor d : desc) { return d.getReadMethod(); }/*from w w w .j a va2 s.co m*/ return null; }
From source file:org.openengsb.core.util.BeanUtilsExtended.java
/** * Analyzes the bean and returns a map containing the property-values. Works similar to * {@link BeanUtils#describe(Object)} but does not convert everything to strings. * // ww w .j av a 2 s.c o m * @throws IllegalArgumentException if the bean cannot be analyzed properly. Probably because some getter throw an * Exception */ public static Map<String, Object> buildObjectAttributeMap(Object bean) throws IllegalArgumentException { BeanInfo beanInfo; try { beanInfo = Introspector.getBeanInfo(bean.getClass(), Object.class); } catch (IntrospectionException e1) { throw new IllegalArgumentException(e1); } Map<String, Object> result; result = Maps.newHashMap(); for (PropertyDescriptor pDesc : beanInfo.getPropertyDescriptors()) { String name = pDesc.getName(); Object propertyValue; try { propertyValue = pDesc.getReadMethod().invoke(bean); } catch (IllegalAccessException e) { // this should never happen since the Introspector only returns accessible read-methods LOGGER.error("WTF: got property descriptor with inaccessible read-method"); throw new IllegalStateException(e); } catch (InvocationTargetException e) { ReflectionUtils.handleInvocationTargetException(e); throw new IllegalStateException("Should never get here"); } if (propertyValue != null) { result.put(name, propertyValue); } } return result; }
From source file:no.sesat.search.datamodel.generic.MapDataObjectBeanInfo.java
/** * * @param name//from w w w . j a v a 2 s . c o m * @param cls * @return */ public static PropertyDescriptor[] addSingleMappedPropertyDescriptor(final String name, final Class<?> cls) { try { final PropertyDescriptor[] existing = Introspector.getBeanInfo(cls, Introspector.IGNORE_ALL_BEANINFO) .getPropertyDescriptors(); // remove this introspection from the cache to avoid reuse of the IGNORE_ALL_BEANINFO result Introspector.flushFromCaches(cls); final PropertyDescriptor[] result = new PropertyDescriptor[existing.length + 2]; System.arraycopy(existing, 0, result, 0, existing.length); result[existing.length] = new MappedPropertyDescriptor(name, cls); if (!System.getProperty("java.version").startsWith("1.6")) { fixForJavaBug4984912(cls, (MappedPropertyDescriptor) result[existing.length]); } final String captialised = Character.toUpperCase(name.charAt(0)) + name.substring(1); try { // try plural with "s" first, then fall back onto "es" result[existing.length + 1] = new PropertyDescriptor(name + 's', cls, "get" + captialised + 's', null); } catch (IntrospectionException ie) { // who on earth designed the english language!?? :@ result[existing.length + 1] = new PropertyDescriptor(name + "es", cls, "get" + captialised + 's', null); } return result; } catch (IntrospectionException ex) { LOG.error(ex.getMessage(), ex); throw new IllegalStateException('[' + cls.getSimpleName() + ']' + ex.getMessage(), ex); } }
From source file:com.gisgraphy.rest.BeanToRestParameter.java
public static Map<String, String> ToMap(Object object) { if (object == null) { throw new RestClientException("Can not get queryString for null object"); }/* ww w. java2 s. com*/ Map<String, String> result = new HashMap<String, String>(); try { for (PropertyDescriptor thisPropertyDescriptor : Introspector .getBeanInfo(object.getClass(), Object.class).getPropertyDescriptors()) { Object property = PropertyUtils.getProperty(object, thisPropertyDescriptor.getName()); if (property != null) { result.put(thisPropertyDescriptor.getName(), property.toString()); } } } catch (Exception e) { throw new RestClientException("can not generate Map for bean : " + e.getMessage(), e); } return result; }
From source file:com.metaparadigm.jsonrpc.BeanSerializer.java
public static BeanData analyzeBean(Class clazz) throws IntrospectionException { // log.info("analyzing " + clazz.getName()); BeanData bd = new BeanData(); bd.beanInfo = Introspector.getBeanInfo(clazz, Object.class); PropertyDescriptor props[] = bd.beanInfo.getPropertyDescriptors(); bd.readableProps = new HashMap(); bd.writableProps = new HashMap(); for (int i = 0; i < props.length; i++) { if (props[i].getWriteMethod() != null) { bd.writableProps.put(props[i].getName(), props[i].getWriteMethod()); }//from ww w .j a va 2 s . c o m if (props[i].getReadMethod() != null) { bd.readableProps.put(props[i].getName(), props[i].getReadMethod()); } } return bd; }
From source file:BeanPropertyTableModel.java
public void refresh() throws RuntimeException { final Vector<Object> columnNames = new Vector<Object>(); columnNames.add(_nameColumnName);//from www.j a v a 2 s . c o m columnNames.add(_valueColumnName); final Vector<Object> columnData = new Vector<Object>(); if (_bean != null) { try { BeanInfo info = Introspector.getBeanInfo(_bean.getClass(), Introspector.USE_ALL_BEANINFO); processBeanInfo(info, columnData); } catch (Exception ex) { throw new RuntimeException(ex); } } // Sort the rows by the property name. Collections.sort(columnData, new DataSorter()); setDataVector(columnData, columnNames); }
From source file:org.apache.axis.utils.BeanUtils.java
private static PropertyDescriptor[] getPropertyDescriptors(final Class secJavaType) { return (PropertyDescriptor[]) AccessController.doPrivileged(new PrivilegedAction() { public Object run() { PropertyDescriptor[] result = null; // START FIX http://nagoya.apache.org/bugzilla/showattachment.cgi?attach_id=4937 try { // privileged code goes here if (AxisFault.class.isAssignableFrom(secJavaType)) { // Don't include AxisFault data result = Introspector.getBeanInfo(secJavaType, AxisFault.class).getPropertyDescriptors(); } else if (Throwable.class != secJavaType && Throwable.class.isAssignableFrom(secJavaType)) { // Don't include Throwable data result = Introspector.getBeanInfo(secJavaType, Throwable.class).getPropertyDescriptors(); } else { // privileged code goes here result = Introspector.getBeanInfo(secJavaType).getPropertyDescriptors(); }//w ww.ja v a 2s . c om // END FIX http://nagoya.apache.org/bugzilla/showattachment.cgi?attach_id=4937 } catch (java.beans.IntrospectionException Iie) { } return result; } }); }
From source file:com.gisgraphy.helper.BeanHelper.java
public boolean equals(final Object other, final Object current) { if (!current.getClass().isAssignableFrom(other.getClass())) { return false; }/*from ww w . j ava2s .c o m*/ final String thisName = current.getClass().getSimpleName(); final String objectName = other.getClass().getSimpleName(); String propertyName; Exception exception; try { for (final PropertyDescriptor thisPropertyDescriptor : Introspector .getBeanInfo(current.getClass(), Object.class).getPropertyDescriptors()) { exception = null; propertyName = thisPropertyDescriptor.getName(); logger.debug("propertyName=" + propertyName); try { if (!compareProperty(current, other, propertyName)) { return false; } } catch (final IllegalAccessException e) { exception = e; } catch (final InvocationTargetException e) { exception = e; } catch (final NoSuchMethodException e) { exception = e; } if (exception != null) { logger.debug("impossible to compare property " + propertyName + " for beans " + thisName + " and " + objectName, exception); continue; } } } catch (final IntrospectionException e) { logger.debug("impossible to get properties for bean " + thisName, e); } return true; }