Skip to main content
skill GitHub Copilot official · GitHub

java-refactoring-extract-method — GitHub Copilot skill

This skill guides Extract Method refactoring for complex Java methods while preserving behaviour. Use it when a method needs smaller, focused units backed by tests.

Install into GitHub Copilot · Skills CLI (Vercel) All methods and platforms ↓
npx skills add github/awesome-copilot --skill java-refactoring-extract-method
Open on GitHub

What java-refactoring-extract-method does

The skill applies Extract Method refactoring to Java code. It looks for candidates such as methods over 15 lines, with over 10 statements, or cyclomatic complexity above 10, then moves cohesive blocks into clearly named methods that match the project’s Java level and style.

It asks for the narrowest green test command before changes and reruns checks after every extraction. If the baseline is red, or the code has no tests, it stops and calls for characterization tests first. Functional logic, exception behaviour, side-effect order, and the original method’s visibility must stay intact.

New extracted methods default to private. The guidance covers changed local variables, checked exceptions, control-flow statements, plus risks in synchronized and transaction-annotated methods. Install it for reviewable Java refactors with test coverage; skip it if you expect it to alter untested code anyway.

What it helps with

  • extract a cohesive block from a long Java method
  • preserve behaviour during Extract Method refactoring
  • run tests before and after each extraction
  • handle local variables and checked exceptions
  • avoid edits to generated code without approval

Example prompts for the agent

  • “Find high-complexity Java methods and apply Extract Method.”
  • “Run `./gradlew test --tests MyTest` first, then extract one cohesive block.”
  • “Review this `synchronized` method and suggest a safe extraction.”

How to install java-refactoring-extract-method

1Skills CLI (Vercel)

npx skills add github/awesome-copilot --skill java-refactoring-extract-method

2GitHub CLI

gh skill install github/awesome-copilot java-refactoring-extract-method

3Manually: put the files in place

.github/skills/java-refactoring-extract-method/SKILL.md

From the same set554

The whole set →
git-commit official github This skill reviews Git changes, stages related files, and creates Conventional Commits. Use it when you want an agent to make one commit or split work into separate commits. GitHub Copilot 47,165installs excalidraw-diagram-generator official github Turn a plain-language description into an Excalidraw diagram. Useful for mapping processes, system structure, and relationships between entities. GitHub Copilot 31,639installs multi-stage-dockerfile official github This skill guides GitHub Copilot in writing multi-stage Dockerfiles for different languages and frameworks. Use it when the final runtime image should contain fewer build dependencies. GitHub Copilot 29,688installs documentation-writer official github Skill that turns the agent into a technical writer following the Diátaxis framework: tutorials, how-to guides, reference, and explanation. Proposes structure before writing. GitHub Copilot 27,757installs prd official github Turns a feature idea into a structured PRD with user stories, technical requirements, and risks. Use it when planning a software feature or an AI-powered one. GitHub Copilot 24,035installs refactor official github This skill reshapes existing code without changing its external behavior. Use it for gradual cleanup of large functions, duplication, weak types, and code smells. GitHub Copilot 22,321installs java-springboot official github Gives GitHub Copilot Spring Boot development practices, from project layout to tests and security. Use it when writing or reviewing Java application code. GitHub Copilot 21,272installs create-readme official github This skill reviews a project and creates a structured README.md. Use it when the repository needs clear documentation based on its actual files and code. GitHub Copilot 19,081installs

Similar

architecture-patterns Seth Hobson Guides backend architecture with Clean Architecture, Hexagonal Architecture, and DDD. Use it when designing a service, untangling a monolith, or tracing dependency cycles. Claude Code CX Codex Cursor 24,144installs thermo-nuclear-code-quality-review cursor Runs a strict maintainability audit of code changes, abstractions, large files, and branching complexity. Use it when a routine review needs deeper structural scrutiny. Claude Code CX Codex Cursor +1 22,200installs improve-codebase-architecture Matt Pocock This skill scans a codebase for architectural friction, then builds a visual HTML report with refactoring candidates. Use it before a deeper architecture review. Claude Code 1,163,035installs tdd Matt Pocock Guides the red-green TDD loop: ensures tests are written first, keep them at public interfaces, and avoid coupling to implementation details. Claude Code 1,141,062installs triage Matt Pocock This skill moves issues and external PRs through triage states, verification, and agent briefs. It fits maintainers managing an issue queue. Claude Code 990,453installs codebase-design Matt Pocock This skill helps design modules with small interfaces and hidden complexity. Use it when refactoring code, choosing a seam, or making tests easier to write. Claude Code 850,344installs code-review Matt Pocock Reviews changes from a chosen reference point against repository standards and the originating spec. Use it for a branch, PR, or work in progress. Claude Code 789,521installs implement Matt Pocock Implements work from a specification or tickets, following a test-driven flow and ending with review and a commit. Use it when a coding task needs a defined delivery routine. Claude Code 774,700installs

Questions about java-refactoring-extract-method

How do I install java-refactoring-extract-method in GitHub Copilot?

Run npx skills add github/awesome-copilot --skill java-refactoring-extract-method. You can also use gh skill install github/awesome-copilot java-refactoring-extract-method.

What does java-refactoring-extract-method do?

The skill guides GitHub Copilot through Java Extract Method refactoring. It keeps the original behaviour intact and asks to validate changes with compilation and tests.

Why does the skill stop when tests are missing or the baseline is red?

It asks for characterization tests first or for the baseline to be fixed. The skill does not permit deleting or weakening existing tests.

Source text

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

See also

Project sponsors

Companies that keep this analytics open
Gold sponsor

Impressions, clicks and code copies — open statistics. We keep the numbers public for the same reason NeBlask itself is public. Become a sponsor