Repository navigation
Commit af4adb2
committed
feat(phase-7): generate_pattern tool with 5 parametrised templates
Adds the fourth MCP tool — generate_pattern — which renders a tailored
Java implementation of a chosen design pattern with the user's package
name and type names baked in. 5 patterns wired in this milestone
(Singleton, Builder, Strategy, Observer, Factory Method); the remaining
18 return a clear tool-error pointing the caller to `pattern_examples`.
Generation engine (com.javapatterns.mcp.generate):
- PatternGenerator: holder-idiom singleton. Loads a small
/templates/<slug>/template-index.json per pattern that lists template
files and resolved file names (the index entries may themselves use
placeholders like ${TYPE_NAME}.java). The actual substitution is a
hand-rolled walk over the source so we avoid both regex pitfalls and
the build complexity of pulling in JTE precompiled templates inside
a shaded jar.
- GeneratedFile: result record with fileName + packageName + source.
generate_pattern tool (com.javapatterns.mcp.tools.GeneratePatternTool):
- Required input: `pattern` (case-insensitive, accepts enum names,
slugs and display names) + `typeName` (validated as a Java
identifier; rejects numerics, hyphens, etc.).
- Optional input: `packageName` (validated as a dotted Java package;
empty falls back to the default package, and the template's
${PACKAGE_DECL} placeholder collapses to nothing in that case).
- Output JSON shape mirrors pattern_examples but carries the generated
sources instead of bundled examples:
{ pattern, category, fileCount, files: [{fileName, packageName, source}] }
- Unsupported pattern → isError=true with the supported slug list and a
hint to use `pattern_examples`.
Bundled templates this phase:
- singleton/ → 1 file (Bill-Pugh holder idiom; rejects reflective
re-entry; uses ${TYPE_NAME} for class + holder name).
- builder/ → 1 file (immutable Car-style record with a fluent
static-nested Builder, mandatory ctor arg + 2
optional fields; build() validates).
- strategy/ → 3 files: ${TYPE_NAME}Strategy interface +
Default${TYPE_NAME}Strategy (identity) +
Reverse${TYPE_NAME}Strategy.
- observer/ → 2 files: ${TYPE_NAME}Listener (functional iface)
+ ${TYPE_NAME}Bus pub/sub hub with re-entrant-safe
snapshot dispatch.
- factory-method/ → 3 files: ${TYPE_NAME} product interface +
${TYPE_NAME}Creator abstract creator (template
method + factory method) + Default${TYPE_NAME}Creator
concrete creator with a private inner product.
Wiring:
- JavaPatternsMcpServer.buildServer() now registers 4 tools:
ping, list_patterns, pattern_examples, generate_pattern.
Tests (86 total, all green):
- GeneratePatternToolTest covers metadata, exact rendered placeholders
for Singleton (typeName + package), empty PACKAGE_DECL on default
package, error paths (missing/invalid typeName, unsupported pattern),
and — critically — that every supported pattern produces sources
that compile standalone with javac --release 21. Strategy and
Observer are compiled with typeName=String so the generated generic
bounds stay self-contained.
Live stdio verification:
generate_pattern(pattern="singleton", typeName="Configuration",
packageName="com.demo.config")
returns a 42-line Configuration.java in package com.demo.config that
starts with the correct package declaration and class definition.
Coverage status update — 23 patterns examples + 5 patterns templated.1 parent 6291690 commit af4adb2
20 files changed
Lines changed: 825 additions & 3 deletions
File tree
- src
- main
- java/com/javapatterns/mcp
- generate
- tools
- resources/templates
- builder
- factory-method
- observer
- singleton
- strategy
- test/java/com/javapatterns/mcp/tools
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
30 | 30 | | |
31 | 31 | | |
32 | 32 | | |
33 | | - | |
| 33 | + | |
34 | 34 | | |
35 | 35 | | |
36 | 36 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2 | 2 | | |
3 | 3 | | |
4 | 4 | | |
| 5 | + | |
| 6 | + | |
5 | 7 | | |
6 | 8 | | |
7 | 9 | | |
| |||
82 | 84 | | |
83 | 85 | | |
84 | 86 | | |
85 | | - | |
| 87 | + | |
| 88 | + | |
86 | 89 | | |
87 | 90 | | |
88 | 91 | | |
| |||
94 | 97 | | |
95 | 98 | | |
96 | 99 | | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
97 | 103 | | |
98 | 104 | | |
99 | | - | |
| 105 | + | |
100 | 106 | | |
101 | 107 | | |
102 | 108 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
0 commit comments