1 /* 2 * Copyright (c) 2009, 2014, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 import java.io.BufferedWriter; 25 import java.io.File; 26 import java.io.FileWriter; 27 import java.io.IOException; 28 import java.io.PrintStream; 29 import java.io.PrintWriter; 30 import java.lang.annotation.*; 31 import java.lang.reflect.*; 32 import java.util.ArrayList; 33 import java.util.Arrays; 34 import java.util.Collections; 35 import java.util.HashMap; 36 import java.util.List; 37 import java.util.Map; 38 39 import com.sun.tools.classfile.ClassFile; 40 import com.sun.tools.classfile.TypeAnnotation; 41 import com.sun.tools.classfile.TypeAnnotation.TargetType; 42 43 public class Driver { 44 45 private static final PrintStream out = System.out; 46 47 public static void main(String[] args) throws Exception { 48 if (args.length == 0 || args.length > 1) 49 throw new IllegalArgumentException("Usage: java Driver <test-name>"); 50 String name = args[0]; 51 Class<?> clazz = Class.forName(name); 52 new Driver().runDriver(clazz.newInstance()); 53 } 54 55 String[][] extraParamsCombinations = new String[][] { 56 new String[] { }, 57 new String[] { "-g" }, 58 }; 59 60 protected void runDriver(Object object) throws Exception { 61 int passed = 0, failed = 0; 62 Class<?> clazz = object.getClass(); 63 out.println("Tests for " + clazz.getName()); 64 65 // Find methods 66 for (Method method : clazz.getMethods()) { 67 Map<String, TypeAnnotation.Position> expected = expectedOf(method); 68 if (expected == null) 69 continue; 70 if (method.getReturnType() != String.class) 71 throw new IllegalArgumentException("Test method needs to return a string: " + method); 72 String testClass = testClassOf(method); 73 74 for (String[] extraParams : extraParamsCombinations) { 75 try { 76 String compact = (String)method.invoke(object); 77 String fullFile = wrap(compact); 78 ClassFile cf = compileAndReturn(fullFile, testClass, extraParams); 79 List<TypeAnnotation> actual = ReferenceInfoUtil.extendedAnnotationsOf(cf); 80 ReferenceInfoUtil.compare(expected, actual, cf); 81 out.println("PASSED: " + method.getName()); 82 ++passed; 83 } catch (Throwable e) { 84 out.println("FAILED: " + method.getName()); 85 out.println(" " + e.toString()); 86 ++failed; 87 } 88 } 89 } 90 91 out.println(); 92 int total = passed + failed; 93 out.println(total + " total tests: " + passed + " PASSED, " + failed + " FAILED"); 94 95 out.flush(); 96 97 if (failed != 0) 98 throw new RuntimeException(failed + " tests failed"); 99 } 100 101 private Map<String, TypeAnnotation.Position> expectedOf(Method m) { 102 TADescription ta = m.getAnnotation(TADescription.class); 103 TADescriptions tas = m.getAnnotation(TADescriptions.class); 104 105 if (ta == null && tas == null) 106 return null; 107 108 Map<String, TypeAnnotation.Position> result = 109 new HashMap<String, TypeAnnotation.Position>(); 110 111 if (ta != null) 112 result.putAll(expectedOf(ta)); 113 114 if (tas != null) { 115 for (TADescription a : tas.value()) { 116 result.putAll(expectedOf(a)); 117 } 118 } 119 120 return result; 121 } 122 123 private Map<String, TypeAnnotation.Position> expectedOf(TADescription d) { 124 String annoName = d.annotation(); 125 126 TypeAnnotation.Position p = new TypeAnnotation.Position(); 127 p.type = d.type(); 128 if (d.offset() != NOT_SET) 129 p.offset = d.offset(); 130 if (d.lvarOffset().length != 0) 131 p.lvarOffset = d.lvarOffset(); 132 if (d.lvarLength().length != 0) 133 p.lvarLength = d.lvarLength(); 134 if (d.lvarIndex().length != 0) 135 p.lvarIndex = d.lvarIndex(); 136 if (d.boundIndex() != NOT_SET) 137 p.bound_index = d.boundIndex(); 138 if (d.paramIndex() != NOT_SET) 139 p.parameter_index = d.paramIndex(); 140 if (d.typeIndex() != NOT_SET) 141 p.type_index = d.typeIndex(); 142 if (d.exceptionIndex() != NOT_SET) 143 p.exception_index = d.exceptionIndex(); 144 if (d.genericLocation().length != 0) { 145 p.location = TypeAnnotation.Position.getTypePathFromBinary(wrapIntArray(d.genericLocation())); 146 } 147 148 return Collections.singletonMap(annoName, p); 149 } 150 151 private List<Integer> wrapIntArray(int[] ints) { 152 List<Integer> list = new ArrayList<Integer>(ints.length); 153 for (int i : ints) 154 list.add(i); 155 return list; 156 } 157 158 private String testClassOf(Method m) { 159 TestClass tc = m.getAnnotation(TestClass.class); 160 if (tc != null) { 161 return tc.value(); 162 } else { 163 return "Test"; 164 } 165 } 166 167 private ClassFile compileAndReturn(String fullFile, String testClass, String... extraParams) throws Exception { 168 File source = writeTestFile(fullFile); 169 File clazzFile = compileTestFile(source, testClass); 170 return ClassFile.read(clazzFile); 171 } 172 173 protected File writeTestFile(String fullFile) throws IOException { 174 File f = new File("Test.java"); 175 PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(f))); 176 out.println(fullFile); 177 out.close(); 178 return f; 179 } 180 181 protected File compileTestFile(File f, String testClass, String... extraParams) { 182 List<String> options = new ArrayList<>(); 183 options.addAll(Arrays.asList(extraParams)); 184 options.add(f.getPath()); 185 int rc = com.sun.tools.javac.Main.compile(options.toArray(new String[options.size()])); 186 if (rc != 0) 187 throw new Error("compilation failed. rc=" + rc); 188 String path; 189 if (f.getParent() != null) { 190 path = f.getParent(); 191 } else { 192 path = ""; 193 } 194 195 return new File(path + testClass + ".class"); 196 } 197 198 private String wrap(String compact) { 199 StringBuilder sb = new StringBuilder(); 200 201 // Automatically import java.util 202 sb.append("\nimport java.util.*;"); 203 sb.append("\nimport java.lang.annotation.*;"); 204 205 sb.append("\n\n"); 206 boolean isSnippet = !(compact.startsWith("class") 207 || compact.contains(" class")) 208 && !compact.contains("interface") 209 && !compact.contains("enum"); 210 if (isSnippet) 211 sb.append("class Test {\n"); 212 213 sb.append(compact); 214 sb.append("\n"); 215 216 if (isSnippet) 217 sb.append("}\n\n"); 218 219 if (isSnippet) { 220 // Have a few common nested types for testing 221 sb.append("class Outer { class Inner {} class Middle { class MInner {} } }"); 222 sb.append("class SOuter { static class SInner {} }"); 223 sb.append("class GOuter<X, Y> { class GInner<X, Y> {} }"); 224 } 225 226 // create A ... F annotation declarations 227 sb.append("\n@interface A {}"); 228 sb.append("\n@interface B {}"); 229 sb.append("\n@interface C {}"); 230 sb.append("\n@interface D {}"); 231 sb.append("\n@interface E {}"); 232 sb.append("\n@interface F {}"); 233 234 // create TA ... TF proper type annotations 235 sb.append("\n"); 236 sb.append("\n@Target({ElementType.TYPE_USE, ElementType.TYPE_PARAMETER}) @interface TA {}"); 237 sb.append("\n@Target({ElementType.TYPE_USE, ElementType.TYPE_PARAMETER}) @interface TB {}"); 238 sb.append("\n@Target({ElementType.TYPE_USE, ElementType.TYPE_PARAMETER}) @interface TC {}"); 239 sb.append("\n@Target({ElementType.TYPE_USE, ElementType.TYPE_PARAMETER}) @interface TD {}"); 240 sb.append("\n@Target({ElementType.TYPE_USE, ElementType.TYPE_PARAMETER}) @interface TE {}"); 241 sb.append("\n@Target({ElementType.TYPE_USE, ElementType.TYPE_PARAMETER}) @interface TF {}"); 242 sb.append("\n@Target({ElementType.TYPE_USE, ElementType.TYPE_PARAMETER}) @interface TG {}"); 243 sb.append("\n@Target({ElementType.TYPE_USE, ElementType.TYPE_PARAMETER}) @interface TH {}"); 244 sb.append("\n@Target({ElementType.TYPE_USE, ElementType.TYPE_PARAMETER}) @interface TI {}"); 245 sb.append("\n@Target({ElementType.TYPE_USE, ElementType.TYPE_PARAMETER}) @interface TJ {}"); 246 sb.append("\n@Target({ElementType.TYPE_USE, ElementType.TYPE_PARAMETER}) @interface TK {}"); 247 sb.append("\n@Target({ElementType.TYPE_USE, ElementType.TYPE_PARAMETER}) @interface TL {}"); 248 sb.append("\n@Target({ElementType.TYPE_USE, ElementType.TYPE_PARAMETER}) @interface TM {}"); 249 250 // create RTA, RTAs, RTB, RTBs for repeating type annotations 251 sb.append("\n"); 252 sb.append("\n@Repeatable(RTAs.class) @Target({ElementType.TYPE_USE, ElementType.TYPE_PARAMETER}) @interface RTA {}"); 253 sb.append("\n@Repeatable(RTBs.class) @Target({ElementType.TYPE_USE, ElementType.TYPE_PARAMETER}) @interface RTB {}"); 254 255 sb.append("\n@Target({ElementType.TYPE_USE, ElementType.TYPE_PARAMETER}) @interface RTAs { RTA[] value(); }"); 256 sb.append("\n@Target({ElementType.TYPE_USE, ElementType.TYPE_PARAMETER}) @interface RTBs { RTB[] value(); }"); 257 258 sb.append("\n@Target(value={ElementType.TYPE,ElementType.FIELD,ElementType.METHOD,ElementType.PARAMETER,ElementType.CONSTRUCTOR,ElementType.LOCAL_VARIABLE})"); 259 sb.append("\n@interface Decl {}"); 260 261 return sb.toString(); 262 } 263 264 public static final int NOT_SET = -888; 265 266 } 267 268 @Retention(RetentionPolicy.RUNTIME) 269 @Target(ElementType.METHOD) 270 @interface TADescription { 271 String annotation(); 272 273 TargetType type(); 274 int offset() default Driver.NOT_SET; 275 int[] lvarOffset() default { }; 276 int[] lvarLength() default { }; 277 int[] lvarIndex() default { }; 278 int boundIndex() default Driver.NOT_SET; 279 int paramIndex() default Driver.NOT_SET; 280 int typeIndex() default Driver.NOT_SET; 281 int exceptionIndex() default Driver.NOT_SET; 282 283 int[] genericLocation() default {}; 284 } 285 286 @Retention(RetentionPolicy.RUNTIME) 287 @Target(ElementType.METHOD) 288 @interface TADescriptions { 289 TADescription[] value() default {}; 290 } 291 292 /** 293 * The name of the class that should be analyzed. 294 * Should only need to be provided when analyzing inner classes. 295 */ 296 @Retention(RetentionPolicy.RUNTIME) 297 @Target(ElementType.METHOD) 298 @interface TestClass { 299 String value() default "Test"; 300 }