Generify Refactoring 

 

�� ������ ��ſ��� Generify  �����丵 ��ɿ� ���� �������� overview ��  how �� �����丵�� IntelliJ IDEA���� ���� �� �ִ°��� �����մϴ�.

����


Generify �����丵�� Generics-aware ������ Generics�� ������� �ʴ� ���� �ڵ带 ���� ������Ű�� ���� �����ε˴ϴ�.  �����丵�� ���� �ڵ带 �м��ϰ�, �� raw Ÿ���� ���� �����ϰ� �ϰ��� �Ű� ���� ���¸� �߷��մϴ�.
IDEA�� �ڹ� �������κ��� ������ �� ��Ȯ�� �ڵ带 �����Ϸ��� ����մϴ�.  �ٲپ� ���ϸ�, �� ������ �Ϻ� Ÿ�� ������ �����ϰ� �����丵�� ��� ������ ���ư� �Բ� ������� �ʴ� ���� ������ Ÿ���� �����մϴ�. �Ʒ���  Code Sample �� ���ʽÿ�.

Using Generify Refactoring  


Generify �����丵�� �����ϱ� ����, ù ��°�� �ڵ� ��ȯ ����, �� Ŭ����/��Ű��/����/������Ʈ�� �����Ͻʽÿ�.



�׸��� ���� �޴����� Refactor Generify  �׸��� �����Ͻʽÿ�.


Generify ���̾�α״� ����� ������ ������ �� �ִ� ���� ��Ÿ���ϴ�.

Drop obsolete  casts  

���� ���õǸ�, ���� �׵��� �����丵�� ���� ����Ǹ� �Ű� ���� ij��Ʈ ��찡 �м��˴ϴ�.  �׸��� ���� ������� �Ű� ���� Ÿ���� �Ű� ������ ij���õǾ��� ���� �����ϸ�, ij���õ� ������Ʈ��Ʈ�� ���ŵ˴ϴ�.

Preserve raw  arrays  

���� ���õǸ�, IDEA�� �Բ� ���� ������ Generics ������ �� Ư¡�� �������� �ʴ� ���� �迭�� parameterized�� Ÿ�԰� �Բ� ������� �ʽ��ϴ�.  ���õ��� ������ �迭�� ������ ���Դϴ�. �׷��� ����� ���� �����Ϸ��� ����Ѵٸ� ����� ���������� ������ ���� �ʴ� �ڵ带 ���� �� ������ �˰� �־�߸� �մϴ� .

Leave Object-parameterized  types raw  

���� ���õǸ� Generics ��ȯ ���Ŀ� �װ͵� �߿� �ϳ��� Object�� ���� �Ű� ������ �Բ� ��� Ŭ������ raw�� �� �� �Դϴ�.

Preview usages  to be generified  

üũ �ڽ��� �����ϸ� ��ſ��� � ����� �߰ߵǾ����� ��� ����ϰ� �ְ�, �׵��� ��ü�� �����ϵ��� ����մϴ�. �� üũ �ڽ��� ���õ��� ���� ��, IDEA�� ��ü�� �ڵ������� �����մϴ�.


������ üũ �ڽ��� Ȯ�ε��� �ʾ��� ����, ���� �߰ߵ� ����� � �б� ���� ���Ͽ��� �����ϸ� ��ſ��� ��� ��ü�� Ȯ���ϱ� ���� ������Ʈ �� �� �Դϴ�.


Refactoring Preview ���̾�α׿��� (���� �װ��� ��Ÿ����) ����� ����� ������ �м��� �� �ֽ��ϴ�.  �����丵�� �Բ� �����ϱ� ���ؼ�, Do Refactor�� Ŭ���մϴ��׷��� �ʴٸ� Cancel �� Ŭ���մϴ�. ���� ���� ������ Refactoring Preview�� ���ʽÿ�.


�ڵ� ��ȯ�� �����ϱ� ���Ͽ� Do Refactor�� �����ʽÿ�.

Code Sample  

 

 
import java.util.List;
import java.util.LinkedList;
 
public class GenerifySamples {
        void generifyDeclaration() {
        List list = new LinkedList();
 
        list.add("");
    }
 
    List generifyMethodReturnType() {
        List list = new LinkedList();
 
        list.add((String) generifyMethodReturnType().get(0));
 
        return list;
    }
 
    void generifyMethodParameter(List list) {
        list.add("");
    }
 
    void covariantGenerify() {
        class Parent {
        }
        class Child extends Parent {
        }
 
        List listOfParent = new LinkedList();
 
        listOfParent.add(new Parent());
        listOfParent.add(new Child());
    }
 
    void invariantGenerify() {
        class Parent {
        }
        class Child extends Parent {
        }
 
        List listOfParent = new LinkedList();
        List listOfChild = new LinkedList();
        List rawList = new LinkedList();
 
        listOfParent.add(new Parent());
        listOfChild.add(new Child());
 
        rawList.add(listOfParent);
        rawList.add(listOfChild);
    }
 
    void safeGenerify(RawContext context) {
        List list = new LinkedList();
 
        list.add("");
 
        list = context.restrictor();
    }
 
    class Parent {
    }
 
    class Child <X> extends Parent {
        void put(X x) {
        }
    }
 
    Parent killingCast() {
        Child child = new Child();
 
        child.put("");
 
        return child;
    }
}

 

 
import java.util.List;
import java.util.LinkedList;
 
public class GenerifySamples {
        void generifyDeclaration() {
        List<String> list = new LinkedList<String>();
 
        list.add("");
    }
 
    List<String> generifyMethodReturnType() {
        List<String> list = new LinkedList<String>();
 
        list.add(generifyMethodReturnType().get(0));
 
        return list;
    }
 
    void generifyMethodParameter(List<String> list) {
        list.add("");
    }
 
    void covariantGenerify() {
        class Parent {
        }
        class Child extends Parent {
        }
 
        List<Parent> listOfParent = new LinkedList<Parent>();
 
        listOfParent.add(new Parent());
        listOfParent.add(new Child());
    }
 
    void invariantGenerify() {
        class Parent {
        }
        class Child extends Parent {
        }
 
        List<Parent> listOfParent = new LinkedList<Parent>();
        List<Parent> listOfChild = new LinkedList<Parent>();
        List<List<Parent>> rawList = new LinkedList<List<Parent>>();
 
        listOfParent.add(new Parent());
        listOfChild.add(new Child());
 
        rawList.add(listOfParent);
        rawList.add(listOfChild);
    }
 
    void safeGenerify(RawContext context) {
        List list = new LinkedList();
 
        list.add("");
 
        list = context.restrictor();
    }
 
    class Parent {
    }
 
    class Child <X> extends Parent {
        void put(X x) {
        }
    }
 
    Parent killingCast() {
        Child child = new Child();
 
        child.put("");
 
        return child;
    }
}