package com.muyu.rule.common.load;

import java.io.IOException;
import java.net.URL;
import java.net.URLClassLoader;
import java.nio.file.Files;
import java.nio.file.Path;

public class ExternalClassLoader extends URLClassLoader {

    public ExternalClassLoader(URL[] urls) {
        super(urls, Thread.currentThread().getContextClassLoader());
    }

    public Class<?> defineClassFromBytes(byte[] classBytes, String className) throws IOException {
        return super.defineClass(className, classBytes, 0, classBytes.length);
    }

    public Class<?> loadClassFromPath(Path classFilePath, String className) throws IOException {
        byte[] classData = Files.readAllBytes(classFilePath);
        return defineClassFromBytes(classData, className);
    }
}
