---
name: x-ipe-tool-implementation-java
description: Java-specific implementation tool skill. Handles Spring Boot, Quarkus, Micronaut, and plain Java projects with built-in best practices (SOLID, clean architecture, JUnit 5). No research step needed — practices are baked in. Called by x-ipe-task-based-code-implementation orchestrator. Triggers on Java tech_stack entries.
---

# Java Implementation Tool Skill

## Purpose

AI Agents follow this skill to implement Java code by:
1. Learning existing code structure and detecting framework/build tool
2. Implementing with built-in Java best practices (SOLID, clean architecture, records, builder)
3. Writing JUnit 5 + Mockito tests mapped to AAA scenario Assert clauses
4. Running tests and linting (Checkstyle + Google Java Format)

---

## Important Notes

BLOCKING: This skill is invoked by the `x-ipe-task-based-code-implementation` orchestrator. Do NOT invoke directly unless testing.

CRITICAL: No research step is needed — Java best practices are built into this skill. Skip identification/research and go straight to learning existing code.

MANDATORY: Follow the standard tool skill I/O contract defined in [implementation-guidelines.md](.github/skills/x-ipe-task-based-code-implementation/references/implementation-guidelines.md).

---

## When to Use

```yaml
triggers:
  - "tech_stack contains Java, Spring Boot, Quarkus, Micronaut"
  - "tech_stack contains Java CLI, Java library"
  - "Orchestrator routes Java-related entry to this skill"

not_for:
  - "x-ipe-tool-implementation-python: for Python/Flask/FastAPI/Django"
  - "x-ipe-tool-implementation-html5: for HTML/CSS/JavaScript"
  - "x-ipe-tool-implementation-typescript: for TypeScript/React/Vue/Angular"
  - "x-ipe-tool-implementation-mcp: for MCP servers"
  - "x-ipe-tool-implementation-general: for unknown/rare stacks"
```

---

## Input Parameters

```yaml
input:
  operation: "implement"
  aaa_scenarios:
    - scenario_text: "{tagged AAA scenario text}"
  source_code_path: "{path to source directory}"
  test_code_path: "{path to test directory}"
  feature_context:
    feature_id: "{FEATURE-XXX-X}"
    feature_title: "{title}"
    technical_design_link: "{path to technical-design.md}"
    specification_link: "{path to specification.md}"
```

---

## Definition of Ready

```xml
<definition_of_ready>
  <checkpoint required="true">
    <name>AAA scenarios provided</name>
    <verification>aaa_scenarios array is non-empty</verification>
  </checkpoint>
  <checkpoint required="true">
    <name>Source code path valid</name>
    <verification>source_code_path directory exists or can be created</verification>
  </checkpoint>
  <checkpoint required="true">
    <name>Feature context complete</name>
    <verification>feature_id and technical_design_link are provided</verification>
  </checkpoint>
</definition_of_ready>
```

---

## Operations

### Operation: implement

**When:** Orchestrator routes a Java tech_stack entry to this skill

```xml
<operation name="implement">
  <action>
    1. LEARN existing code:
       a. Read existing files in source_code_path
       b. Detect build tool:
          - Check for pom.xml → Maven (test: mvn test)
          - Check for build.gradle / build.gradle.kts → Gradle (test: ./gradlew test)
          - Neither found → ERROR: BUILD_TOOL_MISSING
       c. Detect framework from build file dependencies:
          - spring-boot-starter → Spring Boot
          - quarkus-bom → Quarkus
          - micronaut-core → Micronaut
          - None matched → Plain Java
       d. Follow existing conventions (naming, packages, error handling)

    2. IMPLEMENT with built-in Java best practices:
       a. Follow technical design Part 2 exactly
       b. SOLID principles:
          - SRP: one responsibility per class
          - OCP: extend via interfaces, not modification
          - LSP: subtypes substitutable for base types
          - ISP: small, focused interfaces
          - DIP: depend on abstractions, inject via constructor
       c. Clean architecture layers:
          - Controller/Resource → handles HTTP/input
          - Service → business logic
          - Repository → data access
       d. Package-by-feature for new modules
       e. Records for DTOs and value objects (Java 16+)
       f. Builder pattern for complex objects with many optional fields
       g. Exception handling:
          - Checked exceptions for recoverable errors
          - Unchecked (RuntimeException) for programming errors
          - Custom exception classes with meaningful messages
       h. Apply framework-specific patterns:
          - Spring Boot: @RestController, @Service, @Repository, @Autowired / constructor injection
          - Quarkus: @Path, @Inject, CDI beans, REST resources
          - Micronaut: @Controller, @Inject, compile-time DI
          - Plain Java: standard SE patterns, main() entry point
       i. Follow KISS/YAGNI — implement only what design specifies

    3. WRITE JUnit 5 tests mapped to AAA scenarios:
       a. FOR EACH AAA scenario group:
          - Create @Nested class with @DisplayName("{scenario group}")
       b. FOR EACH AAA scenario:
          - Create @Test method with @DisplayName("{scenario name}")
          - Arrange → @BeforeEach setup, @Mock stubs (when().thenReturn())
          - Act → method call, MockMvc request, or RestAssured call
          - Assert → one assertion per Assert clause (assertEquals, assertThrows, assertThat)
       c. Use @ExtendWith(MockitoExtension.class) for mock injection
       d. Framework test patterns:
          - Spring Boot: @SpringBootTest, @WebMvcTest, MockMvc
          - Quarkus: @QuarkusTest, RestAssured
          - Micronaut: @MicronautTest, HttpClient
          - Plain Java: plain JUnit 5 + Mockito

    4. RUN tests:
       a. Maven: mvn test -pl {module} (or mvn test if single module)
       b. Gradle: ./gradlew test
       c. Record pass/fail for each Assert clause

    5. RUN linting:
       a. Execute: checkstyle -c google_checks.xml {source_files}
       b. Execute: google-java-format --replace {source_files}
       c. If tools unavailable → log warning, lint_status: "skipped"
       d. Re-run tests after any formatting changes

    6. RETURN standard output
  </action>
  <constraints>
    - CRITICAL: No research step — Java best practices are built into Step 2
    - CRITICAL: Follow existing code conventions found in Step 1
    - MANDATORY: Every AAA Assert clause must map to exactly one test assertion
    - MANDATORY: Use constructor injection (not field injection) for Spring Boot
  </constraints>
  <output>Standard tool skill output (implementation_files, test_files, test_results, lint_status)</output>
</operation>
```

---

## Output Result

```yaml
operation_output:
  success: true | false
  result:
    implementation_files:
      - "{path to created source file 1}"
    test_files:
      - "{path to created test file 1}"
    test_results:
      - scenario: "{scenario name}"
        assert_clause: "{assert text}"
        status: "pass | fail"
        error: "{error message if fail}"
    lint_status: "pass | fail | skipped"
    lint_details: "{details if fail}"
    stack_identified: "Java/{framework}"
  errors: []
```

---

## Definition of Done

```xml
<definition_of_done>
  <checkpoint required="true">
    <name>Build tool and framework detected</name>
    <verification>stack_identified contains "Java/{framework}" in output</verification>
  </checkpoint>
  <checkpoint required="true">
    <name>Implementation files created</name>
    <verification>implementation_files array is non-empty</verification>
  </checkpoint>
  <checkpoint required="true">
    <name>Test files created</name>
    <verification>test_files array is non-empty</verification>
  </checkpoint>
  <checkpoint required="true">
    <name>All AAA Assert clauses mapped to tests</name>
    <verification>test_results count equals total Assert clauses across all scenarios</verification>
  </checkpoint>
  <checkpoint required="true">
    <name>Lint passes or skipped</name>
    <verification>lint_status == "pass" or lint_status == "skipped"</verification>
  </checkpoint>
</definition_of_done>
```

---

## Error Handling

| Error | Cause | Resolution |
|-------|-------|------------|
| `BUILD_TOOL_MISSING` | No pom.xml or build.gradle found in project | Signal orchestrator; cannot proceed without build tool |
| `JAVA_VERSION_INCOMPATIBLE` | Code requires Java version not available | Log warning; attempt with available JDK; signal if incompatible |
| `DEPENDENCY_CONFLICT` | Conflicting dependency versions in build file | Log conflict details; orchestrator handles retry |
| `TEST_FAILURE` | One or more JUnit assertions fail | Return detailed test_results with error messages |
| `LINT_UNAVAILABLE` | Checkstyle or google-java-format not found | Log warning, return lint_status: "skipped", continue |

---

## Examples

See [references/examples.md](.github/skills/x-ipe-tool-implementation-java/references/examples.md) for usage examples.
