public void testConditional() throws Exception { addSnippetClassDecl("static class D {}"); addSnippetClassDecl("static class C extends D {}"); addSnippetClassDecl("static class B extends C {}"); addSnippetClassDecl("static class A extends C {}"); Result result = optimize( "void", "int x = 0;", "A a1 = new A();", "A a2 = new A();", "B b = new B();", "C c = new C();", "C c1 = (x > 0) ? a1 : b;", "C c2 = (x > 0) ? a1 : a2;", "D d = (x > 0) ? b : c;"); result.intoString( "int x = 0;\n" + "EntryPoint$A a1 = new EntryPoint$A();\n" + "EntryPoint$A a2 = new EntryPoint$A();\n" + "EntryPoint$B b = new EntryPoint$B();\n" + "EntryPoint$C c = new EntryPoint$C();\n" + "EntryPoint$C c1 = x > 0 ? a1 : b;\n" + "EntryPoint$A c2 = x > 0 ? a1 : a2;\n" + "EntryPoint$C d = x > 0 ? b : c;"); }
public void testTightenLocalVariable() throws Exception { addSnippetClassDecl("static abstract class B {};"); addSnippetClassDecl("static class C extends B{};"); addSnippetClassDecl("static class D extends C{};"); addSnippetClassDecl("static class E extends C{};"); Result result = optimize("void", "B b;", "b = new C();", "b = new D();", "b = new E();"); result.intoString( "EntryPoint$C b;\n" + "b = new EntryPoint$C();\n" + "b = new EntryPoint$D();\n" + "b = new EntryPoint$E();"); }
public void testCastOperation() throws Exception { addSnippetClassDecl("static class A { " + "String name; public void set() { name = \"A\";} }"); addSnippetClassDecl("static class B extends A {" + "public void set() { name = \"B\";} }"); addSnippetClassDecl("static class C extends A {" + "public void set() { name = \"C\";} }"); Result result = optimize("void", "A b = new B();", "((A)b).set();", "((B)b).set();", "((C)b).set();"); result.intoString( "EntryPoint$B b = new EntryPoint$B();\n" + "b.set();\n" + "b.set();\n" + "((null) b).nullMethod();"); }
public void testInstanceOf() throws Exception { addSnippetClassDecl("static class A {};"); addSnippetClassDecl("static class B extends A {};"); Result result = optimize( "void", "A a = new A();", "B b = new B();", "boolean test;", "test = a instanceof A;", "test = b instanceof B;", "test = a instanceof B;", "test = b instanceof A;"); result.intoString( "EntryPoint$A a = new EntryPoint$A();\n" + "EntryPoint$B b = new EntryPoint$B();\n" + "boolean test;\n" + "test = true;\n" + "test = true;\n" + "test = a instanceof EntryPoint$B;\n" + "test = true;"); }