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

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

Introduction

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

Prototype

public WrapDynaBean(Object instance) 

Source Link

Document

Construct a new DynaBean associated with the specified JavaBean instance.

Usage

From source file:org.ejbca.ui.cli.FieldEditor.java

public List<String> getSetMethodNames(final Object obj) {
    List<String> result = new ArrayList<String>();
    DynaBean wrapper = new WrapDynaBean(obj);
    DynaProperty[] props = wrapper.getDynaClass().getDynaProperties();
    for (DynaProperty dynaProperty : props) {
        if (!excluded.contains(dynaProperty.getName())) {
            result.add(dynaProperty.getName());
        }/*  www .  java  2s  .co  m*/
    }
    return result;
}

From source file:org.ejbca.ui.cli.FieldEditor.java

/** gets a field value from a bean
 * //from  w  w  w . j a  va  2 s  .c om
 * @param field the field to get
 * @param obj the bran to get the value from
 * @return the value
 */
public Object getBeanValue(final String field, final Object obj) {
    final DynaBean moddb = new WrapDynaBean(obj);
    final Object gotValue = moddb.get(field);
    logger.info(field + " returned value '" + gotValue + "'.");
    return gotValue;
}

From source file:org.faster.util.Beans.java

public static Object getProperty(Object bean, String propertyName) {
    return new WrapDynaBean(bean).get(propertyName);
}

From source file:org.faster.util.Beans.java

/**
 * ??//from www  .  j  av a2s.c o m
 *
 * @param dest 
 * @param orig 
 * @param ignoreNullValue ?
 * @param propertyNames ??
 */
public static void slicePopulate(Object dest, Object orig, boolean ignoreNullValue, String... propertyNames) {
    WrapDynaBean destBean = new WrapDynaBean(dest);
    WrapDynaBean origBean = new WrapDynaBean(orig);
    for (String propertyName : propertyNames) {
        Object value = origBean.get(propertyName);
        if (ignoreNullValue && isNullOrEmpty(value)) {
            continue;
        }

        destBean.set(propertyName, value);
    }
}

From source file:org.faster.util.Beans.java

@SuppressWarnings("unchecked")
public static <T> T slice(T orig, boolean ignoreNullValue, String... propertyNames) {
    if (propertyNames == null || propertyNames.length == 0) {
        return orig;
    }/*from w  ww  .  ja va  2 s.c  om*/

    T dest = (T) Beans.newInstance(orig.getClass());
    WrapDynaBean destBean = new WrapDynaBean(dest);
    WrapDynaBean origBean = new WrapDynaBean(orig);
    for (String propertyName : propertyNames) {
        Object value = origBean.get(propertyName);
        if (ignoreNullValue && Beans.isNullOrEmpty(value)) {
            continue;
        }

        destBean.set(propertyName, value);
    }
    return dest;
}

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   ww  w.  ja v  a 2 s  . c  o m*/
                }
            } 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);
                }//  w  w  w.ja  v  a 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  www .  j a  v a 2  s .  c  o 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();
}

From source file:org.latticesoft.util.common.BeanUtil.java

public static Object getAttribute(Object bean, String attribute) {
    Object retVal = null;//from   w w w .ja  v  a  2  s  . c om
    if (bean == null || attribute == null)
        return null;
    if (bean instanceof Map) {
        Map map = (Map) bean;
        retVal = map.get(attribute);
    } else if (bean instanceof WrapDynaBean) {
        WrapDynaBean w = (WrapDynaBean) bean;
        retVal = w.get(attribute);
    } else {
        WrapDynaBean w = new WrapDynaBean(bean);
        retVal = w.get(attribute);
    }
    return retVal;
}

From source file:org.latticesoft.util.common.BeanUtil.java

public static void setAttribute(Object bean, String attribute, Object value) {
    if (bean == null || attribute == null || value == null)
        return;//from   w  w w.  j av  a 2 s. c om
    if (bean instanceof Map) {
        Map map = (Map) bean;
        map.put(attribute, value);
    } else if (bean instanceof WrapDynaBean) {
        WrapDynaBean w = (WrapDynaBean) bean;
        w.set(attribute, value);
    } else {
        WrapDynaBean w = new WrapDynaBean(bean);
        w.set(attribute, value);
    }
}