Refactors Spring Boot and Java code to improve maintainability, readability, and enterprise best practices. Addresses fat controllers, improper transaction boundaries, field injection anti-patterns, and scattered configuration using Java 21+ features.
Ships in debug-and-refactor. Installing the plugin adds it to Claude Code.
What the skill costs in Claude's context, stage by stage
Name and description: Claude knows it exists.
SKILL.md joins the conversation and stays.
None: SKILL.md is the whole skill.
You are an elite Spring Boot/Java refactoring specialist with deep expertise in writing clean, maintainable enterprise applications following SOLID principles and Spring Boot 3.x best practices.
// BEFORE: Deep nesting
public Order processOrder(OrderRequest request) {
if (request != null) {
if (request.getItems() != null && !request.getItems().isEmpty()) {
if (userService.isValidUser(request.getUserId())) {
// actual logic buried 3 levels deep
return createOrder(request);
}
}
}
return null;
}
// AFTER: Guard clauses with early returns
public Order processOrder(OrderRequest request) {
if (request == null) {
throw new IllegalArgumentException("Request cannot be null");
}
if (request.getItems() == null || request.getItems().isEmpty()) {
throw new ValidationException("Order must contain items");
}
if (!userService.isValidUser(request.getUserId())) {
throw new UnauthorizedException("Invalid user");
}
return createOrder(request);
}
// BEFORE: Manual destructuring
if (shape instanceof Rectangle r) {
double area = r.length() * r.width();
process(area);
}
// AFTER: Record pattern matching
if (shape instanceof Rectangle(double length, double width)) {
double area = length * width;
process(area);
}
// BEFORE: instanceof chains
public double calculateArea(Shape shape) {
if (shape instanceof Circle c) {
return Math.PI * c.radius() * c.radius();
} else if (shape instanceof Rectangle r) {
return r.length() * r.width();
} else if (shape instanceof Triangle t) {
return 0.5 * t.base() * t.height();
}
throw new IllegalArgumentException("Unknown shape");
}
// AFTER: Pattern matching switch
public double calculateArea(Shape shape) {
return switch (shape) {
case Circle(double radius) -> Math.PI * radius * radius;
case Rectangle(double length, double width) -> length * width;
case Triangle(double base, double height) -> 0.5 * base * height;
case null -> throw new IllegalArgumentException("Shape cannot be null");
};
}
// Enable virtual threads in Spring Boot 3.2+
// application.properties
spring.threads.virtual.enabled=true
// Or programmatically for specific use cases
try (var executor = Executors.newVirtualThreadPerTaskExecutor()) {
List<Future<Result>> futures = tasks.stream()
.map(task -> executor.submit(() -> processTask(task)))
.toList();
}
// BEFORE: Awkward first/last element access
List<String> items = getItems();
String first = items.get(0);
String last = items.get(items.size() - 1);
// AFTER: Sequenced collections
SequencedCollection<String> items = getItems();
String first = items.getFirst();
String last = items.getLast();
items.reversed().forEach(System.out::println);
// BEFORE: Verbose DTO class
public class UserResponse {
private final Long id;
private final String name;
private final String email;
public UserResponse(Long id, String name, String email) {
this.id = id;
this.name = name;
this.email = email;
}
// getters, equals, hashCode, toString...
}
// AFTER: Record (immutable, concise)
public record UserResponse(Long id, String name, String email) {}
// When you don't need certain values
if (object instanceof Point(var x, _)) {
// Only need x coordinate
process(x);
}
// In try-with-resources when you don't use the variable
try (var _ = ScopedValue.where(USER, currentUser).call(() -> {
// scoped execution
})) {
// resource auto-closed
}
// ANTI-PATTERN: Field injection
@Service
public class OrderService {
@Autowired
private OrderRepository orderRepository;
@Autowired
private PaymentService paymentService;
}
// BEST PRACTICE: Constructor injection
@Service
@RequiredArgsConstructor // Lombok generates constructor
public class OrderService {
private final OrderRepository orderRepository;
private final PaymentService paymentService;
}
// Or explicit constructor (no Lombok)
@Service
public class OrderService {
private final OrderRepository orderRepository;
private final PaymentService paymentService;
public OrderService(OrderRepository orderRepository,
PaymentService paymentService) {
this.orderRepository = orderRepository;
this.paymentService = paymentService;
}
}
// ANTI-PATTERN: Scattered @Value annotations
@Service
public class EmailService {
@Value("${mail.host}")
private String host;
@Value("${mail.port}")
private int port;
@Value("${mail.username}")
private String username;
}
// BEST PRACTICE: Type-safe configuration
@ConfigurationProperties(prefix = "mail")
public record MailProperties(
String host,
int port,
String username,
String password,
Ssl ssl
) {
public record Ssl(boolean enabled, String protocol) {}
}
@Service
@RequiredArgsConstructor
public class EmailService {
private final MailProperties mailProperties;
}
// Enable in main class
@SpringBootApplication
@ConfigurationPropertiesScan
public class Application { }
// BEFORE (Spring Boot 2.x): javax namespace
import javax.persistence.Entity;
import javax.validation.constraints.NotNull;
import javax.servlet.http.HttpServletRequest;
// AFTER (Spring Boot 3.x): jakarta namespace
import jakarta.persistence.Entity;
import jakarta.validation.constraints.NotNull;
import jakarta.servlet.http.HttpServletRequest;
// Add observability to services
@Service
@Observed(name = "order.service") // Micrometer observation
@RequiredArgsConstructor
public class OrderService {
private final MeterRegistry meterRegistry;
public Order createOrder(OrderRequest request) {
return meterRegistry.timer("order.creation.time")
.record(() -> doCreateOrder(request));
}
}
Controller Layer (@RestController)
|-- Handles HTTP request/response
|-- Input validation (@Valid)
|-- Exception handling (@ControllerAdvice)
v
Service Layer (@Service)
|-- Business logic
|-- Transaction management (@Transactional)
|-- Orchestration between repositories
v
Repository Layer (@Repository)
|-- Data access
|-- Spring Data JPA interfaces
|-- Custom queries (@Query)
v
Entity/Model Layer (@Entity)
|-- Domain objects
|-- JPA mappings
// ANTI-PATTERN: @Transactional on private method (doesn't work!)
@Service
public class OrderService {
@Transactional // IGNORED - Spring proxies can't intercept private methods
private void updateOrder(Order order) { }
}
// ANTI-PATTERN: @Transactional on controller
@RestController
public class OrderController {
@Transactional // Wrong layer - controllers shouldn't manage transactions
@PostMapping("/orders")
public Order create(@RequestBody OrderRequest request) { }
}
// BEST PRACTICE: @Transactional on service methods
@Service
public class OrderService {
@Transactional
public Order createOrder(OrderRequest request) {
Order order = orderRepository.save(new Order(request));
inventoryService.reserve(order.getItems()); // Same transaction
return order;
}
@Transactional(readOnly = true) // Optimization for read operations
public List<Order> findByUser(Long userId) {
return orderRepository.findByUserId(userId);
}
@Transactional(propagation = Propagation.REQUIRES_NEW)
public void logAuditEvent(AuditEvent event) {
// New transaction - won't roll back with parent
auditRepository.save(event);
}
}
@RestControllerAdvice
public class GlobalExceptionHandler {
@ExceptionHandler(EntityNotFoundException.class)
public ResponseEntity<ErrorResponse> handleNotFound(EntityNotFoundException ex) {
return ResponseEntity.status(HttpStatus.NOT_FOUND)
.body(new ErrorResponse("NOT_FOUND", ex.getMessage()));
}
@ExceptionHandler(MethodArgumentNotValidException.class)
public ResponseEntity<ErrorResponse> handleValidation(MethodArgumentNotValidException ex) {
List<String> errors = ex.getBindingResult().getFieldErrors().stream()
.map(error -> error.getField() + ": " + error.getDefaultMessage())
.toList();
return ResponseEntity.badRequest()
.body(new ErrorResponse("VALIDATION_ERROR", String.join(", ", errors)));
}
@ExceptionHandler(Exception.class)
public ResponseEntity<ErrorResponse> handleGeneral(Exception ex) {
log.error("Unexpected error", ex);
return ResponseEntity.internalServerError()
.body(new ErrorResponse("INTERNAL_ERROR", "An unexpected error occurred"));
}
}
public record ErrorResponse(String code, String message) {}
public interface OrderRepository extends JpaRepository<Order, Long> {
// Derived query methods
List<Order> findByStatusAndCreatedAtAfter(OrderStatus status, LocalDateTime after);
// JPQL for complex queries
@Query("SELECT o FROM Order o JOIN FETCH o.items WHERE o.user.id = :userId")
List<Order> findByUserIdWithItems(@Param("userId") Long userId);
// Native query when needed
@Query(value = "SELECT * FROM orders WHERE created_at > NOW() - INTERVAL '1 day'",
nativeQuery = true)
List<Order> findRecentOrders();
// Projections for performance
@Query("SELECT new com.example.dto.OrderSummary(o.id, o.status, o.total) FROM Order o")
List<OrderSummary> findOrderSummaries();
// Modifying queries
@Modifying
@Query("UPDATE Order o SET o.status = :status WHERE o.id = :id")
int updateStatus(@Param("id") Long id, @Param("status") OrderStatus status);
}
@Aspect
@Component
@Slf4j
public class LoggingAspect {
@Around("@annotation(Loggable)")
public Object logExecutionTime(ProceedingJoinPoint joinPoint) throws Throwable {
long start = System.currentTimeMillis();
String methodName = joinPoint.getSignature().getName();
try {
Object result = joinPoint.proceed();
log.info("{} executed in {}ms", methodName, System.currentTimeMillis() - start);
return result;
} catch (Exception e) {
log.error("{} failed after {}ms: {}", methodName,
System.currentTimeMillis() - start, e.getMessage());
throw e;
}
}
}
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface Loggable {}
// Usage
@Service
public class OrderService {
@Loggable
public Order processOrder(OrderRequest request) { }
}
// ANTI-PATTERN: Everything in controller
@RestController
public class OrderController {
@Autowired private OrderRepository orderRepo;
@Autowired private UserRepository userRepo;
@Autowired private PaymentGateway paymentGateway;
@PostMapping("/orders")
public Order createOrder(@RequestBody OrderRequest request) {
// Validation
if (request.getItems().isEmpty()) throw new BadRequestException("No items");
// Business logic
User user = userRepo.findById(request.getUserId()).orElseThrow();
BigDecimal total = request.getItems().stream()
.map(item -> item.getPrice().multiply(BigDecimal.valueOf(item.getQuantity())))
.reduce(BigDecimal.ZERO, BigDecimal::add);
// Payment processing
PaymentResult payment = paymentGateway.charge(user.getPaymentMethod(), total);
if (!payment.isSuccess()) throw new PaymentException("Payment failed");
// Persistence
Order order = new Order();
order.setUser(user);
order.setItems(request.getItems());
order.setTotal(total);
order.setPaymentId(payment.getId());
return orderRepo.save(order);
}
}
// REFACTORED: Proper separation
@RestController
@RequiredArgsConstructor
public class OrderController {
private final OrderService orderService;
@PostMapping("/orders")
public ResponseEntity<OrderResponse> createOrder(
@Valid @RequestBody OrderRequest request) {
Order order = orderService.createOrder(request);
return ResponseEntity.status(HttpStatus.CREATED)
.body(OrderResponse.from(order));
}
}
@Service
@RequiredArgsConstructor
@Transactional
public class OrderService {
private final OrderRepository orderRepository;
private final UserService userService;
private final PaymentService paymentService;
private final OrderValidator validator;
public Order createOrder(OrderRequest request) {
validator.validate(request);
User user = userService.getUser(request.getUserId());
BigDecimal total = calculateTotal(request.getItems());
String paymentId = paymentService.processPayment(user, total);
return orderRepository.save(Order.builder()
.user(user)
.items(request.getItems())
.total(total)
.paymentId(paymentId)
.build());
}
private BigDecimal calculateTotal(List<OrderItem> items) {
return items.stream()
.map(item -> item.getPrice().multiply(BigDecimal.valueOf(item.getQuantity())))
.reduce(BigDecimal.ZERO, BigDecimal::add);
}
}
// ANTI-PATTERN: N+1 queries
@Service
public class OrderService {
public List<OrderDTO> getOrders() {
List<Order> orders = orderRepository.findAll(); // 1 query
return orders.stream()
.map(order -> new OrderDTO(
order.getId(),
order.getUser().getName(), // N queries!
order.getItems().size() // N more queries!
))
.toList();
}
}
// REFACTORED: Fetch join
@Query("SELECT o FROM Order o JOIN FETCH o.user JOIN FETCH o.items")
List<Order> findAllWithUserAndItems();
// Or use EntityGraph
@EntityGraph(attributePaths = {"user", "items"})
List<Order> findAll();
// ANTI-PATTERN
public class PaymentService {
private static final String API_KEY = "sk_live_abc123"; // NEVER!
private static final String API_URL = "https://api.payment.com";
}
// REFACTORED: Externalized configuration
@ConfigurationProperties(prefix = "payment")
public record PaymentProperties(
String apiKey,
String apiUrl,
Duration timeout
) {}
When refactoring code, provide:
Summary of Issues Found
Refactored Code
Changes Made
Testing Considerations
Stop refactoring when:
Do NOT over-engineer by:
This skill incorporates best practices from:
What SKILL.md sets; muted values are Claude Code's defaults
Three short reads, in order
/debug-and-refactor:refactor:spring-bootfrom debug-and-refactor
npx claudepluginhub snakeo/claude-debug-and-refactor-skills-plugin --plugin debug-and-refactor45 skills in this plugin
/debug:angularDebug Angular applications systematically with expert-level diagnostic techniques. This skill provides comprehensive guidance for troubleshooting dependency injection errors, change detection issues (NG0100), RxJS subscription leaks, lazy loading failures, zone.js problems, and common Angular runtime errors. Includes structured four-phase debugging methodology, Angular DevTools usage, console debugging utilities (ng.probe), and performance profiling strategies for modern Angular applications./debug:djangoDebug Django web applications with systematic diagnostic approaches. This skill covers troubleshooting Django-specific errors including TemplateDoesNotExist, ImproperlyConfigured, IntegrityError, migration conflicts, CSRF failures, N+1 query problems, and circular imports. Includes Django Debug Toolbar setup, ORM query logging, pdb/ipdb usage, shell_plus debugging, and comprehensive logging configuration. Provides four-phase methodology for root cause analysis and regression prevention./debug:dockerDebug Docker containers, images, and infrastructure with systematic diagnostic techniques. This skill provides comprehensive guidance for troubleshooting container exit codes, OOM kills, image build failures, networking issues, volume mount problems, and permission errors. Covers four-phase debugging methodology from quick assessment to deep analysis, essential Docker commands, debug container techniques for minimal images, and platform-specific troubleshooting for Windows, Mac, and Linux.Other plugins' skills for the same job