`

JAVA CLASS LOADING技术研究---整理后的代码

阅读更多
以下是整理后的代码部分,欢迎批评指正。

MyClassLoader.java
/**//*
 * @MyClassLoader.java    07/04/17
 *
 * Copyright Zhao Jiucheng. All rights reserved.
 
*/

package com.neusoft.classloader;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Hashtable;
import java.util.jar.JarEntry;
import java.util.jar.JarInputStream;

/** *//**
 * A class loader is an object that is responsible for loading classes. Given
 * the binary name of a class, a class loader should attempt to locate or
 * generate data that constitutes a definition for the class. A typical strategy
 * is to transform the name into a file name and then read a "class file" of
 * that name from a file system.
 * 
 * 
@version 1.0, 07/04/17
 * 
@author Zhao Jiucheng
 * 
 
*/

public class MyClassLoader extends ClassLoader {

    
// a classpath for search
    private static String myClasspath = new String("");

    
// hashtable that memory the loaded classes
    private static Hashtable<String, Class<?>> loadClassHashTable = new Hashtable<String, Class<?>>();

    
// hashtable that memory the time of loading a class
    private static Hashtable<String, Long> loadClassTime = new Hashtable<String, Long>();

    
// the null constructor
    public MyClassLoader() {

    }


    
/** *//**
     * create a classloader and specify a classpath.
     * 
     * 
@param myClasspath
     *            the specified classpath name.
     
*/

    
public MyClassLoader(String myClasspath) {
        
if (!myClasspath.endsWith("\\")) {
            myClasspath 
= myClasspath + "\\";
        }

        MyClassLoader.myClasspath 
= myClasspath;
    }


    
/** *//**
     * set the classpath
     * 
     * 
@param myClasspath
     *            the specified classpath name
     
*/

    
public void SetmyClasspath(String myClasspath) {
        
if (!myClasspath.endsWith("\\")) {
            myClasspath 
= myClasspath + "\\";
        }

        MyClassLoader.myClasspath 
= myClasspath;
    }


    
/** *//**
     * Loads the class with the specified binary name. This method searches for
     * classes in the same manner as the loadClass(String, boolean) method.
     * Invoking this method is equivalent to invoking {loadClass(name,false)}.
     * 
     * 
@param className
     *            The binary name of the class.
     * 
     * 
@return The resulting <tt>Class</tt> object.
     * 
     * 
@throws ClassNotFoundException
     *             If the class was not found.
     
*/

    @SuppressWarnings(
"unchecked")
    
public Class loadClass(String className) throws ClassNotFoundException {
        
return loadClass(className, false);
    }


    
/** *//**
     * Loads the class with the specified binary name. The default
     * implementation of this method searches for classes in the following
     * order:
     * 
     * Invoke {findLoadedClass(String)} to check if the class has already been
     * loaded.
     * 
     * Invoke {findSystemClass(String)} to load the system class.
     * 
     * Invoke the {findClass(String)} method to find the class.
     * 
     * If the class was found using the above steps, and the resolve flag is
     * true, this method will then invoke the {resolveClass(Class)} method on
     * the resulting Class object.
     * 
     * 
@param name
     *            The binary name of the class.
     * 
     * 
@param resolve
     *            If true then resolve the class.
     * 
     * 
@return The resulting Class object.
     * 
     * 
@throws ClassNotFoundException
     *             If the class could not be found.
     
*/

    @SuppressWarnings(
"unchecked")
    
protected Class loadClass(String name, boolean resolve)
            
throws ClassNotFoundException {

        
try {
            Class foundClass 
= findLoadedClass(name);

            
// check if the class has already been loaded.
            if (foundClass != null{
                System.out.println(
"Complete to load the class: " + name);
                
return foundClass;
            }


            
// if the class is systemClass, load the system class by system
            if (name.startsWith("java.")) {
                foundClass 
= findSystemClass(name);
                loadClassHashTable.put(name, foundClass);
                System.out.println(
"System is loading the class: " + name);
                
return foundClass;
            }


            
// invoke the findClass() method to load the class
            try {
                foundClass 
= findClass(name);
            }
 catch (Exception fnfe) {
            }


            
if (resolve && (foundClass != null)) {
                resolveClass(foundClass);
            }

            
return foundClass;
        }
 catch (Exception e) {
            
throw new ClassNotFoundException(e.toString());
        }

    }


    
/** *//**
     * Finds the class with the specified binary name.The default implementation
     * throws a ClassNotFoundException.
     * 
     * 
@param className
     *            The binary name of the class.
     * 
     * 
@return The resulting Class object.
     * 
     * 
@throws ClassNotFoundException
     *             If the class could not be found.
     
*/

    @SuppressWarnings(
"unchecked")
    
public Class findClass(String className) {

        
byte[] classData = null;
        
try {
            classData 
= loadClassData(className);
        }
 catch (IOException e) {
            e.printStackTrace();
        }

        
if( classData == null)
分享到:
评论

相关推荐

    Java虚拟机的深入研究

    ### Java虚拟机的深入研究 #### 一、Java技术与Java虚拟机 Java不仅仅是一种编程语言,更是一种全面的技术框架,由以下四个方面构成: 1. **Java编程语言**:提供了编写程序的基础语法和规则。 2. **Java类文件...

    The Java Virtual Machine Specification 3rd

    该书作为官方权威指南,不仅适用于软件开发者,也适合对Java技术栈有深入了解需求的技术人员和研究人员。 #### 二、JVM的核心概念 1. **字节码(Bytecode)** - 字节码是Java程序在编译后的中间表示形式,它是一种...

    Tomcat源代码学习研究

    - **Classloading**:Tomcat使用定制的ClassLoader加载Web应用的类,遵循“父类加载优先”原则。 6. **连接器与协议处理** - **NIO和Apr**:Tomcat提供了多种连接器实现,如基于Java NIO的 Coyote Connector 和...

    spring-instrument-tomcat源码

    这个模块主要实现了`org.springframework.instrument.classloading`包下的接口,例如`LoadTimeWeaver`,它的作用是在类加载时进行代码修改或增强,这在AOP(面向切面编程)和代理类生成等场景下尤其有用。...

    JVM演讲PPT分享

    Hotspot的技术演进对于Java开发者而言也是学习和研究的重点。 通过以上内容的深入学习,可以对JVM有一个全面的认识,理解它在Java生态系统中的核心地位,以及如何通过学习JVM来提升Java程序的性能。

    Tomcat6-7-8-9版本

    7. **源码分析**:深入学习Tomcat源码有助于理解其工作流程,包括请求处理、连接器(Connector)、容器(Container)架构、线程池管理、类加载机制(Class Loading)以及会话管理等核心概念。通过阅读源码,开发者...

    安卓逆向学习笔记之ART定制方案比较和流程.docx

    逆向工程是研究软件内部工作原理的一种技术手段,在移动开发、安全审计等领域具有广泛的应用价值。 #### 二、ART定制方案概述 Android系统自5.0版本起采用ART作为默认运行时环境,取代了之前的Dalvik虚拟机。ART...

    Talantra-Old

    此外,可能采用了分块加载技术(Chunk Loading),以优化内存使用和提高性能。 2. **角色与种族**:不同的种族可能拥有独特的属性、技能和故事线,这需要精心设计的数据结构来存储和管理。例如,使用类(Class)来...

    安卓自定义ProgressDialog实现帧动画。

    在提供的`Test4Dialog`文件中,可能包含了实现这个功能的源代码和资源文件,你可以进一步研究和学习。 总之,自定义`ProgressDialog`是提升Android应用用户体验的重要手段,通过帧动画的使用,我们可以让加载过程...

    hibernate 3.3 源码

    Hibernate 3.3 是一个历史悠久但仍然非常重要的Java持久化框架,它允许开发人员通过对象关系映射(ORM)技术将数据库操作与业务对象无缝集成。这个源码包提供了深入理解Hibernate工作原理的机会,这对于任何想要优化...

Global site tag (gtag.js) - Google Analytics