Example usage for org.apache.commons.lang StringUtils capitalize

List of usage examples for org.apache.commons.lang StringUtils capitalize

Introduction

In this page you can find the example usage for org.apache.commons.lang StringUtils capitalize.

Prototype

public static String capitalize(String str) 

Source Link

Document

Capitalizes a String changing the first letter to title case as per Character#toTitleCase(char) .

Usage

From source file:org.apache.openjpa.lib.util.Options.java

/**
 * Finds all the options that can be set on the provided class. This does
 * not look for path-traversal expressions.
 *
 * @param type The class for which available options should be listed.
 * @return The available option names in <code>type</code>. The
 * names will have initial caps. They will be ordered alphabetically.
 *//*ww  w.j  a v a2  s  .c o m*/
public static Collection<String> findOptionsFor(Class<?> type) {
    Collection<String> names = new TreeSet<String>();
    // look for a setter method matching the key
    Method[] meths = type.getMethods();
    Class<?>[] params;
    for (int i = 0; i < meths.length; i++) {
        if (meths[i].getName().startsWith("set")) {
            params = meths[i].getParameterTypes();
            if (params.length == 0)
                continue;
            if (params[0].isArray())
                continue;

            names.add(StringUtils.capitalize(meths[i].getName().substring(3)));
        }
    }

    // check for public fields
    Field[] fields = type.getFields();
    for (int i = 0; i < fields.length; i++)
        names.add(StringUtils.capitalize(fields[i].getName()));

    return names;
}

From source file:org.apache.openjpa.lib.util.Options.java

/**
 * Matches a key to an object/setter pair.
 *
 * @param key the key given at the command line; may be of the form
 * 'foo.bar' to signify the 'bar' property of the 'foo' owned object
 * @param match an array of length 2, where the first index is set
 * to the object to retrieve the setter for
 * @return true if a match was made, false otherwise; additionally,
 * the first index of the match array will be set to
 * the matching object and the second index will be
 * set to the setter method or public field for the
 * property named by the key/*from ww  w.  j  a  v a2s. co  m*/
 */
private static boolean matchOptionToMember(String key, Object[] match) throws Exception {
    if (StringUtils.isEmpty(key))
        return false;

    // unfortunately we can't use bean properties for setters; any
    // setter with more than 1 argument is ignored; calculate setter and getter
    // name to look for
    String[] find = Strings.split(key, ".", 2);
    String base = StringUtils.capitalize(find[0]);
    String set = "set" + base;
    String get = "get" + base;

    // look for a setter/getter matching the key; look for methods first
    Class<? extends Object> type = match[0].getClass();
    Method[] meths = type.getMethods();
    Method setMeth = null;
    Method getMeth = null;
    Class[] params;
    for (int i = 0; i < meths.length; i++) {
        if (meths[i].getName().equals(set)) {
            params = meths[i].getParameterTypes();
            if (params.length == 0)
                continue;
            if (params[0].isArray())
                continue;

            // use this method if we haven't found any other setter, if
            // it has less parameters than any other setter, or if it uses
            // string parameters
            if (setMeth == null)
                setMeth = meths[i];
            else if (params.length < setMeth.getParameterTypes().length)
                setMeth = meths[i];
            else if (params.length == setMeth.getParameterTypes().length && params[0] == String.class)
                setMeth = meths[i];
        } else if (meths[i].getName().equals(get))
            getMeth = meths[i];
    }

    // if no methods found, check for public field
    Member setter = setMeth;
    Member getter = getMeth;
    if (setter == null) {
        Field[] fields = type.getFields();
        String uncapBase = StringUtils.uncapitalize(find[0]);
        for (int i = 0; i < fields.length; i++) {
            if (fields[i].getName().equals(base) || fields[i].getName().equals(uncapBase)) {
                setter = fields[i];
                getter = fields[i];
                break;
            }
        }
    }

    // if no way to access property, give up
    if (setter == null && getter == null)
        return false;

    // recurse on inner object with remainder of key?
    if (find.length > 1) {
        Object inner = null;
        if (getter != null)
            inner = invoke(match[0], getter, null);

        // if no getter or current inner is null, try to create a new
        // inner instance and set it in object
        if (inner == null && setter != null) {
            Class<?> innerType = getType(setter)[0];
            try {
                inner = AccessController.doPrivileged(J2DoPrivHelper.newInstanceAction(innerType));
            } catch (PrivilegedActionException pae) {
                throw pae.getException();
            }
            invoke(match[0], setter, new Object[] { inner });
        }
        match[0] = inner;
        return matchOptionToMember(find[1], match);
    }

    // got match; find setter for property
    match[1] = setter;
    return match[1] != null;
}

From source file:org.apache.openjpa.meta.InterfaceImplGenerator.java

/**
 * Add bean getters and setters, also recording seen methods
 * into the given set./*from   w  w w.j  ava2s  .  co  m*/
 */
private void addField(BCClass bc, Class<?> iface, FieldMetaData fmd, Set<Method> methods) {
    String name = fmd.getName();
    Class<?> type = fmd.getDeclaredType();
    BCField field = bc.declareField(name, type);
    field.setAccessFlags(Constants.ACCESS_PRIVATE);

    // getter
    name = StringUtils.capitalize(name);
    String prefix = isGetter(iface, fmd) ? "get" : "is";
    BCMethod meth = bc.declareMethod(prefix + name, type, null);
    meth.makePublic();
    Code code = meth.getCode(true);
    code.aload().setThis();
    code.getfield().setField(field);
    code.xreturn().setType(type);
    code.calculateMaxStack();
    code.calculateMaxLocals();
    methods.add(getMethodSafe(iface, meth.getName(), null));

    // setter
    meth = bc.declareMethod("set" + name, void.class, new Class[] { type });
    meth.makePublic();
    code = meth.getCode(true);
    code.aload().setThis();
    code.xload().setParam(0).setType(type);
    code.putfield().setField(field);
    code.vreturn();
    code.calculateMaxStack();
    code.calculateMaxLocals();
    methods.add(getMethodSafe(iface, meth.getName(), type));
}

From source file:org.apache.openjpa.meta.InterfaceImplGenerator.java

private static boolean isGetter(Class<?> iface, FieldMetaData fmd) {
    if (fmd.getType() != boolean.class && fmd.getType() != Boolean.class)
        return true;
    try {/*from   w w  w.  j  a va2s .  c  o  m*/
        Method meth = AccessController.doPrivileged(J2DoPrivHelper.getDeclaredMethodAction(iface,
                "is" + StringUtils.capitalize(fmd.getName()), (Class[]) null));
        return meth == null;
    } catch (PrivilegedActionException pae) {
    }
    return true;
}

From source file:org.apache.openjpa.persistence.JPAProperties.java

/**
 * Gets a bean-style property name from the given key.
 * //from w  w w.ja v  a 2 s  .  co  m
 * @param key must begin with JPA property prefix <code>javax.persistence</code>
 * 
 * @return concatenates each part of the string leaving out <code>javax.persistence.</code> prefix. 
 * Part of string is what appears between DOT character.
 */
public static String getBeanProperty(String key) {
    if (!isValidKey(key))
        throw new IllegalArgumentException("Invalid JPA property " + key);
    String[] parts = key.split(REGEX_DOT);
    StringBuilder buf = new StringBuilder();
    for (int i = 2; i < parts.length; i++) {
        buf.append(StringUtils.capitalize(parts[i]));
    }
    return buf.toString();
}

From source file:org.apache.openjpa.persistence.PersistenceMetaDataDefaults.java

protected boolean isDefaultPersistent(ClassMetaData meta, Member member, String name, boolean ignoreTransient) {
    int mods = member.getModifiers();
    if (Modifier.isTransient(mods))
        return false;
    int access = meta.getAccessType();

    if (member instanceof Field) {
        // If mixed or unknown, default property access, keep explicit 
        // field members
        if (AccessCode.isProperty(access)) {
            if (!isAnnotatedAccess(member, AccessType.FIELD))
                return false;
        }/*from   w w  w .  ja  v  a2s  .co m*/
    } else if (member instanceof Method) {
        // If mixed or unknown, field default access, keep explicit property
        // members
        if (AccessCode.isField(access)) {
            if (!isAnnotatedAccess(member, AccessType.PROPERTY))
                return false;
        }
        try {
            // check for setters for methods
            Method setter = (Method) AccessController.doPrivileged(J2DoPrivHelper.getDeclaredMethodAction(
                    meta.getDescribedType(), "set" + StringUtils.capitalize(name),
                    new Class[] { ((Method) member).getReturnType() }));
            if (setter == null && !isAnnotatedTransient(member)) {
                logNoSetter(meta, name, null);
                return false;
            }
        } catch (Exception e) {
            // e.g., NoSuchMethodException
            if (!isAnnotatedTransient(member))
                logNoSetter(meta, name, e);
            return false;
        }
    }

    PersistenceStrategy strat = getPersistenceStrategy(null, member, ignoreTransient);
    if (strat == null) {
        warn(meta, _loc.get("no-pers-strat", name));
        return false;
    } else if (strat == PersistenceStrategy.TRANSIENT) {
        return false;
    } else {
        return true;
    }
}

From source file:org.apache.openjpa.util.ProxyManagerImpl.java

/**
 * Proxy recognized methods to invoke helpers in given helper class.
 *//*from w ww.  j a  v  a  2 s.  c om*/
private void proxyRecognizedMethods(BCClass bc, Class type, Class helper, Class proxyType) {
    Method[] meths = type.getMethods();
    Class[] params;
    Class[] afterParams;
    Method match;
    Method after;
    for (int i = 0; i < meths.length; i++) {
        params = toHelperParameters(meths[i].getParameterTypes(), proxyType);

        // first check for overriding method 
        try {
            match = helper.getMethod(meths[i].getName(), params);
            proxyOverrideMethod(bc, meths[i], match, params);
            continue;
        } catch (NoSuchMethodException nsme) {
        } catch (Exception e) {
            throw new GeneralException(e);
        }

        // check for before and after methods, either of which may not
        // exist
        match = null;
        try {
            match = helper.getMethod("before" + StringUtils.capitalize(meths[i].getName()), params);
        } catch (NoSuchMethodException nsme) {
        } catch (Exception e) {
            throw new GeneralException(e);
        }
        after = null;
        afterParams = null;
        try {
            afterParams = toHelperAfterParameters(params, meths[i].getReturnType(),
                    (match == null) ? void.class : match.getReturnType());
            after = helper.getMethod("after" + StringUtils.capitalize(meths[i].getName()), afterParams);
        } catch (NoSuchMethodException nsme) {
        } catch (Exception e) {
            throw new GeneralException(e);
        }
        if (match != null || after != null)
            proxyBeforeAfterMethod(bc, type, meths[i], match, params, after, afterParams);
    }
}

From source file:org.apache.openmeetings.utils.mappings.StructureMethodList.java

public LinkedHashMap<String, LinkedHashMap<String, Object>> parseClassToMethodList(Class<?> targetClass) {
    try {/*  w ww  .ja  v a  2s  . c o m*/
        LinkedHashMap<String, LinkedHashMap<String, Object>> returnMap = new LinkedHashMap<String, LinkedHashMap<String, Object>>();

        for (Field field : targetClass.getDeclaredFields()) {
            String fieldName = field.getName();
            String capitalizedFieldName = StringUtils.capitalize(fieldName);
            String setterPre = "set";

            for (Method method : targetClass.getMethods()) {
                // check that method is declared in current class. 
                if (method.getName().equals(setterPre + capitalizedFieldName)) {
                    String methodName = method.getName();

                    Class<?>[] paramTypes = method.getParameterTypes();
                    //log.error("parseClassToMethodList methodName: "+methodName);
                    if (methodName.startsWith("set")) {
                        //Found setter get Attribute name
                        if (returnMap.get(fieldName) != null) {
                            LinkedHashMap<String, Object> methodListMap = returnMap.get(fieldName);
                            methodListMap.put("setter", methodName);
                            methodListMap.put("setterParamTypes", paramTypes);
                        } else {
                            LinkedHashMap<String, Object> methodListMap = new LinkedHashMap<String, Object>();
                            methodListMap.put("setter", methodName);
                            returnMap.put(fieldName, methodListMap);
                            methodListMap.put("setterParamTypes", paramTypes);
                        }
                    } else if (methodName.startsWith("is")) {
                        //Found setter(boolean) get Attribute name
                        if (returnMap.get(fieldName) != null) {
                            LinkedHashMap<String, Object> methodListMap = returnMap.get(fieldName);
                            methodListMap.put("getter", methodName);
                        } else {
                            LinkedHashMap<String, Object> methodListMap = new LinkedHashMap<String, Object>();
                            methodListMap.put("getter", methodName);
                            returnMap.put(fieldName, methodListMap);
                        }
                    } else if (methodName.startsWith("get")) {
                        //Found setter(boolean) get Attribute name
                        if (returnMap.get(fieldName) != null) {
                            LinkedHashMap<String, Object> methodListMap = returnMap.get(fieldName);
                            methodListMap.put("getter", methodName);
                        } else {
                            LinkedHashMap<String, Object> methodListMap = new LinkedHashMap<String, Object>();
                            methodListMap.put("getter", methodName);
                            returnMap.put(fieldName, methodListMap);
                        }
                    }
                    break;
                }
            }

        }

        return returnMap;
    } catch (Exception ex) {
        log.error("[parseClassToMethodList]", ex);
        return new LinkedHashMap<String, LinkedHashMap<String, Object>>();
    }

}

From source file:org.apache.qpid.disttest.client.MessageProvider.java

protected void setStandardProperty(Message message, String property, Object propertyValue) throws JMSException {
    String propertyName = "JMS" + StringUtils.capitalize(property);
    try {//from w  w w.  jav  a2 s  . com
        BeanUtils.setProperty(message, propertyName, propertyValue);
    } catch (IllegalAccessException e) {
        throw new DistributedTestException("Unable to set property " + propertyName + " :" + e.getMessage(), e);
    } catch (InvocationTargetException e) {
        if (e.getCause() instanceof JMSException) {
            throw ((JMSException) e.getCause());
        } else {
            throw new DistributedTestException("Unable to set property " + propertyName + " :" + e.getMessage(),
                    e);
        }
    }
}

From source file:org.apache.qpid.disttest.client.property.PropertyValueFactory.java

public Class<?> getPropertyValueClass(String type) throws ClassNotFoundException {
    String className = "org.apache.qpid.disttest.client.property." + StringUtils.capitalize(type)
            + "PropertyValue";
    return Class.forName(className);
}