⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 fastclassforname.java

📁 java编写的OCR软件
💻 JAVA
字号:
package de.spieleck.util;

import java.util.HashMap;

/**
 * This is a replacement for Class.forName(...).newInstance().
 * <p>
 * <b>It is fast.</b>
 * In my bench it is much faster than the latter construct.
 * Efficient caching and loading mechanism to obtain classes
 * with a default constructor in the current ContextClassLoader.
 * </p>
 * <p>
 * <b>It treats some classloader intricaties.</b>
 * </p>
 *
 * @author fsn
 * @version 0
 *
 */
public class FastClassForName
{
    /** You do not want to construct this. */
    private FastClassForName() { }

    /** Keep track of all classes aquired for faster second access. */
    protected static HashMap classCache = new HashMap(15);

    public static Object newInstance(String className, Class require)
    {
        return newInstance(className, null, require);
    }

    public static Object newInstance(String className, String defaultPackage, 
                                     Class require)
    {
        return newInstance(className, defaultPackage, require, require);
    }

    public static Object newInstance(String className, String defaultPackage, 
                                     Class require, Class context)
    {
        if (className == null)
            return null;
        try
        {
            Class clazz = lookupClass(className, defaultPackage, context);
            if ( clazz == null )
            {
                System.err.println("?no Class '"+className
                                              +"@"+defaultPackage+"' found.");
            }
            else if ( require.isAssignableFrom(clazz))
                return clazz.newInstance();
            else
            {
                System.err.println("?'"+clazz+"' not assignable to '"
                        +require+"'!");
            }
        }
        catch (Exception help)
        {
            System.err.println("?no Class '" + className + "' found." + help);
            help.printStackTrace();
        }
        return null;
    }

    /** 
     * Lookup the class, first in cache, then try loading it from context
     * with two trials on the classname.
     */
    public static Class lookupClass(String className, String defaultPackage, 
                                     Class context)
    {
        if (className == null)
            return null;
        Class clazz = null;
        String classIndex;
        if (defaultPackage != null)
            classIndex = defaultPackage + className;
        else
            classIndex = className;
        try
        {
            //
            // Probe the class cache (this is supposed to speed things up
            // Note: This might find a wrong version of a class on a 
            // multi classloader context. This is a XXX.
            clazz = (Class)classCache.get(classIndex);

            //
            // If we failed...
            if (clazz == null)
            {
                try
                {
                    clazz = contextClassForName(className, context);
                }
                catch (Exception oneMoreTrial)
                {
                    // Second trial with extended path
                    if (defaultPackage != null)
                        clazz = contextClassForName(classIndex, context);
                }
                // Well, we do not cache unloadable things. XXX Should we??
                if (clazz != null)
                    classCache.put(classIndex, clazz);
            }
        }
        catch (Exception help)
        {
            System.err.println("?no Class '" + className + "' found." + help);
            help.printStackTrace();
        }
        return clazz;
    }


    /**
     * Try to load a class from the right contextual classloader.
     * The right classloader can be somewhat strange in an Ant or
     * Servlet context.
     */
    public static Class contextClassForName(String className, Class context)
       throws ClassNotFoundException
    {
        if ( className == null )
            return null;
        try
        {
            ClassLoader loader = context.getClassLoader();
            return Class.forName(className, false, loader);
        }
        catch (NoSuchMethodError ignore)
        {
            // JDK1.1 fallback
            return Class.forName(className);
        }
        catch ( ClassNotFoundException e )
        {
            ClassLoader loader = Thread.currentThread().getContextClassLoader();
            return Class.forName(className, false, loader);
        }
    }
}
//
//    Jacson - Text Filtering with Java.
//    Copyright (C) 2002 Frank S. Nestel (nestefan -at- users.sourceforge.net)
//
//    This library is free software; you can redistribute it and/or
//    modify it under the terms of the GNU Lesser General Public
//    License as published by the Free Software Foundation; either
//    version 2.1 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
//    Lesser General Public License for more details.
//
//    You should have received a copy of the GNU Lesser General Public
//    License along with this library; if not, write to the Free Software
//    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
//

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -