public interface ClassLookup
Implementations of this interface should handle the logic for aliasing classes and provide a mechanism for looking up classes by either their canonical name or an associated alias.
| Modifier and Type | Method and Description |
|---|---|
void |
addAlias(Class<?>... classes)
Adds one or more classes to the class lookup.
|
void |
addAlias(Class<?> clazz,
String names)
Adds a class to the class lookup with one or more specified aliases.
|
default CharSequence |
applyAlias(CharSequence name)
Applies an alias transformation to the given class name if an alias exists.
|
Class<?> |
forName(CharSequence name)
Looks up and returns the Class object associated with the given class name.
|
String |
nameFor(Class<?> clazz)
Retrieves the alias for the given class.
|
default @NotNull ClassLookup |
wrap()
Creates a new ClassLookup instance that wraps this instance.
|
default @NotNull ClassLookup |
wrap(@NotNull ClassLoader classLoader)
Creates a new ClassLookup instance that wraps this instance and uses the provided
class loader to look up new classes.
|
@NotNull default @NotNull ClassLookup wrap()
@NotNull default @NotNull ClassLookup wrap(@NotNull @NotNull ClassLoader classLoader)
classLoader - The ClassLoader used to look up new classes.NullPointerException - if classLoader is null.Class<?> forName(CharSequence name) throws ClassNotFoundRuntimeException
name - The fully qualified name of the desired class.ClassNotFoundRuntimeException - If the class cannot be located.String nameFor(Class<?> clazz) throws IllegalArgumentException
clazz - The class to retrieve the alias for.IllegalArgumentException - If this method is used on a lambda function.void addAlias(Class<?>... classes)
classes - The classes to be added to the class lookup.void addAlias(Class<?> clazz, String names)
clazz - The class to be added to the class lookup.names - A single alias or a comma-separated string of aliases for the class.default CharSequence applyAlias(CharSequence name)
This method first looks up the provided class name in the internal alias mappings. If a direct match is found, it returns the corresponding class name as registered in the alias. If no direct match is found, it attempts to find a match using a case-insensitive search. This dual-step lookup ensures that aliases can be effectively used regardless of case sensitivity. If after both steps no alias is found, the method returns the original class name, ensuring that the operation is always safe and will not result in a loss of reference.
Usage of this method can significantly reduce the verbosity of class references in scenarios where short, memorable aliases are preferred over full class names. It is particularly useful in configuration files, dynamic class loading scenarios, or any context where class names are used as strings and brevity or clarity is desired.
name - The CharSequence representing the class name or alias to be transformed.null or an invalid class name.NullPointerException - if the provided name is null.for how aliases are added to the pool.Copyright © 2024. All rights reserved.