Example usage for org.apache.commons.beanutils PropertyUtils describe

List of usage examples for org.apache.commons.beanutils PropertyUtils describe

Introduction

In this page you can find the example usage for org.apache.commons.beanutils PropertyUtils describe.

Prototype

public static Map describe(Object bean)
        throws IllegalAccessException, InvocationTargetException, NoSuchMethodException 

Source Link

Document

Return the entire set of properties for which the specified bean provides a read method.

For more details see PropertyUtilsBean.

Usage

From source file:com.feilong.commons.core.bean.PropertyUtil.java

/**
 * <p>/*from   www. j  a v a2 s. c  om*/
 * <code>bean</code>???/Map.
 * </p>
 * 
 * ???classObject??classjava.lang.Object
 * 
 * @param bean
 *            Bean whose properties are to be extracted
 * @return The set of properties for the bean
 * @throws BeanUtilException
 *             if IllegalAccessException | InvocationTargetException | NoSuchMethodException
 * @see org.apache.commons.beanutils.BeanUtils#describe(Object)
 * @see org.apache.commons.beanutils.PropertyUtils#describe(Object)
 * @see com.feilong.commons.core.bean.BeanUtil#describe(Object)
 */
public static Map<String, Object> describe(Object bean) throws BeanUtilException {
    try {
        //Return the entire set of properties for which the specified bean provides a read method.
        Map<String, Object> propertyMap = PropertyUtils.describe(bean);
        return propertyMap;
    } catch (Exception e) {
        log.error(e.getClass().getName(), e);
        throw new BeanUtilException(e);
    }
}

From source file:com.yimidida.shards.utils.ParameterUtil.java

/**
 * ??/*from  w  w  w.  j  a  v  a2 s . co m*/
 * 
 * @param obj
 * @param shardId
 * @return
 */
@SuppressWarnings({ "rawtypes", "unchecked" })
public static Object resolve(final Object obj, final ShardId shardId) {
    try {
        // 
        if (obj instanceof String || obj instanceof Number || obj instanceof Boolean
                || obj instanceof Character) {
            Enhancer enhancer = new Enhancer();
            enhancer.setSuperclass(HashMap.class);
            enhancer.setCallback(new MethodInterceptor() {

                private final String prefix = shardId.getPrefix();
                private final String suffix = shardId.getSuffix();
                private final Object parameter = obj;

                @Override
                public Object intercept(Object object, Method method, Object[] args, MethodProxy proxy)
                        throws Throwable {

                    if ("containsKey".equals(method.getName())) {
                        //MapcontainsKey?TRUE
                        return true;
                    }

                    if (args.length > 0 && "get".equals(method.getName())) {
                        if ("prefix".equals(args[0])) {
                            return prefix;
                        } else if ("suffix".equals(args[0])) {
                            return suffix;
                        } else {
                            return parameter;
                        }
                    }

                    return proxy.invokeSuper(object, args);
                }
            });

            return (HashMap) enhancer.create();
        } else if (obj instanceof Map) {
            Map parameter = (Map) obj;
            parameter.put("prefix", shardId.getPrefix());
            parameter.put("suffix", shardId.getSuffix());

            return parameter;
        } else if (obj instanceof List) {
            Map<String, Object> parameter = Maps.newHashMap();
            parameter.put("list", obj);
            parameter.put("prefix", shardId.getPrefix());
            parameter.put("suffix", shardId.getSuffix());

            return parameter;
        } else if (obj != null && obj.getClass().isArray()) {
            Map<String, Object> parameter = Maps.newHashMap();
            parameter.put("array", obj);
            parameter.put("prefix", shardId.getPrefix());
            parameter.put("suffix", shardId.getSuffix());

            return parameter;
        } else if (obj instanceof Object) {
            Map<String, Object> parameter = PropertyUtils.describe(obj);
            parameter.put("prefix", shardId.getPrefix());
            parameter.put("suffix", shardId.getSuffix());

            return parameter;
        } else if (obj != null) {
            throw new UnsupportedOperationException(
                    String.format("The parameter of type {%s} is not supported.", obj.getClass()));
        }

    } catch (Exception e) {
        e.printStackTrace();
        throw new RuntimeException(e);
    }

    return null;
}

From source file:net.firejack.platform.generate.VelocityGenerator.java

/**
 * @param name/*from   w w  w  .j  a va2  s  .c  o  m*/
 * @param generate
 * @param model
 * @param file
 * @param prepare
 */
public void compose(String name, Base generate, Map model, File file, boolean prepare) {
    try {
        Map describe = PropertyUtils.describe(generate);

        if (model != null) {
            describe.putAll(model);
        }

        compose(name, describe, file, prepare);
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    } catch (InvocationTargetException e) {
        e.printStackTrace();
    } catch (NoSuchMethodException e) {
        e.printStackTrace();
    }
}

From source file:com.liferay.wsrp.proxy.TypeConvertorUtil.java

public static Object convert(Object source, int sourceVersion) throws Exception {

    if (source == null) {
        return null;
    }//from  www.  j  a  va  2  s.  c o m

    String sourcePackage = _V1_PACKAGE;
    String destinationPackage = _V2_PACKAGE;

    if (sourceVersion == 2) {
        sourcePackage = _V2_PACKAGE;
        destinationPackage = _V1_PACKAGE;
    }

    Class<?> sourceClass = source.getClass();

    String sourceClassName = sourceClass.getSimpleName();

    Object destination = null;

    if (sourceClass.isArray()) {
        destination = source;

        Class<?> componentType = sourceClass.getComponentType();

        if (componentType.getName().contains(sourcePackage)) {
            Object[] sourceArray = (Object[]) source;

            Class<?> destinationComponentType = Class
                    .forName(destinationPackage + componentType.getSimpleName());

            Object[] destinationArray = (Object[]) Array.newInstance(destinationComponentType,
                    sourceArray.length);

            for (int i = 0; i < sourceArray.length; i++) {
                Object sourceArrayValue = sourceArray[i];

                destinationArray[i] = convert(sourceArrayValue, sourceVersion);
            }

            destination = destinationArray;
        }
    } else if (sourceClass == CookieProtocol.class) {
        CookieProtocol cookieProtocol = (CookieProtocol) source;

        destination = oasis.names.tc.wsrp.v2.types.CookieProtocol.fromValue(cookieProtocol.getValue());
    } else if (sourceClass == oasis.names.tc.wsrp.v2.types.CookieProtocol.class) {

        oasis.names.tc.wsrp.v2.types.CookieProtocol cookieProtocol = (oasis.names.tc.wsrp.v2.types.CookieProtocol) source;

        destination = CookieProtocol.fromValue(cookieProtocol.getValue());
    } else if (sourceClass == StateChange.class) {
        StateChange stateChange = (StateChange) source;

        destination = oasis.names.tc.wsrp.v2.types.StateChange.fromValue(stateChange.getValue());
    } else if (sourceClass == oasis.names.tc.wsrp.v2.types.StateChange.class) {

        oasis.names.tc.wsrp.v2.types.StateChange stateChange = (oasis.names.tc.wsrp.v2.types.StateChange) source;

        destination = StateChange.fromValue(stateChange.getValue());
    } else {
        Class<?> destinationClass = Class.forName(destinationPackage + sourceClassName);

        destination = destinationClass.newInstance();

        Map<String, Object> sourceChildren = PropertyUtils.describe(source);

        for (Map.Entry<String, Object> sourceChildEntry : sourceChildren.entrySet()) {

            String sourceChildName = sourceChildEntry.getKey();

            if (sourceChildName.equals("class")) {
                continue;
            }

            Object sourceChild = sourceChildEntry.getValue();

            if (sourceChild == null) {
                continue;
            }

            _convert(sourceVersion, sourcePackage, sourceClass, sourceChild, sourceChildName, destination);
        }
    }

    return destination;
}

From source file:net.firejack.platform.generate.VelocityGenerator.java

public void compose(String name, iPad generate, File file) {
    try {//from  w  w w .  j a  va 2 s.  com
        Map describe = PropertyUtils.describe(generate);
        compose(name, describe, file, false);
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    } catch (InvocationTargetException e) {
        e.printStackTrace();
    } catch (NoSuchMethodException e) {
        e.printStackTrace();
    }
}

From source file:net.sourceforge.atunes.utils.ReflectionUtils.java

/**
 * Returns map with properties and values of given object
 * /* w  w  w .  ja v  a  2  s  . c o  m*/
 * @param bean
 * @return
 */
@SuppressWarnings("unchecked")
public static Map<String, String> describe(final Object bean) {
    try {
        Map<String, String> description = PropertyUtils.describe(bean);
        description.remove("class");
        return description;
    } catch (IllegalAccessException e) {
        Logger.error(e);
    } catch (InvocationTargetException e) {
        Logger.error(e);
    } catch (NoSuchMethodException e) {
        Logger.error(e);
    }
    return null;
}

From source file:com.npower.dm.setup.task.ModelFamilyManager.java

/**
 * Copy family information into modelItem.
 * srcdestEmpty.//from   w w  w  .  j  av  a  2 s .co  m
 * @param modelItem
 * @param familyItem
 * 
 * @throws IllegalAccessException
 * @throws InvocationTargetException
 * @throws NoSuchMethodException
 */
@SuppressWarnings("unchecked")
private void copy(Object src, Object dest)
        throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
    Map<String, ?> props = PropertyUtils.describe(dest);
    for (String name : props.keySet()) {
        Object value = PropertyUtils.getProperty(dest, name);
        try {
            if (value == null) {
                Object valueOfFamily = PropertyUtils.getProperty(src, name);
                PropertyUtils.setProperty(dest, name, valueOfFamily);
            } else if (value instanceof List) {
                if (((List<?>) value).size() == 0) {
                    Object valueOfFamily = PropertyUtils.getProperty(src, name);
                    PropertyUtils.setProperty(dest, name, valueOfFamily);
                }
            }
        } catch (NoSuchMethodException e) {

        }
    }
}

From source file:hermes.ext.ems.TibcoEMSAdmin.java

public Map getStatistics(final DestinationConfig destination) throws JMSException {
    try {/*from   w  w  w. ja  v a2s.  c o  m*/
        final DestinationInfo info = getDestinationInfo(destination);
        final TreeMap rval = new TreeMap();

        rval.putAll(PropertyUtils.describe(info));

        rval.remove("inboundStatistics");
        rval.remove("outboundStatistics");

        rval.put("inboundByteRate", new Long(info.getInboundStatistics().getByteRate()));
        rval.put("inboundMessageRate", new Long(info.getInboundStatistics().getMessageRate()));
        rval.put("inboundTotalBytes", new Long(info.getInboundStatistics().getTotalBytes()));
        rval.put("inboundTotalMessages", new Long(info.getInboundStatistics().getTotalMessages()));

        rval.put("outboundByteRate", new Long(info.getOutboundStatistics().getByteRate()));
        rval.put("outboundMessageRate", new Long(info.getOutboundStatistics().getMessageRate()));
        rval.put("outboundTotalBytes", new Long(info.getOutboundStatistics().getTotalBytes()));
        rval.put("outboundTotalMessages", new Long(info.getOutboundStatistics().getTotalMessages()));

        return rval;
    } catch (IllegalAccessException e) {
        throw new HermesException(e);
    } catch (InvocationTargetException e) {
        throw new HermesException(e);
    } catch (NoSuchMethodException e) {
        throw new HermesException(e);
    }
}

From source file:hermes.browser.dialog.EditNamingConfigDialog.java

protected void onOK() {
    try {/*from  www. j  ava 2 s  .  c o m*/
        final NamingConfig config = (NamingConfig) namingConfigsByName.get(comboBox.getSelectedItem());
        final Map map = PropertyUtils.describe(bean);

        config.setClasspathId(classpathIdProperty.getValue().toString());

        if (config.getProperties() == null) {
            config.setProperties(new PropertySetConfig());
        } else {
            config.getProperties().getProperty().clear();
        }

        HermesBrowser.getConfigDAO().populatePropertySet(map, config.getProperties());

        if (config == newConfig) {
            namingConfigs.add(config);
        }

        HermesBrowser.getBrowser().saveConfig();
        HermesBrowser.getBrowser().loadConfig();
    } catch (Exception e) {
        log.error(e.getMessage(), e);

        HermesBrowser.getBrowser().showErrorDialog(e);
    }
}

From source file:com.utest.webservice.builders.Builder.java

public Ti toInfo(final To object, final UriBuilder ub, Object... uriBuilderArgs) throws Exception {
    Ti result;/*from  w w w . j  a v a 2 s  . c  om*/
    final Constructor<Ti> constr = infoClass.getConstructor(new Class[] {});
    if (constr == null) {
        throw new IllegalArgumentException("No default constructor found for " + infoClass.getName());
    }
    result = constr.newInstance(new Object[] {});
    ConvertUtilsBean cub = new ConvertUtilsBean();
    // do not throw exceptions on null values
    cub.register(false, true, 0);
    BeanUtilsBean bub = new BeanUtilsBean(cub);
    bub.copyProperties(result, object);
    // PropertyUtils.copyProperties(result, object);
    if (object instanceof LocalizedEntity) {
        LocalizedEntity localizedEntity = (LocalizedEntity) object;
        LocaleDescriptable localDescriptable = localizedEntity.getLocale(Locale.DEFAULT_LOCALE);
        // PropertyUtils.copyProperties(result, localDescriptable);
        bub.copyProperties(result, localDescriptable);
    }
    Map<?, ?> resultProperties = PropertyUtils.describe(result);
    // don't return password field
    if (resultProperties.containsKey("password")) {
        PropertyUtils.setProperty(result, "password", null);
    }
    //
    populateLocators(result, ub);
    //
    if (result instanceof BaseInfo) {
        populateIdentityAndTimeline((BaseInfo) result, object, ub, uriBuilderArgs);
    }

    // special handling for descendands of Builder
    populateExtendedProperties(result, object, ub, uriBuilderArgs);

    return result;
}