Java Class Load loadClass(ClassLoader classLoader, String className)

Here you can find the source of loadClass(ClassLoader classLoader, String className)

Description

Load class with given className over given classLoader and suppress ClassNotFoundException .

License

Open Source License

Declaration

public static Class<?> loadClass(ClassLoader classLoader, String className) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2011 Google, Inc.//  www  . j a  v a  2  s.c  o m
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    Google, Inc. - initial API and implementation
 *******************************************************************************/

public class Main {
    /**
     * Load class with given <code>className</code> over given <code>classLoader</code> and suppress
     * {@link ClassNotFoundException}.
     */
    public static Class<?> loadClass(ClassLoader classLoader, String className) {
        try {
            return load(classLoader, className);
        } catch (ClassNotFoundException e) {
            return null;
        }
    }

    /**
     * Load class with given <code>className</code> over given <code>classLoader</code>. Support load
     * inner classes over Java class name (auto replace last "." to "$").
     */
    public static Class<?> load(ClassLoader classLoader, String className) throws ClassNotFoundException {
        try {
            return classLoader.loadClass(className);
        } catch (ClassNotFoundException e) {
            int index = className.lastIndexOf('.');
            if (index > 0) {
                try {
                    return classLoader
                            .loadClass(className.substring(0, index) + "$" + className.substring(index + 1));
                } catch (Throwable t) {
                }
            }
            throw e;
        }
    }
}

Related

  1. loadClass(Class base, String name)
  2. loadClass(Class clazz)
  3. loadClass(Class contextClass, String className)
  4. loadClass(ClassLoader classLoader, Class clazz, String postfix)
  5. loadClass(ClassLoader classLoader, String className)
  6. loadClass(ClassLoader classLoader, String fullyQualifiedClassName)
  7. loadClass(ClassLoader loader, String className)
  8. loadClass(ClassLoader loader, String className, String errorMessage)
  9. loadClass(final ClassLoader cl, final String cname)