Example usage for java.beans Introspector getBeanInfo

List of usage examples for java.beans Introspector getBeanInfo

Introduction

In this page you can find the example usage for java.beans Introspector getBeanInfo.

Prototype

public static BeanInfo getBeanInfo(Class<?> beanClass, Class<?> stopClass) throws IntrospectionException 

Source Link

Document

Introspect on a Java bean and learn all about its properties, exposed methods, below a given "stop" point.

Usage

From source file:com.orange.mmp.api.ws.jsonrpc.SimpleBeanSerializer.java

@Override
public Object marshall(SerializerState state, Object p, Object o) throws MarshallException {
    JSONObject jsonObj = new JSONObject();
    try {//  w ww  .j a v a2s .c o m
        BeanInfo beanInfo = Introspector.getBeanInfo(o.getClass(), Object.class);
        for (PropertyDescriptor propertyDescriptor : beanInfo.getPropertyDescriptors()) {
            Method readMethod = propertyDescriptor.getReadMethod();
            if (readMethod != null) {
                Object propValue = readMethod.invoke(o);
                Object json = ser.marshall(state, o, propValue, propertyDescriptor.getName());
                jsonObj.put(propertyDescriptor.getName(), json);
            }
        }
    } catch (JSONException jse) {
        throw new MarshallException("Failed to marshall Bean");
    } catch (IllegalAccessException iae) {
        throw new MarshallException("Failed to analyse Bean");
    } catch (InvocationTargetException ite) {
        throw new MarshallException("Failed to analyse Bean");
    } catch (IntrospectionException ie) {
        throw new MarshallException("Failed to analyse Bean");
    }
    return jsonObj;
}

From source file:com.jetyun.pgcd.rpc.serializer.impl.BeanSerializer.java

/**
 * Analyses a bean, returning a BeanData with the data extracted from it.
 * /*from  w  ww .j  av  a  2 s  .c o  m*/
 * @param clazz
 *            The class of the bean to analyse
 * @return A populated BeanData
 * @throws IntrospectionException
 *             If a problem occurs during getting the bean info.
 */
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());
        }
        if (props[i].getReadMethod() != null) {
            bd.readableProps.put(props[i].getName(), props[i].getReadMethod());
        }
    }
    return bd;
}

From source file:org.jabsorb.serializer.impl.BeanSerializer.java

/**
 * Analyses a bean, returning a BeanData with the data extracted from it.
 * /*  w  w w . j  ava  2s.  c o m*/
 * @param clazz The class of the bean to analyse
 * @return A populated BeanData
 * @throws IntrospectionException If a problem occurs during getting the bean
 *           info.
 */
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++) {
        //This is declared by enums and shouldn't be shown.
        if (props[i].getName().equals("declaringClass")) {
            continue;
        }
        if (props[i].getWriteMethod() != null) {
            bd.writableProps.put(props[i].getName(), props[i].getWriteMethod());
        }
        if (props[i].getReadMethod() != null) {
            bd.readableProps.put(props[i].getName(), props[i].getReadMethod());
        }
    }
    return bd;
}

From source file:org.jabsorb.ng.serializer.impl.BeanSerializer.java

/**
 * Analyses a bean, returning a BeanData with the data extracted from it.
 * //from ww  w .  j a va2  s . c om
 * @param clazz
 *            The class of the bean to analyse
 * @return A populated BeanData
 * @throws IntrospectionException
 *             If a problem occurs during getting the bean info.
 */
public static BeanData analyzeBean(final Class<? extends Object> clazz) throws IntrospectionException {

    log.info("analyzeBean", "analyzing " + clazz.getName());
    final BeanData bd = new BeanData();
    bd.beanInfo = Introspector.getBeanInfo(clazz, Object.class);
    final PropertyDescriptor props[] = bd.beanInfo.getPropertyDescriptors();
    bd.readableProps = new HashMap<String, Method>();
    bd.writableProps = new HashMap<String, Method>();
    for (int i = 0; i < props.length; i++) {
        // This is declared by enums and shouldn't be shown.
        if (props[i].getName().equals("declaringClass")) {
            continue;
        }
        if (props[i].getWriteMethod() != null) {
            bd.writableProps.put(props[i].getName(), props[i].getWriteMethod());
        }
        if (props[i].getReadMethod() != null) {
            bd.readableProps.put(props[i].getName(), props[i].getReadMethod());
        }
    }
    return bd;
}

From source file:com.gisgraphy.helper.BeanHelper.java

public static String toString(Object current) {
    if (current == null) {
        return "null";
    }//from w  w  w.j a v  a  2  s .c  o  m
    final StringBuffer buffer = new StringBuffer();
    final String beanName = current.getClass().getSimpleName();
    buffer.append(beanName);
    buffer.append(" { ");
    String propertyName;
    Object propertyValue = null;
    boolean first = true;
    Exception exception;
    try {
        for (final PropertyDescriptor propertyDescriptor : Introspector
                .getBeanInfo(current.getClass(), Object.class).getPropertyDescriptors()) {
            exception = null;
            propertyName = propertyDescriptor.getName();
            logger.debug("propertyName=" + propertyName);
            try {
                propertyValue = PropertyUtils.getProperty(current, propertyName);
            } 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 get value of property " + propertyName + " of bean " + beanName,
                        exception);
                continue;
            }
            if (first) {
                first = false;
            } else {
                buffer.append(", ");
            }
            buffer.append(propertyName);
            buffer.append(':');
            buffer.append(propertyValue);
        }
    } catch (final IntrospectionException e) {
        logger.error("impossible to get properties of bean " + beanName, e);
    }
    buffer.append(" }");
    return buffer.toString();
}

From source file:dhbw.ka.mwi.businesshorizon2.ui.process.period.input.AbstractInputPresenter.java

@Override
public boolean isValid() {
    if (period != null) {

        try {/*w  ww  .  ja  v a  2  s.  c  om*/
            ArrayList<String> wrongFields = new ArrayList<String>();
            boolean anyFalse = false;
            wrongFields.add(period.getYear() + "");
            for (PropertyDescriptor pdr : Introspector.getBeanInfo(period.getClass(), Object.class)
                    .getPropertyDescriptors()) {
                if (Arrays.asList(shownProperties)
                        .contains(pdr.getDisplayName().substring(0, pdr.getDisplayName().length() - 3))) {
                    if (!(boolean) pdr.getReadMethod().invoke(period)) {
                        wrongFields.add(pdr.getDisplayName().substring(0, pdr.getDisplayName().length() - 3));
                        anyFalse = true;
                    }
                }
            }
            if (anyFalse) {
                eventBus.fireEvent(new WrongFieldsEvent(wrongFields));
                return false;
            }
        } catch (IllegalAccessException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IllegalArgumentException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IntrospectionException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return true;
    } else
        return false;
}

From source file:com.reignite.messaging.server.SpringInitializedDestination.java

@Override
public void afterPropertiesSet() throws Exception {
    if (target != null) {
        // introspect and cache methods
        BeanInfo info = Introspector.getBeanInfo(target.getClass(), Object.class);
        MethodDescriptor[] meths = info.getMethodDescriptors();
        for (MethodDescriptor meth : meths) {
            Method method = meth.getMethod();
            List<Method> methods = methodMap.get(method.getName());
            if (methods == null) {
                methods = new ArrayList<Method>();
                methodMap.put(method.getName(), methods);
            }/*w w w.j  a v a  2 s  .c o  m*/
            methods.add(method);
        }
    }
    initialized = true;
}

From source file:org.apache.axis2.classloader.BeanInfoCache.java

/**
 * Introspect on a Java bean and return a cached {@link BeanInfo} object.
 * //from  www .  ja v  a2 s.c o 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.
 * @see Introspector#getBeanInfo(Class, Class)
 */
public BeanInfo getBeanInfo(Class<?> beanClass, Class<?> stopClass) throws IntrospectionException {
    CacheKey key = new CacheKey(beanClass, stopClass);
    BeanInfo beanInfo = cache.get(key);
    if (beanInfo == null) {
        beanInfo = Introspector.getBeanInfo(beanClass, stopClass);
        cache.put(key, beanInfo);
    }
    return beanInfo;
}

From source file:com.google.feedserver.util.BeanUtil.java

/**
 * Converts a JavaBean to a collection of properties
 * //from   ww w.j  a v  a 2  s  . c  om
 * @param bean The JavaBean to convert
 * @return A map of properties
 */
public Map<String, Object> convertBeanToProperties(Object bean) throws IntrospectionException,
        IllegalArgumentException, IllegalAccessException, InvocationTargetException {
    Map<String, Object> properties = new HashMap<String, Object>();
    BeanInfo beanInfo = Introspector.getBeanInfo(bean.getClass(), Object.class);
    for (PropertyDescriptor p : beanInfo.getPropertyDescriptors()) {
        String name = p.getName();
        Method reader = p.getReadMethod();
        if (reader != null) {
            Object value = reader.invoke(bean);
            if (null != value) {
                if (value instanceof Timestamp) {
                    properties.put(name, value.toString());
                } else if (isBean(value.getClass())) {
                    if (value.getClass().isArray()) {
                        Object[] valueArray = (Object[]) value;
                        if (valueArray.length == 0) {
                            properties.put(name, null);
                        } else {
                            List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
                            for (Object object : (Object[]) value) {
                                list.add(convertBeanToProperties(object));
                            }
                            properties.put(name, list.toArray(new Map[0]));
                        }
                    } else {
                        properties.put(name, convertBeanToProperties(value));
                    }
                } else {
                    properties.put(name, value);
                }
            }
        }
    }
    return properties;
}

From source file:org.codehaus.enunciate.modules.xfire.EnunciatedJAXWSWebFaultHandler.java

@Override
protected Object getFaultBean(Throwable fault, MessagePartInfo faultPart, MessageContext context) {
    Class<? extends Throwable> faultClass = fault.getClass();

    boolean conformsToJAXWSFaultPattern = conformsToJAXWSFaultPattern(faultClass);
    if (conformsToJAXWSFaultPattern) {
        try {/*from ww w.java2  s  .c  o m*/
            return faultClass.getMethod("getFaultInfo").invoke(fault);
        } catch (NoSuchMethodException e) {
            //fall through.  doesn't conform to the spec pattern.
        } catch (IllegalAccessException e) {
            throw new XFireRuntimeException("Couldn't invoke getFaultInfo method.", e);
        } catch (InvocationTargetException e) {
            throw new XFireRuntimeException("Couldn't invoke getFaultInfo method.", e);
        }
    }

    //doesn't conform to the spec pattern, use the generated fault bean class.
    Class faultBeanClass = getFaultBeanClass(faultClass);

    if (faultBeanClass == null) {
        return null;
    }

    try {
        BeanInfo beanInfo = Introspector.getBeanInfo(faultBeanClass, Object.class);
        Object faultBean = faultBeanClass.newInstance();
        for (PropertyDescriptor property : beanInfo.getPropertyDescriptors()) {
            if ((property.getWriteMethod() != null) && (property.getReadMethod() != null)) {
                Method getter = faultClass.getMethod(property.getReadMethod().getName());
                property.getWriteMethod().invoke(faultBean, getter.invoke(fault));
            }
        }
        return faultBean;
    } catch (IntrospectionException e) {
        throw new XFireRuntimeException("Unable to introspect fault bean class.", e);
    } catch (IllegalAccessException e) {
        throw new XFireRuntimeException("Unable to create fault bean.", e);
    } catch (InstantiationException e) {
        throw new XFireRuntimeException("Unable to create fault bean.", e);
    } catch (NoSuchMethodException e) {
        throw new XFireRuntimeException("The fault " + faultClass.getName()
                + " doesn't have a needed getter method used to fill in its fault bean.", e);
    } catch (InvocationTargetException e) {
        throw new XFireRuntimeException("Unable to create fault bean.", e);
    }
}