Lightweight dependency injection framework for Java 8 and above that eliminates factories and the use of 'new' through @Inject annotation
—
Introspection and tooling APIs for analyzing injector configuration, visiting bindings, and integrating with development tools.
Utilities for extracting and rewriting elements from modules.
/**
* Exposes elements of a module so they can be inspected, validated or rewritten.
* Elements are extractable from modules and can be used to build new modules.
*/
public final class Elements {
/**
* Records the elements executed by modules so they can be analyzed.
* @param modules Modules to extract elements from
* @return List of elements from the modules
*/
public static List<Element> getElements(Module... modules);
/**
* Records the elements executed by modules so they can be analyzed.
* @param stage Development stage
* @param modules Modules to extract elements from
* @return List of elements from the modules
*/
public static List<Element> getElements(Stage stage, Iterable<? extends Module> modules);
/**
* Creates a module from the given elements.
* @param elements Elements to build module from
* @return Module containing the elements
*/
public static Module getModule(Iterable<? extends Element> elements);
}Core component representing configuration elements for introspection.
/**
* A core component of a module or injector. Elements are extractable from modules
* and can be inspected, validated or rewritten.
*/
public interface Element {
/**
* Returns the source object which was used to create this element.
* @return Source object
*/
Object getSource();
/**
* Accepts a visitor for this element.
* @param visitor Element visitor
* @return Result from visitor
*/
<T> T acceptVisitor(ElementVisitor<T> visitor);
/**
* Writes this element to the given binder.
* @param binder Binder to write to
*/
void applyTo(Binder binder);
}Usage Examples:
// Extract elements from modules for analysis
public class ModuleAnalyzer {
public void analyzeModule(Module module) {
List<Element> elements = Elements.getElements(module);
for (Element element : elements) {
element.acceptVisitor(new DefaultElementVisitor<Void>() {
@Override
public <T> Void visit(Binding<T> binding) {
System.out.println("Found binding: " + binding.getKey());
return null;
}
@Override
public Void visit(PrivateElements privateElements) {
System.out.println("Found private module with " +
privateElements.getElements().size() + " elements");
return null;
}
});
}
}
}
// Rewrite modules
public Module rewriteModule(Module originalModule) {
List<Element> elements = Elements.getElements(originalModule);
// Filter or transform elements as needed
List<Element> filteredElements = elements.stream()
.filter(element -> shouldIncludeElement(element))
.collect(Collectors.toList());
return Elements.getModule(filteredElements);
}Install with Tessl CLI
npx tessl i tessl/maven-com-google-inject--guice