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