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

📄 ozoneclassloader.java

📁 Java的面向对象数据库系统的源代码
💻 JAVA
字号:
// You can redistribute this software and/or modify it under the terms of
// the Ozone Core License version 1 published by ozone-db.org.
//
// The original code and portions created by SMB are
// Copyright (C) 1997-@year@ by SMB GmbH. All rights reserved.
//
// $Id: OzoneClassLoader.java,v 1.5 2002/12/29 11:15:56 per_nyfelt Exp $

package org.ozoneDB.core;

import org.ozoneDB.DxLib.DxHashMap;
import org.ozoneDB.DxLib.DxMap;

import java.io.InputStream;


/**
 * Ozone specific class loader. This compiles/runs with JDK 1.2 only. To make
 * it run with JDK 1.1 see ClassManager.classForName() method.
 *
 *
 * @author <a href="http://www.softwarebuero.de/">SMB</a>
 * @version $Revision: 1.5 $Date: 2002/12/29 11:15:56 $
 */
final class OzoneClassLoader extends ClassLoader {

    private DxMap classTable;


    public OzoneClassLoader() {
        super();
        flushCache();
    }


    protected void flushCache() {
        classTable = new DxHashMap( 100 );
    }


    protected Class findClass( String name ) throws ClassNotFoundException {
        if (name.startsWith( "java" )) {
            throw new ClassNotFoundException( "I'm not here to load system classes." );
        }

        System.out.println("Finding class " + name);
        String resourceName = name.replace('.', '/') + ".class";
//        StringBuffer resourceName = new StringBuffer( name );
//        for (int i = 0; i < resourceName.length(); i++) {
//            if (resourceName.charAt( i ) == '.') {
//                resourceName.setCharAt( i, '/' );
//            }
//        }
//        resourceName.append( ".class" );

        // we try the context classloader first
        InputStream classIn = getResourceAsStream(resourceName);
        if ( classIn == null ) {
            System.out.println("could not find class " + name + " using context classloader, trying system:");
            classIn = getSystemClassLoader().getSystemResourceAsStream( resourceName );
        }
        if (classIn == null) {
            throw new ClassNotFoundException( "Unable to locate resource: " + resourceName );
        }

        try {
            byte[] classBytes = new byte[classIn.available()];
            classIn.read( classBytes );
            classIn.close();

            // TODO: this does not work, causes a ClassFormatError
            Class cl = defineClass( name, classBytes, 0, classBytes.length, null );
            return cl;
        } catch (Exception e) {
            e.printStackTrace();
            throw new ClassNotFoundException( e.toString() );
        }
    }


    public Class loadClass( String name ) throws ClassNotFoundException {
        return loadClass( name, false );
    }


    protected Class loadClass( String name, boolean resolve ) throws ClassNotFoundException {
        if (name.startsWith( "java" )) {
            return getSystemClassLoader().loadClass( name );
        } else {
            Class cl = (Class)classTable.elementForKey( name );
            if (cl == null) {
                cl = primitiveType( name );
                if (cl == null) {
                    // this really loads the requested class in this ClassLoader but cause
                    // compatibility problems with already loaded classes
                    // cl = findClass (name);
                    // if (resolve) {
                    //    resolveClass (cl);
                    //}

                    // this works around the compatibility problems but causes the reloadClasses()
                    // method to not work
                    cl = Thread.currentThread().getContextClassLoader().loadClass( name );
                }
                classTable.addForKey( cl, name );
            }
            return cl;
        }
    }


    /**
     * Load primitive types with default class loader.
     */
    protected static Class primitiveType( String name ) {
        if (name.equals( "int" )) {
            return Integer.TYPE;
        } else if (name.equals( "char" )) {
            return Character.TYPE;
        } else if (name.equals( "byte" )) {
            return Byte.TYPE;
        } else if (name.equals( "double" )) {
            return Double.TYPE;
        } else if (name.equals( "float" )) {
            return Float.TYPE;
        } else if (name.equals( "long" )) {
            return Long.TYPE;
        } else if (name.equals( "short" )) {
            return Short.TYPE;
        } else if (name.equals( "boolean" )) {
            return Boolean.TYPE;
        } else {
            return null;
        }
    }

}

⌨️ 快捷键说明

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