List of usage examples for java.beans Introspector getBeanInfo
public static BeanInfo getBeanInfo(Class<?> beanClass, Class<?> stopClass) throws IntrospectionException
From source file:org.apache.axis2.classloader.BeanInfoCache.java
/** * Locate an appropriate {@link BeanInfoCache} and return a cached {@link BeanInfo} object. * This method ensures that caching the {@link BeanInfo} object will not result in a class * loader leak./*from w w w.jav a2s . co m*/ * * @param beanClass * The bean class to be analyzed. * @param stopClass * The base class at which to stop the analysis; may be <code>null</code>. * @return A {@link BeanInfo} object describing the target bean. * @exception IntrospectionException * if an exception occurs during introspection. */ public static BeanInfo getCachedBeanInfo(Class<?> beanClass, Class<?> stopClass) throws IntrospectionException { ClassLoader classLoader = beanClass.getClassLoader(); BeanInfoCache cache; if (classLoader instanceof BeanInfoCachingClassLoader) { cache = ((BeanInfoCachingClassLoader) classLoader).getBeanInfoCache(); } else if (classLoader == BeanInfoCache.class.getClassLoader()) { cache = localCache; } else { cache = null; } if (cache != null) { return cache.getBeanInfo(beanClass, stopClass); } else { if (log.isWarnEnabled()) { log.warn("Unable to locate a BeanInfo cache for " + beanClass + " (stopClass=" + stopClass + "). This will negatively affect performance!"); } return Introspector.getBeanInfo(beanClass, stopClass); } }
From source file:com.unboundid.scim2.common.utils.SchemaUtils.java
/** * Gets property descriptors for the given class. * * @param cls The class to get the property descriptors for. * @return a collection of property values. * @throws java.beans.IntrospectionException throw if there are any * introspection errors./*from ww w.j av a 2s. c om*/ */ public static Collection<PropertyDescriptor> getPropertyDescriptors(final Class cls) throws IntrospectionException { BeanInfo beanInfo = Introspector.getBeanInfo(cls, Object.class); PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors(); return Arrays.asList(propertyDescriptors); }
From source file:com.gisgraphy.helper.BeanHelper.java
public static PropertyDescriptor[] getBeanProperties(final Object bean) throws IntrospectionException { return Introspector.getBeanInfo(bean.getClass(), Object.class).getPropertyDescriptors(); }
From source file:dhbw.ka.mwi.businesshorizon2.ui.process.period.input.AbstractInputPresenter.java
public void processEvent(ShowInputViewEventInterface event) { logger.debug("ShowDirektViewEvent erhalten"); period = event.getPeriod();/*from w ww .j a v a 2s. co m*/ getView().initForm(); getView().addHeader(period.getYear()); try { for (PropertyDescriptor pd : Introspector.getBeanInfo(period.getClass(), Object.class) .getPropertyDescriptors()) { logger.debug("Processing: " + pd.getName()); if (Arrays.asList(shownProperties).contains(pd.getDisplayName())) { try { String germanName; germanName = germanNamesProperties[Arrays.asList(shownProperties) .indexOf(pd.getDisplayName())]; boolean skipInitialContent = true; for (PropertyDescriptor pdr : Introspector.getBeanInfo(period.getClass(), Object.class) .getPropertyDescriptors()) { if ((pd.getDisplayName() + "Set").equals(pdr.getDisplayName())) { skipInitialContent = !(boolean) pdr.getReadMethod().invoke(period); logger.debug("method found and skipInitialContent set to " + skipInitialContent); } } if (skipInitialContent) { getView().addInputField(germanName); logger.debug("initialContent skipped"); } else { getView().addInputField(germanName, (double) pd.getReadMethod().invoke(period)); logger.debug("initialContent written"); } } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) { e.printStackTrace(); } } } } catch (IntrospectionException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
From source file:org.archive.crawler.restlet.XmlMarshaller.java
/** * generate nested XML structure for a bean {@code obj}. enclosing element * will not be generated if {@code key} is empty. each readable JavaBeans property * is mapped to an nested element named after its name. Those properties * annotated with {@link XmlTransient} are ignored. * @param xmlWriter XmlWriter/*from ww w . j a v a 2 s . c om*/ * @param key name of enclosing element * @param obj bean * @throws SAXException */ protected static void marshalBean(XmlWriter xmlWriter, String key, Object obj) throws SAXException { if (!StringUtils.isEmpty(key)) xmlWriter.startElement(key); try { BeanInfo beanInfo = Introspector.getBeanInfo(obj.getClass(), Object.class); PropertyDescriptor[] props = beanInfo.getPropertyDescriptors(); XmlType xmlType = obj.getClass().getAnnotation(XmlType.class); if (xmlType != null) { String[] propOrder = xmlType.propOrder(); if (propOrder != null) { // TODO: should cache this sorted version? orderProperties(props, propOrder); } } for (PropertyDescriptor prop : props) { Method m = prop.getReadMethod(); if (m == null || m.getAnnotation(XmlTransient.class) != null) continue; try { Object propValue = m.invoke(obj); if (propValue != null && !"".equals(propValue)) { marshal(xmlWriter, prop.getName(), propValue); } } catch (Exception ex) { // generate empty element, for now. generate comment? xmlWriter.emptyElement(prop.getName()); } } } catch (IntrospectionException ex) { // ignored, for now. } if (!StringUtils.isEmpty(key)) xmlWriter.endElement(key); }
From source file:corner.util.ognl.OgnlUtil.java
/** * Copies the properties in the object "from" and sets them in the object "to" * using specified type converter, or {@link com.opensymphony.xwork.util.CornerTypeConverter} if none is specified. * * @param from the source object/*from w ww .j a v a 2 s . c o m*/ * @param to the target object * @param context the action context we're running under */ public static void copy(Object from, Object to, Map context) { Map contextFrom = Ognl.createDefaultContext(from); Map contextTo = Ognl.createDefaultContext(to); BeanInfo beanInfoFrom = null; try { beanInfoFrom = Introspector.getBeanInfo(from.getClass(), Object.class); } catch (IntrospectionException e) { log.error("An error occured", e); return; } PropertyDescriptor[] pds = beanInfoFrom.getPropertyDescriptors(); for (int i = 0; i < pds.length; i++) { PropertyDescriptor pd = pds[i]; try { Object expr = compile(pd.getName()); Object value = Ognl.getValue(expr, contextFrom, from); Ognl.setValue(expr, contextTo, to, value); } catch (OgnlException e) { // ignore, this is OK } } }
From source file:de.xwic.appkit.core.transport.xml.EtoSerializer.java
/** * @param o/* w w w. j av a 2s .c om*/ * @return * @throws IntrospectionException */ private static Map<String, PropertyDescriptor> getMap(final Object o) throws IntrospectionException { final BeanInfo beanInfo = Introspector.getBeanInfo(o.getClass(), Introspector.USE_ALL_BEANINFO); final List<PropertyDescriptor> asList = Arrays.asList(beanInfo.getPropertyDescriptors()); final Map<String, PropertyDescriptor> map = MapUtil.generateMap(asList, new Function<PropertyDescriptor, String>() { @Override public String evaluate(final PropertyDescriptor obj) { return obj.getName(); } }); return map; }
From source file:org.apache.struts2.views.xslt.BeanAdapter.java
/** * Caching facade method to Introspector.getBeanInfo(Class, Class).getPropertyDescriptors(); *//*from w ww . j a va 2s . co m*/ private synchronized PropertyDescriptor[] getPropertyDescriptors(Object bean) { try { if (propertyDescriptorCache == null) { propertyDescriptorCache = new HashMap<Class, PropertyDescriptor[]>(); } PropertyDescriptor[] props = propertyDescriptorCache.get(bean.getClass()); if (props == null) { log.debug("Caching property descriptor for " + bean.getClass().getName()); props = Introspector.getBeanInfo(bean.getClass(), Object.class).getPropertyDescriptors(); propertyDescriptorCache.put(bean.getClass(), props); } return props; } catch (IntrospectionException e) { e.printStackTrace(); throw new StrutsException("Error getting property descriptors for " + bean + " : " + e.getMessage()); } }
From source file:org.jaxygen.converters.properties.PropertiesToBeanConverter.java
/** * Fill the field in bean by the value pointed by the name. Name format name=<(KEY([N])?)+> where KEY bean property name, N index in table (if bean field is * List of java array).//from w ww.j a va2 s . c om * * @param name * @param value * @param beanClass * @param baseBean * @param conversionReport * @return * @throws IntrospectionException * @throws IllegalArgumentException * @throws IllegalAccessException * @throws InvocationTargetException * @throws InstantiationException * @throws WrongProperyIndex */ private static Object fillBeanValueByName(final String name, Object value, Class<?> beanClass, Object baseBean) throws IntrospectionException, IllegalArgumentException, IllegalAccessException, InvocationTargetException, InstantiationException, WrongProperyIndex { // parse name x.y[i].z[n].v Object bean = baseBean; if (bean == null) { bean = beanClass.newInstance(); } Class<?> c = beanClass; BeanInfo beanInfo = Introspector.getBeanInfo(c, Object.class); final String childName = name.substring(name.indexOf(".") + 1); String path[] = name.split("\\."); final String fieldName = path[0]; // parse arrays [n] if (fieldName.endsWith("]")) { int bracketStart = fieldName.indexOf("["); int len = fieldName.length(); if (bracketStart > 0) { fillBeanArrayField(name, value, bean, beanInfo, path, fieldName, bracketStart, len); } else { throw new WrongProperyIndex(name); } } else { // parse non arrays for (PropertyDescriptor pd : beanInfo.getPropertyDescriptors()) { if (pd.getName().equals(fieldName)) { Method writter = pd.getWriteMethod(); Method reader = pd.getReadMethod(); if (writter != null && reader != null) { Class<?> valueType = reader.getReturnType(); if (path.length == 1) { Object valueObject = parsePropertyToValue(value, valueType); writter.invoke(bean, valueObject); } else { Object childBean = reader.invoke(bean); Object valueObject = fillBeanValueByName(childName, value, valueType, childBean); writter.invoke(bean, valueObject); } } } } } // Object bean = c.newInstance(); return bean; }
From source file:com.googlecode.jsonplugin.JSONWriter.java
/** * Instrospect bean and serialize its properties *///from ww w .j a va2s .co m private void bean(Object object) throws JSONException { this.add("{"); BeanInfo info; try { Class clazz = object.getClass(); info = ((object == this.root) && this.ignoreHierarchy) ? Introspector.getBeanInfo(clazz, clazz.getSuperclass()) : Introspector.getBeanInfo(clazz); PropertyDescriptor[] props = info.getPropertyDescriptors(); boolean hasData = false; for (int i = 0; i < props.length; ++i) { PropertyDescriptor prop = props[i]; String name = prop.getName(); Method accessor = prop.getReadMethod(); Method baseAccessor = null; if (clazz.getName().indexOf("$$EnhancerByCGLIB$$") > -1) { try { baseAccessor = Class.forName(clazz.getName().substring(0, clazz.getName().indexOf("$$"))) .getMethod(accessor.getName(), accessor.getParameterTypes()); } catch (Exception ex) { log.debug(ex.getMessage(), ex); } } else baseAccessor = accessor; if (baseAccessor != null) { JSON json = baseAccessor.getAnnotation(JSON.class); if (json != null) { if (!json.serialize()) continue; else if (json.name().length() > 0) name = json.name(); } //ignore "class" and others if (this.shouldExcludeProperty(clazz, prop)) { continue; } String expr = null; if (this.buildExpr) { expr = this.expandExpr(name); if (this.shouldExcludeProperty(expr)) { continue; } expr = this.setExprStack(expr); } Object value = accessor.invoke(object, new Object[0]); boolean propertyPrinted = this.add(name, value, accessor, hasData); hasData = hasData || propertyPrinted; if (this.buildExpr) { this.setExprStack(expr); } } } // special-case handling for an Enumeration - include the name() as a property */ if (object instanceof Enum) { Object value = ((Enum) object).name(); this.add("_name", value, object.getClass().getMethod("name"), hasData); } } catch (Exception e) { throw new JSONException(e); } this.add("}"); }