All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.skjolber.stcsv.CsvReaderClassLoader Maven / Gradle / Ivy

There is a newer version: 1.0.25
Show newest version
package com.github.skjolber.stcsv;

import java.util.Objects;

public class CsvReaderClassLoader {
	
    protected final ClassLoader contextClassLoader;

    public CsvReaderClassLoader(ClassLoader contextClassLoader) {
        this.contextClassLoader = contextClassLoader;
    }

    @SuppressWarnings("unchecked")
    public Class load(byte[] classBytes, String className) throws Exception {
        return (Class) new DynamicClassLoader(contextClassLoader, classBytes, className).loadClass(className);
    }

    protected static class DynamicClassLoader extends ClassLoader {
    	protected byte[] classBytes;
    	protected final String className;

    	protected DynamicClassLoader(ClassLoader contextClassLoader, byte[] classBytes, String className) {
            super(contextClassLoader);
            this.classBytes = classBytes;
            this.className = className;
        }

        @Override
        public Class findClass(String className) throws ClassNotFoundException {
            if (Objects.equals(this.className, className)) {
                return defineClass(className, this.classBytes, 0, this.classBytes.length);
            }

            throw new ClassNotFoundException(className);
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy