Java Class Load classForName(String name)

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

Description

copied here from ObjectUtil to make the package org.jacorb.test.common independent from the orb core

License

Open Source License

Declaration

public static Class<?> classForName(String name) throws ClassNotFoundException, IllegalArgumentException 

Method Source Code

//package com.java2s;
/*/*from   w  ww  .java  2 s  . c o  m*/
 *        JacORB - a free Java ORB
 *
 *   Copyright (C) 1999-2012 Gerald Brose / The JacORB Team.
 *
 *   This library is free software; you can redistribute it and/or
 *   modify it under the terms of the GNU Library General Public
 *   License as published by the Free Software Foundation; either
 *   version 2 of the License, or (at your option) any later version.
 *
 *   This library is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *   Library General Public License for more details.
 *
 *   You should have received a copy of the GNU Library General Public
 *   License along with this library; if not, write to the Free
 *   Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 */

public class Main {
    /**
     * copied here from ObjectUtil to make the package org.jacorb.test.common independent from the orb core
     */
    public static Class<?> classForName(String name) throws ClassNotFoundException, IllegalArgumentException {
        if (name == null) {
            throw new IllegalArgumentException("Class name must not be null!");
        }

        try {
            // Here we prefer classLoader.loadClass() over the three-argument
            // form of Class.forName(), as the latter is reported to cause
            // caching of stale Class instances (due to a buggy cache of
            // loaded classes).
            return Thread.currentThread().getContextClassLoader().loadClass(name);
        } catch (Exception e) {
            // As a fallback, we prefer Class.forName(name) because it loads
            // array classes (i.e., it handles arguments like
            // "[Lsome.class.Name;" or "[[I;", which classLoader.loadClass()
            // does not handle).
            return Class.forName(name);
        }
    }
}

Related

  1. classForName(String listener)
  2. classForName(String name)
  3. classForName(String name)
  4. classForName(String name)
  5. classForName(String name)
  6. classForName(String name)
  7. classForName(String name)
  8. classForName(String name)
  9. classForName(String name, ClassLoader classLoader)