Skip to content

Changing Types

Jon Schneider edited this page Nov 28, 2016 · 1 revision

Changing a type, anywhere it occurs

Suppose we have two types A1 and A2, and we have this contrived class:

class B extends A1 {
	A1[] aArr = new A1[0];
	A1 a = new A1();
	
	public <T extends A1> T generic(T n, List<? super A1> in)
	public void test() {
		A1.stat();
        this.<A1>generic(null, null);
    }
}

All references to A1 can be changed to A2 in one operation:

Tr.CompilationUnit cu = parser.parse(a)
cu.refactor().changeType("A1", "A2").fix();

Which yields:

class B extends A2 {
	A2[] aArr = new A2[0];
	A2 a = new A2();
	
	public <T extends A2> T generic(T n, List<? super A2> in)
	public void test() {
		A2.stat();
        this.<A2>generic(null, null);
    }
}

This operation finds types (whether imported or fully qualified) anywhere they can occur in source code. For a listing of all possibilities, see Find Type.