se.jbee.inject.bootstrap.BootstrapperBundle Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of silk-di Show documentation
Show all versions of silk-di Show documentation
Silk Java dependency injection framework
/*
* Copyright (c) 2012, Jan Bernitt
*
* Licensed under the Apache License, Version 2.0, http://www.apache.org/licenses/LICENSE-2.0
*/
package se.jbee.inject.bootstrap;
import se.jbee.inject.util.Metaclass;
/**
* The default utility {@link Bundle} that is a {@link Bootstrap} as well so that bindings can be
* declared nicer.
*
* @author Jan Bernitt ([email protected])
*/
public abstract class BootstrapperBundle
implements Bundle, Bootstrapper {
private Bootstrapper bootstrap;
@Override
public final void bootstrap( Bootstrapper bootstrap ) {
Bootstrap.nonnullThrowsReentranceException( this.bootstrap );
this.bootstrap = bootstrap;
bootstrap();
}
@Override
public final void install( Class extends Bundle> bundle ) {
bootstrap.install( bundle );
}
@Override
public final void install( Module module ) {
bootstrap.install( module );
}
@Override
public final void install( PresetModule module ) {
bootstrap.install( module );
}
@Override
public final void uninstall( Class extends Bundle> bundle ) {
bootstrap.uninstall( bundle );
}
@Override
public final & ModularBundle> void install( M... modules ) {
bootstrap.install( modules );
}
@Override
public final > void install( Class extends ModularBundle> bundle,
Class property ) {
bootstrap.install( bundle, property );
}
@Override
public final & ModularBundle> void uninstall( M... modules ) {
bootstrap.uninstall( modules );
}
protected final & ModularBundle> void installAll( Class modules ) {
install( modules.getEnumConstants() );
}
protected final & ModularBundle> void uninstallAll( Class modules ) {
uninstall( modules.getEnumConstants() );
}
/**
* Installs the given {@link Module} using the given {@link Inspector} when declaring binds.
*/
protected final void install( Module module, Inspector inspector ) {
install( new InspectorModule( module, inspector ) );
}
protected final void install( Class extends Module> module, Inspector inspector ) {
install( newInstance( module ), inspector );
}
protected static Module newInstance( Class extends Module> module ) {
return Invoke.constructor( Metaclass.accessible( Inspect.noArgsConstructor( module ) ) );
}
@Override
public String toString() {
return "bundle " + getClass().getSimpleName();
}
protected abstract void bootstrap();
private static final class InspectorModule
implements Module {
private final Module module;
private final Inspector inspector;
InspectorModule( Module module, Inspector inspector ) {
super();
this.module = module;
this.inspector = inspector;
}
@Override
public void declare( Bindings bindings, Inspector inspector ) {
module.declare( bindings, this.inspector );
}
@Override
public String toString() {
return module + "[" + inspector + "]";
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy