001
014
015 package com.liferay.portal.javadoc;
016
017 import com.liferay.portal.kernel.util.StringPool;
018 import com.liferay.portal.kernel.util.StringUtil;
019
020 import java.util.Arrays;
021
022
025 public class JavadocUtil {
026
027 public static Class<?> loadClass(ClassLoader classLoader, String className)
028 throws ClassNotFoundException {
029
030 className = _getLoadableClassName(className);
031
032 if ((className.indexOf('.') == -1) || (className.indexOf('[') == -1)) {
033 int primitiveIndex = _getPrimitiveIndex(className);
034
035 if (primitiveIndex >= 0) {
036 return _PRIMITIVE_TYPES[primitiveIndex];
037 }
038 }
039
040 if (classLoader != null) {
041 try {
042 return classLoader.loadClass(className);
043 }
044 catch (ClassNotFoundException cnfe) {
045 }
046 }
047
048 Thread currentThread = Thread.currentThread();
049
050 ClassLoader contextClassLoader = currentThread.getContextClassLoader();
051
052 if (classLoader != contextClassLoader) {
053 try {
054 return contextClassLoader.loadClass(className);
055 }
056 catch (ClassNotFoundException cnfe) {
057 }
058 }
059
060 return Class.forName(className);
061 }
062
063 private static String _getLoadableClassName(String className) {
064 int bracketCount = StringUtil.count(className, StringPool.OPEN_BRACKET);
065
066 if (bracketCount == 0) {
067 return className;
068 }
069
070 StringBuilder sb = new StringBuilder(bracketCount);
071
072 for (int i = 0; i < bracketCount; i++) {
073 sb.append('[');
074 }
075
076 int bracketIndex = className.indexOf('[');
077
078 className = className.substring(0, bracketIndex);
079
080 int primitiveIndex = _getPrimitiveIndex(className);
081
082 if (primitiveIndex >= 0) {
083 className = String.valueOf(
084 _PRIMITIVE_BYTECODE_NAME[primitiveIndex]);
085
086 return sb.toString() + className;
087 }
088 else {
089 return sb.toString() + 'L' + className + ';';
090 }
091 }
092
093 private static int _getPrimitiveIndex(String className) {
094 if (className.indexOf('.') != -1) {
095 return -1;
096 }
097
098 return Arrays.binarySearch(_PRIMITIVE_TYPE_NAMES, className);
099 }
100
101 private static final char[] _PRIMITIVE_BYTECODE_NAME = {
102 'Z', 'B', 'C', 'D', 'F', 'I', 'J', 'S'
103 };
104
105 private static final String[] _PRIMITIVE_TYPE_NAMES = {
106 "boolean", "byte", "char", "double", "float", "int", "long", "short",
107 };
108
109 private static final Class<?>[] _PRIMITIVE_TYPES = new Class[] {
110 boolean.class, byte.class, char.class, double.class, float.class,
111 int.class, long.class, short.class,
112 };
113
114 }