List of usage examples for java.beans BeanInfo getPropertyDescriptors
PropertyDescriptor[] getPropertyDescriptors();
From source file:org.springframework.beans.CachedIntrospectionResults.java
/** * Create a new CachedIntrospectionResults instance for the given class. * @param beanClass the bean class to analyze * @throws BeansException in case of introspection failure *//*from ww w. java 2 s .c o m*/ private CachedIntrospectionResults(Class<?> beanClass) throws BeansException { try { if (logger.isTraceEnabled()) { logger.trace("Getting BeanInfo for class [" + beanClass.getName() + "]"); } this.beanInfo = getBeanInfo(beanClass, shouldIntrospectorIgnoreBeaninfoClasses); if (logger.isTraceEnabled()) { logger.trace("Caching PropertyDescriptors for class [" + beanClass.getName() + "]"); } this.propertyDescriptorCache = new LinkedHashMap<>(); // This call is slow so we do it once. PropertyDescriptor[] pds = this.beanInfo.getPropertyDescriptors(); for (PropertyDescriptor pd : pds) { if (Class.class == beanClass && ("classLoader".equals(pd.getName()) || "protectionDomain".equals(pd.getName()))) { // Ignore Class.getClassLoader() and getProtectionDomain() methods - nobody needs to bind to those continue; } if (logger.isTraceEnabled()) { logger.trace("Found bean property '" + pd.getName() + "'" + (pd.getPropertyType() != null ? " of type [" + pd.getPropertyType().getName() + "]" : "") + (pd.getPropertyEditorClass() != null ? "; editor [" + pd.getPropertyEditorClass().getName() + "]" : "")); } pd = buildGenericTypeAwarePropertyDescriptor(beanClass, pd); this.propertyDescriptorCache.put(pd.getName(), pd); } // Explicitly check implemented interfaces for setter/getter methods as well, // in particular for Java 8 default methods... Class<?> clazz = beanClass; while (clazz != null && clazz != Object.class) { Class<?>[] ifcs = clazz.getInterfaces(); for (Class<?> ifc : ifcs) { if (!ClassUtils.isJavaLanguageInterface(ifc)) { BeanInfo ifcInfo = getBeanInfo(ifc, true); PropertyDescriptor[] ifcPds = ifcInfo.getPropertyDescriptors(); for (PropertyDescriptor pd : ifcPds) { if (!this.propertyDescriptorCache.containsKey(pd.getName())) { pd = buildGenericTypeAwarePropertyDescriptor(beanClass, pd); this.propertyDescriptorCache.put(pd.getName(), pd); } } } } clazz = clazz.getSuperclass(); } this.typeDescriptorCache = new ConcurrentReferenceHashMap<>(); } catch (IntrospectionException ex) { throw new FatalBeanException("Failed to obtain BeanInfo for class [" + beanClass.getName() + "]", ex); } }
From source file:com.medigy.persist.model.data.EntitySeedDataPopulator.java
public void populateEntityCacheData() throws HibernateException { Iterator itr = null;//from w ww .j a v a 2 s .co m if (!useEjb) itr = configuration.getClassMappings(); else itr = ejb3Configuration.getClassMappings(); while (itr.hasNext()) { Class entityClass = ((PersistentClass) itr.next()).getMappedClass(); //(Class) classMappings.next(); log.warn(entityClass.getName()); if (!Entity.class.isAssignableFrom(entityClass)) continue; Class[] innerClasses = entityClass.getDeclaredClasses(); for (Class innerClass : innerClasses) { // TODO: assume that this is the inner CACHE class !???!!! maybe make Cache extend an interface to indicate this?? if (innerClass.isEnum() && !entityClass.equals(Party.class)) { try { final BeanInfo beanInfo = Introspector.getBeanInfo(entityClass); final PropertyDescriptor[] descriptors = beanInfo.getPropertyDescriptors(); final Hashtable pdsByName = new Hashtable(); for (int i = 0; i < descriptors.length; i++) { final PropertyDescriptor descriptor = descriptors[i]; if (descriptor.getWriteMethod() != null) pdsByName.put(descriptor.getReadMethod().getName(), descriptor.getWriteMethod()); } Object[] enumObjects = innerClass.getEnumConstants(); // now match the enum methods with the enclosing class' methods for (Object enumObj : enumObjects) { Object entityObj = entityClass.newInstance(); final Method[] enumMethods = enumObj.getClass().getMethods(); for (Method enumMethod : enumMethods) { final Method writeMethod = (Method) pdsByName.get(enumMethod.getName()); if (writeMethod != null) { writeMethod.invoke(entityObj, enumMethod.invoke(enumObj)); } } HibernateUtil.getSession().save(entityObj); } } catch (IntrospectionException e) { log.error(e); } catch (IllegalAccessException e) { log.error(e); } catch (InstantiationException e) { log.error(e); } catch (InvocationTargetException e) { log.error(e); } catch (HibernateException e) { log.error(e); } } } } }
From source file:org.apache.struts2.interceptor.debugging.DebuggingInterceptor.java
/** * Recursive function to serialize objects to XML. Currently it will * serialize Collections, maps, Arrays, and JavaBeans. It maintains a stack * of objects serialized already in the current functioncall. This is used * to avoid looping (stack overflow) of circular linked objects. Struts and * XWork objects are ignored./*w w w .j av a 2s.c o m*/ * * @param bean The object you want serialized. * @param name The name of the object, used for element <name/> * @param writer The XML writer * @param stack List of objects we're serializing since the first calling * of this function (to prevent looping on circular references). */ protected void serializeIt(Object bean, String name, PrettyPrintWriter writer, List<Object> stack) { writer.flush(); // Check stack for this object if ((bean != null) && (stack.contains(bean))) { if (log.isInfoEnabled()) { log.info("Circular reference detected, not serializing object: " + name); } return; } else if (bean != null) { // Push object onto stack. // Don't push null objects ( handled below) stack.add(bean); } if (bean == null) { return; } String clsName = bean.getClass().getName(); writer.startNode(name); // It depends on the object and it's value what todo next: if (bean instanceof Collection) { Collection col = (Collection) bean; // Iterate through components, and call ourselves to process // elements for (Object aCol : col) { serializeIt(aCol, "value", writer, stack); } } else if (bean instanceof Map) { Map map = (Map) bean; // Loop through keys and call ourselves for (Object key : map.keySet()) { Object Objvalue = map.get(key); serializeIt(Objvalue, key.toString(), writer, stack); } } else if (bean.getClass().isArray()) { // It's an array, loop through it and keep calling ourselves for (int i = 0; i < Array.getLength(bean); i++) { serializeIt(Array.get(bean, i), "arrayitem", writer, stack); } } else { if (clsName.startsWith("java.lang")) { writer.setValue(bean.toString()); } else { // Not java.lang, so we can call ourselves with this object's // values try { BeanInfo info = Introspector.getBeanInfo(bean.getClass()); PropertyDescriptor[] props = info.getPropertyDescriptors(); for (PropertyDescriptor prop : props) { String n = prop.getName(); Method m = prop.getReadMethod(); // Call ourselves with the result of the method // invocation if (m != null) { serializeIt(m.invoke(bean), n, writer, stack); } } } catch (Exception e) { log.error(e, e); } } } writer.endNode(); // Remove object from stack stack.remove(bean); }
From source file:es.logongas.ix3.web.json.impl.JsonReaderImplEntityJackson.java
/** * Obtiene el valor de la propiedad de un Bean * * @param obj El objeto Bean//from ww w . j a v a2 s . co m * @param propertyName El nombre de la propiedad * @return El valor de la propiedad */ private Object getValueFromBean(Object obj, String propertyName) { try { BeanInfo beanInfo = Introspector.getBeanInfo(obj.getClass()); PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors(); Method readMethod = null; for (PropertyDescriptor propertyDescriptor : propertyDescriptors) { if (propertyDescriptor.getName().equals(propertyName)) { readMethod = propertyDescriptor.getReadMethod(); } } if (readMethod == null) { throw new RuntimeException( "No existe la propiedad:" + propertyName + " en " + obj.getClass().getName()); } return readMethod.invoke(obj); } catch (Exception ex) { throw new RuntimeException(ex); } }
From source file:es.logongas.ix3.web.json.impl.JsonReaderImplEntityJackson.java
/** * Establece el valor de la propiedad de un Bean * * @param obj El objeto Bean//from w ww. j a v a2 s . com * @param value El valor de la propiedad * @param propertyName El nombre de la propiedad */ private void setValueToBean(Object obj, Object value, String propertyName) { try { BeanInfo beanInfo = Introspector.getBeanInfo(obj.getClass()); PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors(); Method writeMethod = null; for (PropertyDescriptor propertyDescriptor : propertyDescriptors) { if (propertyDescriptor.getName().equals(propertyName)) { writeMethod = propertyDescriptor.getWriteMethod(); } } if (writeMethod == null) { throw new RuntimeException( "No existe la propiedad:" + propertyName + " en " + obj.getClass().getName()); } writeMethod.invoke(obj, value); } catch (Exception ex) { throw new RuntimeException(ex); } }
From source file:org.apache.tiles.evaluator.el.TilesContextELResolver.java
/** * Collects bean infos from a class and filling a list. * * @param clazz The class to be inspected. * @param list The list to fill./*from w w w .j a v a 2 s . c om*/ * @param properties The properties set to be filled. * @since 2.1.0 */ protected void collectBeanInfo(Class<?> clazz, List<FeatureDescriptor> list, Set<String> properties) { BeanInfo info = null; try { info = Introspector.getBeanInfo(clazz); } catch (Exception ex) { if (log.isDebugEnabled()) { log.debug("Cannot inspect class " + clazz, ex); } } if (info == null) { return; } for (PropertyDescriptor pd : info.getPropertyDescriptors()) { pd.setValue("type", pd.getPropertyType()); pd.setValue("resolvableAtDesignTime", Boolean.TRUE); list.add(pd); properties.add(pd.getName()); } }
From source file:things.thing.ThingUtils.java
public Map<String, Map<String, String>> getRegisteredTypeProperties() { if (typePropertiesMap == null) { Map<String, Map<String, String>> temp = Maps.newTreeMap(); for (String type : tr.getAllTypes()) { Class typeClass = tr.getTypeClass(type); BeanInfo info = null; try { info = Introspector.getBeanInfo(typeClass); } catch (IntrospectionException e) { throw new TypeRuntimeException("Can't generate info for type: " + type, type, e); }/*from w w w . j a v a2s.com*/ Map<String, String> properties = Maps.newTreeMap(); for (PropertyDescriptor desc : info.getPropertyDescriptors()) { String name = desc.getName(); if ("class".equals(name) || "id".equals(name)) { continue; } Class propClass = desc.getPropertyType(); properties.put(name, propClass.getSimpleName()); } temp.put(type, properties); } typePropertiesMap = ImmutableMap.copyOf(temp); } return typePropertiesMap; }
From source file:es.logongas.ix3.web.json.impl.JsonWriterImplEntityJackson.java
/** * Obtiene el valor de la propiedad de un Bean * * @param obj El objeto Bean//from ww w.ja v a 2 s. c om * @param propertyName El nombre de la propiedad * @return El valor de la propiedad */ private Object getValueFromBean(Object obj, String propertyName) { try { BeanInfo beanInfo = Introspector.getBeanInfo(obj.getClass()); PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors(); Method readMethod = null; for (PropertyDescriptor propertyDescriptor : propertyDescriptors) { if (propertyDescriptor.getName().equals(propertyName)) { readMethod = propertyDescriptor.getReadMethod(); break; } } if (readMethod == null) { throw new RuntimeException( "No existe la propiedad:" + propertyName + " en la clase " + obj.getClass().getName()); } return readMethod.invoke(obj); } catch (Exception ex) { throw new RuntimeException(ex); } }
From source file:eu.qualityontime.commons.DefaultBeanIntrospector.java
/** * Performs introspection of a specific Java class. This implementation uses * the {@code java.beans.Introspector.getBeanInfo()} method to obtain all * property descriptors for the current class and adds them to the passed in * introspection context.//www . j a v a 2 s. co m * * @param icontext * the introspection context */ @Override public void introspect(final IntrospectionContext icontext) { BeanInfo beanInfo = null; try { beanInfo = Introspector.getBeanInfo(icontext.getTargetClass()); } catch (final IntrospectionException e) { // no descriptors are added to the context log.error("Error when inspecting class " + icontext.getTargetClass(), e); return; } PropertyDescriptor[] descriptors = beanInfo.getPropertyDescriptors(); if (descriptors == null) { descriptors = new PropertyDescriptor[0]; } handleIndexedPropertyDescriptors(icontext.getTargetClass(), descriptors); icontext.addPropertyDescriptors(descriptors); }
From source file:com.palantir.ptoss.cinch.core.BindingContext.java
private Map<String, ObjectFieldMethod> indexBindableProperties(Function<PropertyDescriptor, Method> methodFn) throws IntrospectionException { final Map<ObjectFieldMethod, String> getterOfms = Maps.newHashMap(); for (Field field : Sets.newHashSet(bindableModels.values())) { BeanInfo beanInfo = Introspector.getBeanInfo(field.getType()); PropertyDescriptor[] props = beanInfo.getPropertyDescriptors(); for (PropertyDescriptor descriptor : props) { Method method = methodFn.apply(descriptor); if (method == null) { continue; }//from ww w. j a v a2 s. c o m BindableModel model = getFieldObject(field, BindableModel.class); getterOfms.put(new ObjectFieldMethod(model, field, method), descriptor.getName()); } } return dotIndex(getterOfms.keySet(), ObjectFieldMethod.TO_FIELD_NAME, Functions.forMap(getterOfms)); }