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

com.softicar.platform.common.core.language.CachingClassLoader Maven / Gradle / Ivy

Go to download

The SoftiCAR Platform is a lightweight, Java-based library to create interactive business web applications.

There is a newer version: 50.0.0
Show newest version
package com.softicar.platform.common.core.language;

import com.softicar.platform.common.core.utils.DevNull;
import java.util.Map;
import java.util.TreeMap;

/**
 * This is a caching implementation of {@link IClassLoader}.
 * 

* Loading the same class multiple times via the standard {@link ClassLoader} is * not very efficient, because it is calling a native method (Jdk 7). Using a * class loader cache can greatly improve the performance, according to * empirical measurements. * * @author Oliver Richers */ public class CachingClassLoader extends SimpleClassLoader { private final Map> classMap = new TreeMap<>(); public CachingClassLoader() { super(); } public CachingClassLoader(ClassLoader classLoader) { super(classLoader); } /** * Loads the class with the specified class name. *

* This class first checks an internal cache if the corresponding class has * already been loaded. Only then, if no such class can be found in the * cache, the actual class loader is called to load the class. * * @param className * the name of the class to load * @return the loaded class, never null * @throws ClassNotFoundRuntimeException * if no matching class could be found */ @Override public Class loadClass(String className) { // check class map Class loadedClass = classMap.get(className); if (loadedClass != null) { return loadedClass; } // load class via class loader and put into map loadedClass = super.loadClass(className); classMap.put(className, loadedClass); return loadedClass; } @Override public Class loadClass(String className, Class defaultClass) { try { return this.loadClass(className); } catch (ClassNotFoundRuntimeException exception) { DevNull.swallow(exception); return defaultClass; } } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy