github/awesome-copilot

java-refactoring-extract-method

Refactoring using Extract Methods in Java Language

소스 보기

사용 방법

  1. 설치 명령을 복사하고 자신의 에이전트 환경에 설치하기 전에 소스 패키지를 확인하세요.
  2. 필요한 도구, 인증 정보, 지원 환경은 원본 문서에서 확인하세요.
  3. 입력과 원하는 산출물을 설명하고 출처와 결과를 대조하세요.
npx skills add https://github.com/github/awesome-copilot --skill java-refactoring-extract-method

시작 전 확인

  • 설치에는 모델 구독, API 한도, 파일 접근 권한이 포함되지 않습니다.
  • 별점 수와 설치 수는 활동 지표이며 독립적인 품질·보안 검증이 아닙니다.
원본 Skill 문서

원본 저장소의 제목, 예시, 코드, 표, 링크, 이미지를 유지해 표시합니다.

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

같은 저장소의 Skills

더 많은 Skills

모든 Skills
github
공식

java-docs

Ensure that Java types are documented with Javadoc comments and follow best practices for documentation.

설치 수
3
GitHub Stars
4만
업데이트
10월 22일
github
공식

java-junit

Write, review and fix Java unit tests with JUnit 5 and JUnit 6 (Jupiter), AssertJ and Mockito: assertions, parameterized and nested tests, mocking rules, deterministic time and randomness, Surefire vs Failsafe, JaCoCo coverage, optional PIT mutation testing, and guardrails for agents (never skip or delete failing tests). Use when adding, refactoring or debugging tests in Maven or Gradle projects. Framework-neutral.

설치 수
2
GitHub Stars
4만
업데이트
10월 22일
github
공식

java-mcp-server-generator

Use this skill when the user wants to create a Model Context Protocol (MCP) server in Java or in a Spring Boot app. Also use it when the user wants AI agents, such as GitHub Copilot or Claude, to use Java code, an API, or a database. Use it even if the user does not say "MCP". The skill makes a Maven or Gradle project with the MCP Java SDK 2.x, tools, JUnit tests, and client configuration. Do not use it for Quarkus/Quarkiverse MCP servers (use the quarkus-mcp-server-sse instruction), for an MCP

설치 수
1
GitHub Stars
4만
업데이트
10월 22일
github
공식

java-refactoring-remove-parameter

Refactoring using Remove Parameter in Java Language

설치 수
1
GitHub Stars
4만
업데이트
10월 22일