Java Class Load classForName(String className)

Here you can find the source of classForName(String className)

Description

class For Name

License

Apache License

Declaration

public static <T> Class<T> classForName(String className) throws Exception 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.util.HashMap;
import java.util.Map;

public class Main {
    private static Map<Object, Class<?>> __primitiveTypes = new HashMap<Object, Class<?>>();

    public static <T> Class<T> classForName(String className) throws Exception {
        Class c = __primitiveTypes.get(className);
        if (null != c)
            return c;
        try {//from ww w.  j a  v  a2s .  co  m
            return (Class<T>) Class.forName(className);
        } catch (ClassNotFoundException e) {
            throw new Exception(e);
        }
    }

    public static <T> Class<T> classForName(String className, ClassLoader classLoader) throws Exception {
        Class c = __primitiveTypes.get(className);
        if (null != c)
            return c;
        try {
            if (className.contains("[")) {
                StringBuffer buffer = new StringBuffer();
                className = buffer.append("[L").append(beforeFirst(className, "[")).append(";").toString();
            }
            return (Class<T>) Class.forName(className, true, classLoader);
        } catch (ClassNotFoundException e) {
            throw new Exception(e);
        }
    }

    public static String beforeFirst(String s0, String search) {
        if (null == s0) {
            return "";
        }
        int i = s0.indexOf(search);
        if (i == -1) {
            return "";
        }
        return s0.substring(0, i);
    }
}

Related

  1. classForName(String className)
  2. classForName(String className)
  3. classForName(String className)
  4. classForName(String className)
  5. ClassForName(String className)
  6. classForName(String className)
  7. classForName(String className)
  8. classForName(String className)
  9. classForName(String className, Class caller)