Package ortus.boxlang.runtime.loader
Class ClassLocator
java.lang.Object
java.lang.ClassLoader
ortus.boxlang.runtime.loader.ClassLocator
This is a Class Loader is in charge of locating Box classes in the lookup algorithm
and also Java classes in the classloader paths. The resolution is done once
per class path and stored for quicker lookups.
Example: apppath.models.User
- Verify if the class is in the cache
- If not, verify if the class is in the declared class mappings path
- If not, verify if the class is in the application path
- If not, verify if the class is a Java class
If the class is found, it will be cached for future lookups.
-
Field Summary
Fields -
Method Summary
Modifier and TypeMethodDescriptionclassSet()Get all the class paths keys in the resolver cacheclear()Clear the resolver cacheClear a specific key from the resolver cachevoidClear all the class loadersClass<?> findClass(IBoxContext context, String name, List<ImportDefinition> imports) This method ONLY returns the class representation, it does not cache it.Shortcut to get the Box ResolverGet a list of all the cached resolver classesgetClassLoader(String loaderKey) Get a class loader by cache keylongCount how many class loaders we have loadedGet all the class loaders registeredstatic ClassLocatorGet the singleton instancestatic ClassLocatorgetInstance(BoxRuntime runtime) Get the singleton instanceShortcut to get the Java ResolverGet the registered resolver prefixesgetResolver(String prefix) Get a registered resolver by prefixGet the cache of resolved classesGet the runtime associated with this locatorbooleanVerifies if the passed path key is in the resolver cachebooleanhasClassLoader(String loaderKey) Verify if the class loader exists by cache keyhasResolver(String prefix) Verify if a resolver is registeredisEmpty()Verifies if the class resolver is empty or notload(IBoxContext context, String name) Load a class without a direct resolver.load(IBoxContext context, String name, String resolverPrefix) Load a class from a specific resolver This is a convenience method that will throw an exception if the class is not foundload(IBoxContext context, String name, String resolverPrefix, Boolean throwException) Load a class from a specific resolverload(IBoxContext context, String name, String resolverPrefix, Boolean throwException, List<ImportDefinition> imports) Load a class from a specific resolverload(IBoxContext context, String name, String resolverPrefix, Boolean throwException, List<ImportDefinition> imports, Boolean useCaching) Load a class from a specific resolverload(IBoxContext context, String name, List<ImportDefinition> imports) Load a class without a direct resolver or with a resovler prefix.loadFromClassPaths(IBoxContext context, String name, Array classPaths, Boolean throwException, List<ImportDefinition> imports) Load a class from a specific array of class pathsvoidregisterResolver(IClassResolver resolver) Register a class resolverremoveResolver(String prefix) Remove a resolver by prefixsafeLoad(IBoxContext context, String name) Same as the load method, but it will not throw an exception if the class is not found, it will return an empty optional instead.safeLoad(IBoxContext context, String name, String resolverPrefix) Load a class from a specific resolversafeLoad(IBoxContext context, String name, String resolverPrefix, List<ImportDefinition> imports) Load a class from a specific resolversafeLoad(IBoxContext context, String name, List<ImportDefinition> imports) Same as the load method, but it will not throw an exception if the class is not found, it will return an empty optional instead.intsize()Verify the size of the resolver cacheMethods inherited from class java.lang.ClassLoader
clearAssertionStatus, defineClass, defineClass, defineClass, defineClass, definePackage, findClass, findClass, findLibrary, findLoadedClass, findResource, findResource, findResources, findSystemClass, getClassLoadingLock, getDefinedPackage, getDefinedPackages, getName, getPackage, getPackages, getParent, getPlatformClassLoader, getResource, getResourceAsStream, getResources, getSystemClassLoader, getSystemResource, getSystemResourceAsStream, getSystemResources, getUnnamedModule, isRegisteredAsParallelCapable, loadClass, loadClass, registerAsParallelCapable, resolveClass, resources, setClassAssertionStatus, setDefaultAssertionStatus, setPackageAssertionStatus, setSigners
-
Field Details
-
TYPE_BX
public static final int TYPE_BXThe internal type of a BoxLang class- See Also:
-
TYPE_JAVA
public static final int TYPE_JAVAThe internal type of a Java class- See Also:
-
BX_PREFIX
- See Also:
-
JAVA_PREFIX
- See Also:
-
DEFAULT_RESOLVER
The default resolver name- See Also:
-
-
Method Details
-
getInstance
Get the singleton instance- Returns:
- ClassLocator
-
getInstance
Get the singleton instance- Parameters:
runtime- The current runtime- Returns:
- ClassLocator
-
getRuntime
Get the runtime associated with this locator -
getJavaResolver
Shortcut to get the Java Resolver -
getBoxResolver
Shortcut to get the Box Resolver -
getResolverCache
Get the cache of resolved classes- Returns:
- The cache of resolved classes
-
registerResolver
Register a class resolver- Parameters:
resolver- The class resolver to register
-
getResolvedPrefixes
Get the registered resolver prefixes- Returns:
- The registered resolver names
-
getResolver
Get a registered resolver by prefix- Parameters:
prefix- The prefix of the resolver- Returns:
- The registered resolver or null if not found
-
hasResolver
Verify if a resolver is registered- Parameters:
prefix- The prefix of the resolver- Returns:
- True if the resolver is registered, false otherwise
-
removeResolver
Remove a resolver by prefix- Parameters:
prefix- The prefix of the resolver- Returns:
- True if the resolver was removed, false otherwise
-
isEmpty
Verifies if the class resolver is empty or not- Returns:
- True if the resolver cache is empty, false otherwise
-
size
public int size()Verify the size of the resolver cache- Returns:
- The size of the resolver cache
-
clear
Clear the resolver cache- Returns:
- The class locator instance
-
clear
Clear a specific key from the resolver cache- Parameters:
name- The fully qualified path of the class to remove- Returns:
- True, if it was removed, else if it didn't exist
-
hasClass
Verifies if the passed path key is in the resolver cache- Parameters:
name- The fully qualified path of the class to verify- Returns:
- True if the key is in the resolver cache, false otherwise
-
classSet
Get all the class paths keys in the resolver cache- Returns:
- The keys in the resolver cache
-
getClassList
Get a list of all the cached resolver classes- Returns:
- The list of cached classes as a list of key names
-
load
Load a class without a direct resolver. This will require a system resolution of the class location and will cache the result for future lookups. The lookup order is: 1. Bx Resolver 2. Java Resolver- Parameters:
context- The current context of executionname- The fully qualified path/name of the class to load- Returns:
- The invokable representation of the class
-
load
Load a class without a direct resolver or with a resovler prefix. If there is not a resolver prefix-- Ex: java.lang.String app.models.User This will require a system resolution of the class location and will cache the result for future lookups. The lookup order is: 1. Bx Resolver 2. Java Resolver If there is a resolver prefix, then it will be used directly. Ex: java:java.lang.String bx:app.models.User- Parameters:
context- The current context of executionname- The fully qualified path/name of the class to loadimports- The list of imports to use when resolving the class- Returns:
- The invokable representation of the class
-
load
Load a class from a specific resolver This is a convenience method that will throw an exception if the class is not found- Parameters:
context- The current context of executionname- The fully qualified path/name of the class to loadresolverPrefix- The prefix of the resolver to use- Returns:
- The invokable representation of the class
-
load
public DynamicObject load(IBoxContext context, String name, String resolverPrefix, Boolean throwException) Load a class from a specific resolver- Parameters:
context- The current context of executionname- The fully qualified path/name of the class to loadresolverPrefix- The prefix of the resolver to usethrowException- If true, it will throw an exception if the class is not found, else it will return null- Returns:
- The invokable representation of the class
-
load
public DynamicObject load(IBoxContext context, String name, String resolverPrefix, Boolean throwException, List<ImportDefinition> imports) Load a class from a specific resolver- Parameters:
context- The current context of executionname- The fully qualified path/name of the class to loadresolverPrefix- The prefix of the resolver to usethrowException- If true, it will throw an exception if the class is not found, else it will return nullimports- The list of imports to use when resolving the class- Returns:
- The invokable representation of the class
-
load
public DynamicObject load(IBoxContext context, String name, String resolverPrefix, Boolean throwException, List<ImportDefinition> imports, Boolean useCaching) Load a class from a specific resolver- Parameters:
context- The current context of executionname- The fully qualified path/name of the class to loadresolverPrefix- The prefix of the resolver to usethrowException- If true, it will throw an exception if the class is not found, else it will return nullimports- The list of imports to use when resolving the classuseCaching- If true, it will cache the resolved class if allowed, else just does discovery and passthrough- Returns:
- The invokable representation of the class
-
loadFromClassPaths
public DynamicObject loadFromClassPaths(IBoxContext context, String name, Array classPaths, Boolean throwException, List<ImportDefinition> imports) Load a class from a specific array of class paths- Parameters:
context- The current context of executionname- The fully qualified path/name of the class to loadclassPaths- The array of class paths to use when resolving the classthrowException- If true, it will throw an exception if the class is not found, else it will return nullimports- The list of imports to use when resolving the class- Returns:
- The invokable representation of the class
-
safeLoad
Same as the load method, but it will not throw an exception if the class is not found, it will return an empty optional instead.- Parameters:
context- The current context of executionname- The fully qualified path/name of the class to load- Returns:
- The invokable representation of the class or an empty optional if not found
-
safeLoad
public Optional<DynamicObject> safeLoad(IBoxContext context, String name, List<ImportDefinition> imports) Same as the load method, but it will not throw an exception if the class is not found, it will return an empty optional instead.- Parameters:
context- The current context of executionname- The fully qualified path/name of the class to loadimports- The list of imports to use when resolving the class- Returns:
- The invokable representation of the class or an empty optional if not found
-
safeLoad
Load a class from a specific resolver- Parameters:
context- The current context of executionname- The fully qualified path/name of the class to loadresolverPrefix- The prefix of the resolver to use- Returns:
- The invokable representation of the class
-
safeLoad
public Optional<DynamicObject> safeLoad(IBoxContext context, String name, String resolverPrefix, List<ImportDefinition> imports) Load a class from a specific resolver- Parameters:
context- The current context of executionname- The fully qualified path/name of the class to loadresolverPrefix- The prefix of the resolver to useimports- The list of imports to use when resolving the class- Returns:
- The invokable representation of the class
-
findClass
This method ONLY returns the class representation, it does not cache it. It is here to support the Java ClassLoader interface.- Parameters:
context- The current context of executionname- The fully qualified path/name of the class to loadimports- The list of imports to use when resolving the class- Returns:
- The class requested to be loaded represented by the incoming name
-
getClassLoaders
Get all the class loaders registered- Returns:
- The class loader map
-
hasClassLoader
Verify if the class loader exists by cache key- Parameters:
loaderKey- The key of the class loader
-
getClassLoader
Get a class loader by cache key- Parameters:
loaderKey- The key of the class loader- Returns:
- The class loader
-
getClassLoaderCount
public long getClassLoaderCount()Count how many class loaders we have loaded -
clearClassLoaders
public void clearClassLoaders()Clear all the class loaders
-