All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.carrot2.util.ReflectionUtils Maven / Gradle / Ivy

Go to download

Carrot2 search results clustering framework. Minimal functional subset (core algorithms and infrastructure, no document sources).

There is a newer version: 3.16.3
Show newest version

/*
 * Carrot2 project.
 *
 * Copyright (C) 2002-2019, Dawid Weiss, Stanisław Osiński.
 * All rights reserved.
 *
 * Refer to the full license file "carrot2.LICENSE"
 * in the root folder of the repository checkout or at:
 * http://www.carrot2.org/carrot2.LICENSE
 */

package org.carrot2.util;


/**
 * Utilities related to Java reflection.
 */
public final class ReflectionUtils
{
    private ReflectionUtils()
    {
        // No instances.
    }

    /**
     * Load and initialize (or return, if already defined) a given class using context
     * class loader. If class cannot be found, a {@link ClassNotFoundException} is thrown
     * and logged.
     */
    public static Class classForName(String clazzName) throws ClassNotFoundException
    {
        return classForName(clazzName, true);
    }

    /**
     * Load and initialize (or return, if already defined) a given class using context
     * class loader.
     * 
     * @param clazzName class name to load
     * @param logWarning if true, a warning will be logged if class cannot be found
     */
    public static Class classForName(String clazzName, boolean logWarning)
        throws ClassNotFoundException
    {
        try
        {
            ClassLoader cl = Thread.currentThread().getContextClassLoader();
            if (cl != null) {              
              return Class.forName(clazzName, true, cl);
            }
        }
        catch (SecurityException e)
        {
            if (logWarning)
            {
              org.slf4j.LoggerFactory.getLogger(ReflectionUtils.class).warn(
                  "Could not access CCL.", e);
            }
        }
        catch (ClassNotFoundException e)
        {
            // Retry with our CL.
        }

        try
        {
          return Class.forName(clazzName, true, ReflectionUtils.class.getClassLoader());
        }
        catch (Exception e)
        {
          if (logWarning)
          {
              org.slf4j.LoggerFactory.getLogger(ReflectionUtils.class).warn(
                  "Could not load class: " + clazzName + " (" + e.getMessage() + ").", e);
          }
          throw e;
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy