private static Object coerceType()

in repository/service/src/main/java/org/apache/karaf/cave/repository/service/bundlerepository/CapabilitySet.java [407:444]


    private static Object coerceType(Object lhs, String rhsString) throws Exception {
        // If the LHS expects a string, then we can just return
        // the RHS since it is a string.
        if (lhs.getClass() == rhsString.getClass()) {
            return rhsString;
        }

        // Try to convert the RHS type to the LHS type by using
        // the string constructor of the LHS class, if it has one.
        Object rhs;
        try {
            if (lhs instanceof Version) {
                rhs = VersionTable.getVersion(rhsString, false);
            } else
                // The Character class is a special case, since its constructor
                // does not take a string, so handle it separately.
                if (lhs instanceof Character) {
                    rhs = rhsString.charAt(0);
                } else {
                    // Spec says we should trim number types.
                    if ((lhs instanceof Number) || (lhs instanceof Boolean)) {
                        rhsString = rhsString.trim();
                    }
                    Constructor ctor = lhs.getClass().getConstructor(STRING_CLASS);
                    ctor.setAccessible(true);
                    rhs = ctor.newInstance(rhsString);
                }
        } catch (Exception ex) {
            throw new Exception(
                    "Could not instantiate class "
                            + lhs.getClass().getName()
                            + " from string constructor with argument '"
                            + rhsString + "' because " + ex
            );
        }

        return rhs;
    }