001package daikon.test; 002 003import static org.junit.Assert.assertEquals; 004 005import daikon.tools.jtb.*; 006import java.io.IOException; 007import java.io.InputStream; 008import java.io.UncheckedIOException; 009import java.util.ArrayList; 010import java.util.List; 011import jtb.*; 012import jtb.syntaxtree.*; 013import jtb.visitor.*; 014import junit.framework.*; 015import org.junit.Test; 016import org.plumelib.util.StringsPlume; 017 018public final class TestClassOrInterfaceTypeDecorateVisitor { 019 020 public static class UngenerifiedTypeCollector extends DepthFirstVisitor { 021 // These two lists have the same length. 022 List<ClassOrInterfaceType> generifieds = new ArrayList<>(); 023 List<ClassOrInterfaceType> ungenerifieds = new ArrayList<>(); 024 025 @Override 026 public void visit(ClassOrInterfaceType n) { 027 generifieds.add(n); 028 ungenerifieds.add(n.unGenerifiedVersionOfThis); 029 } 030 031 /** 032 * A printed representation of the results. 033 * 034 * @return a printed representation of the results 035 */ 036 public String collectionResults() { 037 StringBuilder b = new StringBuilder(); 038 b.append("Collection results:"); 039 b.append(System.lineSeparator()); 040 for (int i = 0; i < generifieds.size(); i++) { 041 MethodDeclaration m = 042 (MethodDeclaration) Ast.getParent(MethodDeclaration.class, generifieds.get(i)); 043 if (m != null) { 044 b.append("Method: "); 045 m.f0.accept(new TreeFormatter()); 046 b.append(Ast.format(m.f0)); 047 m.f1.accept(new TreeFormatter()); 048 b.append(Ast.format(m.f1)); 049 m.f2.accept(new TreeFormatter()); 050 b.append(Ast.format(m.f2)); 051 b.append(System.lineSeparator()); 052 } 053 generifieds.get(i).accept(new TreeFormatter()); 054 b.append(" " + Ast.format(generifieds.get(i))); 055 b.append(" -->"); 056 ungenerifieds.get(i).accept(new TreeFormatter()); 057 b.append(" " + Ast.format(ungenerifieds.get(i))); 058 b.append(System.lineSeparator()); 059 } 060 return b.toString(); 061 } 062 } 063 064 @Test 065 public void testTheVisitor() { 066 067 CompilationUnit compilationUnit; 068 069 // Parse the file "GenericTestClass.java" (under same dir as this class) 070 try (InputStream sourceIn = this.getClass().getResourceAsStream("GenericTestClass.java")) { 071 if (sourceIn == null) { 072 throw new Error("Couldn't find file GenericTestClass.java"); 073 } 074 JavaParser parser = new JavaParser(sourceIn); 075 076 try { 077 compilationUnit = parser.CompilationUnit(); 078 } catch (ParseException e) { 079 throw new Error(e); 080 } 081 } catch (IOException e) { 082 throw new UncheckedIOException(e); 083 } 084 085 UngenerifiedTypeCollector ungenerifiedCollector = new UngenerifiedTypeCollector(); 086 compilationUnit.accept(new ClassOrInterfaceTypeDecorateVisitor()); 087 compilationUnit.accept(ungenerifiedCollector); 088 089 /* 090 for (int ii = 0; ii < result.length(); ii++) { 091 if (result.charAt(ii) != expected.charAt(ii)) { 092 System.out.printf("diff at offset %d: '%c' - '%c'%n", ii, 093 result.charAt(ii), expected.charAt(ii)); 094 System.out.printf("last:%n%s%n%s%n", result.substring (ii-50, ii+2), 095 expected.substring (ii-50, ii+2)); 096 break; 097 } 098 } 099 */ 100 101 String result = ungenerifiedCollector.collectionResults().trim(); 102 String[] result_arr = StringsPlume.splitLines(result); 103 104 // FilesPlume.writeFile(new File("expected.txt"), expected); 105 // FilesPlume.writeFile(new File("result.txt"), result); 106 107 assertEquals(expectedAnswerLines.length, result_arr.length); 108 for (int ii = 0; ii < expectedAnswerLines.length; ii++) { 109 assertEquals(expectedAnswerLines[ii], result_arr[ii]); 110 } 111 /* 112 assertEquals(expectedAnswerBuffer.toString().trim(), ungenerifiedCollector.collectionResults().trim()); 113 */ 114 } 115 116 /** The expected results, as a collection of lines. */ 117 private static String[] expectedAnswerLines = 118 new String[] { 119 "Collection results:", 120 " String --> String", 121 " java.lang.Object --> java.lang.Object", 122 "Method: Listfoo1()", 123 " List --> List", 124 "Method: List<String>foo2()", 125 " List<String> --> List", 126 "Method: Ufoo3()", 127 " U --> Object", 128 "Method: <D extends Comparable >List<String>foo4()", 129 " Comparable --> Comparable", 130 "Method: <D extends Comparable >List<String>foo4()", 131 " List<String> --> List", 132 "Method: <E extends java.lang.Object >List<U>foo5()", 133 " java.lang.Object --> java.lang.Object", 134 "Method: <E extends java.lang.Object >List<U>foo5()", 135 " List<U> --> List", 136 "Method: <F >List<String>foo55()", 137 " List<String> --> List", 138 "Method: Listfoo6(List x)", 139 " List --> List", 140 "Method: Listfoo6(List x)", 141 " List --> List", 142 "Method: Listfoo7(List<A> x)", 143 " List --> List", 144 "Method: Listfoo7(List<A> x)", 145 " List<A> --> List", 146 "Method: Listfoo8(A x)", 147 " List --> List", 148 "Method: Listfoo8(A x)", 149 " A --> Object", 150 "Method: Listfoo9(B x)", 151 " List --> List", 152 "Method: Listfoo9(B x)", 153 " B --> String", 154 "Method: Listfoo10(C x)", 155 " List --> List", 156 "Method: Listfoo10(C x)", 157 " C --> java.lang.Object", 158 "Method: <G extends Comparable >List<U>foo11(G x, C y)", 159 " Comparable --> Comparable", 160 "Method: <G extends Comparable >List<U>foo11(G x, C y)", 161 " List<U> --> List", 162 "Method: <G extends Comparable >List<U>foo11(G x, C y)", 163 " G --> Comparable", 164 "Method: <G extends Comparable >List<U>foo11(G x, C y)", 165 " C --> java.lang.Object", 166 "Method: <C extends Comparable >List<U>foo115(C x, B y)", 167 " Comparable --> Comparable", 168 "Method: <C extends Comparable >List<U>foo115(C x, B y)", 169 " List<U> --> List", 170 "Method: <C extends Comparable >List<U>foo115(C x, B y)", 171 " C --> Comparable", 172 "Method: <C extends Comparable >List<U>foo115(C x, B y)", 173 " B --> String", 174 "Method: <G extends Comparable >List<String>foo12(A x, List<B> y)", 175 " Comparable --> Comparable", 176 "Method: <G extends Comparable >List<String>foo12(A x, List<B> y)", 177 " List<String> --> List", 178 "Method: <G extends Comparable >List<String>foo12(A x, List<B> y)", 179 " A --> Object", 180 "Method: <G extends Comparable >List<String>foo12(A x, List<B> y)", 181 " List<B> --> List", 182 "Method: <G extends Comparable >List<String>foo13(A x, List<U> y)", 183 " Comparable --> Comparable", 184 "Method: <G extends Comparable >List<String>foo13(A x, List<U> y)", 185 " List<String> --> List", 186 "Method: <G extends Comparable >List<String>foo13(A x, List<U> y)", 187 " A --> Object", 188 "Method: <G extends Comparable >List<String>foo13(A x, List<U> y)", 189 " List<U> --> List", 190 "Method: <H extends java.lang.Object >List<String>foo14(H x)", 191 " java.lang.Object --> java.lang.Object", 192 "Method: <H extends java.lang.Object >List<String>foo14(H x)", 193 " List<String> --> List", 194 "Method: <H extends java.lang.Object >List<String>foo14(H x)", 195 " H --> java.lang.Object", 196 "Method: <H extends java.lang.Object >List<U>foo15(B x)", 197 " java.lang.Object --> java.lang.Object", 198 "Method: <H extends java.lang.Object >List<U>foo15(B x)", 199 " List<U> --> List", 200 "Method: <H extends java.lang.Object >List<U>foo15(B x)", 201 " B --> String", 202 "Method: <I >List<String>foo16(I x)", 203 " List<String> --> List", 204 "Method: <I >List<String>foo16(I x)", 205 " I --> Object", 206 "Method: <I >List<String>foo17(I[] x)", 207 " List<String> --> List", 208 "Method: <I >List<String>foo17(I[] x)", 209 " I --> Object", 210 "Method: <I >List<String>foo18(I[][] x)", 211 " List<String> --> List", 212 "Method: <I >List<String>foo18(I[][] x)", 213 " I --> Object", 214 "Method: <G extends Comparable >List<U>foo19(G[] x, C[] y)", 215 " Comparable --> Comparable", 216 "Method: <G extends Comparable >List<U>foo19(G[] x, C[] y)", 217 " List<U> --> List", 218 "Method: <G extends Comparable >List<U>foo19(G[] x, C[] y)", 219 " G --> Comparable", 220 "Method: <G extends Comparable >List<U>foo19(G[] x, C[] y)", 221 " C --> java.lang.Object", 222 "Method: // Ugh! But this is legal.//", 223 "/* */", 224 "List[]foo20(Comparable[][] x[], Object[] y[])[]", 225 " // Ugh! But this is legal.//", 226 "/* */", 227 "List --> // Ugh! But this is legal.//", 228 "/* */", 229 "List", 230 "Method: // Ugh! But this is legal.//", 231 "/* */", 232 "List[]foo20(Comparable[][] x[], Object[] y[])[]", 233 " Comparable --> Comparable", 234 "Method: // Ugh! But this is legal.//", 235 "/* */", 236 "List[]foo20(Comparable[][] x[], Object[] y[])[]", 237 " Object --> Object", 238 // This is illegal in Java 6. 239 // " Map --> Map", 240 // " U.Entry --> Map.Entry", 241 // "Method: voidfoo1(V x)", 242 // " V --> Map.Entry", 243 // "Method: voidfoo2(U.Entry x)", 244 // " U.Entry --> Map.Entry", 245 }; 246}