Example usage for org.apache.commons.beanutils WrapDynaBean getDynaClass

List of usage examples for org.apache.commons.beanutils WrapDynaBean getDynaClass

Introduction

In this page you can find the example usage for org.apache.commons.beanutils WrapDynaBean getDynaClass.

Prototype

public DynaClass getDynaClass() 

Source Link

Document

Return the DynaClass instance that describes the set of properties available for this DynaBean.

Usage

From source file:com.expressui.core.util.ReflectionUtil.java

/**
 * Finds all properties in the bean that are complex, that is not BeanUtils.isSimpleValueType
 *
 * @param bean bean to reflectively analyze
 * @return collection of properties on the bean
 */// w  w w.j a v  a  2  s. c om
public static Collection<String> findComplexProperties(Object bean) {
    Collection<String> complexProperties = new ArrayList<String>();

    WrapDynaBean wrapDynaBean = new WrapDynaBean(bean);
    DynaProperty[] properties = wrapDynaBean.getDynaClass().getDynaProperties();
    for (DynaProperty property : properties) {
        String propertyName = property.getName();
        Class propertyType = property.getType();
        if (!BeanUtils.isSimpleValueType(propertyType)) {
            complexProperties.add(propertyName);
        }
    }

    return complexProperties;
}

From source file:com.expressui.core.util.ReflectionUtil.java

/**
 * Asks if the bean's properties are empty. boolean properties that are false and numbers
 * that are zero are considered empty. String values that are zero-length are considered empty.
 * All other property types must be null to be considered empty.
 *
 * @param bean bean to check/*from  www. j a  v  a 2 s. c  om*/
 * @return true if bean has no values
 */
public static boolean isBeanEmpty(Object bean) {
    if (bean == null) {
        return true;
    }

    WrapDynaBean wrapDynaBean = new WrapDynaBean(bean);
    DynaProperty[] properties = wrapDynaBean.getDynaClass().getDynaProperties();
    for (DynaProperty property : properties) {
        String propertyName = property.getName();
        Class propertyType = property.getType();

        Object value = wrapDynaBean.get(propertyName);
        if (propertyType.isPrimitive()) {
            if (value instanceof Number && !value.toString().equals("0")) {
                return false;
            } else if (value instanceof Boolean && !value.toString().equals("false")) {
                return false;
            } else if (!value.toString().isEmpty()) {
                return false;
            }
        } else if (value != null) {
            if (!(value instanceof Collection)) {
                String convertedStringValue = ConvertUtils.convert(value);
                if (!StringUtil.isEmpty(convertedStringValue)) {
                    return false;
                }
            }
        }
    }

    return true;
}

From source file:org.gbif.registry.ws.guice.StringTrimInterceptor.java

private void trimStringsOf(Object target, int level) {
    if (target != null && level <= MAX_RECURSION) {
        LOG.debug("Trimming class: {}", target.getClass());

        WrapDynaBean wrapped = new WrapDynaBean(target);
        DynaClass dynaClass = wrapped.getDynaClass();
        for (DynaProperty dynaProp : dynaClass.getDynaProperties()) {
            if (String.class.isAssignableFrom(dynaProp.getType())) {
                String prop = dynaProp.getName();
                String orig = (String) wrapped.get(prop);
                if (orig != null) {
                    String trimmed = Strings.emptyToNull(orig.trim());
                    if (!Objects.equal(orig, trimmed)) {
                        LOG.debug("Overriding value of [{}] from [{}] to [{}]", prop, orig, trimmed);
                        wrapped.set(prop, trimmed);
                    }//from www .j a va  2  s  .  c om
                }
            } else {
                try {
                    // trim everything in the registry model package (assume that Dataset resides in the correct package here)
                    Object property = wrapped.get(dynaProp.getName());
                    if (property != null && Dataset.class.getPackage() == property.getClass().getPackage()) {
                        trimStringsOf(property, level + 1);
                    }

                } catch (IllegalArgumentException e) {
                    // expected for non accessible properties
                }
            }
        }
    }
}

From source file:org.gbif.registry2.ws.guice.StringTrimInterceptor.java

private void trimStringsOf(Object target) {
    WrapDynaBean wrapped = new WrapDynaBean(target);
    DynaClass dynaClass = wrapped.getDynaClass();
    for (DynaProperty dynaProp : dynaClass.getDynaProperties()) {
        // Only operate on strings
        if (String.class.isAssignableFrom(dynaProp.getType())) {
            String prop = dynaProp.getName();
            String orig = (String) wrapped.get(prop);
            if (orig != null) {
                String trimmed = Strings.emptyToNull(orig.trim());
                if (!Objects.equal(orig, trimmed)) {
                    LOG.debug("Overriding value of [{}] from [{}] to [{}]", prop, orig, trimmed);
                    wrapped.set(prop, trimmed);
                }/*from   w  ww.  j a  va 2  s  . c  o m*/
            }
        }
    }
}

From source file:org.hyperic.lather.util.Latherize.java

public void latherize(boolean xDocletStyle, String outPackage, Class fClass, OutputStream os)
        throws LatherizeException, IOException {
    final String INDENT = "        ";
    PrintWriter pWriter;/*from w  w  w . j ava 2 s .co m*/
    DynaProperty[] dProps;
    WrapDynaBean dBean;
    DynaClass dClass;
    Object beanInstance;
    String className, lClassName;

    try {
        beanInstance = fClass.newInstance();
    } catch (IllegalAccessException exc) {
        throw new LatherizeException("Illegal access trying to create " + "new instance");
    } catch (InstantiationException exc) {
        throw new LatherizeException("Unable to instantiate: " + exc.getMessage());
    }

    dBean = new WrapDynaBean(beanInstance);
    dClass = dBean.getDynaClass();
    dProps = dClass.getDynaProperties();

    pWriter = new PrintWriter(os);

    className = fClass.getName();
    className = className.substring(className.lastIndexOf(".") + 1);
    lClassName = "Lather" + className;

    pWriter.println("package " + outPackage + ";");
    pWriter.println();
    pWriter.println("import " + LatherValue.class.getName() + ";");
    pWriter.println("import " + LatherRemoteException.class.getName() + ";");
    pWriter.println("import " + LatherKeyNotFoundException.class.getName() + ";");
    pWriter.println("import " + fClass.getName() + ";");
    pWriter.println();
    pWriter.println("public class " + lClassName);
    pWriter.println("    extends LatherValue");
    pWriter.println("{");
    for (int i = 0; i < dProps.length; i++) {
        pWriter.print("    ");
        if (!this.isLatherStyleProp(dProps[i])) {
            pWriter.print("// ");
        }

        pWriter.println("private static final String " + this.makePropVar(dProps[i]) + " = \""
                + dProps[i].getName() + "\";");
    }

    pWriter.println();
    pWriter.println("    public " + lClassName + "(){");
    pWriter.println("        super();");
    pWriter.println("    }");
    pWriter.println();
    pWriter.println("    public " + lClassName + "(" + className + " v){");
    pWriter.println("        super();");
    pWriter.println();
    for (int i = 0; i < dProps.length; i++) {
        String propVar = this.makePropVar(dProps[i]);
        String getter = "v." + this.makeGetter(dProps[i]) + "()";

        if (!this.isLatherStyleProp(dProps[i])) {
            continue;
        }

        if (xDocletStyle) {
            String lName;

            lName = dProps[i].getName();
            lName = lName.substring(0, 1).toLowerCase() + lName.substring(1);
            pWriter.println(INDENT + "if(v." + lName + "HasBeenSet()){");
            pWriter.print("    ");
        }

        if (dProps[i].getType().equals(String.class)) {
            pWriter.println(INDENT + "this.setStringValue(" + propVar + ", " + getter + ");");
        } else if (dProps[i].getType().equals(Integer.TYPE)) {
            pWriter.println(INDENT + "this.setIntValue(" + propVar + ", " + getter + ");");
        } else if (dProps[i].getType().equals(Integer.class)) {
            pWriter.println(INDENT + "this.setIntValue(" + propVar + ", " + getter + ".intValue());");
        } else if (dProps[i].getType().equals(Long.TYPE)) {
            pWriter.println(INDENT + "this.setDoubleValue(" + propVar + ", (double)" + getter + ");");
        } else if (dProps[i].getType().equals(Long.class)) {
            pWriter.println(
                    INDENT + "this.setDoubleValue(" + propVar + ", (double)" + getter + ".longValue());");
        } else if (dProps[i].getType().equals(Boolean.TYPE)) {
            pWriter.println(INDENT + "this.setIntValue(" + propVar + ", " + getter + " ? 1 : 0);");
        } else if (dProps[i].getType().equals(Boolean.class)) {
            pWriter.println(
                    INDENT + "this.setIntValue(" + propVar + ", " + getter + ".booleanValue() ? 1 : 0);");
        } else if (dProps[i].getType().equals(Double.TYPE)) {
            pWriter.println(INDENT + "this.setDoubleValue(" + propVar + ", " + getter + ");");
        } else if (dProps[i].getType().equals(Double.class)) {
            pWriter.println(INDENT + "this.setDoubleValue(" + propVar + ", " + getter + ".doubleValue());");
        } else if (dProps[i].getType().equals(byte[].class)) {
            pWriter.println(INDENT + "this.setByteAValue(" + propVar + ", " + getter + ");");
        } else {
            pWriter.println(INDENT + "this.setObjectValue(" + "DONT KNOW HOW TO HANDLE THIS, " + getter + ");");
        }

        if (xDocletStyle) {
            pWriter.println(INDENT + "}");
            pWriter.println();
        }
    }
    pWriter.println("    }");

    pWriter.println();
    pWriter.println("    public " + className + " get" + className + "(){");
    pWriter.println(INDENT + className + " r = new " + className + "();");
    pWriter.println();
    for (int i = 0; i < dProps.length; i++) {
        String propVar = this.makePropVar(dProps[i]);
        String setter = "r." + this.makeSetter(dProps[i]) + "(";

        if (!this.isLatherStyleProp(dProps[i])) {
            continue;
        }

        if (xDocletStyle) {
            pWriter.println(INDENT + "try {");
            pWriter.print("    ");
        }

        pWriter.print(INDENT + setter);
        if (dProps[i].getType().equals(String.class)) {
            pWriter.println("this.getStringValue(" + propVar + "));");
        } else if (dProps[i].getType().equals(Integer.TYPE)) {
            pWriter.println("this.getIntValue(" + propVar + "));");
        } else if (dProps[i].getType().equals(Integer.class)) {
            pWriter.println("new Integer(this.getIntValue(" + propVar + ")));");
        } else if (dProps[i].getType().equals(Long.TYPE)) {
            pWriter.println("(long)this.getDoubleValue(" + propVar + "));");
        } else if (dProps[i].getType().equals(Long.class)) {
            pWriter.println("new Long((long)this.getDoubleValue(" + propVar + ")));");
        } else if (dProps[i].getType().equals(Boolean.TYPE)) {
            pWriter.println("this.getIntValue(" + propVar + ") == 1 ? " + "true : false);");
        } else if (dProps[i].getType().equals(Boolean.class)) {
            pWriter.println("this.getIntValue(" + propVar + ") == 1 ? " + "Boolean.TRUE : Boolean.FALSE);");
        } else if (dProps[i].getType().equals(Double.TYPE)) {
            pWriter.println("this.getDoubleValue(" + propVar + "));");
        } else if (dProps[i].getType().equals(Double.class)) {
            pWriter.println("new Double(this.getDoubleValue(" + propVar + ")));");
        } else if (dProps[i].getType().equals(byte[].class)) {
            pWriter.println("this.getByteAValue(" + propVar + "));");
        } else {
            pWriter.println("DONT KNOW HOW TO HANDLE " + propVar + "));");
        }

        if (xDocletStyle) {
            pWriter.println(INDENT + "} catch(LatherKeyNotFoundException " + "exc){}");
            pWriter.println();
        }
    }

    pWriter.println(INDENT + "return r;");
    pWriter.println("    }");
    pWriter.println();
    pWriter.println("    protected void validate()");
    pWriter.println("        throws LatherRemoteException");
    pWriter.println("    {");
    if (!xDocletStyle) {
        pWriter.println("        try { ");
        pWriter.println("            this.get" + className + "();");
        pWriter.println("        } catch(LatherKeyNotFoundException e){");
        pWriter.println("            throw new LatherRemoteException(\"" + "All values not set\");");
        pWriter.println("        }");
    }
    pWriter.println("    }");
    pWriter.println("}");
    pWriter.flush();
}