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