Example usage for org.apache.commons.beanutils MethodUtils getPrimitiveWrapper

List of usage examples for org.apache.commons.beanutils MethodUtils getPrimitiveWrapper

Introduction

In this page you can find the example usage for org.apache.commons.beanutils MethodUtils getPrimitiveWrapper.

Prototype

public static Class getPrimitiveWrapper(Class primitiveType) 

Source Link

Document

Gets the wrapper object class for the given primitive type class.

Usage

From source file:com.jaspersoft.ireport.designer.data.fieldsproviders.hibernate.HQLFieldsReader.java

public Vector readFields() throws Exception {
    prepareQuery();// w ww. j a  v a2 s.  c o m

    SessionFactory hb_sessionFactory = null;
    Session hb_session = null;
    Transaction transaction = null;

    notScalars.clear();

    try {
        IReportConnection conn = IReportManager.getInstance().getDefaultConnection();
        if (!(conn instanceof JRHibernateConnection)) {
            throw new Exception("No Hibernate connection selected.");
        }

        hb_session = ((JRHibernateConnection) conn).createSession();

        if (hb_session == null) {
            throw new Exception("Problem creating the Session object for Hibernate");
        }

        transaction = hb_session.beginTransaction();
        Query q = hb_session.createQuery(getQueryString());

        Iterator paramIterator = queryParameters.keySet().iterator();

        while (paramIterator.hasNext()) {
            String hqlParamName = "" + paramIterator.next();
            setParameter(hb_session, q, hqlParamName, queryParameters.get(hqlParamName));
        }

        q.setFetchSize(1);
        java.util.Iterator iterator = q.iterate();
        // this is a stupid thing: iterator.next();

        String[] aliases = q.getReturnAliases();
        Type[] types = q.getReturnTypes();

        Vector fields = new Vector();

        for (int i = 0; i < types.length; ++i) {
            if (types[i].isComponentType() || types[i].isEntityType()) {

                // look for alias...
                String aliasName = null;
                if (aliases != null && aliases.length > i && !aliases[i].equals(i + "")) {
                    aliasName = aliases[i];
                    JRDesignField field = new JRDesignField();
                    field.setName(aliases[i]);

                    Class clazzRT = types[i].getReturnedClass();

                    if (clazzRT.isPrimitive()) {
                        clazzRT = MethodUtils.getPrimitiveWrapper(clazzRT);
                    }

                    String returnType = clazzRT.getName();

                    field.setValueClassName(returnType);
                    field.setDescription(aliases[i]);
                    fields.add(field);
                }

                // look for fields like for a javabean...
                java.beans.PropertyDescriptor[] pd = org.apache.commons.beanutils.PropertyUtils
                        .getPropertyDescriptors(types[i].getReturnedClass());

                if (aliasName != null) {
                    notScalars.add(new FieldClassWrapper(aliasName, types[i].getReturnedClass().getName()));
                } else {
                    notScalars.add(types[i].getReturnedClass().getName());
                }

                for (int nd = 0; nd < pd.length; ++nd) {
                    String fieldName = pd[nd].getName();
                    if (pd[nd].getPropertyType() != null && pd[nd].getReadMethod() != null) {
                        if (fieldName.equals("class"))
                            continue;

                        Class clazzRT = pd[nd].getPropertyType();

                        if (clazzRT.isPrimitive()) {
                            clazzRT = MethodUtils.getPrimitiveWrapper(clazzRT);
                        }

                        String returnType = clazzRT.getName();

                        JRDesignField field = new JRDesignField();
                        field.setName(fieldName);
                        field.setValueClassName(returnType);

                        if (types.length > 1 && aliasName != null) {
                            fieldName = aliasName + "." + fieldName;
                            field.setDescription(fieldName); //Field returned by " +methods[i].getName() + " (real type: "+ returnType +")");
                            field.setName(fieldName);
                        }
                        fields.add(field);
                    }
                }

            } else {
                String fieldName = types[i].getName();
                if (aliases != null && aliases.length > i && !aliases[i].equals("" + i))
                    fieldName = aliases[i];

                Class clazzRT = types[i].getReturnedClass();

                if (clazzRT.isPrimitive()) {
                    clazzRT = MethodUtils.getPrimitiveWrapper(clazzRT);
                }
                String returnType = clazzRT.getName();

                JRDesignField field = new JRDesignField();
                field.setName(fieldName);
                field.setValueClassName(returnType);
                //field.setDescription("");
                fields.add(field);
            }
        }
        /*
        else
        {
            for (int i =0; i<types.length; ++i)
            {
                if (aliases != null && aliases.length > 0 && !aliases[0].equals(""+i))
                {
                    JRField field = new JRField(aliases[i], types[i].getReturnedClass().getName());
                    field.setDescription("The whole entity/component object");
                    fields.add(field);
                            
                            
                }
                        
                        
            //    out.println(types[i].getName() + " " + types[i].getReturnedClass().getName() +  "<br>");
            }
        }
         */

        return fields;

    } catch (Exception ex) {
        ex.printStackTrace();
        throw ex;
    } finally {

        if (transaction != null)
            try {
                transaction.rollback();
            } catch (Exception ex) {
            }
        if (hb_session != null)
            try {
                hb_session.close();
            } catch (Exception ex) {
            }
    }
}

From source file:com.jaspersoft.ireport.designer.data.fieldsproviders.BeanInspectorPanel.java

public void exploreBean(DefaultMutableTreeNode root, String classname, String parentPath) {
    try {/*from  w  w w.j  a  va2 s.  c  o  m*/

        root.removeAllChildren();
        if (parentPath.length() > 0)
            parentPath += ".";

        Class clazz = Class.forName(classname, true, IReportManager.getInstance().getReportClassLoader());

        java.beans.PropertyDescriptor[] pd = org.apache.commons.beanutils.PropertyUtils
                .getPropertyDescriptors(clazz);
        for (int nd = 0; nd < pd.length; ++nd) {
            String fieldName = pd[nd].getName();
            if (pd[nd].getPropertyType() != null && pd[nd].getReadMethod() != null) {
                Class clazzRT = pd[nd].getPropertyType();

                if (clazzRT.isPrimitive()) {
                    clazzRT = MethodUtils.getPrimitiveWrapper(clazzRT);
                }

                String returnType = clazzRT.getName();

                JRDesignField field = new JRDesignField();
                field.setName(fieldName);
                field.setValueClassName(returnType);

                if (isPathOnDescription()) {
                    field.setDescription(parentPath + fieldName);
                } else {
                    field.setName(parentPath + fieldName);
                }

                TreeJRField jtf = new TreeJRField();

                jtf.setField(field);
                jtf.setObj(pd[nd].getPropertyType());

                boolean bChildrens = true;
                if (pd[nd].getPropertyType().isPrimitive()
                        || pd[nd].getPropertyType().getName().startsWith("java.lang.")) {
                    bChildrens = false;
                }
                root.add(new DefaultMutableTreeNode(jtf, bChildrens));
            }
        }

        jTree1.expandPath(new TreePath(root.getPath()));
        jTree1.updateUI();

    } catch (ClassNotFoundException cnf) {
        javax.swing.JOptionPane.showMessageDialog(this, Misc.formatString( //"messages.BeanInspectorPanel.classNotFoundError",
                I18n.getString("BeanInspectorPanel.Message.Error"), new Object[] { cnf.getMessage() }),
                I18n.getString("BeanInspectorPanel.Message.Error2"), javax.swing.JOptionPane.ERROR_MESSAGE);
        return;
    } catch (Exception ex) {
        ex.printStackTrace();
        javax.swing.JOptionPane.showMessageDialog(this, ex.getMessage(),
                I18n.getString("BeanInspectorPanel.Message.Error2"), javax.swing.JOptionPane.ERROR_MESSAGE);
        return;
    }
}

From source file:org.apache.empire.commons.ObjectUtils.java

/**
 * Generic conversion function that will convert a object to another value type.
 * /*from  w w  w.  j a v a 2 s .com*/
 * @param <T> the type to convert to
 * @param c the class type to convert to
 * @param v the object to convert
 * 
 * @return the Date value of o or null
 * 
 * @throws ClassCastException if the object is not null and is not assignable to the type T.
 */
@SuppressWarnings("unchecked")
public static <T> T convert(Class<T> c, Object v) throws ClassCastException {
    if (v == null || c.isInstance(v))
        return (T) v;
    // Get Class form Primitive Type
    if (c.isPrimitive()) { // Get's the Java Class representing the primitive type
        c = MethodUtils.getPrimitiveWrapper(c);
    }
    // Convert
    if (c.isAssignableFrom(Boolean.class))
        return c.cast(getBoolean(v));
    if (c.isAssignableFrom(Integer.class))
        return c.cast(getInteger(v));
    if (c.isAssignableFrom(Long.class))
        return c.cast(getLong(v));
    if (c.isAssignableFrom(Double.class))
        return c.cast(getDouble(v));
    if (c.isAssignableFrom(String.class))
        return c.cast(v.toString());
    // other
    return c.cast(v);
}

From source file:org.apache.empire.commons.ObjectUtils.java

public static boolean isAssignmentCompatible(Class<?> target, Class<?> source) {
    // try plain assignment
    if (target.isAssignableFrom(source))
        return true;
    // Get Class form Primitive Type
    if (source.isPrimitive()) { // Get's the Java Class representing the primitive type
        source = MethodUtils.getPrimitiveWrapper(source);
        if (source == null)
            return false;
        if (target.isAssignableFrom(source))
            return true;
    }//from   w ww . j  a  v a  2  s  .  com
    // Get Class form Primitive Type
    if (target.isPrimitive()) { // Get's the Java Class representing the primitive type
        target = MethodUtils.getPrimitiveWrapper(target);
        if (target == null)
            return false;
        if (target.isAssignableFrom(source))
            return true;
    }
    // Assume all numeric types can be converted to target class
    Class<Number> numberClass = Number.class;
    if (numberClass.isAssignableFrom(target) && numberClass.isAssignableFrom(source)) { // Both are numeric
        return true;
    }
    // Special case: Allow character to string assignment
    if (source == Character.class && target == String.class) {
        return true;
    }
    // Not compatible
    return false;
}

From source file:org.mule.transformer.TransformerWeighting.java

/**
 * This is a very basic algorithm for creating a match rating for two classes. An offset weighting
 * can also be passed in. Where w is weighting,
 * if the classes are not assignable but the src is a primitive type, the w for the corresponding object type will be returned, otherwise return -1
 * if the classes match exactly and dest is Object then w+3 is returned
 * if the classes match exactly and dest is not Object then w+1 is returned
 * if the classes are assignable and there is a direct equality to an interface on the class, w+2  is returned,
 * If there a super class, that will get matched using the above criteria but using w = w + 1
 * If there is no match -1 is returned//from  w  w w .j  a  v a 2 s.  c  om
 *
 * @param weighting an offset weighting, by default -1 should be used
 * @param src the src class being matched
 * @param dest the destination class to match to
 * @return a weighting where 0 would be an exact match, -1 would be no match and a positive integer that defines how close the match is
 */
protected int getWeighting(int weighting, Class src, Class dest) {
    if (!dest.isAssignableFrom(src)) {
        if (src.isPrimitive()) {
            return getWeighting(weighting, MethodUtils.getPrimitiveWrapper(src), dest);
        }

        return -1;
    }

    if (dest.equals(src)) {
        if (dest.equals(Object.class)) {
            return weighting + 3;
        } else {
            return weighting + 1;
        }
    }

    if (dest.isInterface() && src.getInterfaces().length > 0) {
        for (int i = 0; i < src.getInterfaces().length; i++) {
            Class aClass = src.getInterfaces()[i];
            if (dest.equals(aClass)) {
                return weighting + 2;
            }
        }
    }

    if (src.getSuperclass() != null) {
        return getWeighting(weighting + 1, src.getSuperclass(), dest);
    }

    return -1;
}

From source file:org.mule.transformer.types.SimpleDataType.java

private Class<?> fromPrimitive(Class<?> type) {
    Class<?> primitiveWrapper = MethodUtils.getPrimitiveWrapper(type);
    if (primitiveWrapper != null) {
        return primitiveWrapper;
    } else {//ww w. jav a 2 s  .c o  m
        return type;
    }
}