自定义类加载器,打破双亲委派机制,实现动态加载jar

package org.hotloading;


import com.google.common.io.ByteStreams;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.util.*;
import java.util.jar.Attributes;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import java.util.jar.Manifest;
import java.util.zip.ZipEntry;

public class KMHotClassLoader extends ClassLoader {
    private String jarPath = "E:\\study\\plant\\hotLoaderJarTest\\target\\hotLoaderJarTest-1.0-SNAPSHOT.jar";

    private List<JarFile> jars = new ArrayList<>();

    public KMHotClassLoader(String jarPath,ClassLoader parent){
        super(parent);
        this.jarPath = jarPath;
        File[] jarFiles = new File(jarPath).listFiles(file -> file.getName().endsWith(".jar"));
        if (null == jarFiles) {
        }
        try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
            for (File each : Objects.requireNonNull(jarFiles)) {
                outputStream.reset();
                JarFile jar = new JarFile(each, true);
                jars.add(jar);
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public Class<?> loadClass(String name) throws ClassNotFoundException {
        synchronized (getClassLoadingLock(name)) {
            // First, check if the class has already been loaded
            Class<?> c = findLoadedClass(name);
            if (c == null) {
                long t0 = System.nanoTime();
                // If still not found, then invoke findClass in order
                // to find the class.
                long t1 = System.nanoTime();
                // 打破双亲委派机制,先使用自己的类加载器加载
                c = findClass(name);
                // this is the defining class loader; record the stats
                sun.misc.PerfCounter.getParentDelegationTime().addTime(t1 - t0);
                sun.misc.PerfCounter.getFindClassTime().addElapsedTimeFrom(t1);
                sun.misc.PerfCounter.getFindClasses().increment();
            }
            if (c == null) {
                c = super.loadClass(name);
            }
            return c;
        }
    }

    /**
     * 找到类
     *
     * @param name 名字
     * @return {@link Class}<{@link ?}>
     * @throws ClassNotFoundException 类没有发现异常
     */
    @Override
    protected Class<?> findClass(final String name) throws ClassNotFoundException {
        String path = classNameToPath(name);
        for (JarFile jar : jars) {
            ZipEntry entry = jar.getEntry(path);
            if (Objects.nonNull(entry)) {
                try {
                    int index = name.lastIndexOf('.');
                    if (index != -1) {
                        String packageName = name.substring(0, index);
                        definePackageInternal(packageName, jar.getManifest());
                    }
                    byte[] data = ByteStreams.toByteArray(jar.getInputStream(entry));
                    return defineClass(name, data, 0, data.length);
                } catch (final IOException ex) {
                    System.out.println("加载class失败【" + name + "】");
                    ex.printStackTrace();
                }
            }
        }
        throw new ClassNotFoundException(String.format("Class name is %s not found.", name));
    }

    /**
     * 定义包内部
     *
     * @param packageName 包名
     * @param manifest    清单
     */
    private void definePackageInternal(final String packageName, final Manifest manifest) {
        if (null != getPackage(packageName)) {
            return;
        }
        Attributes attributes = manifest.getMainAttributes();
        String specTitle = attributes.getValue(Attributes.Name.SPECIFICATION_TITLE);
        String specVersion = attributes.getValue(Attributes.Name.SPECIFICATION_VERSION);
        String specVendor = attributes.getValue(Attributes.Name.SPECIFICATION_VENDOR);
        String implTitle = attributes.getValue(Attributes.Name.IMPLEMENTATION_TITLE);
        String implVersion = attributes.getValue(Attributes.Name.IMPLEMENTATION_VERSION);
        String implVendor = attributes.getValue(Attributes.Name.IMPLEMENTATION_VENDOR);
        definePackage(packageName, specTitle, specVersion, specVendor, implTitle, implVersion, implVendor, null);
    }

    /**
     * 类名路径
     *
     * @param className 类名
     * @return {@link String}
     */
    private String classNameToPath(final String className) {
        return String.join("", className.replace(".", "/"), ".class");
    }

}

使用:

package org.example1;

import org.hotloading.KMHotClassLoader;

import java.lang.reflect.Method;

public class Main {
    public static void main(String[] args) {
        KMHotClassLoader kmHotClassLoader = new KMHotClassLoader("E:\\study\\plant\\hotLoaderJarTest\\target",Main.class.getClassLoader());
        try {
            Class<?> aClass = kmHotClassLoader.loadClass("org.example.Fang");
            Object o = aClass.newInstance();
            Method testFang01 = aClass.getDeclaredMethod("testFang01", null);
            Object invoke = testFang01.invoke(o, null);

        } catch (Exception e) {
            throw new RuntimeException(e);
        }


    }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值