Builder Pattern
bytekin provides a fluent Builder API for constructing transformers programmatically.
Basic Usage
BytekinTransformer transformer = new BytekinTransformer.Builder(MyHooks.class)
.build();
Using Mappings
Apply name mappings during builder construction:
BytekinTransformer transformer = new BytekinTransformer.Builder(MyHooks.class)
.mapping(new CustomMappingProvider())
.build();
Adding Programmatic Transformations
Mix annotation-based and programmatic configurations:
BytekinTransformer transformer = new BytekinTransformer.Builder(AnnotationHooks.class)
.inject("com.example.Extra", new Injection(...))
.invoke("com.example.Another", new Invocation(...))
.build();
Multiple Hook Classes
BytekinTransformer transformer = new BytekinTransformer.Builder(
LoggingHooks.class,
SecurityHooks.class,
PerformanceHooks.class
)
.mapping(myMappings)
.build();
Builder Methods
mapping(IMappingProvider)
Set a mapping provider for class/method name translation.
inject(String, Injection)
Add injection transformation programmatically.
invoke(String, Invocation)
Add invocation transformation programmatically.
redirect(String, RedirectData)
Add redirect transformation programmatically.
modifyConstant(String, ConstantModification)
Add constant modification programmatically.
modifyVariable(String, VariableModification)
Add variable modification programmatically.
build()
Build and return the transformer. This method:
- Scans all hook classes
- Extracts annotations
- Adds programmatic transformations
- Creates internal transformer map
- Returns ready-to-use transformer
Best Practices
- Build once: Create transformers during initialization
- Reuse: Use the same transformer for multiple transformations
- Combine patterns: Mix annotations and programmatic API
- Document configuration: Comment why specific transformations are applied
Performance Tips
// Good: Build once
BytekinTransformer transformer = new BytekinTransformer.Builder(Hooks.class).build();
for (String className : classes) {
byte[] transformed = transformer.transform(className, bytecode);
}
// Bad: Building multiple times
for (String className : classes) {
BytekinTransformer transformer = new BytekinTransformer.Builder(Hooks.class).build();
byte[] transformed = transformer.transform(className, bytecode);
}
Next Steps
- Learn Advanced Usage
- Review Best Practices