@softspark/ai-toolkit 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/AGENTS.md +412 -0
- package/CHANGELOG.md +68 -0
- package/LICENSE +21 -0
- package/README.md +632 -0
- package/action.yml +53 -0
- package/app/.claude-plugin/plugin.json +44 -0
- package/app/ARCHITECTURE.md +306 -0
- package/app/CLAUDE.md.template +23 -0
- package/app/agents/ai-engineer.md +128 -0
- package/app/agents/backend-specialist.md +193 -0
- package/app/agents/business-intelligence.md +54 -0
- package/app/agents/chaos-monkey.md +67 -0
- package/app/agents/chief-of-staff.md +51 -0
- package/app/agents/code-archaeologist.md +127 -0
- package/app/agents/code-reviewer.md +184 -0
- package/app/agents/command-expert.md +131 -0
- package/app/agents/data-analyst.md +205 -0
- package/app/agents/data-scientist.md +151 -0
- package/app/agents/database-architect.md +317 -0
- package/app/agents/debugger.md +238 -0
- package/app/agents/devops-implementer.md +194 -0
- package/app/agents/documenter.md +364 -0
- package/app/agents/explorer-agent.md +145 -0
- package/app/agents/fact-checker.md +172 -0
- package/app/agents/frontend-specialist.md +209 -0
- package/app/agents/game-developer.md +216 -0
- package/app/agents/incident-responder.md +226 -0
- package/app/agents/infrastructure-architect.md +127 -0
- package/app/agents/infrastructure-validator.md +247 -0
- package/app/agents/llm-ops-engineer.md +237 -0
- package/app/agents/mcp-expert.md +228 -0
- package/app/agents/mcp-server-architect.md +195 -0
- package/app/agents/mcp-testing-engineer.md +292 -0
- package/app/agents/meta-architect.md +58 -0
- package/app/agents/ml-engineer.md +136 -0
- package/app/agents/mobile-developer.md +190 -0
- package/app/agents/night-watchman.md +55 -0
- package/app/agents/nlp-engineer.md +154 -0
- package/app/agents/orchestrator.md +437 -0
- package/app/agents/performance-optimizer.md +254 -0
- package/app/agents/predictive-analyst.md +57 -0
- package/app/agents/product-manager.md +194 -0
- package/app/agents/project-planner.md +287 -0
- package/app/agents/prompt-engineer.md +103 -0
- package/app/agents/qa-automation-engineer.md +182 -0
- package/app/agents/rag-engineer.md +201 -0
- package/app/agents/research-synthesizer.md +138 -0
- package/app/agents/search-specialist.md +101 -0
- package/app/agents/security-architect.md +62 -0
- package/app/agents/security-auditor.md +293 -0
- package/app/agents/seo-specialist.md +111 -0
- package/app/agents/system-governor.md +57 -0
- package/app/agents/tech-lead.md +62 -0
- package/app/agents/technical-researcher.md +103 -0
- package/app/agents/test-engineer.md +264 -0
- package/app/constitution.md +38 -0
- package/app/hooks/_profile-check.sh +11 -0
- package/app/hooks/guard-destructive.sh +74 -0
- package/app/hooks/guard-path.sh +73 -0
- package/app/hooks/post-tool-use.sh +35 -0
- package/app/hooks/pre-compact.sh +31 -0
- package/app/hooks/quality-check.sh +22 -0
- package/app/hooks/quality-gate.sh +49 -0
- package/app/hooks/save-session.sh +24 -0
- package/app/hooks/session-end.sh +37 -0
- package/app/hooks/session-start.sh +29 -0
- package/app/hooks/subagent-start.sh +16 -0
- package/app/hooks/subagent-stop.sh +16 -0
- package/app/hooks/track-usage.sh +50 -0
- package/app/hooks/user-prompt-submit.sh +25 -0
- package/app/hooks.json +178 -0
- package/app/mcp-defaults.json +23 -0
- package/app/output-styles/golden-rules.md +43 -0
- package/app/plugins/README.md +19 -0
- package/app/plugins/csharp-pack/README.md +11 -0
- package/app/plugins/csharp-pack/plugin.json +18 -0
- package/app/plugins/enterprise-pack/README.md +16 -0
- package/app/plugins/enterprise-pack/hooks/output-style.sh +6 -0
- package/app/plugins/enterprise-pack/hooks/status-line.sh +8 -0
- package/app/plugins/enterprise-pack/plugin.json +24 -0
- package/app/plugins/frontend-pack/README.md +14 -0
- package/app/plugins/frontend-pack/plugin.json +22 -0
- package/app/plugins/java-pack/README.md +11 -0
- package/app/plugins/java-pack/plugin.json +18 -0
- package/app/plugins/kotlin-pack/README.md +11 -0
- package/app/plugins/kotlin-pack/plugin.json +18 -0
- package/app/plugins/memory-pack/README.md +24 -0
- package/app/plugins/memory-pack/hooks/observation-capture.sh +67 -0
- package/app/plugins/memory-pack/hooks/session-summary.sh +71 -0
- package/app/plugins/memory-pack/plugin.json +22 -0
- package/app/plugins/memory-pack/scripts/init_db.py +81 -0
- package/app/plugins/memory-pack/scripts/strip_private.py +22 -0
- package/app/plugins/memory-pack/skills/mem-search/SKILL.md +70 -0
- package/app/plugins/research-pack/README.md +14 -0
- package/app/plugins/research-pack/plugin.json +22 -0
- package/app/plugins/ruby-pack/README.md +11 -0
- package/app/plugins/ruby-pack/plugin.json +18 -0
- package/app/plugins/rust-pack/README.md +11 -0
- package/app/plugins/rust-pack/plugin.json +18 -0
- package/app/plugins/security-pack/README.md +15 -0
- package/app/plugins/security-pack/plugin.json +23 -0
- package/app/plugins/swift-pack/README.md +11 -0
- package/app/plugins/swift-pack/plugin.json +18 -0
- package/app/rules/claude-toolkit-rules.md +21 -0
- package/app/rules/git-conventions.md +5 -0
- package/app/rules/quality-gates.md +10 -0
- package/app/skills/_lib/__init__.py +1 -0
- package/app/skills/_lib/detect_utils.py +150 -0
- package/app/skills/agent-creator/SKILL.md +82 -0
- package/app/skills/analyze/SKILL.md +92 -0
- package/app/skills/analyze/scripts/complexity.py +165 -0
- package/app/skills/api-patterns/SKILL.md +305 -0
- package/app/skills/app-builder/SKILL.md +187 -0
- package/app/skills/architecture-audit/SKILL.md +141 -0
- package/app/skills/architecture-decision/SKILL.md +55 -0
- package/app/skills/architecture-decision/templates/adr-template.md +36 -0
- package/app/skills/biz-scan/SKILL.md +30 -0
- package/app/skills/briefing/SKILL.md +27 -0
- package/app/skills/build/SKILL.md +97 -0
- package/app/skills/build/scripts/detect-build.py +151 -0
- package/app/skills/chaos/SKILL.md +32 -0
- package/app/skills/ci/SKILL.md +77 -0
- package/app/skills/ci/scripts/ci-detect.py +135 -0
- package/app/skills/ci/templates/github-actions-node.yml +38 -0
- package/app/skills/ci/templates/github-actions-python.yml +42 -0
- package/app/skills/ci-cd-patterns/SKILL.md +299 -0
- package/app/skills/clean-code/SKILL.md +110 -0
- package/app/skills/clean-code/reference/dart.md +18 -0
- package/app/skills/clean-code/reference/go.md +23 -0
- package/app/skills/clean-code/reference/php.md +32 -0
- package/app/skills/clean-code/reference/python.md +180 -0
- package/app/skills/clean-code/reference/typescript.md +26 -0
- package/app/skills/command-creator/SKILL.md +83 -0
- package/app/skills/commit/SKILL.md +98 -0
- package/app/skills/commit/scripts/pre-commit-check.py +87 -0
- package/app/skills/commit/templates/conventional-commit.md +52 -0
- package/app/skills/csharp-patterns/SKILL.md +450 -0
- package/app/skills/database-patterns/SKILL.md +297 -0
- package/app/skills/debug/SKILL.md +154 -0
- package/app/skills/debug/scripts/error-parser.py +187 -0
- package/app/skills/debugging-tactics/SKILL.md +136 -0
- package/app/skills/deploy/SKILL.md +130 -0
- package/app/skills/deploy/scripts/pre_deploy_check.py +171 -0
- package/app/skills/deploy/templates/deployment-checklist.md +31 -0
- package/app/skills/design-an-interface/SKILL.md +105 -0
- package/app/skills/design-engineering/SKILL.md +260 -0
- package/app/skills/docker-devops/SKILL.md +303 -0
- package/app/skills/docs/SKILL.md +145 -0
- package/app/skills/docs/scripts/doc-inventory.py +176 -0
- package/app/skills/docs/templates/adr-template.md +36 -0
- package/app/skills/docs/templates/readme-template.md +67 -0
- package/app/skills/documentation-standards/SKILL.md +191 -0
- package/app/skills/ecommerce-patterns/SKILL.md +209 -0
- package/app/skills/evaluate/SKILL.md +132 -0
- package/app/skills/evolve/SKILL.md +27 -0
- package/app/skills/explain/SKILL.md +54 -0
- package/app/skills/explain/scripts/dependency-graph.py +215 -0
- package/app/skills/explore/SKILL.md +112 -0
- package/app/skills/explore/scripts/visualize.py +117 -0
- package/app/skills/fix/SKILL.md +78 -0
- package/app/skills/fix/scripts/error-classifier.py +191 -0
- package/app/skills/flutter-patterns/SKILL.md +254 -0
- package/app/skills/git-mastery/SKILL.md +70 -0
- package/app/skills/grill-me/SKILL.md +38 -0
- package/app/skills/health/SKILL.md +91 -0
- package/app/skills/health/scripts/health_check.py +162 -0
- package/app/skills/hive-mind/SKILL.md +56 -0
- package/app/skills/hook-creator/SKILL.md +107 -0
- package/app/skills/index/SKILL.md +74 -0
- package/app/skills/instinct-review/SKILL.md +77 -0
- package/app/skills/java-patterns/SKILL.md +442 -0
- package/app/skills/kotlin-patterns/SKILL.md +446 -0
- package/app/skills/lint/SKILL.md +103 -0
- package/app/skills/lint/scripts/detect-linters.py +112 -0
- package/app/skills/mcp-patterns/SKILL.md +270 -0
- package/app/skills/mem-search/SKILL.md +70 -0
- package/app/skills/migrate/SKILL.md +90 -0
- package/app/skills/migrate/scripts/migration-status.py +195 -0
- package/app/skills/migration-patterns/SKILL.md +260 -0
- package/app/skills/night-watch/SKILL.md +28 -0
- package/app/skills/observability-patterns/SKILL.md +203 -0
- package/app/skills/onboard/SKILL.md +76 -0
- package/app/skills/orchestrate/SKILL.md +86 -0
- package/app/skills/panic/SKILL.md +30 -0
- package/app/skills/performance-profiling/SKILL.md +59 -0
- package/app/skills/plan/SKILL.md +110 -0
- package/app/skills/plan/templates/plan-template.md +40 -0
- package/app/skills/plan-writing/SKILL.md +201 -0
- package/app/skills/plugin-creator/SKILL.md +78 -0
- package/app/skills/pr/SKILL.md +129 -0
- package/app/skills/pr/scripts/pr-summary.py +175 -0
- package/app/skills/prd-to-issues/SKILL.md +108 -0
- package/app/skills/prd-to-plan/SKILL.md +120 -0
- package/app/skills/predict/SKILL.md +30 -0
- package/app/skills/qa-session/SKILL.md +110 -0
- package/app/skills/rag-patterns/SKILL.md +203 -0
- package/app/skills/refactor/SKILL.md +124 -0
- package/app/skills/refactor/scripts/refactor-scan.py +210 -0
- package/app/skills/refactor-plan/SKILL.md +112 -0
- package/app/skills/repeat/SKILL.md +149 -0
- package/app/skills/research-mastery/SKILL.md +56 -0
- package/app/skills/review/SKILL.md +141 -0
- package/app/skills/review/scripts/diff-analyzer.py +170 -0
- package/app/skills/rollback/SKILL.md +87 -0
- package/app/skills/rollback/scripts/rollback_info.py +149 -0
- package/app/skills/ruby-patterns/SKILL.md +454 -0
- package/app/skills/rust-patterns/SKILL.md +446 -0
- package/app/skills/search/SKILL.md +64 -0
- package/app/skills/security-patterns/SKILL.md +91 -0
- package/app/skills/security-patterns/reference/authentication.md +37 -0
- package/app/skills/security-patterns/reference/authorization.md +22 -0
- package/app/skills/security-patterns/reference/input-validation.md +30 -0
- package/app/skills/security-patterns/reference/oauth-csrf-audit.md +131 -0
- package/app/skills/skill-creator/SKILL.md +154 -0
- package/app/skills/skill-creator/templates/dashboard/index.html +130 -0
- package/app/skills/skill-creator/templates/reasoning-engine/assets/example.json +12 -0
- package/app/skills/skill-creator/templates/reasoning-engine/search.py +110 -0
- package/app/skills/subagent-development/SKILL.md +225 -0
- package/app/skills/subagent-development/reference/code-quality-reviewer-prompt.md +145 -0
- package/app/skills/subagent-development/reference/implementer-prompt.md +118 -0
- package/app/skills/subagent-development/reference/spec-reviewer-prompt.md +100 -0
- package/app/skills/swarm/SKILL.md +81 -0
- package/app/skills/swift-patterns/SKILL.md +500 -0
- package/app/skills/tdd/SKILL.md +174 -0
- package/app/skills/tdd/reference/deep-modules.md +32 -0
- package/app/skills/tdd/reference/interface-design.md +32 -0
- package/app/skills/tdd/reference/mocking.md +52 -0
- package/app/skills/tdd/reference/refactoring.md +10 -0
- package/app/skills/tdd/reference/tests.md +59 -0
- package/app/skills/teams/SKILL.md +101 -0
- package/app/skills/test/SKILL.md +107 -0
- package/app/skills/test/scripts/detect-runner.py +113 -0
- package/app/skills/testing-patterns/SKILL.md +73 -0
- package/app/skills/testing-patterns/reference/flutter-testing.md +33 -0
- package/app/skills/testing-patterns/reference/go-testing.md +52 -0
- package/app/skills/testing-patterns/reference/php-phpunit.md +39 -0
- package/app/skills/testing-patterns/reference/python-pytest.md +228 -0
- package/app/skills/testing-patterns/reference/typescript-vitest.md +50 -0
- package/app/skills/triage-issue/SKILL.md +120 -0
- package/app/skills/typescript-patterns/SKILL.md +256 -0
- package/app/skills/ubiquitous-language/SKILL.md +74 -0
- package/app/skills/verification-before-completion/SKILL.md +108 -0
- package/app/skills/workflow/SKILL.md +250 -0
- package/app/skills/write-a-prd/SKILL.md +129 -0
- package/app/skills/write-a-prd/reference/visual-companion.md +78 -0
- package/app/skills/write-a-prd/scripts/frame-template.html +111 -0
- package/app/skills/write-a-prd/scripts/visual-server.cjs +79 -0
- package/app/templates/skill/generator/SKILL.md.template +40 -0
- package/app/templates/skill/knowledge/SKILL.md.template +52 -0
- package/app/templates/skill/linter/SKILL.md.template +34 -0
- package/app/templates/skill/reviewer/SKILL.md.template +51 -0
- package/app/templates/skill/workflow/SKILL.md.template +49 -0
- package/benchmarks/README.md +111 -0
- package/benchmarks/ecosystem-dashboard.json +148 -0
- package/benchmarks/ecosystem-harvest.json +148 -0
- package/benchmarks/results.json +38 -0
- package/benchmarks/run.py +351 -0
- package/bin/ai-toolkit.js +345 -0
- package/kb/best-practices/README.md +11 -0
- package/kb/howto/README.md +11 -0
- package/kb/procedures/maintenance-sop.md +306 -0
- package/kb/reference/agents-catalog.md +124 -0
- package/kb/reference/anti-pattern-registry-format.md +221 -0
- package/kb/reference/architecture-overview.md +232 -0
- package/kb/reference/benchmark-config.md +62 -0
- package/kb/reference/ci-integration.md +66 -0
- package/kb/reference/claude-ecosystem-benchmark-snapshot.md +80 -0
- package/kb/reference/claude-ecosystem-expansion-foundations.md +102 -0
- package/kb/reference/commands-catalog.md +21 -0
- package/kb/reference/distribution-model.md +63 -0
- package/kb/reference/global-install-model.md +56 -0
- package/kb/reference/hierarchical-override-pattern.md +200 -0
- package/kb/reference/hooks-catalog.md +306 -0
- package/kb/reference/integrations.md +88 -0
- package/kb/reference/language-packs.md +52 -0
- package/kb/reference/merge-friendly-install-model.md +58 -0
- package/kb/reference/plugin-pack-conventions.md +151 -0
- package/kb/reference/quick-wins-implementation-summary.md +70 -0
- package/kb/reference/skill-templates.md +50 -0
- package/kb/reference/skills-catalog.md +215 -0
- package/kb/reference/skills-unification.md +57 -0
- package/kb/reference/stats.md +69 -0
- package/kb/reference/sync.md +76 -0
- package/kb/troubleshooting/README.md +11 -0
- package/llms-full.txt +3068 -0
- package/llms.txt +39 -0
- package/package.json +75 -0
- package/scripts/_common.py +160 -0
- package/scripts/add_rule.py +50 -0
- package/scripts/benchmark_config.py +127 -0
- package/scripts/benchmark_ecosystem.py +288 -0
- package/scripts/check_deps.py +260 -0
- package/scripts/create_skill.py +118 -0
- package/scripts/doctor.py +504 -0
- package/scripts/eject.py +113 -0
- package/scripts/emission.py +256 -0
- package/scripts/evaluate_skills.py +260 -0
- package/scripts/frontmatter.py +58 -0
- package/scripts/generate_agents_md.py +91 -0
- package/scripts/generate_aider_conf.py +51 -0
- package/scripts/generate_cline.py +35 -0
- package/scripts/generate_copilot.py +30 -0
- package/scripts/generate_cursor_rules.py +35 -0
- package/scripts/generate_gemini.py +28 -0
- package/scripts/generate_llms_txt.py +164 -0
- package/scripts/generate_roo_modes.py +80 -0
- package/scripts/generate_windsurf.py +35 -0
- package/scripts/generator_base.py +140 -0
- package/scripts/harvest_ecosystem.py +50 -0
- package/scripts/inject_rule_cli.py +101 -0
- package/scripts/inject_section_cli.py +47 -0
- package/scripts/injection.py +180 -0
- package/scripts/install.py +236 -0
- package/scripts/install_git_hooks.py +71 -0
- package/scripts/install_steps/__init__.py +5 -0
- package/scripts/install_steps/ai_tools.py +261 -0
- package/scripts/install_steps/hooks.py +90 -0
- package/scripts/install_steps/markers.py +79 -0
- package/scripts/install_steps/symlinks.py +87 -0
- package/scripts/merge-hooks.py +192 -0
- package/scripts/plugin.py +642 -0
- package/scripts/plugin_schema.py +138 -0
- package/scripts/remove_rule.py +58 -0
- package/scripts/stats.py +81 -0
- package/scripts/sync.py +215 -0
- package/scripts/uninstall.py +292 -0
- package/scripts/validate.py +700 -0
|
@@ -0,0 +1,442 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: java-patterns
|
|
3
|
+
description: "Loaded when user asks about Java development patterns"
|
|
4
|
+
effort: medium
|
|
5
|
+
user-invocable: false
|
|
6
|
+
---
|
|
7
|
+
|
|
8
|
+
# Java Patterns Skill
|
|
9
|
+
|
|
10
|
+
## Project Structure
|
|
11
|
+
|
|
12
|
+
### Maven / Gradle Standard Layout
|
|
13
|
+
|
|
14
|
+
```
|
|
15
|
+
my-app/
|
|
16
|
+
├── pom.xml (or build.gradle.kts + settings.gradle.kts)
|
|
17
|
+
├── src/
|
|
18
|
+
│ ├── main/
|
|
19
|
+
│ │ ├── java/com/example/myapp/
|
|
20
|
+
│ │ │ ├── MyApplication.java
|
|
21
|
+
│ │ │ ├── config/
|
|
22
|
+
│ │ │ ├── controller/
|
|
23
|
+
│ │ │ ├── service/
|
|
24
|
+
│ │ │ ├── repository/
|
|
25
|
+
│ │ │ ├── model/
|
|
26
|
+
│ │ │ │ ├── entity/
|
|
27
|
+
│ │ │ │ └── dto/
|
|
28
|
+
│ │ │ └── exception/
|
|
29
|
+
│ │ └── resources/
|
|
30
|
+
│ │ ├── application.yml
|
|
31
|
+
│ │ └── db/migration/
|
|
32
|
+
│ └── test/
|
|
33
|
+
│ ├── java/com/example/myapp/
|
|
34
|
+
│ └── resources/application-test.yml
|
|
35
|
+
└── target/ (or build/)
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
### Multi-Module
|
|
39
|
+
|
|
40
|
+
```
|
|
41
|
+
parent/
|
|
42
|
+
├── pom.xml (packaging=pom)
|
|
43
|
+
├── common/ (shared utilities)
|
|
44
|
+
├── domain/ (entities, business rules)
|
|
45
|
+
├── api/ (REST controllers, DTOs)
|
|
46
|
+
└── app/ (Spring Boot main, wiring)
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
---
|
|
50
|
+
|
|
51
|
+
## Idioms / Code Style
|
|
52
|
+
|
|
53
|
+
### Records (Java 16+)
|
|
54
|
+
```java
|
|
55
|
+
public record UserDto(Long id, String name, String email) {
|
|
56
|
+
public UserDto { // compact constructor for validation
|
|
57
|
+
Objects.requireNonNull(name, "name must not be null");
|
|
58
|
+
Objects.requireNonNull(email, "email must not be null");
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
### Sealed Classes (Java 17+)
|
|
64
|
+
```java
|
|
65
|
+
public sealed interface Shape permits Circle, Rectangle, Triangle {
|
|
66
|
+
double area();
|
|
67
|
+
}
|
|
68
|
+
public record Circle(double radius) implements Shape {
|
|
69
|
+
public double area() { return Math.PI * radius * radius; }
|
|
70
|
+
}
|
|
71
|
+
public record Rectangle(double w, double h) implements Shape {
|
|
72
|
+
public double area() { return w * h; }
|
|
73
|
+
}
|
|
74
|
+
public record Triangle(double base, double height) implements Shape {
|
|
75
|
+
public double area() { return 0.5 * base * height; }
|
|
76
|
+
}
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
### Switch Expressions (Java 14+)
|
|
80
|
+
```java
|
|
81
|
+
String describe(Shape shape) {
|
|
82
|
+
return switch (shape) {
|
|
83
|
+
case Circle c -> "Circle r=" + c.radius();
|
|
84
|
+
case Rectangle r -> "Rect %sx%s".formatted(r.w(), r.h());
|
|
85
|
+
case Triangle t -> "Triangle base=" + t.base();
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
// Guard patterns (Java 21+)
|
|
89
|
+
String classify(Shape shape) {
|
|
90
|
+
return switch (shape) {
|
|
91
|
+
case Circle c when c.radius() > 100 -> "large circle";
|
|
92
|
+
case Circle c -> "small circle";
|
|
93
|
+
case Rectangle r -> "rectangle";
|
|
94
|
+
case Triangle t -> "triangle";
|
|
95
|
+
};
|
|
96
|
+
}
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
### var, Streams, Optional
|
|
100
|
+
```java
|
|
101
|
+
// var -- use when RHS makes type obvious
|
|
102
|
+
var users = new ArrayList<User>();
|
|
103
|
+
var response = client.send(request, HttpResponse.BodyHandlers.ofString());
|
|
104
|
+
// Avoid: var result = service.process(data); -- type unclear
|
|
105
|
+
|
|
106
|
+
// Streams
|
|
107
|
+
List<String> names = users.stream()
|
|
108
|
+
.filter(User::isActive)
|
|
109
|
+
.map(User::name)
|
|
110
|
+
.sorted()
|
|
111
|
+
.toList(); // Java 16+, unmodifiable
|
|
112
|
+
|
|
113
|
+
Map<Department, List<User>> byDept = users.stream()
|
|
114
|
+
.collect(Collectors.groupingBy(User::department));
|
|
115
|
+
|
|
116
|
+
// Optional -- return type only, never as field or parameter
|
|
117
|
+
String city = findByEmail(email)
|
|
118
|
+
.map(User::address)
|
|
119
|
+
.map(Address::city)
|
|
120
|
+
.orElse("Unknown");
|
|
121
|
+
// Never call .get() without guard -- use orElse/orElseThrow
|
|
122
|
+
|
|
123
|
+
// Text blocks (Java 15+)
|
|
124
|
+
String json = """
|
|
125
|
+
{"name": "%s", "email": "%s"}
|
|
126
|
+
""".formatted(name, email);
|
|
127
|
+
```
|
|
128
|
+
|
|
129
|
+
---
|
|
130
|
+
|
|
131
|
+
## Error Handling
|
|
132
|
+
|
|
133
|
+
| Type | When | Examples |
|
|
134
|
+
|------|------|---------|
|
|
135
|
+
| Checked | Recoverable I/O the caller must handle | IOException, SQLException |
|
|
136
|
+
| Unchecked | Programming errors, business rule violations | IllegalArgumentException, custom domain exceptions |
|
|
137
|
+
|
|
138
|
+
### Custom Exception Hierarchy
|
|
139
|
+
```java
|
|
140
|
+
public abstract class DomainException extends RuntimeException {
|
|
141
|
+
private final String errorCode;
|
|
142
|
+
protected DomainException(String errorCode, String message) {
|
|
143
|
+
super(message);
|
|
144
|
+
this.errorCode = errorCode;
|
|
145
|
+
}
|
|
146
|
+
public String errorCode() { return errorCode; }
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
public class EntityNotFoundException extends DomainException {
|
|
150
|
+
public EntityNotFoundException(String entity, Object id) {
|
|
151
|
+
super("NOT_FOUND", "%s with id %s not found".formatted(entity, id));
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
```
|
|
155
|
+
|
|
156
|
+
### Try-With-Resources
|
|
157
|
+
```java
|
|
158
|
+
try (var conn = dataSource.getConnection();
|
|
159
|
+
var stmt = conn.prepareStatement(sql);
|
|
160
|
+
var rs = stmt.executeQuery()) {
|
|
161
|
+
while (rs.next()) { results.add(mapRow(rs)); }
|
|
162
|
+
}
|
|
163
|
+
```
|
|
164
|
+
|
|
165
|
+
### Global Handler (Spring)
|
|
166
|
+
```java
|
|
167
|
+
@RestControllerAdvice
|
|
168
|
+
public class GlobalExceptionHandler {
|
|
169
|
+
@ExceptionHandler(EntityNotFoundException.class)
|
|
170
|
+
public ResponseEntity<ErrorResponse> handleNotFound(EntityNotFoundException ex) {
|
|
171
|
+
return ResponseEntity.status(404).body(new ErrorResponse(ex.errorCode(), ex.getMessage()));
|
|
172
|
+
}
|
|
173
|
+
@ExceptionHandler(MethodArgumentNotValidException.class)
|
|
174
|
+
public ResponseEntity<ErrorResponse> handleValidation(MethodArgumentNotValidException ex) {
|
|
175
|
+
var errors = ex.getBindingResult().getFieldErrors().stream()
|
|
176
|
+
.map(e -> e.getField() + ": " + e.getDefaultMessage()).toList();
|
|
177
|
+
return ResponseEntity.badRequest().body(new ErrorResponse("VALIDATION_ERROR", errors.toString()));
|
|
178
|
+
}
|
|
179
|
+
@ExceptionHandler(Exception.class)
|
|
180
|
+
public ResponseEntity<ErrorResponse> handleGeneric(Exception ex) {
|
|
181
|
+
log.error("Unhandled exception", ex);
|
|
182
|
+
return ResponseEntity.internalServerError()
|
|
183
|
+
.body(new ErrorResponse("INTERNAL_ERROR", "An unexpected error occurred"));
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
public record ErrorResponse(String code, String message) {}
|
|
187
|
+
```
|
|
188
|
+
|
|
189
|
+
---
|
|
190
|
+
|
|
191
|
+
## Testing Patterns
|
|
192
|
+
|
|
193
|
+
### JUnit 5 + Mockito + AssertJ
|
|
194
|
+
```java
|
|
195
|
+
@DisplayName("UserService")
|
|
196
|
+
class UserServiceTest {
|
|
197
|
+
private UserRepository repository;
|
|
198
|
+
private UserService service;
|
|
199
|
+
|
|
200
|
+
@BeforeEach
|
|
201
|
+
void setUp() {
|
|
202
|
+
repository = mock(UserRepository.class);
|
|
203
|
+
service = new UserService(repository);
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
@Test
|
|
207
|
+
void createsUserWithValidData() {
|
|
208
|
+
var request = new CreateUserRequest("Alice", "alice@example.com");
|
|
209
|
+
when(repository.save(any())).thenAnswer(inv -> inv.getArgument(0));
|
|
210
|
+
|
|
211
|
+
var user = service.createUser(request);
|
|
212
|
+
|
|
213
|
+
assertThat(user.name()).isEqualTo("Alice");
|
|
214
|
+
verify(repository).save(any(User.class));
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
@Test
|
|
218
|
+
void throwsWhenEmailExists() {
|
|
219
|
+
when(repository.existsByEmail("taken@test.com")).thenReturn(true);
|
|
220
|
+
assertThatThrownBy(() -> service.createUser(new CreateUserRequest("Bob", "taken@test.com")))
|
|
221
|
+
.isInstanceOf(BusinessRuleViolationException.class)
|
|
222
|
+
.hasMessageContaining("email already exists");
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
```
|
|
226
|
+
|
|
227
|
+
### Parameterized Tests
|
|
228
|
+
```java
|
|
229
|
+
@ParameterizedTest
|
|
230
|
+
@CsvSource({"1,1,2", "0,0,0", "-1,1,0", "100,200,300"})
|
|
231
|
+
void addReturnsSumOfArguments(int a, int b, int expected) {
|
|
232
|
+
assertThat(calculator.add(a, b)).isEqualTo(expected);
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
@ParameterizedTest
|
|
236
|
+
@MethodSource("invalidEmails")
|
|
237
|
+
void rejectsInvalidEmail(String email) {
|
|
238
|
+
assertThatThrownBy(() -> new Email(email)).isInstanceOf(IllegalArgumentException.class);
|
|
239
|
+
}
|
|
240
|
+
static Stream<String> invalidEmails() {
|
|
241
|
+
return Stream.of("", "no-at-sign", "@no-local", "spaces in@email.com");
|
|
242
|
+
}
|
|
243
|
+
```
|
|
244
|
+
|
|
245
|
+
### Mockito Extras
|
|
246
|
+
```java
|
|
247
|
+
// Argument captor
|
|
248
|
+
var captor = ArgumentCaptor.forClass(User.class);
|
|
249
|
+
verify(repository).save(captor.capture());
|
|
250
|
+
assertThat(captor.getValue().name()).isEqualTo("Alice");
|
|
251
|
+
|
|
252
|
+
// BDD style
|
|
253
|
+
given(repository.findById(1L)).willReturn(Optional.of(user));
|
|
254
|
+
then(repository).should().findById(1L);
|
|
255
|
+
```
|
|
256
|
+
|
|
257
|
+
### Testcontainers (Integration)
|
|
258
|
+
```java
|
|
259
|
+
@Testcontainers
|
|
260
|
+
@SpringBootTest
|
|
261
|
+
class UserRepositoryIT {
|
|
262
|
+
@Container
|
|
263
|
+
static PostgreSQLContainer<?> pg = new PostgreSQLContainer<>("postgres:16-alpine");
|
|
264
|
+
|
|
265
|
+
@DynamicPropertySource
|
|
266
|
+
static void props(DynamicPropertyRegistry r) {
|
|
267
|
+
r.add("spring.datasource.url", pg::getJdbcUrl);
|
|
268
|
+
r.add("spring.datasource.username", pg::getUsername);
|
|
269
|
+
r.add("spring.datasource.password", pg::getPassword);
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
@Autowired UserRepository repository;
|
|
273
|
+
|
|
274
|
+
@Test
|
|
275
|
+
void savesAndRetrievesUser() {
|
|
276
|
+
var saved = repository.save(new User("Alice", "alice@test.com"));
|
|
277
|
+
assertThat(repository.findById(saved.getId())).isPresent()
|
|
278
|
+
.get().extracting("name").isEqualTo("Alice");
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
```
|
|
282
|
+
|
|
283
|
+
---
|
|
284
|
+
|
|
285
|
+
## Common Frameworks
|
|
286
|
+
|
|
287
|
+
### Spring Boot (Controller / Service / Repository)
|
|
288
|
+
```java
|
|
289
|
+
@RestController
|
|
290
|
+
@RequestMapping("/api/v1/users")
|
|
291
|
+
@RequiredArgsConstructor
|
|
292
|
+
public class UserController {
|
|
293
|
+
private final UserService userService;
|
|
294
|
+
|
|
295
|
+
@GetMapping
|
|
296
|
+
public List<UserDto> list(@RequestParam(defaultValue = "0") int page,
|
|
297
|
+
@RequestParam(defaultValue = "20") int size) {
|
|
298
|
+
return userService.list(PageRequest.of(page, size));
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
@PostMapping @ResponseStatus(HttpStatus.CREATED)
|
|
302
|
+
public UserDto create(@Valid @RequestBody CreateUserRequest request) {
|
|
303
|
+
return userService.create(request);
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
@Service @Transactional(readOnly = true) @RequiredArgsConstructor
|
|
308
|
+
public class UserService {
|
|
309
|
+
private final UserRepository repository;
|
|
310
|
+
private final UserMapper mapper;
|
|
311
|
+
|
|
312
|
+
@Transactional
|
|
313
|
+
public UserDto create(CreateUserRequest request) {
|
|
314
|
+
if (repository.existsByEmail(request.email()))
|
|
315
|
+
throw new BusinessRuleViolationException("email already exists");
|
|
316
|
+
return mapper.toDto(repository.save(mapper.toEntity(request)));
|
|
317
|
+
}
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
public interface UserRepository extends JpaRepository<User, Long> {
|
|
321
|
+
boolean existsByEmail(String email);
|
|
322
|
+
@Query("SELECT u FROM User u WHERE u.active = true AND u.role = :role")
|
|
323
|
+
List<User> findActiveByRole(@Param("role") Role role);
|
|
324
|
+
}
|
|
325
|
+
```
|
|
326
|
+
|
|
327
|
+
### Quarkus
|
|
328
|
+
Uses Jakarta REST (`@Path`, `@GET`, `@POST`) + CDI (`@Inject`, `@ApplicationScoped`). Panache simplifies JPA: `implements PanacheRepository<User>` gives `find()`, `persist()`, `list()` out of the box. Use `@Transactional` on mutating endpoints.
|
|
329
|
+
|
|
330
|
+
### Jackson, Lombok, MapStruct
|
|
331
|
+
```java
|
|
332
|
+
// Jackson -- snake_case response
|
|
333
|
+
@JsonNaming(PropertyNamingStrategies.SnakeCaseStrategy.class)
|
|
334
|
+
public record ApiResponse<T>(T data, @JsonInclude(Include.NON_NULL) String error) {}
|
|
335
|
+
|
|
336
|
+
// Lombok -- JPA entities only (use records for DTOs)
|
|
337
|
+
@Entity @Getter @Setter @NoArgsConstructor(access = PROTECTED)
|
|
338
|
+
@Builder @ToString(exclude = "password") @EqualsAndHashCode(of = "id")
|
|
339
|
+
public class User {
|
|
340
|
+
@Id @GeneratedValue(strategy = IDENTITY) private Long id;
|
|
341
|
+
private String name;
|
|
342
|
+
private String email;
|
|
343
|
+
private String password;
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
// MapStruct
|
|
347
|
+
@Mapper(componentModel = "spring")
|
|
348
|
+
public interface UserMapper {
|
|
349
|
+
UserDto toDto(User entity);
|
|
350
|
+
User toEntity(CreateUserRequest request);
|
|
351
|
+
}
|
|
352
|
+
```
|
|
353
|
+
|
|
354
|
+
---
|
|
355
|
+
|
|
356
|
+
## Performance Tips
|
|
357
|
+
|
|
358
|
+
### JVM Tuning
|
|
359
|
+
```bash
|
|
360
|
+
# Container-friendly (Java 17+)
|
|
361
|
+
java -XX:+UseContainerSupport -XX:MaxRAMPercentage=75.0 -jar app.jar
|
|
362
|
+
|
|
363
|
+
# GC: -XX:+UseZGC (low latency) | -XX:+UseG1GC (balanced, default) | -XX:+UseParallelGC (throughput)
|
|
364
|
+
```
|
|
365
|
+
|
|
366
|
+
### Virtual Threads (Java 21+)
|
|
367
|
+
```java
|
|
368
|
+
try (var executor = Executors.newVirtualThreadPerTaskExecutor()) {
|
|
369
|
+
var futures = urls.stream().map(url -> executor.submit(() -> fetch(url))).toList();
|
|
370
|
+
var results = futures.stream().map(f -> { try { return f.get(); }
|
|
371
|
+
catch (Exception e) { throw new RuntimeException(e); } }).toList();
|
|
372
|
+
}
|
|
373
|
+
// Spring Boot 3.2+: spring.threads.virtual.enabled=true
|
|
374
|
+
```
|
|
375
|
+
|
|
376
|
+
### Profiling with JFR
|
|
377
|
+
```bash
|
|
378
|
+
java -XX:StartFlightRecording=duration=60s,filename=profile.jfr -jar app.jar
|
|
379
|
+
jfr print --events jdk.CPULoad,jdk.GCHeapSummary profile.jfr
|
|
380
|
+
```
|
|
381
|
+
|
|
382
|
+
### Common Pitfalls
|
|
383
|
+
|
|
384
|
+
| Pitfall | Fix |
|
|
385
|
+
|---------|-----|
|
|
386
|
+
| String concat in loop | `StringBuilder` or `String.join()` |
|
|
387
|
+
| Unbounded caches | `Caffeine` with `maximumSize` + `expireAfterWrite` |
|
|
388
|
+
| Autoboxing in hot path | Primitive streams (`mapToInt`) or primitive collections |
|
|
389
|
+
| Synchronized everything | `ConcurrentHashMap`, `StampedLock`, virtual threads |
|
|
390
|
+
| Reflection in tight loop | Cache `MethodHandle` or use code generation |
|
|
391
|
+
|
|
392
|
+
---
|
|
393
|
+
|
|
394
|
+
## Build / Package Management
|
|
395
|
+
|
|
396
|
+
### Maven (key elements)
|
|
397
|
+
```xml
|
|
398
|
+
<properties>
|
|
399
|
+
<java.version>21</java.version>
|
|
400
|
+
</properties>
|
|
401
|
+
<parent>
|
|
402
|
+
<groupId>org.springframework.boot</groupId>
|
|
403
|
+
<artifactId>spring-boot-starter-parent</artifactId>
|
|
404
|
+
<version>3.3.0</version>
|
|
405
|
+
</parent>
|
|
406
|
+
<!-- Use dependencyManagement + BOM for non-Boot projects -->
|
|
407
|
+
```
|
|
408
|
+
|
|
409
|
+
### Gradle Kotlin DSL
|
|
410
|
+
```kotlin
|
|
411
|
+
plugins { java; id("org.springframework.boot") version "3.3.0" }
|
|
412
|
+
java { toolchain { languageVersion = JavaLanguageVersion.of(21) } }
|
|
413
|
+
dependencies {
|
|
414
|
+
implementation("org.springframework.boot:spring-boot-starter-web")
|
|
415
|
+
testImplementation("org.springframework.boot:spring-boot-starter-test")
|
|
416
|
+
}
|
|
417
|
+
tasks.test { useJUnitPlatform() }
|
|
418
|
+
```
|
|
419
|
+
|
|
420
|
+
### Dependency Management
|
|
421
|
+
|
|
422
|
+
| Concern | Approach |
|
|
423
|
+
|---------|----------|
|
|
424
|
+
| Version alignment | BOM import via `dependencyManagement` / `platform()` |
|
|
425
|
+
| Vulnerability scan | OWASP plugin (`dependencyCheckAnalyze`) |
|
|
426
|
+
| Unused deps | `mvn dependency:analyze` or Gradle `dependency-analysis` plugin |
|
|
427
|
+
| Reproducible builds | Pin plugin versions, `maven-enforcer-plugin` |
|
|
428
|
+
|
|
429
|
+
---
|
|
430
|
+
|
|
431
|
+
## Anti-Patterns
|
|
432
|
+
|
|
433
|
+
| Anti-Pattern | Problem | Solution |
|
|
434
|
+
|--------------|---------|----------|
|
|
435
|
+
| God service class | 1000+ lines | Split by domain concern |
|
|
436
|
+
| Anemic domain model | Logic only in services | Put behavior on domain objects |
|
|
437
|
+
| Catching `Exception` | Hides bugs | Catch specific types |
|
|
438
|
+
| `@Autowired` on fields | Hidden deps, untestable | Constructor injection |
|
|
439
|
+
| Mutable DTOs | Thread-safety issues | Use records |
|
|
440
|
+
| Raw JDBC everywhere | Injection risk, boilerplate | JPA/jOOQ with parameterized queries |
|
|
441
|
+
| Missing `@Transactional` | Inconsistent data | Annotate service methods |
|
|
442
|
+
| N+1 queries | Performance death | `JOIN FETCH` or `@EntityGraph` |
|