Wed, 15 Jan 2014 13:49:57 +0100
8028576: Incorrect RuntimeVisibleTypeAnnotations for exception parameters when not generating debuging info
Summary: The exception parameters with type annotations need to be added into the varBuffer even if not generating debug info
Reviewed-by: jjg, emc
jjg@1521 | 1 | /* |
jlahoda@2303 | 2 | * Copyright (c) 2009, 2014, 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; |
jlahoda@2303 | 33 | import java.util.Arrays; |
jjg@1521 | 34 | import java.util.Collections; |
jjg@1521 | 35 | import java.util.HashMap; |
jjg@1521 | 36 | import java.util.List; |
jjg@1521 | 37 | import java.util.Map; |
jjg@1521 | 38 | |
jjg@1521 | 39 | import com.sun.tools.classfile.ClassFile; |
jjg@1521 | 40 | import com.sun.tools.classfile.TypeAnnotation; |
jjg@1521 | 41 | import com.sun.tools.classfile.TypeAnnotation.TargetType; |
jjg@1521 | 42 | |
jjg@1521 | 43 | public class Driver { |
jjg@1521 | 44 | |
jjg@1521 | 45 | private static final PrintStream out = System.out; |
jjg@1521 | 46 | |
jjg@1521 | 47 | public static void main(String[] args) throws Exception { |
jjg@1521 | 48 | if (args.length == 0 || args.length > 1) |
jjg@1521 | 49 | throw new IllegalArgumentException("Usage: java Driver <test-name>"); |
jjg@1521 | 50 | String name = args[0]; |
jjg@1521 | 51 | Class<?> clazz = Class.forName(name); |
jjg@1521 | 52 | new Driver().runDriver(clazz.newInstance()); |
jjg@1521 | 53 | } |
jjg@1521 | 54 | |
jlahoda@2303 | 55 | String[][] extraParamsCombinations = new String[][] { |
jlahoda@2303 | 56 | new String[] { }, |
jlahoda@2303 | 57 | new String[] { "-g" }, |
jlahoda@2303 | 58 | }; |
jlahoda@2303 | 59 | |
jjg@1521 | 60 | protected void runDriver(Object object) throws Exception { |
jjg@1521 | 61 | int passed = 0, failed = 0; |
jjg@1521 | 62 | Class<?> clazz = object.getClass(); |
jjg@1521 | 63 | out.println("Tests for " + clazz.getName()); |
jjg@1521 | 64 | |
jjg@1521 | 65 | // Find methods |
jjg@1521 | 66 | for (Method method : clazz.getMethods()) { |
jjg@1521 | 67 | Map<String, TypeAnnotation.Position> expected = expectedOf(method); |
jjg@1521 | 68 | if (expected == null) |
jjg@1521 | 69 | continue; |
jjg@1521 | 70 | if (method.getReturnType() != String.class) |
jjg@1521 | 71 | throw new IllegalArgumentException("Test method needs to return a string: " + method); |
jjg@1521 | 72 | String testClass = testClassOf(method); |
jjg@1521 | 73 | |
jlahoda@2303 | 74 | for (String[] extraParams : extraParamsCombinations) { |
jlahoda@2303 | 75 | try { |
jlahoda@2303 | 76 | String compact = (String)method.invoke(object); |
jlahoda@2303 | 77 | String fullFile = wrap(compact); |
jlahoda@2303 | 78 | ClassFile cf = compileAndReturn(fullFile, testClass, extraParams); |
jlahoda@2303 | 79 | List<TypeAnnotation> actual = ReferenceInfoUtil.extendedAnnotationsOf(cf); |
jlahoda@2303 | 80 | ReferenceInfoUtil.compare(expected, actual, cf); |
jlahoda@2303 | 81 | out.println("PASSED: " + method.getName()); |
jlahoda@2303 | 82 | ++passed; |
jlahoda@2303 | 83 | } catch (Throwable e) { |
jlahoda@2303 | 84 | out.println("FAILED: " + method.getName()); |
jlahoda@2303 | 85 | out.println(" " + e.toString()); |
jlahoda@2303 | 86 | ++failed; |
jlahoda@2303 | 87 | } |
jjg@1521 | 88 | } |
jjg@1521 | 89 | } |
jjg@1521 | 90 | |
jjg@1521 | 91 | out.println(); |
jjg@1521 | 92 | int total = passed + failed; |
jjg@1521 | 93 | out.println(total + " total tests: " + passed + " PASSED, " + failed + " FAILED"); |
jjg@1521 | 94 | |
jjg@1521 | 95 | out.flush(); |
jjg@1521 | 96 | |
jjg@1521 | 97 | if (failed != 0) |
jjg@1521 | 98 | throw new RuntimeException(failed + " tests failed"); |
jjg@1521 | 99 | } |
jjg@1521 | 100 | |
jjg@1521 | 101 | private Map<String, TypeAnnotation.Position> expectedOf(Method m) { |
jjg@1521 | 102 | TADescription ta = m.getAnnotation(TADescription.class); |
jjg@1521 | 103 | TADescriptions tas = m.getAnnotation(TADescriptions.class); |
jjg@1521 | 104 | |
jjg@1521 | 105 | if (ta == null && tas == null) |
jjg@1521 | 106 | return null; |
jjg@1521 | 107 | |
jjg@1521 | 108 | Map<String, TypeAnnotation.Position> result = |
jjg@1521 | 109 | new HashMap<String, TypeAnnotation.Position>(); |
jjg@1521 | 110 | |
jjg@1521 | 111 | if (ta != null) |
jjg@1521 | 112 | result.putAll(expectedOf(ta)); |
jjg@1521 | 113 | |
jjg@1521 | 114 | if (tas != null) { |
jjg@1521 | 115 | for (TADescription a : tas.value()) { |
jjg@1521 | 116 | result.putAll(expectedOf(a)); |
jjg@1521 | 117 | } |
jjg@1521 | 118 | } |
jjg@1521 | 119 | |
jjg@1521 | 120 | return result; |
jjg@1521 | 121 | } |
jjg@1521 | 122 | |
jjg@1521 | 123 | private Map<String, TypeAnnotation.Position> expectedOf(TADescription d) { |
jjg@1521 | 124 | String annoName = d.annotation(); |
jjg@1521 | 125 | |
jjg@1521 | 126 | TypeAnnotation.Position p = new TypeAnnotation.Position(); |
jjg@1521 | 127 | p.type = d.type(); |
jjg@1521 | 128 | if (d.offset() != NOT_SET) |
jjg@1521 | 129 | p.offset = d.offset(); |
jjg@1521 | 130 | if (d.lvarOffset().length != 0) |
jjg@1521 | 131 | p.lvarOffset = d.lvarOffset(); |
jjg@1521 | 132 | if (d.lvarLength().length != 0) |
jjg@1521 | 133 | p.lvarLength = d.lvarLength(); |
jjg@1521 | 134 | if (d.lvarIndex().length != 0) |
jjg@1521 | 135 | p.lvarIndex = d.lvarIndex(); |
jjg@1521 | 136 | if (d.boundIndex() != NOT_SET) |
jjg@1521 | 137 | p.bound_index = d.boundIndex(); |
jjg@1521 | 138 | if (d.paramIndex() != NOT_SET) |
jjg@1521 | 139 | p.parameter_index = d.paramIndex(); |
jjg@1521 | 140 | if (d.typeIndex() != NOT_SET) |
jjg@1521 | 141 | p.type_index = d.typeIndex(); |
jjg@1521 | 142 | if (d.exceptionIndex() != NOT_SET) |
jjg@1521 | 143 | p.exception_index = d.exceptionIndex(); |
jjg@1521 | 144 | if (d.genericLocation().length != 0) { |
jjg@1521 | 145 | p.location = TypeAnnotation.Position.getTypePathFromBinary(wrapIntArray(d.genericLocation())); |
jjg@1521 | 146 | } |
jjg@1521 | 147 | |
jjg@1521 | 148 | return Collections.singletonMap(annoName, p); |
jjg@1521 | 149 | } |
jjg@1521 | 150 | |
jjg@1521 | 151 | private List<Integer> wrapIntArray(int[] ints) { |
jjg@1521 | 152 | List<Integer> list = new ArrayList<Integer>(ints.length); |
jjg@1521 | 153 | for (int i : ints) |
jjg@1521 | 154 | list.add(i); |
jjg@1521 | 155 | return list; |
jjg@1521 | 156 | } |
jjg@1521 | 157 | |
jjg@1521 | 158 | private String testClassOf(Method m) { |
jjg@1521 | 159 | TestClass tc = m.getAnnotation(TestClass.class); |
jjg@1521 | 160 | if (tc != null) { |
jjg@1521 | 161 | return tc.value(); |
jjg@1521 | 162 | } else { |
jjg@1521 | 163 | return "Test"; |
jjg@1521 | 164 | } |
jjg@1521 | 165 | } |
jjg@1521 | 166 | |
jlahoda@2303 | 167 | private ClassFile compileAndReturn(String fullFile, String testClass, String... extraParams) throws Exception { |
jjg@1521 | 168 | File source = writeTestFile(fullFile); |
jjg@1521 | 169 | File clazzFile = compileTestFile(source, testClass); |
jjg@1521 | 170 | return ClassFile.read(clazzFile); |
jjg@1521 | 171 | } |
jjg@1521 | 172 | |
jjg@1521 | 173 | protected File writeTestFile(String fullFile) throws IOException { |
jjg@1521 | 174 | File f = new File("Test.java"); |
jjg@1521 | 175 | PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(f))); |
jjg@1521 | 176 | out.println(fullFile); |
jjg@1521 | 177 | out.close(); |
jjg@1521 | 178 | return f; |
jjg@1521 | 179 | } |
jjg@1521 | 180 | |
jlahoda@2303 | 181 | protected File compileTestFile(File f, String testClass, String... extraParams) { |
jlahoda@2303 | 182 | List<String> options = new ArrayList<>(); |
jlahoda@2303 | 183 | options.addAll(Arrays.asList("-source", "1.8")); |
jlahoda@2303 | 184 | options.addAll(Arrays.asList(extraParams)); |
jlahoda@2303 | 185 | options.add(f.getPath()); |
jlahoda@2303 | 186 | int rc = com.sun.tools.javac.Main.compile(options.toArray(new String[options.size()])); |
jjg@1521 | 187 | if (rc != 0) |
jjg@1521 | 188 | throw new Error("compilation failed. rc=" + rc); |
jjg@1521 | 189 | String path; |
jjg@1521 | 190 | if (f.getParent() != null) { |
jjg@1521 | 191 | path = f.getParent(); |
jjg@1521 | 192 | } else { |
jjg@1521 | 193 | path = ""; |
jjg@1521 | 194 | } |
jjg@1521 | 195 | |
jjg@1521 | 196 | return new File(path + testClass + ".class"); |
jjg@1521 | 197 | } |
jjg@1521 | 198 | |
jjg@1521 | 199 | private String wrap(String compact) { |
jjg@1521 | 200 | StringBuilder sb = new StringBuilder(); |
jjg@1521 | 201 | |
jjg@1521 | 202 | // Automatically import java.util |
jjg@1521 | 203 | sb.append("\nimport java.util.*;"); |
jjg@1521 | 204 | sb.append("\nimport java.lang.annotation.*;"); |
jjg@1521 | 205 | |
jjg@1521 | 206 | sb.append("\n\n"); |
jjg@1521 | 207 | boolean isSnippet = !(compact.startsWith("class") |
jjg@1521 | 208 | || compact.contains(" class")) |
jjg@1521 | 209 | && !compact.contains("interface") |
jjg@1521 | 210 | && !compact.contains("enum"); |
jjg@1521 | 211 | if (isSnippet) |
jjg@1521 | 212 | sb.append("class Test {\n"); |
jjg@1521 | 213 | |
jjg@1521 | 214 | sb.append(compact); |
jjg@1521 | 215 | sb.append("\n"); |
jjg@1521 | 216 | |
jjg@1521 | 217 | if (isSnippet) |
jjg@1521 | 218 | sb.append("}\n\n"); |
jjg@1521 | 219 | |
jjg@1521 | 220 | if (isSnippet) { |
jjg@1521 | 221 | // Have a few common nested types for testing |
jjg@1755 | 222 | sb.append("class Outer { class Inner {} class Middle { class MInner {} } }"); |
jjg@1521 | 223 | sb.append("class SOuter { static class SInner {} }"); |
jjg@1521 | 224 | sb.append("class GOuter<X, Y> { class GInner<X, Y> {} }"); |
jjg@1521 | 225 | } |
jjg@1521 | 226 | |
jjg@1521 | 227 | // create A ... F annotation declarations |
jjg@1521 | 228 | sb.append("\n@interface A {}"); |
jjg@1521 | 229 | sb.append("\n@interface B {}"); |
jjg@1521 | 230 | sb.append("\n@interface C {}"); |
jjg@1521 | 231 | sb.append("\n@interface D {}"); |
jjg@1521 | 232 | sb.append("\n@interface E {}"); |
jjg@1521 | 233 | sb.append("\n@interface F {}"); |
jjg@1521 | 234 | |
jjg@1521 | 235 | // create TA ... TF proper type annotations |
jjg@1521 | 236 | sb.append("\n"); |
jjg@1521 | 237 | sb.append("\n@Target({ElementType.TYPE_USE, ElementType.TYPE_PARAMETER}) @interface TA {}"); |
jjg@1521 | 238 | sb.append("\n@Target({ElementType.TYPE_USE, ElementType.TYPE_PARAMETER}) @interface TB {}"); |
jjg@1521 | 239 | sb.append("\n@Target({ElementType.TYPE_USE, ElementType.TYPE_PARAMETER}) @interface TC {}"); |
jjg@1521 | 240 | sb.append("\n@Target({ElementType.TYPE_USE, ElementType.TYPE_PARAMETER}) @interface TD {}"); |
jjg@1521 | 241 | sb.append("\n@Target({ElementType.TYPE_USE, ElementType.TYPE_PARAMETER}) @interface TE {}"); |
jjg@1521 | 242 | sb.append("\n@Target({ElementType.TYPE_USE, ElementType.TYPE_PARAMETER}) @interface TF {}"); |
jjg@1521 | 243 | sb.append("\n@Target({ElementType.TYPE_USE, ElementType.TYPE_PARAMETER}) @interface TG {}"); |
jjg@1521 | 244 | sb.append("\n@Target({ElementType.TYPE_USE, ElementType.TYPE_PARAMETER}) @interface TH {}"); |
jjg@1521 | 245 | sb.append("\n@Target({ElementType.TYPE_USE, ElementType.TYPE_PARAMETER}) @interface TI {}"); |
jjg@1521 | 246 | sb.append("\n@Target({ElementType.TYPE_USE, ElementType.TYPE_PARAMETER}) @interface TJ {}"); |
jjg@1521 | 247 | sb.append("\n@Target({ElementType.TYPE_USE, ElementType.TYPE_PARAMETER}) @interface TK {}"); |
jjg@1521 | 248 | sb.append("\n@Target({ElementType.TYPE_USE, ElementType.TYPE_PARAMETER}) @interface TL {}"); |
jjg@1521 | 249 | sb.append("\n@Target({ElementType.TYPE_USE, ElementType.TYPE_PARAMETER}) @interface TM {}"); |
jjg@1521 | 250 | |
jjg@1521 | 251 | // create RTA, RTAs, RTB, RTBs for repeating type annotations |
jjg@1521 | 252 | sb.append("\n"); |
jjg@1521 | 253 | sb.append("\n@Repeatable(RTAs.class) @Target({ElementType.TYPE_USE, ElementType.TYPE_PARAMETER}) @interface RTA {}"); |
jjg@1521 | 254 | sb.append("\n@Repeatable(RTBs.class) @Target({ElementType.TYPE_USE, ElementType.TYPE_PARAMETER}) @interface RTB {}"); |
jjg@1521 | 255 | |
darcy@1531 | 256 | sb.append("\n@Target({ElementType.TYPE_USE, ElementType.TYPE_PARAMETER}) @interface RTAs { RTA[] value(); }"); |
darcy@1531 | 257 | sb.append("\n@Target({ElementType.TYPE_USE, ElementType.TYPE_PARAMETER}) @interface RTBs { RTB[] value(); }"); |
jjg@1521 | 258 | |
jjg@1521 | 259 | sb.append("\n@Target(value={ElementType.TYPE,ElementType.FIELD,ElementType.METHOD,ElementType.PARAMETER,ElementType.CONSTRUCTOR,ElementType.LOCAL_VARIABLE})"); |
jjg@1521 | 260 | sb.append("\n@interface Decl {}"); |
jjg@1521 | 261 | |
jjg@1521 | 262 | return sb.toString(); |
jjg@1521 | 263 | } |
jjg@1521 | 264 | |
jjg@1521 | 265 | public static final int NOT_SET = -888; |
jjg@1521 | 266 | |
jjg@1521 | 267 | } |
jjg@1521 | 268 | |
jjg@1521 | 269 | @Retention(RetentionPolicy.RUNTIME) |
jjg@1521 | 270 | @Target(ElementType.METHOD) |
jjg@1521 | 271 | @interface TADescription { |
jjg@1521 | 272 | String annotation(); |
jjg@1521 | 273 | |
jjg@1521 | 274 | TargetType type(); |
jjg@1521 | 275 | int offset() default Driver.NOT_SET; |
jjg@1521 | 276 | int[] lvarOffset() default { }; |
jjg@1521 | 277 | int[] lvarLength() default { }; |
jjg@1521 | 278 | int[] lvarIndex() default { }; |
jjg@1521 | 279 | int boundIndex() default Driver.NOT_SET; |
jjg@1521 | 280 | int paramIndex() default Driver.NOT_SET; |
jjg@1521 | 281 | int typeIndex() default Driver.NOT_SET; |
jjg@1521 | 282 | int exceptionIndex() default Driver.NOT_SET; |
jjg@1521 | 283 | |
jjg@1521 | 284 | int[] genericLocation() default {}; |
jjg@1521 | 285 | } |
jjg@1521 | 286 | |
jjg@1521 | 287 | @Retention(RetentionPolicy.RUNTIME) |
jjg@1521 | 288 | @Target(ElementType.METHOD) |
jjg@1521 | 289 | @interface TADescriptions { |
jjg@1521 | 290 | TADescription[] value() default {}; |
jjg@1521 | 291 | } |
jjg@1521 | 292 | |
jjg@1521 | 293 | /** |
jjg@1521 | 294 | * The name of the class that should be analyzed. |
jjg@1521 | 295 | * Should only need to be provided when analyzing inner classes. |
jjg@1521 | 296 | */ |
jjg@1521 | 297 | @Retention(RetentionPolicy.RUNTIME) |
jjg@1521 | 298 | @Target(ElementType.METHOD) |
jjg@1521 | 299 | @interface TestClass { |
jjg@1521 | 300 | String value() default "Test"; |
jjg@1521 | 301 | } |