|
| 1 | +# AutoCloseableWrapper Refactoring Progress |
| 2 | + |
| 3 | +## Overview |
| 4 | +Applied AutoCloseableWrapper pattern to replace explicit stack popping throughout the Jinjava codebase, following the pattern established in commit 92f9f0a8 for ImportTag. |
| 5 | + |
| 6 | +## Completed Tasks ✅ |
| 7 | + |
| 8 | +### 1. Context Helper Methods |
| 9 | +**File**: `src/main/java/com/hubspot/jinjava/interpret/Context.java` |
| 10 | +- Added `pushCurrentPath()` - wraps getCurrentPathStack().push() with AutoCloseableWrapper |
| 11 | +- Added `pushImportPath()` - wraps getImportPathStack().push() with AutoCloseableWrapper |
| 12 | +- Added `pushIncludePath()` - wraps getIncludePathStack().push() with AutoCloseableWrapper |
| 13 | +- Added `pushFromStackWithWrapper()` - wraps pushFromStack() with AutoCloseableWrapper |
| 14 | +- Added `pushMacroStack()` - wraps getMacroStack().push() with AutoCloseableWrapper |
| 15 | +- Added `withDualStackPush()` - handles dual stack operations for IncludeTag use case |
| 16 | + |
| 17 | +### 2. MacroFunction.java |
| 18 | +**File**: `src/main/java/com/hubspot/jinjava/lib/fn/MacroFunction.java` |
| 19 | +- ✅ **COMPLETED**: Refactored `doEvaluate()` method to use `getImportFileWithWrapper()` |
| 20 | +- Added `getImportFileWithWrapper()` method that returns `AutoCloseableWrapper<Optional<String>>` |
| 21 | +- Replaced manual finally block with try-with-resources |
| 22 | +- Pattern: `try (AutoCloseableWrapper<Optional<String>> importFile = getImportFileWithWrapper(interpreter))` |
| 23 | + |
| 24 | +### 3. EagerMacroFunction.java |
| 25 | +**File**: `src/main/java/com/hubspot/jinjava/lib/fn/eager/EagerMacroFunction.java` |
| 26 | +- ✅ **COMPLETED**: Refactored reconstructing branch in `doEvaluate()` |
| 27 | +- Uses `getImportFileWithWrapper()` from parent MacroFunction class |
| 28 | +- Replaced manual finally block with try-with-resources |
| 29 | + |
| 30 | +### 4. IncludeTag.java |
| 31 | +**File**: `src/main/java/com/hubspot/jinjava/lib/tag/IncludeTag.java` |
| 32 | +- ✅ **COMPLETED**: Refactored dual stack operations (includePathStack + currentPathStack) |
| 33 | +- Kept original exception handling for IncludeTagCycleException outside try-with-resources |
| 34 | +- Used custom AutoCloseableWrapper to pop both stacks in correct order |
| 35 | +- Note: Some diagnostic warnings about lambda variable scope |
| 36 | + |
| 37 | +## Completed Tasks ✅ (Continued) |
| 38 | + |
| 39 | +### 5. FromTag.java |
| 40 | +**File**: `src/main/java/com/hubspot/jinjava/lib/tag/FromTag.java` |
| 41 | +- ✅ **COMPLETED**: Created `getTemplateFileWithWrapper()` method that returns `AutoCloseableWrapper<String>` |
| 42 | +- Added `@Deprecated` to original `getTemplateFile()` method for backwards compatibility |
| 43 | +- Updated `interpret()` method to use try-with-resources pattern |
| 44 | +- Returns `null` from wrapper for cycle exceptions with no-op cleanup |
| 45 | + |
| 46 | +### 6. EagerFromTag.java |
| 47 | +**File**: `src/main/java/com/hubspot/jinjava/lib/tag/eager/EagerFromTag.java` |
| 48 | +- ✅ **COMPLETED**: Refactored to use `FromTag.getTemplateFileWithWrapper()` |
| 49 | +- Replaced manual finally block with try-with-resources |
| 50 | +- Moved DeferredValueException handling to catch block outside try-with-resources |
| 51 | +- Uses same AutoCloseable pattern as parent FromTag |
| 52 | + |
| 53 | +### 7. ImportTag.java (Remaining) |
| 54 | +**File**: `src/main/java/com/hubspot/jinjava/lib/tag/ImportTag.java` |
| 55 | +- ✅ **COMPLETED**: Created `getTemplateFileWithWrapper()` method that wraps import path stack operations |
| 56 | +- Added `@Deprecated` to original `getTemplateFile()` method for backwards compatibility |
| 57 | +- Updated `interpret()` method to use nested try-with-resources (template file + node) |
| 58 | +- Eliminated manual finally block that was popping import path stack |
| 59 | + |
| 60 | +### 8. EagerImportTag.java (Remaining) |
| 61 | +**File**: `src/main/java/com/hubspot/jinjava/lib/tag/eager/EagerImportTag.java` |
| 62 | +- ✅ **COMPLETED**: Refactored to use `ImportTag.getTemplateFileWithWrapper()` |
| 63 | +- Replaced manual finally block with try-with-resources |
| 64 | +- Moved DeferredValueException handling to catch block outside try-with-resources |
| 65 | +- Uses nested try-with-resources for both template file and node parsing |
| 66 | + |
| 67 | +### 9. AstMacroFunction.java |
| 68 | +**File**: `src/main/java/com/hubspot/jinjava/el/ext/AstMacroFunction.java` |
| 69 | +- ✅ **COMPLETED**: Created `checkAndPushMacroStackWithWrapper()` method |
| 70 | +- Returns `AutoCloseableWrapper<Boolean>` where Boolean indicates if early return needed |
| 71 | +- Added `@Deprecated` to original `checkAndPushMacroStack()` method |
| 72 | +- Handles complex macro stack logic (max depth, cycle check, validation mode) |
| 73 | +- Separate code paths for caller vs non-caller macros |
| 74 | + |
| 75 | +### 10. JinjavaInterpreter.java Review |
| 76 | +**File**: `src/main/java/com/hubspot/jinjava/interpret/JinjavaInterpreter.java` |
| 77 | +- ✅ **COMPLETED**: Created `conditionallyPushParentPath()` helper method |
| 78 | +- Refactored conditional push/pop pattern in `resolveBlockStubs()` method |
| 79 | +- Returns `AutoCloseableWrapper<Boolean>` indicating if path was pushed |
| 80 | +- Eliminated manual boolean tracking and conditional pop logic |
| 81 | + |
| 82 | +## Key Patterns Established |
| 83 | + |
| 84 | +### Try-With-Resources Pattern |
| 85 | +```java |
| 86 | +try (AutoCloseableWrapper<T> resource = getResourceWithWrapper(...)) { |
| 87 | + // use resource.get() |
| 88 | +} // automatic cleanup |
| 89 | +``` |
| 90 | + |
| 91 | +### Stack Operation Wrapper Pattern |
| 92 | +```java |
| 93 | +public AutoCloseableWrapper<StackType> pushSomethingWithWrapper(...) { |
| 94 | + stack.push(...); |
| 95 | + return AutoCloseableWrapper.of(stack, StackType::pop); |
| 96 | +} |
| 97 | +``` |
| 98 | + |
| 99 | +### Exception Handling Pattern |
| 100 | +For operations that can throw cycle exceptions, handle outside try-with-resources: |
| 101 | +```java |
| 102 | +try { |
| 103 | + stack.push(...); |
| 104 | +} catch (CycleException e) { |
| 105 | + // handle error, return early |
| 106 | +} |
| 107 | +try (AutoCloseableWrapper<T> wrapper = ...) { |
| 108 | + // main logic |
| 109 | +} |
| 110 | +``` |
| 111 | + |
| 112 | +## Files Modified |
| 113 | +1. `src/main/java/com/hubspot/jinjava/interpret/Context.java` ✅ |
| 114 | +2. `src/main/java/com/hubspot/jinjava/lib/fn/MacroFunction.java` ✅ |
| 115 | +3. `src/main/java/com/hubspot/jinjava/lib/fn/eager/EagerMacroFunction.java` ✅ |
| 116 | +4. `src/main/java/com/hubspot/jinjava/lib/tag/IncludeTag.java` ✅ |
| 117 | +5. `src/main/java/com/hubspot/jinjava/lib/tag/FromTag.java` ✅ |
| 118 | +6. `src/main/java/com/hubspot/jinjava/lib/tag/eager/EagerFromTag.java` ✅ |
| 119 | +7. `src/main/java/com/hubspot/jinjava/lib/tag/ImportTag.java` ✅ |
| 120 | +8. `src/main/java/com/hubspot/jinjava/lib/tag/eager/EagerImportTag.java` ✅ |
| 121 | +9. `src/main/java/com/hubspot/jinjava/el/ext/AstMacroFunction.java` ✅ |
| 122 | +10. `src/main/java/com/hubspot/jinjava/interpret/JinjavaInterpreter.java` ✅ |
| 123 | + |
| 124 | +## Testing Results ✅ |
| 125 | +- **Compilation**: ✅ All files compile successfully |
| 126 | +- **Checkstyle**: ✅ No style violations |
| 127 | +- **FromTag Tests**: ✅ All tests pass |
| 128 | +- **ImportTag Tests**: ✅ All tests pass |
| 129 | +- **Macro Tests**: ✅ All tests pass |
| 130 | +- **No Regressions**: ✅ Verified through targeted testing |
| 131 | + |
| 132 | +## Benefits Achieved |
| 133 | +- Automatic resource cleanup via try-with-resources |
| 134 | +- Elimination of manual finally blocks for stack operations |
| 135 | +- More robust exception handling (stacks auto-pop even on exceptions) |
| 136 | +- Consistent pattern across codebase |
| 137 | +- Reduced chance of forgetting to pop stacks |
0 commit comments