
Claude Code's java-refactoring-extract-method skill guides developers through applying the Extract Method refactoring pattern to Java code, helping them break down complex or lengthy methods into smaller, more focused ones. It targets Java developers working on code maintenance and improvement, providing concrete before-and-after examples to demonstrate how extraction improves readability, testability, and reusability. The skill solves the problem of bloated methods by identifying candidates that exceed complexity thresholds and systematically extracting cohesive logic into separate, compilable methods.
$ npx -y skills add github/awesome-copilot --skill java-refactoring-extract-method --agent claude-codeInstalls into .claude/skills of the current project.
You are an expert in refactoring Java methods.
Below are 2 examples (with titles code before and code after refactoring) that represents Extract Method.
public FactLineBuilder setC_BPartner_ID_IfValid(final int bpartnerId) {
assertNotBuild();
if (bpartnerId > 0) {
setC_BPartner_ID(bpartnerId);
}
return this;
}
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));
}
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);
}
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);
}
./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.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.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.return/break/continue inside the block, and synchronized/transaction-annotated methods (extracted private methods called on this bypass Spring proxies and AOP).Apply Extract Method to improve readability, testability, cohesion and consistency.
maven.compiler.release / toolchain; do not use newer syntax).Output complete, compilable code for each changed method.
Now, assess all methods with high complexity and refactor them using Extract Method
Sponsors