Example #1
0
  /**
   * Loads the specified class using the passed classloader, or, if it is <code>null</code> the
   * Infinispan classes' classloader.
   *
   * <p>If loadtime instrumentation via GenerateInstrumentedClassLoader is used, this class may be
   * loaded by the bootstrap classloader.
   *
   * @param classname name of the class to load
   * @return the class
   * @param userClassLoader the application classloader which should be used to load the class, or
   *     null if the class is always packaged with Infinispan
   * @throws ClassNotFoundException if the class cannot be loaded
   */
  @SuppressWarnings("unchecked")
  public static <T> Class<T> loadClassStrict(String classname, ClassLoader userClassLoader)
      throws ClassNotFoundException {
    ClassLoader[] cls = getClassLoaders(userClassLoader);
    ClassNotFoundException e = null;
    NoClassDefFoundError ne = null;
    for (ClassLoader cl : cls) {
      if (cl == null) continue;

      try {
        return (Class<T>) Class.forName(classname, true, cl);
      } catch (ClassNotFoundException ce) {
        e = ce;
      } catch (NoClassDefFoundError ce) {
        ne = ce;
      }
    }

    if (e != null) throw e;
    else if (ne != null) {
      // Before we wrap this, make sure we appropriately log this.
      log.unableToLoadClass(classname, Arrays.toString(cls), ne);
      throw new ClassNotFoundException(classname, ne);
    } else throw new IllegalStateException();
  }