descriptionbuilderutils.java

来自「开源的axis2框架的源码。用于开发WEBSERVER」· Java 代码 · 共 353 行 · 第 1/2 页

JAVA
353
字号
    /**
     * Return a prefix suitable for passing to Class.forName(String) for an array.  Each array
     * dimension represented by "[]" will be represented by a single "[".
     *
     * @param arrayClassName
     * @return
     */
    static String getArrayDimensionPrefix(String arrayClassName) {
        StringBuffer arrayDimPrefix = new StringBuffer();

        if (arrayClassName != null) {
            int arrayDimIndex = arrayClassName.indexOf("[]");
            while (arrayDimIndex > 0) {
                arrayDimPrefix.append("[");
                // Skip over this "[]" and see if there are any more.
                int startNext = arrayDimIndex + 2;
                arrayDimIndex = arrayClassName.indexOf("[]", startNext);
            }
        }

        if (arrayDimPrefix.length() > 0)
            return arrayDimPrefix.toString();
        else
            return null;
    }

    /**
     * For primitives, return the appropriate primitive class.  Note that arrays of primitives are
     * handled differently, like arrays of objects.  Only non-array primitives are processed by this
     * method.  This method understands both the typical primitive declaration (e.g. "int") and the
     * encoding used as for arrays (e.g. "I").
     *
     * @param classType
     * @return
     */
    static Class getPrimitiveClass(String classType) {

        Class paramClass = null;

        if (INT_PRIMITIVE.equals(classType) || INT_PRIMITIVE_ENCODING.equals(classType)) {
            paramClass = int.class;
        } else if (BYTE_PRIMITIVE.equals(classType) || BYTE_PRIMITIVE_ENCODING.equals(classType)) {
            paramClass = byte.class;
        } else if (CHAR_PRIMITIVE.equals(classType) || CHAR_PRIMITIVE_ENCODING.equals(classType)) {
            paramClass = char.class;
        } else
        if (SHORT_PRIMITIVE.equals(classType) || SHORT_PRIMITIVE_ENCODING.equals(classType)) {
            paramClass = short.class;
        } else
        if (BOOLEAN_PRIMITIVE.equals(classType) || BOOLEAN_PRIMITIVE_ENCODING.equals(classType)) {
            paramClass = boolean.class;
        } else if (LONG_PRIMITIVE.equals(classType) || LONG_PRIMITIVE_ENCODING.equals(classType)) {
            paramClass = long.class;
        } else
        if (FLOAT_PRIMITIVE.equals(classType) || FLOAT_PRIMITIVE_ENCODING.equals(classType)) {
            paramClass = float.class;
        } else
        if (DOUBLE_PRIMITIVE.equals(classType) || DOUBLE_PRIMITIVE_ENCODING.equals(classType)) {
            paramClass = double.class;
        } else if (VOID_PRIMITIVE.equals(classType) || VOID_PRIMITIVE_ENCODING.equals(classType)) {
            paramClass = void.class;
        }
        return paramClass;
    }

    /**
     * Returns the encoding used to represent a Class for an array of a primitive type.  For
     * example, an array of boolean is represented by "Z". This is as described in the javadoc for
     * Class.getName().  If the argument is not a primitive type, a null will be returned.
     * <p/>
     * Note that arrays of voids are not allowed; a null will be returned.
     *
     * @param primitiveType
     * @return
     */
    static String getPrimitiveTypeArrayEncoding(String primitiveType) {
        String encoding = null;

        if (BOOLEAN_PRIMITIVE.equals(primitiveType)) {
            encoding = BOOLEAN_PRIMITIVE_ENCODING;
        } else if (BYTE_PRIMITIVE.equals(primitiveType)) {
            encoding = BYTE_PRIMITIVE_ENCODING;
        } else if (CHAR_PRIMITIVE.equals(primitiveType)) {
            encoding = CHAR_PRIMITIVE_ENCODING;
        } else if (DOUBLE_PRIMITIVE.equals(primitiveType)) {
            encoding = DOUBLE_PRIMITIVE_ENCODING;
        } else if (FLOAT_PRIMITIVE.equals(primitiveType)) {
            encoding = FLOAT_PRIMITIVE_ENCODING;
        } else if (INT_PRIMITIVE.equals(primitiveType)) {
            encoding = INT_PRIMITIVE_ENCODING;
        } else if (LONG_PRIMITIVE.equals(primitiveType)) {
            encoding = LONG_PRIMITIVE_ENCODING;
        } else if (SHORT_PRIMITIVE.equals(primitiveType)) {
            encoding = SHORT_PRIMITIVE_ENCODING;
        }
        return encoding;
    }

    /**
     * If the parameter represents and array, then the returned string is in a format that a
     * Class.forName(String) can be done on it.  This format is described by Class.getName(). If the
     * parameter does not represent an array, the parememter is returned unmodified.
     * <p/>
     * Note that arrays of primitives are processed as well as arrays of objects.
     *
     * @param classToLoad
     * @return
     */
    static String reparseIfArray(String classToLoad) {

        String reparsedClassName = classToLoad;
        if (isClassAnArray(classToLoad)) {
            String baseType = getBaseArrayClassName(classToLoad);
            String dimensionPrefix = getArrayDimensionPrefix(classToLoad);
            if (getPrimitiveTypeArrayEncoding(baseType) != null) {
                reparsedClassName = dimensionPrefix + getPrimitiveTypeArrayEncoding(baseType);
            } else {
                reparsedClassName = dimensionPrefix + "L" + baseType + ";";
            }
        }
        return reparsedClassName;
    }

    /**
     * Load a class represented in a Description Builder Composite.  If a classloader is specified,
     * it will be used; otherwise the default classloader is used.
     *
     * @param classToLoad
     * @param classLoader
     * @return
     */
    static Class loadClassFromComposite(String classToLoad, ClassLoader classLoader) {
        Class returnClass = null;

        // If this is an array, then create a string version as described by Class.getName.
        // For example, "Foo[][]" becomes "[[LFoo".  Note that arrays of primitives must also be parsed.
        classToLoad = DescriptionBuilderUtils.reparseIfArray(classToLoad);

        if (classLoader != null) {
            // Use the specified classloader to load the class.
            try {
                returnClass = ClassLoaderUtils.forName(classToLoad, false, classLoader);
            }
            //Catch Throwable as ClassLoader can throw an NoClassDefFoundError that
            //does not extend Exception, so lets catch everything that extends Throwable
            //rather than just Exception.
            catch (Throwable ex) {
                throw ExceptionFactory.makeWebServiceException(
                        "DescriptionBuilderUtils: Class not found for parameter: "
                                + classToLoad + " using classloader: " + classLoader);
            }
        } else {
            //Use the thread context class loader to load the class.
            try {
                returnClass = ClassLoaderUtils.forName(classToLoad, false,
                                                       ClassLoaderUtils.getContextClassLoader());
            }
            catch (Throwable ex) {
                //Use the default classloader to load the class.
                try {
                    returnClass = ClassLoaderUtils.forName(classToLoad);
                }
                //Catch Throwable as ClassLoader can throw an NoClassDefFoundError that
                //does not extend Exception
                catch (Throwable ex2) {
                    throw ExceptionFactory.makeWebServiceException(
                            "DescriptionBuilderUtils: Class not found using default classloader for parameter: " +
                                    classToLoad);
                }
            }
        }
        return returnClass;
    }

}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?