The EngineContext class serves as the core of the framework's component lifecycle management system. It scans, initializes, and wires annotated components while providing mechanisms for dependency injection and runtime context management. This documentation details the class's purpose, features, methods, and usage examples.
The EngineContext is responsible for:
- Component Scanning: Locates classes annotated with
@EngineComponentor@GameLoop. - Initialization: Instantiates and stores these components in a managed context.
- Dependency Injection: Resolves and injects dependencies annotated with
@Inject. - Lifecycle Management: Facilitates clean startup and shutdown operations.
- Scans a specified package for framework components.
- Automates the instantiation and management of annotated components.
- Supports dependency injection for annotated fields.
- Provides runtime component retrieval by type.
- Logs application lifecycle events.
Initializes the application context by scanning and setting up framework components.
basePackage: The package to scan for annotated components.mainClass: The main application class, used to determine the parent package.
Exceptionif an error occurs during scanning, initialization, or dependency injection.
EngineContext context = new EngineContext("dark.cat", Main.class);- Type:
Map<Class<?>, Object> - Stores instantiated framework components, where the key is the class type, and the value is the instance.
- Type:
Class<?> - Holds the reference to the main application class for package resolution.
Registers built-in dependencies (e.g., RenderManager) into the context for automatic management.
Scans the specified package for classes annotated with @EngineComponent or @GameLoop and initializes them.
basePackage: The base package to scan.
Exceptionif errors occur during class loading or instantiation.
Recursively scans directories for class files, loads them, and initializes components if they are annotated.
dir: The directory to scan.packageName: The corresponding package name.
Performs dependency injection by resolving and assigning dependencies to fields annotated with @Inject.
IllegalAccessExceptionif field access fails.RuntimeExceptionif a dependency is not found.
Retrieves a managed component by its class type.
clazz: The type of the desired component.
- The component instance, or
nullif no component of the specified type exists.
RenderManager renderManager = context.getComponent(RenderManager.class);Sets the main application class.
mainClass: The main application class.
Returns the main application class.
- The
Class<?>of the main application.
Derives the parent package name from the main class.
- The parent package name as a
String.
Shuts down the context gracefully by:
- Terminating managed threads using
ThreadManagerPool.shutdown(). - Logging the shutdown event.
public class Main {
public static void main(String[] args) throws Exception {
EngineContext context = new EngineContext("dark.cat", Main.class);
RenderManager renderManager = context.getComponent(RenderManager.class);
renderManager.start();
// Shutdown the context gracefully on exit
Runtime.getRuntime().addShutdownHook(new Thread(context::shutdown));
}
}@EngineComponent
public class Game {
@Inject
private RenderManager renderManager;
public void start() {
renderManager.render();
}
}EngineContext context = new EngineContext("dark.cat", Main.class);
// Manually adding a component
RenderManager customRenderManager = new RenderManager();
context.getComponents().put(RenderManager.class, customRenderManager);
// Retrieving a component
RenderManager renderManager = context.getComponent(RenderManager.class);- Annotations: Works in conjunction with
@EngineComponent,@GameLoop, and@Injectannotations for component management. - Exception Handling: Errors during component resolution or dependency injection are logged and handled gracefully.
- Reflection-Based: Relies on reflection; ensure runtime environments support it.