High level API to generate and transform JAVA byte code for dynamic proxy objects and runtime class enhancement.
—
Utility classes for efficient string operations, specialized sorting algorithms, and other performance-oriented helper functionality.
import net.sf.cglib.util.StringSwitcher;
import net.sf.cglib.util.ParallelSorter;
import net.sf.cglib.util.SorterTemplate;
import net.sf.cglib.util.KeyFactory;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;Efficient string-based switch statement implementation that generates optimized bytecode for string comparisons, much faster than traditional if-else chains or HashMap lookups for small sets of strings.
/**
* Efficient string-based switch statement implementation
*/
public abstract class StringSwitcher {
/**
* Create StringSwitcher for given strings and corresponding integer values
* @param strings - Array of strings to match
* @param ints - Array of integers corresponding to each string
* @param fixedInput - Whether input strings are guaranteed to be from the strings array
* @return StringSwitcher instance
*/
public static StringSwitcher create(String[] strings, int[] ints, boolean fixedInput);
/**
* Get integer value for given string
* @param s - String to lookup
* @return Corresponding integer value, or -1 if not found
*/
public abstract int intValue(String s);
}Usage Examples:
import net.sf.cglib.util.StringSwitcher;
// Create string switcher for HTTP methods
String[] methods = {"GET", "POST", "PUT", "DELETE", "PATCH"};
int[] values = {1, 2, 3, 4, 5};
StringSwitcher switcher = StringSwitcher.create(methods, values, false);
// Use in switch-like logic (much faster than string comparison chains)
String httpMethod = "POST";
int methodCode = switcher.intValue(httpMethod); // Returns 2
switch (methodCode) {
case 1:
System.out.println("Handle GET request");
break;
case 2:
System.out.println("Handle POST request");
break;
case 3:
System.out.println("Handle PUT request");
break;
case 4:
System.out.println("Handle DELETE request");
break;
case 5:
System.out.println("Handle PATCH request");
break;
default:
System.out.println("Unknown method");
}
// Fixed input version (slightly more optimized when input is guaranteed to be from the array)
StringSwitcher fixedSwitcher = StringSwitcher.create(methods, values, true);Sorts multiple arrays in parallel based on the order of one primary array, maintaining correspondence between array elements across all arrays.
/**
* Sorts multiple arrays in parallel based on one array's values
*/
public class ParallelSorter {
/**
* Sort two arrays in parallel based on first array's order
* @param a - Primary array to sort by
* @param b - Secondary array to rearrange in parallel
*/
public static void sort(Object[] a, Object[] b);
/**
* Sort multiple arrays in parallel based on first array's order
* @param arrays - Arrays to sort, first array determines sort order
*/
public static void sort(Object[][] arrays);
/**
* Sort with custom comparator
* @param a - Primary array to sort by
* @param b - Secondary array to rearrange in parallel
* @param cmp - Comparator for primary array
*/
public static void sort(Object[] a, Object[] b, Comparator cmp);
/**
* Sort multiple arrays with custom comparator
* @param arrays - Arrays to sort, first array determines sort order
* @param cmp - Comparator for primary array
*/
public static void sort(Object[][] arrays, Comparator cmp);
}Usage Examples:
import net.sf.cglib.util.ParallelSorter;
import java.util.Arrays;
// Sort students by age while keeping names in sync
String[] names = {"Alice", "Bob", "Charlie", "Diana"};
Integer[] ages = {25, 19, 30, 22};
// Sort by ages (primary array), names will be rearranged to match
ParallelSorter.sort(ages, names);
// Result: ages = [19, 22, 25, 30], names = ["Bob", "Diana", "Alice", "Charlie"]
System.out.println("Sorted ages: " + Arrays.toString(ages));
System.out.println("Corresponding names: " + Arrays.toString(names));
// Multiple arrays example
String[] firstNames = {"John", "Jane", "Bob", "Alice"};
String[] lastNames = {"Doe", "Smith", "Johnson", "Wilson"};
Integer[] scores = {85, 92, 78, 95};
// Sort by scores, keeping first and last names in sync
Object[][] data = {scores, firstNames, lastNames};
ParallelSorter.sort(data);
// With custom comparator (reverse order)
ParallelSorter.sort(ages, names, (a, b) -> ((Integer)b).compareTo((Integer)a));Abstract template for implementing custom parallel sorting algorithms.
/**
* Abstract template for custom parallel sorting algorithms
*/
public abstract class SorterTemplate {
/**
* Perform the sorting operation
* @param from - Start index (inclusive)
* @param to - End index (exclusive)
*/
public final void quickSort(int from, int to);
/**
* Merge sort implementation
* @param from - Start index (inclusive)
* @param to - End index (exclusive)
*/
public final void mergeSort(int from, int to);
/**
* Compare elements at two indices
* @param i - First index
* @param j - Second index
* @return Negative if i < j, zero if i == j, positive if i > j
*/
protected abstract int compare(int i, int j);
/**
* Swap elements at two indices
* @param i - First index
* @param j - Second index
*/
protected abstract void swap(int i, int j);
/**
* Get length of data to sort
* @return Data length
*/
protected abstract int length();
}Generates efficient key objects for use in caching and hash-based operations.
/**
* Generates efficient key objects for caching and hash operations
*/
public abstract class KeyFactory {
/**
* Create KeyFactory for given interface
* @param keyInterface - Interface defining key methods
* @return KeyFactory instance
*/
public static KeyFactory create(Class keyInterface);
/**
* Create KeyFactory with custom ClassLoader
* @param loader - ClassLoader to use
* @param keyInterface - Interface defining key methods
* @return KeyFactory instance
*/
public static KeyFactory create(ClassLoader loader, Class keyInterface);
/**
* Create new key instance
* @return Key object implementing the key interface
*/
public abstract Object newInstance();
}Usage Examples:
import net.sf.cglib.util.KeyFactory;
// Define key interface
interface CacheKey {
void setUserId(int userId);
void setTimestamp(long timestamp);
void setType(String type);
}
// Create key factory
KeyFactory factory = KeyFactory.create(CacheKey.class);
// Create efficient key objects for caching
CacheKey key1 = (CacheKey) factory.newInstance();
key1.setUserId(123);
key1.setTimestamp(System.currentTimeMillis());
key1.setType("user-profile");
CacheKey key2 = (CacheKey) factory.newInstance();
key2.setUserId(456);
key2.setTimestamp(System.currentTimeMillis());
key2.setType("user-settings");
// Keys properly implement equals() and hashCode() for efficient caching
Map<Object, Object> cache = new HashMap<>();
cache.put(key1, "profile data");
cache.put(key2, "settings data");Install with Tessl CLI
npx tessl i tessl/maven-cglib--cglib