java-refactoring-extract-method — скилл для GitHub Copilot
Скилл помогает выделять связные блоки из сложных Java-методов в отдельные методы. Подходит, когда нужно упростить код и сохранить его поведение.
npx skills add github/awesome-copilot --skill java-refactoring-extract-method -a claude-codeЧто делает java-refactoring-extract-method
Скилл предлагает рефакторинг Extract Method для Java: ищет кандидатов среди методов длиннее 15 строк, с более чем 10 операторами или цикломатической сложностью выше 10. Каждый блок выносится отдельным шагом, а новые методы получают понятные имена и следуют стилю проекта.
Перед изменениями он требует запустить самый узкий зелёный тест, затем повторять проверку после каждого извлечения. При красном baseline или отсутствии тестов работа останавливается: сначала нужны characterization tests. Логику, исключения, порядок побочных эффектов и видимость исходного метода менять нельзя.
По умолчанию извлечённый метод остаётся private. Скилл учитывает изменяемые локальные переменные, checked exceptions, return, break, continue, а также риски в synchronized и transaction-annotated методах. Его стоит ставить командам, которые хотят делать проверяемый Extract Method в Java, а для правок без тестов он намеренно не подойдёт.
Какие задачи решает
- выделить связный блок из длинного Java-метода
- сохранить поведение при Extract Method
- проверить тесты до и после каждого извлечения
- учесть локальные переменные и checked exceptions
- не вынести код из generated-файлов без запроса
Примеры запросов агенту
- «Найди сложные Java-методы и примени Extract Method.»
- «Сначала запусти `./mvnw -q -Dtest=MyTest test`, затем вынеси один связный блок.»
- «Проверь этот `synchronized` метод и предложи безопасное извлечение.»
Как установить java-refactoring-extract-method
1Через Skills CLI (Vercel)
npx skills add github/awesome-copilot --skill java-refactoring-extract-method -a claude-code2Вручную: положить файлы
~/.claude/skills/java-refactoring-extract-method/SKILL.mdПапка скилла целиком; для одного проекта — .claude/skills/java-refactoring-extract-method/
Вручную: положить файлы
.agents/skills/java-refactoring-extract-method/SKILL.mdДля всех проектов — ~/.agents/skills/
Вручную: положить файлы
.cursor/skills/java-refactoring-extract-method/SKILL.mdCursor читает и .agents/skills/, и .claude/skills/
1Через Skills CLI (Vercel)
npx skills add github/awesome-copilot --skill java-refactoring-extract-method2GitHub CLI
gh skill install github/awesome-copilot java-refactoring-extract-method3Вручную: положить файлы
.github/skills/java-refactoring-extract-method/SKILL.mdКомандой платформы
gemini skills install https://github.com/github/awesome-copilot --path skills/java-refactoring-extract-methodВручную: положить файлы
.devin/skills/java-refactoring-extract-method/SKILL.mdСтарый путь .windsurf/skills/ тоже читается
Claude.ai
Упакуйте папку в zip и загрузите в Settings → Capabilities → Skills
Похожие
Вопросы о java-refactoring-extract-method
Как установить java-refactoring-extract-method в GitHub Copilot?
Выполните npx skills add github/awesome-copilot --skill java-refactoring-extract-method. Также доступна команда gh skill install github/awesome-copilot java-refactoring-extract-method.
Что делает java-refactoring-extract-method?
Скилл направляет GitHub Copilot при рефакторинге Java по паттерну Extract Method. Он требует сохранить поведение кода и проверить результат компиляцией и тестами.
Почему скилл останавливается, если тесты отсутствуют или baseline красный?
Он предлагает сначала добавить characterization tests либо исправить baseline. Удалять или ослаблять существующие тесты скилл не разрешает.
Исходный текст
SKILL.md · MIT
---
name: java-refactoring-extract-method
description: 'Refactoring using Extract Methods in Java Language'
---
# Refactoring Java Methods with Extract Method
## Role
You are an expert in refactoring Java methods.
Below are **2 examples** (with titles code before and code after refactoring) that represents **Extract Method**.
## Code Before Refactoring 1:
```java
public FactLineBuilder setC_BPartner_ID_IfValid(final int bpartnerId) {
assertNotBuild();
if (bpartnerId > 0) {
setC_BPartner_ID(bpartnerId);
}
return this;
}
```
## Code After Refactoring 1:
```java
public FactLineBuilder bpartnerIdIfNotNull(final BPartnerId bpartnerId) {
if (bpartnerId != null) {
return bpartnerId(bpartnerId);
} else {
return this;
}
}
public FactLineBuilder setC_BPartner_ID_IfValid(final int bpartnerRepoId) {
return bpartnerIdIfNotNull(BPartnerId.ofRepoIdOrNull(bpartnerRepoId));
}
```
## Code Before Refactoring 2:
```java
public DefaultExpander add(RelationshipType type, Direction direction) {
Direction existingDirection = directions.get(type.name());
final RelationshipType[] newTypes;
if (existingDirection != null) {
if (existingDirection == direction) {
return this;
}
newTypes = types;
} else {
newTypes = new RelationshipType[types.length + 1];
System.arraycopy(types, 0, newTypes, 0, types.length);
newTypes[types.length] = type;
}
Map<String, Direction> newDirections = new HashMap<String, Direction>(directions);
newDirections.put(type.name(), direction);
return new DefaultExpander(newTypes, newDirections);
}
```
## Code After Refactoring 2:
```java
public DefaultExpander add(RelationshipType type, Direction direction) {
Direction existingDirection = directions.get(type.name());
final RelationshipType[] newTypes;
if (existingDirection != null) {
if (existingDirection == direction) {
return this;
}
newTypes = types;
} else {
newTypes = new RelationshipType[types.length + 1];
System.arraycopy(types, 0, newTypes, 0, types.length);
newTypes[types.length] = type;
}
Map<String, Direction> newDirections = new HashMap<String, Direction>(directions);
newDirections.put(type.name(), direction);
return (DefaultExpander) newExpander(newTypes, newDirections);
}
protected RelationshipExpander newExpander(RelationshipType[] types,
Map<String, Direction> directions) {
return new DefaultExpander(types, directions);
}
```
## Safety Rules
- Behaviour-preserving only: no functional change, no removed logic, no changed exceptions, side-effect order or visibility of the original method.
- Baseline first: run the narrowest green test command for the code (`./mvnw -q -Dtest=MyTest test`, `./gradlew test --tests MyTest`) or `./mvnw verify` / `./gradlew build`. Re-run it after every extraction. If the baseline is red, stop and report.
- If the code has no tests, stop and report; propose characterization tests first. Never delete or weaken tests.
- One extraction per step (one commit each when the user asks for commits) so each is reviewable and revertible.
- Prefer the IDE refactoring: use an available IDE tool that exposes Extract Method (it handles parameters and return values safely). The Copilot CLI `lsp` tool offers navigation and rename but no code actions, so configuring jdtls through `lsp-setup` does not provide it; otherwise extract by hand and validate with the compiler and tests.
- Extracted methods are `private` by default. Do not add public/protected methods, change the existing signature, or touch generated code (`target/`, `build/generated`, `@Generated`) without asking the user.
- Watch for: local variables modified in the block (return a value or introduce a small result type), multiple results, checked exceptions, `return`/`break`/`continue` inside the block, and `synchronized`/transaction-annotated methods (extracted `private` methods called on `this` bypass Spring proxies and AOP).
## Task
Apply **Extract Method** to improve readability, testability, cohesion and consistency.
1. Identify candidate methods (rough guide: > 15 lines, > 10 statements or cyclomatic complexity > 10) and cohesive blocks inside them.
2. Extract each block into a descriptively named method, following the existing code style and the project's Java level (check `maven.compiler.release` / toolchain; do not use newer syntax).
3. Add a one-line comment above each new method only if its name does not already convey its purpose.
4. Compile and run the tests; report the methods changed, the commands run and their results.
Output complete, compilable code for each changed method.
## Code to be Refactored:
Now, assess all methods with high complexity and refactor them using **Extract Method**
© github · MIT · github/awesome-copilot