private Object convertToResultType()

in bval-jsr/src/main/java/org/apache/bval/jsr/metadata/XmlBuilder.java [630:682]


    private Object convertToResultType(Class<?> returnType, String value) {
        /**
         * Class is represented by the fully qualified class name of the class. spec: Note that if the raw string is
         * unqualified, default package is taken into account.
         */
        if (String.class.equals(returnType)) {
            return value;
        }
        if (Class.class.equals(returnType)) {
            return resolveClass(value);
        }
        if (returnType.isEnum()) {
            try {
                @SuppressWarnings({ "rawtypes", "unchecked" })
                final Enum e = Enum.valueOf(returnType.asSubclass(Enum.class), value);
                return e;
            } catch (IllegalArgumentException e) {
                throw new ConstraintDeclarationException(e);
            }
        }
        try {
            if (Byte.class.equals(returnType) || byte.class.equals(returnType)) {
                // spec mandates it:
                return Byte.parseByte(value);
            }
            if (Short.class.equals(returnType) || short.class.equals(returnType)) {
                return Short.parseShort(value);
            }
            if (Integer.class.equals(returnType) || int.class.equals(returnType)) {
                return Integer.parseInt(value);
            }
            if (Long.class.equals(returnType) || long.class.equals(returnType)) {
                return Long.parseLong(value);
            }
            if (Float.class.equals(returnType) || float.class.equals(returnType)) {
                return Float.parseFloat(value);
            }
            if (Double.class.equals(returnType) || double.class.equals(returnType)) {
                return Double.parseDouble(value);
            }
            if (Boolean.class.equals(returnType) || boolean.class.equals(returnType)) {
                return Boolean.parseBoolean(value);
            }
        } catch (Exception e) {
            Exceptions.raise(ValidationException::new, e, "Unable to coerce value '%s' to %s", value, returnType);
        }
        if (Character.class.equals(returnType) || char.class.equals(returnType)) {
            Exceptions.raiseIf(value.length() > 1, ConstraintDeclarationException::new,
                "a char must have a length of 1");
            return value.charAt(0);
        }
        return Exceptions.raise(ValidationException::new, "Unknown annotation value type %s", returnType.getName());
    }