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

org.bbottema.javareflection.LookupCaches Maven / Gradle / Ivy

Go to download

Java Reflection provides a small package with nifty reflection features that will help with finding constructors, methods and value conversions

There is a newer version: 4.1.0
Show newest version
package org.bbottema.javareflection;

import org.bbottema.javareflection.model.InvokableObject;
import org.bbottema.javareflection.model.LookupMode;
import org.bbottema.javareflection.util.ArrayKey;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.lang.reflect.Method;
import java.util.EnumSet;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * For internal use for improving repeated lookup performances.
 */
public class LookupCaches {
	
	/**
	 * {@link Class} cache optionally used when looking up classes with {@link ClassUtils#locateClass(String, boolean, ClassLoader)}.
	 */
	final static Map> CLASS_CACHE = new HashMap<>();
	
	/**
	 * {@link Method} cache categorized by owning Classes (since several owners can have a method with the same name and signature).
	 * Methods are stored based on Method reference along with their unique signature (per owner), so multiple methods on one owner with
	 * the same name can coexist.
*
* This cache is being maintained to reduce lookup times when trying to find signature compatible Java methods. The possible signature * combinations using autoboxing and/or automatic common conversions can become very large (7000+ with only three parameters) and can become a * real problem. The more frequently a method is being called the larger the performance gain, especially for methods with long parameter lists * * @see MethodUtils#addMethodToCache(Class, String, Set, Class[]) * @see MethodUtils#getMethodFromCache(Class, String, Class[]) */ final static Map, Map[], Set>>> METHOD_CACHE = new LinkedHashMap<>(); static final Map, Set>> CACHED_REGISTERED_COMPATIBLE_TARGET_TYPES = new HashMap<>(); static final Map, Set>> CACHED_COMPATIBLE_TARGET_TYPES = new HashMap<>(); static final Map, Map[]>>> CACHED_COMPATIBLE_TYPE_LISTS = new HashMap<>(); @SuppressWarnings({"WeakerAccess", "unused"}) public static void resetCache() { CLASS_CACHE.clear(); METHOD_CACHE.clear(); CACHED_REGISTERED_COMPATIBLE_TARGET_TYPES.clear(); CACHED_COMPATIBLE_TARGET_TYPES.clear(); CACHED_COMPATIBLE_TYPE_LISTS.clear(); } @Nullable static List[]> getCachedCompatibleSignatures(EnumSet lookupMode, ArrayKey arrayKey) { final Map[]>> cachedCompatibleSignatures = CACHED_COMPATIBLE_TYPE_LISTS.get(lookupMode); if (cachedCompatibleSignatures != null) { final List[]> cachedResult = cachedCompatibleSignatures.get(arrayKey); if (cachedResult != null) { return cachedResult; } } return null; } @NotNull static List[]> addCompatiblesignaturesToCache(EnumSet lookupMode, ArrayKey arrayKey, List[]> compatibleTypeLists) { Map[]>> cachedCompatibleSignatures = CACHED_COMPATIBLE_TYPE_LISTS.get(lookupMode); if (cachedCompatibleSignatures == null) { CACHED_COMPATIBLE_TYPE_LISTS.put(lookupMode, cachedCompatibleSignatures = new HashMap<>()); } cachedCompatibleSignatures.put(arrayKey, compatibleTypeLists); return compatibleTypeLists; } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy