Code generation library for creating dynamic proxies, bean utilities, and fast reflection alternatives with bundled dependencies
—
Fast alternatives to Java reflection for method and constructor invocation, plus method delegation utilities. These utilities provide significant performance improvements over standard Java reflection while maintaining similar functionality.
Faster alternative to Java reflection for method and constructor invocation.
/**
* Faster alternative to Java reflection for class operations
*/
public abstract class FastClass {
/**
* Create FastClass for a type
* @param type Class to wrap
* @return FastClass instance
*/
public static FastClass create(Class type);
/**
* Create FastClass with specific ClassLoader
* @param loader ClassLoader to use
* @param type Class to wrap
* @return FastClass instance
*/
public static FastClass create(ClassLoader loader, Class type);
/**
* Invoke method by name and parameter types
* @param name Method name
* @param parameterTypes Parameter types
* @param obj Target object
* @param args Method arguments
* @return Method result
* @throws InvocationTargetException If method throws exception
*/
public abstract Object invoke(String name, Class[] parameterTypes, Object obj, Object[] args)
throws InvocationTargetException;
/**
* Create new instance with default constructor
* @return New instance
* @throws InvocationTargetException If constructor throws exception
*/
public abstract Object newInstance() throws InvocationTargetException;
/**
* Create new instance with specific constructor
* @param parameterTypes Constructor parameter types
* @param args Constructor arguments
* @return New instance
* @throws InvocationTargetException If constructor throws exception
*/
public abstract Object newInstance(Class[] parameterTypes, Object[] args)
throws InvocationTargetException;
/**
* Get FastMethod wrapper for a Method
* @param method Method to wrap
* @return FastMethod instance
*/
public abstract FastMethod getMethod(Method method);
/**
* Get FastConstructor wrapper for a Constructor
* @param constructor Constructor to wrap
* @return FastConstructor instance
*/
public abstract FastConstructor getConstructor(Constructor constructor);
/**
* Get FastMethod by signature
* @param name Method name
* @param parameterTypes Parameter types
* @return FastMethod instance
*/
public abstract FastMethod getMethod(String name, Class[] parameterTypes);
/**
* Get FastConstructor by parameter types
* @param parameterTypes Parameter types
* @return FastConstructor instance
*/
public abstract FastConstructor getConstructor(Class[] parameterTypes);
/**
* Invoke method by index (fastest)
* @param index Method index
* @param obj Target object
* @param args Method arguments
* @return Method result
* @throws InvocationTargetException If method throws exception
*/
public Object invoke(int index, Object obj, Object[] args) throws InvocationTargetException;
/**
* Create instance by constructor index
* @param index Constructor index
* @param args Constructor arguments
* @return New instance
* @throws InvocationTargetException If constructor throws exception
*/
public Object newInstance(int index, Object[] args) throws InvocationTargetException;
/**
* Get method index for name and parameters
* @param name Method name
* @param parameterTypes Parameter types
* @return Method index
*/
public int getIndex(String name, Class[] parameterTypes);
/**
* Get method index for signature
* @param signature Method signature
* @return Method index
*/
public int getIndex(Signature signature);
/**
* Get maximum method index
* @return Maximum index
*/
public int getMaxIndex();
/**
* Get wrapped Java class
* @return Java class
*/
public Class getJavaClass();
/**
* Get class name
* @return Class name
*/
public String getName();
/**
* String representation
* @return String
*/
public String toString();
}Usage Examples:
// Create FastClass for better performance than reflection
FastClass fastClass = FastClass.create(MyService.class);
// Invoke methods by name (faster than Method.invoke)
MyService service = new MyService();
Object result = fastClass.invoke("processData",
new Class[]{String.class, Integer.class},
service,
new Object[]{"hello", 42});
// Create instances (faster than Constructor.newInstance)
Object newInstance = fastClass.newInstance();
Object instanceWithArgs = fastClass.newInstance(
new Class[]{String.class},
new Object[]{"constructor arg"});
// Get method wrappers for repeated use
FastMethod fastMethod = fastClass.getMethod("processData",
new Class[]{String.class, Integer.class});
Object result2 = fastMethod.invoke(service, new Object[]{"world", 99});
// Use indexes for maximum performance
int methodIndex = fastClass.getIndex("processData",
new Class[]{String.class, Integer.class});
Object result3 = fastClass.invoke(methodIndex, service, new Object[]{"fast", 123});
// Performance comparison
long start = System.nanoTime();
for (int i = 0; i < 100000; i++) {
// FastClass approach
fastClass.invoke(methodIndex, service, new Object[]{"test", i});
}
long fastTime = System.nanoTime() - start;
start = System.nanoTime();
Method method = MyService.class.getMethod("processData", String.class, Integer.class);
for (int i = 0; i < 100000; i++) {
// Standard reflection
method.invoke(service, "test", i);
}
long reflectionTime = System.nanoTime() - start;
// FastClass is typically 10-50x fasterFast method invocation wrapper.
/**
* Fast method invocation wrapper
*/
public class FastMethod {
/**
* Invoke the method
* @param obj Target object
* @param args Method arguments
* @return Method result
* @throws InvocationTargetException If method throws exception
*/
public Object invoke(Object obj, Object[] args) throws InvocationTargetException;
/**
* Get the wrapped Java method
* @return Method instance
*/
public Method getJavaMethod();
/**
* Get parameter types
* @return Parameter types
*/
public Class[] getParameterTypes();
/**
* Get return type
* @return Return type
*/
public Class getReturnType();
/**
* Get method index in FastClass
* @return Method index
*/
public int getIndex();
/**
* Get method name
* @return Method name
*/
public String getName();
/**
* Get method signature
* @return Signature
*/
public Signature getSignature();
/**
* String representation
* @return String
*/
public String toString();
}Fast constructor invocation wrapper.
/**
* Fast constructor invocation wrapper
*/
public class FastConstructor {
/**
* Create new instance with no arguments
* @return New instance
* @throws InvocationTargetException If constructor throws exception
*/
public Object newInstance() throws InvocationTargetException;
/**
* Create new instance with arguments
* @param args Constructor arguments
* @return New instance
* @throws InvocationTargetException If constructor throws exception
*/
public Object newInstance(Object[] args) throws InvocationTargetException;
/**
* Get the wrapped Java constructor
* @return Constructor instance
*/
public Constructor getJavaConstructor();
/**
* Get parameter types
* @return Parameter types
*/
public Class[] getParameterTypes();
/**
* Get constructor index in FastClass
* @return Constructor index
*/
public int getIndex();
/**
* String representation
* @return String
*/
public String toString();
}Creates optimized delegates for method calls, providing better performance than reflection or interface implementations.
/**
* Creates optimized method delegates
*/
public abstract class MethodDelegate {
/**
* Delegate for static methods only
*/
public static final MethodDelegate STATIC_ONLY;
/**
* Create method delegate
* @param target Target object
* @param methodName Method name
* @param iface Interface that delegate should implement
* @return MethodDelegate instance
*/
public static MethodDelegate create(Object target, String methodName, Class iface);
/**
* Create new delegate instance
* @param target New target object
* @param methodName Method name
* @return New delegate instance
*/
public abstract Object newInstance(Object target, String methodName);
}Usage Examples:
// Define interface for delegate
interface StringProcessor {
String process(String input);
}
// Create object with method matching interface
class TextUtils {
public String toUpperCase(String text) {
return text.toUpperCase();
}
public String toLowerCase(String text) {
return text.toLowerCase();
}
}
// Create method delegate
TextUtils utils = new TextUtils();
StringProcessor upperDelegate = (StringProcessor) MethodDelegate.create(
utils, "toUpperCase", StringProcessor.class);
// Use delegate (much faster than reflection)
String result = upperDelegate.process("hello"); // "HELLO"
// Create delegate for different method
StringProcessor lowerDelegate = (StringProcessor) MethodDelegate.create(
utils, "toLowerCase", StringProcessor.class);
String result2 = lowerDelegate.process("WORLD"); // "world"
// Create new delegate for different target object
TextUtils utils2 = new TextUtils();
Object newDelegate = upperDelegate.newInstance(utils2, "toUpperCase");
StringProcessor typedDelegate = (StringProcessor) newDelegate;
// Performance comparison
long start = System.nanoTime();
for (int i = 0; i < 100000; i++) {
upperDelegate.process("test" + i);
}
long delegateTime = System.nanoTime() - start;
start = System.nanoTime();
Method method = TextUtils.class.getMethod("toUpperCase", String.class);
for (int i = 0; i < 100000; i++) {
method.invoke(utils, "test" + i);
}
long reflectionTime = System.nanoTime() - start;
// Delegate is typically much faster than reflectionMulticasts method calls to multiple delegate objects.
/**
* Multicasts method calls to multiple delegates
*/
public abstract class MulticastDelegate {
/**
* Create multicast delegate for interface
* @param iface Interface to implement
* @return MulticastDelegate instance
*/
public static MulticastDelegate create(Class iface);
/**
* Add delegate to multicast
* @param delegate Delegate to add
* @return New multicast delegate including added delegate
*/
public abstract MulticastDelegate add(Object delegate);
/**
* Remove delegate from multicast
* @param delegate Delegate to remove
* @return New multicast delegate excluding removed delegate
*/
public abstract MulticastDelegate remove(Object delegate);
/**
* Create new multicast delegate instance
* @return New instance
*/
public abstract MulticastDelegate newInstance();
}Usage Examples:
// Define interface for multicast
interface EventListener {
void onEvent(String event);
}
// Create individual listeners
EventListener listener1 = event -> System.out.println("Listener 1: " + event);
EventListener listener2 = event -> System.out.println("Listener 2: " + event);
EventListener listener3 = event -> System.out.println("Listener 3: " + event);
// Create multicast delegate
MulticastDelegate multicast = MulticastDelegate.create(EventListener.class);
// Add listeners
multicast = multicast.add(listener1);
multicast = multicast.add(listener2);
multicast = multicast.add(listener3);
// Cast to interface and use
EventListener broadcaster = (EventListener) multicast;
broadcaster.onEvent("Hello");
// Prints:
// Listener 1: Hello
// Listener 2: Hello
// Listener 3: Hello
// Remove a listener
multicast = multicast.remove(listener2);
EventListener broadcaster2 = (EventListener) multicast;
broadcaster2.onEvent("World");
// Prints:
// Listener 1: World
// Listener 3: World
// Create new instance for different delegate set
MulticastDelegate multicast2 = multicast.newInstance();
multicast2 = multicast2.add(listener2);Fast constructor delegation for object creation.
/**
* Fast constructor delegation
*/
public abstract class ConstructorDelegate {
/**
* Create constructor delegate
* @param targetClass Class to create instances of
* @param iface Interface that delegate should implement
* @return ConstructorDelegate instance
*/
public static ConstructorDelegate create(Class targetClass, Class iface);
/**
* Create new instance using constructor
* @param args Constructor arguments
* @return New instance
*/
public abstract Object newInstance(Object[] args);
}Usage Examples:
// Define interface for constructor delegate
interface PersonFactory {
Person create(String name, int age);
}
// Create constructor delegate
ConstructorDelegate delegate = ConstructorDelegate.create(Person.class, PersonFactory.class);
PersonFactory factory = (PersonFactory) delegate;
// Use factory to create instances (faster than reflection)
Person person1 = factory.create("Alice", 30);
Person person2 = factory.create("Bob", 25);
// Performance comparison with reflection
Constructor<Person> constructor = Person.class.getConstructor(String.class, Integer.TYPE);
long start = System.nanoTime();
for (int i = 0; i < 100000; i++) {
factory.create("Test" + i, i);
}
long delegateTime = System.nanoTime() - start;
start = System.nanoTime();
for (int i = 0; i < 100000; i++) {
constructor.newInstance("Test" + i, i);
}
long reflectionTime = System.nanoTime() - start;
// Constructor delegate is typically fasterBase class for FastMethod and FastConstructor providing common functionality.
/**
* Base class for fast member access
*/
public abstract class FastMember {
/**
* Get member index
* @return Member index
*/
public abstract int getIndex();
/**
* Get member name
* @return Member name
*/
public abstract String getName();
/**
* Get parameter types
* @return Parameter types
*/
public abstract Class[] getParameterTypes();
}/**
* Exception wrapping exceptions thrown by invoked methods/constructors
*/
public class InvocationTargetException extends Exception {
/**
* Get the target exception
* @return Target exception
*/
public Throwable getTargetException();
/**
* Get the cause
* @return Cause exception
*/
public Throwable getCause();
}/**
* Represents a method signature
*/
public class Signature {
/**
* Get method name
* @return Method name
*/
public String getName();
/**
* Get descriptor
* @return Method descriptor
*/
public String getDescriptor();
/**
* String representation
* @return String
*/
public String toString();
}Install with Tessl CLI
npx tessl i tessl/maven-cglib--cglib-nodep