$npx -y skills add ducpm2303/claude-java-plugins --skill java-refactorSuggests and applies version-gated Java refactorings. Use when user asks to "refactor this", "clean up this code", "modernize this Java", "simplify this", "extract method", "improve this class", or "make this more idiomatic".
| 1 | Refactor the Java code I've selected or provided. Follow these steps: |
| 2 | |
| 3 | ## Step 1 — Detect Java version |
| 4 | Check `pom.xml` (`<java.version>` or `<maven.compiler.source>`) or `build.gradle` (`sourceCompatibility`). If not found, ask: "What Java version are you targeting? (8, 11, 17, 21, or other)" |
| 5 | |
| 6 | ## Step 2 — Apply safe refactorings (all Java versions) |
| 7 | These refactorings are safe regardless of Java version: |
| 8 | - **Extract method:** if a method is longer than 20 lines or has a distinct sub-step, extract it with a descriptive name |
| 9 | - **Rename for clarity:** rename variables, methods, or classes with unclear names |
| 10 | - **Remove duplication:** identify repeated logic blocks and extract to a shared method |
| 11 | - **Replace magic literals:** replace inline numbers/strings with named `static final` constants |
| 12 | - **Simplify conditionals:** replace nested if/else chains with early returns or ternary where clearer |
| 13 | - **Remove dead code:** delete unreachable branches, unused variables, unused imports |
| 14 | |
| 15 | ## Step 3 — Apply version-gated refactorings (only if target version supports it) |
| 16 | State the minimum Java version required for each suggestion: |
| 17 | - **Java 8+ — Replace anonymous classes with lambdas:** e.g., `new Comparator<String>() { ... }` → `(a, b) -> a.compareTo(b)` |
| 18 | - **Java 8+ — Replace imperative loops with streams:** e.g., for-each + list.add → `list.stream().filter(...).collect(Collectors.toList())` |
| 19 | - **Java 8+ — Use Optional instead of null returns:** e.g., `return null` → `return Optional.empty()` |
| 20 | - **Java 9+ — Use List.of / Map.of:** e.g., `new ArrayList<>(Arrays.asList(...))` → `List.of(...)` |
| 21 | - **Java 10+ — Use var for local variables:** where the type is obvious from the right-hand side |
| 22 | - **Java 16+ — Replace data classes with records:** e.g., a class with only final fields, a constructor, and getters → `record` |
| 23 | - **Java 17+ — Replace instanceof + cast chains with pattern matching:** e.g., `if (obj instanceof String) { String s = (String) obj; }` → `if (obj instanceof String s)` |
| 24 | |
| 25 | ## Output format |
| 26 | For each refactoring: |
| 27 | 1. **What:** one sentence describing the change |
| 28 | 2. **Why:** one sentence explaining the benefit |
| 29 | 3. **Before:** the original code snippet |
| 30 | 4. **After:** the refactored code snippet |
| 31 | 5. **Java version required:** (e.g., "Java 8+" or "All versions") |
| 32 | |
| 33 | Then offer: "Want me to apply these changes to the file?" |
| 34 | |
| 35 | ## Next Steps |
| 36 | After completing refactoring: |
| 37 | - If tests don't exist for the refactored code → suggest running `/java-test` to generate tests |
| 38 | - If the refactoring changed public API signatures → suggest running `/java-docs` to update Javadoc |
| 39 | - Remind the user to run their build: `mvn compile -q` or `./gradlew build -q` |