작성
·
882
·
수정됨
0
메서드 추출은 되는데
IntelliJ IDEA 2023.2.2 Ultimate
(컴퓨터는 맥북)
해당 버전 사용중입니다
중복된 메서드일때 intellij 에서
Extract Parameters to Replace Duplicate
(10:34 초부분)
이건 안되네요.
인텔리제이 버전 업하면서 그런거같은데
혹시 해당 화면처럼 하는 팁 있을까요?
package jpabook.jpashop;
import jakarta.annotation.PostConstruct;
import jakarta.persistence.EntityManager;
import jpabook.jpashop.domain.*;
import jpabook.jpashop.domain.item.Book;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
/**
* 총 주문 2개
* userA
* * JPA1 BOOK
* * JPA2 BOOK
* userB
* * SPRING1 BOOK
* * SPRING2 BOOK
*/
@Component
@RequiredArgsConstructor
public class initDb {
private final InitService initService;
@PostConstruct
public void init() {
initService.dbInit1(); // 여기에 코드들 다 넣는 경우는 작동하지 않는다
initService.dbInit2();
}
@Component
@Transactional
@RequiredArgsConstructor
static class InitService {
private final EntityManager em;
public void dbInit1() {
Member member = new Member();
member.setName("userA");
member.setAddress(new Address("서울", "1", "1111"));
em.persist(member);
Book book1 = new Book();
book1.setName("JPA1 BOOK");
book1.setPrice(10000);
book1.setStockQuantity(100);
em.persist(book1);
Book book2 = new Book();
book2.setName("JPA2 BOOK");
book2.setPrice(20000);
book2.setStockQuantity(100);
em.persist(book2);
OrderItem orderItem1 = OrderItem.createOrderItem(book1, 10000, 1);
OrderItem orderItem2 = OrderItem.createOrderItem(book1, 20000, 2);
Delivery delivery = new Delivery();
delivery.setAddress(member.getAddress());
Order order = Order.createOrder(member, delivery, orderItem1, orderItem2);// ... 되어있으면 여러개 넘길 수 있다
em.persist(order);
}
public void dbInit2() {
Member member = new Member();
member.setName("userB");
member.setAddress(new Address("진주", "2", "2222"));
em.persist(member);
Book book1 = new Book();
book1.setName("JPA1 BOOK");
book1.setPrice(10000);
book1.setStockQuantity(100);
em.persist(book1);
Book book2 = new Book();
book2.setName("JPA2 BOOK");
book2.setPrice(20000);
book2.setStockQuantity(100);
em.persist(book2);
OrderItem orderItem1 = OrderItem.createOrderItem(book1, 10000, 1);
OrderItem orderItem2 = OrderItem.createOrderItem(book1, 20000, 2);
Delivery delivery = new Delivery();
delivery.setAddress(member.getAddress());
Order order = Order.createOrder(member, delivery, orderItem1, orderItem2);// ... 되어있으면 여러개 넘길 수 있다
em.persist(order);
}
}
}
전체코드고
아래 부분을 메서드 추출하면 중복 체크하고 매개변수 넘기는 방식으로는 안되네요..
Member member = new Member();
member.setName("userA");
member.setAddress(new Address("서울", "1", "1111"));
em.persist(member);
답변 2
0
맥북 IntelliJ IDEA 2024.1 (Ultimate Edition) 기준입니다!
중복 메서드 추출이후, extract parameter를 수행해주니, IDE가 자동으로 컨텍스트 찾아서 제안해주네요.
https://www.jetbrains.com/help/idea/extract-parameter.html
0