# Eo / Ejb / Example

## Overview

The `eo.ejb.example` module is a small demonstration package that showcases a reflective class-loading pattern in Java. Its sole class, [PatternOneVar](PatternOneVar.java:3), illustrates how to instantiate a class dynamically at runtime using only its fully qualified name — a technique often encountered in plugin architectures, testing harnesses, and EJB container scenarios. This module appears to serve as a reference example for understanding reflective object creation.

## Key Classes and Interfaces

### PatternOneVar

**Source:** [PatternOneVar.java](PatternOneVar.java:3)

`PatternOneVar` is a simple utility class whose purpose is to load and instantiate an arbitrary class by its fully qualified name at runtime. It demonstrates the `Class.forName()` + `newInstance()` reflection pattern.

#### Method: `load(String clsName)`

| Attribute  | Value                                            |
|-----------|---------------------------------------------------|
| **Signature** | `public void load(String clsName) throws Exception` |
| **Line**    | 4-7                                               |
| **Parameters** | `clsName` — the fully qualified class name (e.g. `com.example.MyClass`) |
| **Returns** | `void` — no return value                         |
| **Throws**  | `Exception` — covers `ClassNotFoundException`, `InstantiationException`, `IllegalAccessException` |

**What it does:**

1. Calls `Class.forName(clsName)` to resolve the class object from its name string at runtime.
2. Calls `newInstance()` on the resolved `Class` object to create a new instance via the class's no-argument constructor.
3. Prints the resulting object (via its `toString()`) to `System.out`.

This is a classic reflective instantiation pattern. The class must:
- Be visible to the current classloader.
- Have a public no-arg constructor.

Any deviation (missing class, no default constructor, non-public visibility) will throw an `Exception`.

## How It Works

The module implements a single, straightforward flow:

```mermaid
flowchart LR
    A["PatternOneVar.load()"] --> B["Class.forName(clsName)"]
    B --> C["Resolve Class object"]
    C --> D["Class.newInstance()"]
    D --> E["Create new instance"]
    E --> F["System.out.println(obj)"]
```

**Step by step:**

1. **Input** — The caller passes a fully qualified class name as a `String`.
2. **Class resolution** — `Class.forName(clsName)` queries the classloader for the corresponding `Class` object. If the class cannot be found, a `ClassNotFoundException` is thrown.
3. **Instantiation** — `newInstance()` invokes the class's public no-argument constructor. If the class lacks one (or it is not public), an `InstantiationException` or `IllegalAccessException` is thrown.
4. **Output** — The newly created object is printed to stdout.

## Data Model

This module has no data model — no entity classes, DTOs, or persistent structures. The only "data" flows through is the `String` class name passed as a parameter.

## Dependencies and Integration

### Reflection dependency

The `load` method relies on Java reflection to dynamically resolve and instantiate a target class by name at runtime. The target class is not known at compile time.

| Method            | Reflection target             | Confidence |
|-------------------|-------------------------------|------------|
| `load(String clsName)` | `Class.forName(clsName).newInstance()` | 0.6        |

### No explicit package dependencies

The module does not import any third-party libraries or external packages. It depends only on core Java (`java.lang.Class`, `java.lang.System`).

## Notes for Developers

- **Deprecated API:** `Class.newInstance()` was deprecated in Java 9 in favor of `Class.getDeclaredConstructor().newInstance()`. If migrating to newer Java versions, consider updating this pattern.
- **No error handling:** The method propagates all exceptions to the caller. In production code, you may want to wrap or handle specific exception types.
- **Security:** Since any class name can be passed, this pattern can be a security risk if untrusted input reaches `load()`. Validate or whitelist class names in sensitive contexts.
- **Extensibility:** To support classes without no-arg constructors, you would need to extend this method to accept constructor argument information and use `getDeclaredConstructor` instead.
