Sat, 22 Dec 2018 13:10:38 -0800
Added tag jdk8u202-b25 for changeset 15d8c732039b
emc@1808 | 1 | /* |
emc@1808 | 2 | * Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved. |
emc@1808 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
emc@1808 | 4 | * |
emc@1808 | 5 | * This code is free software; you can redistribute it and/or modify it |
emc@1808 | 6 | * under the terms of the GNU General Public License version 2 only, as |
emc@1808 | 7 | * published by the Free Software Foundation. |
emc@1808 | 8 | * |
emc@1808 | 9 | * This code is distributed in the hope that it will be useful, but WITHOUT |
emc@1808 | 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
emc@1808 | 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
emc@1808 | 12 | * version 2 for more details (a copy is included in the LICENSE file that |
emc@1808 | 13 | * accompanied this code). |
emc@1808 | 14 | * |
emc@1808 | 15 | * You should have received a copy of the GNU General Public License version |
emc@1808 | 16 | * 2 along with this work; if not, write to the Free Software Foundation, |
emc@1808 | 17 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
emc@1808 | 18 | * |
emc@1808 | 19 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
emc@1808 | 20 | * or visit www.oracle.com if you need additional information or have any |
emc@1808 | 21 | * questions. |
emc@1808 | 22 | */ |
emc@1808 | 23 | |
emc@1808 | 24 | /* |
emc@1808 | 25 | * @test |
emc@1808 | 26 | * @bug 8015701 |
emc@1975 | 27 | * @summary Test method parameter attribute generation with captured locals. |
emc@1975 | 28 | * @compile -parameters CaptureTest.java |
emc@1975 | 29 | * @run main CaptureTest |
emc@1808 | 30 | */ |
emc@1808 | 31 | import java.lang.Class; |
emc@1808 | 32 | import java.lang.reflect.Constructor; |
emc@1808 | 33 | import java.lang.reflect.Parameter; |
emc@1975 | 34 | import java.lang.reflect.Modifier; |
emc@1975 | 35 | import java.util.List; |
emc@1975 | 36 | import java.util.ArrayList; |
emc@1808 | 37 | |
emc@1975 | 38 | public class CaptureTest { |
emc@1808 | 39 | |
emc@1975 | 40 | private static final int SYNTHETIC = 0x1000; |
emc@1975 | 41 | private static final int MANDATED = 0x8000; |
emc@1808 | 42 | |
emc@1808 | 43 | public static void main(String... args) throws Exception { |
emc@1975 | 44 | new CaptureTest().run(); |
emc@1808 | 45 | } |
emc@1808 | 46 | |
emc@1808 | 47 | |
emc@1975 | 48 | private void run() throws Exception { |
emc@1975 | 49 | final Encloser pn = new Encloser(); |
emc@1808 | 50 | |
emc@1975 | 51 | /* Cases covered here: |
emc@1975 | 52 | * |
emc@1975 | 53 | * - Local class |
emc@1975 | 54 | * - Inner class |
emc@1975 | 55 | * - Anonymous class |
emc@1975 | 56 | * - Anonymous class extending a local |
emc@1975 | 57 | * - Anonymous class extending an inner |
emc@1975 | 58 | */ |
emc@1975 | 59 | pn.makeLocal("hello").check(); |
emc@1975 | 60 | pn.makeInner("hello").check(); |
emc@1975 | 61 | pn.makeAnon("hello").check(); |
emc@1975 | 62 | pn.makeAnonExtendsLocal("hello").check(); |
emc@1975 | 63 | pn.makeAnonExtendsInner("hello").check(); |
emc@1975 | 64 | |
emc@1975 | 65 | if (0 != errors) |
emc@1808 | 66 | throw new Exception("MethodParameters test failed with " + |
emc@1808 | 67 | errors + " errors"); |
emc@1808 | 68 | } |
emc@1808 | 69 | |
emc@1975 | 70 | private void error(final String msg) { |
emc@1808 | 71 | System.err.println("Error: " + msg); |
emc@1808 | 72 | errors++; |
emc@1808 | 73 | } |
emc@1808 | 74 | |
emc@1808 | 75 | int errors; |
emc@1808 | 76 | |
emc@1975 | 77 | abstract class Tester { |
emc@1808 | 78 | |
emc@1975 | 79 | public Tester(final int param) {} |
emc@1975 | 80 | |
emc@1975 | 81 | protected abstract String[] names(); |
emc@1975 | 82 | protected abstract int[] modifiers(); |
emc@1975 | 83 | protected abstract Class[] types(); |
emc@1975 | 84 | |
emc@1975 | 85 | public void check() { |
emc@1975 | 86 | final Class<?> cls = this.getClass(); |
emc@1975 | 87 | final Constructor<?> ctor = cls.getDeclaredConstructors()[0]; |
emc@1975 | 88 | final Parameter[] params = ctor.getParameters(); |
emc@1975 | 89 | final String[] names = names(); |
emc@1975 | 90 | final int[] modifiers = modifiers(); |
emc@1975 | 91 | final Class[] types = types(); |
emc@1975 | 92 | |
emc@1975 | 93 | System.err.println("Testing class " + cls); |
emc@1975 | 94 | |
emc@1975 | 95 | if (params.length == names.length) { |
emc@1975 | 96 | for (int i = 0; i < names.length; i++) { |
emc@1975 | 97 | System.err.println("Testing parameter " + params[i].getName()); |
emc@1975 | 98 | if (!params[i].getName().equals(names[i])) |
emc@1975 | 99 | error("Expected parameter name " + names[i] + |
emc@1975 | 100 | " got " + params[i].getName()); |
emc@1975 | 101 | if (params[i].getModifiers() != modifiers[i]) |
emc@1975 | 102 | error("Expected parameter modifiers " + |
emc@1975 | 103 | modifiers[i] + " got " + |
emc@1975 | 104 | params[i].getModifiers()); |
emc@1975 | 105 | if (!params[i].getType().equals(types[i])) |
emc@1975 | 106 | error("Expected parameter type " + types[i] + |
emc@1975 | 107 | " got " + params[i].getType()); |
emc@1975 | 108 | } |
emc@1975 | 109 | } else |
emc@1975 | 110 | error("Expected " + names.length + " parameters"); |
emc@1975 | 111 | |
emc@1975 | 112 | } |
emc@1975 | 113 | |
emc@1808 | 114 | } |
emc@1808 | 115 | |
emc@1975 | 116 | class Encloser { |
emc@1975 | 117 | private class InnerTester extends Tester { |
emc@1975 | 118 | public InnerTester(final int innerparam) { |
emc@1975 | 119 | super(innerparam); |
emc@1975 | 120 | } |
emc@1975 | 121 | |
emc@1975 | 122 | protected String[] names() { |
emc@1975 | 123 | return new String[] { |
emc@1975 | 124 | "this$1", |
emc@1975 | 125 | "innerparam" |
emc@1975 | 126 | }; |
emc@1975 | 127 | } |
emc@1975 | 128 | |
emc@1975 | 129 | protected int[] modifiers() { |
emc@1975 | 130 | return new int[] { |
emc@1975 | 131 | Modifier.FINAL | SYNTHETIC, |
emc@1975 | 132 | Modifier.FINAL |
emc@1975 | 133 | }; |
emc@1975 | 134 | } |
emc@1975 | 135 | |
emc@1975 | 136 | protected Class[] types() { |
emc@1975 | 137 | return new Class[] { |
emc@1975 | 138 | Encloser.class, |
emc@1975 | 139 | int.class |
emc@1975 | 140 | }; |
emc@1975 | 141 | } |
emc@1975 | 142 | } |
emc@1975 | 143 | |
emc@1975 | 144 | public Tester makeInner(final String message) { |
emc@1975 | 145 | return new InnerTester(2); |
emc@1975 | 146 | } |
emc@1975 | 147 | |
emc@1975 | 148 | public Tester makeLocal(final String message) { |
emc@1975 | 149 | class LocalTester extends Tester { |
emc@1975 | 150 | public LocalTester(final int localparam) { |
emc@1975 | 151 | super(localparam); |
emc@1975 | 152 | } |
emc@1975 | 153 | |
emc@1975 | 154 | protected String[] names() { |
emc@1975 | 155 | return new String[] { |
emc@1975 | 156 | "this$1", |
emc@1975 | 157 | "localparam", |
emc@1975 | 158 | "val$message" |
emc@1975 | 159 | }; |
emc@1975 | 160 | } |
emc@1975 | 161 | |
emc@1975 | 162 | protected int[] modifiers() { |
emc@1975 | 163 | return new int[] { |
emc@1975 | 164 | Modifier.FINAL | MANDATED, |
emc@1975 | 165 | Modifier.FINAL, |
emc@1975 | 166 | Modifier.FINAL | SYNTHETIC |
emc@1975 | 167 | }; |
emc@1975 | 168 | } |
emc@1975 | 169 | |
emc@1975 | 170 | protected Class[] types() { |
emc@1975 | 171 | return new Class[] { |
emc@1975 | 172 | Encloser.class, |
emc@1975 | 173 | int.class, |
emc@1975 | 174 | String.class |
emc@1975 | 175 | }; |
emc@1975 | 176 | } |
emc@1975 | 177 | |
emc@1975 | 178 | public String message() { |
emc@1975 | 179 | return message; |
emc@1975 | 180 | } |
emc@1975 | 181 | } |
emc@1975 | 182 | |
emc@1975 | 183 | return new LocalTester(2); |
emc@1975 | 184 | } |
emc@1975 | 185 | |
emc@1975 | 186 | public Tester makeAnonExtendsLocal(final String message) { |
emc@1975 | 187 | abstract class LocalTester extends Tester { |
emc@1975 | 188 | public LocalTester(final int localparam) { |
emc@1975 | 189 | super(localparam); |
emc@1975 | 190 | } |
emc@1975 | 191 | |
emc@1975 | 192 | protected String[] names() { |
emc@1975 | 193 | return new String[] { |
emc@1975 | 194 | "this$1", |
emc@1975 | 195 | "localparam", |
emc@1975 | 196 | "val$message" |
emc@1975 | 197 | }; |
emc@1975 | 198 | } |
emc@1975 | 199 | |
emc@1975 | 200 | protected int[] modifiers() { |
emc@1975 | 201 | return new int[] { |
emc@1975 | 202 | Modifier.FINAL | MANDATED, |
emc@1975 | 203 | Modifier.FINAL, |
emc@1975 | 204 | Modifier.FINAL | SYNTHETIC |
emc@1975 | 205 | }; |
emc@1975 | 206 | } |
emc@1975 | 207 | |
emc@1975 | 208 | protected Class[] types() { |
emc@1975 | 209 | return new Class[] { |
emc@1975 | 210 | Encloser.class, |
emc@1975 | 211 | int.class, |
emc@1975 | 212 | String.class |
emc@1975 | 213 | }; |
emc@1975 | 214 | } |
emc@1975 | 215 | |
emc@1975 | 216 | } |
emc@1975 | 217 | |
emc@1975 | 218 | return new LocalTester(2) { |
emc@1975 | 219 | public String message() { |
emc@1975 | 220 | return message; |
emc@1975 | 221 | } |
emc@1975 | 222 | }; |
emc@1975 | 223 | } |
emc@1975 | 224 | |
emc@1975 | 225 | public Tester makeAnonExtendsInner(final String message) { |
emc@1975 | 226 | return new InnerTester(2) { |
emc@1975 | 227 | protected String[] names() { |
emc@1975 | 228 | return new String[] { |
emc@1975 | 229 | "this$1", |
emc@1975 | 230 | "innerparam", |
emc@1975 | 231 | "val$message" |
emc@1975 | 232 | }; |
emc@1975 | 233 | } |
emc@1975 | 234 | |
emc@1975 | 235 | protected int[] modifiers() { |
emc@1975 | 236 | return new int[] { |
emc@1975 | 237 | Modifier.FINAL | MANDATED, |
emc@1975 | 238 | Modifier.FINAL, |
emc@1975 | 239 | Modifier.FINAL | SYNTHETIC |
emc@1975 | 240 | }; |
emc@1975 | 241 | } |
emc@1975 | 242 | |
emc@1975 | 243 | protected Class[] types() { |
emc@1975 | 244 | return new Class[] { |
emc@1975 | 245 | Encloser.class, |
emc@1975 | 246 | int.class, |
emc@1975 | 247 | String.class |
emc@1975 | 248 | }; |
emc@1975 | 249 | } |
emc@1975 | 250 | |
emc@1975 | 251 | public String message() { |
emc@1975 | 252 | return message; |
emc@1975 | 253 | } |
emc@1975 | 254 | }; |
emc@1975 | 255 | } |
emc@1975 | 256 | |
emc@1975 | 257 | public Tester makeAnon(final String message) { |
emc@1975 | 258 | return new Tester(2) { |
emc@1975 | 259 | protected String[] names() { |
emc@1975 | 260 | return new String[] { |
emc@1975 | 261 | "this$1", |
emc@1975 | 262 | "param", |
emc@1975 | 263 | "val$message" |
emc@1975 | 264 | }; |
emc@1975 | 265 | } |
emc@1975 | 266 | |
emc@1975 | 267 | protected int[] modifiers() { |
emc@1975 | 268 | return new int[] { |
emc@1975 | 269 | Modifier.FINAL | MANDATED, |
emc@1975 | 270 | Modifier.FINAL, |
emc@1975 | 271 | Modifier.FINAL | SYNTHETIC |
emc@1975 | 272 | }; |
emc@1975 | 273 | } |
emc@1975 | 274 | |
emc@1975 | 275 | protected Class[] types() { |
emc@1975 | 276 | return new Class[] { |
emc@1975 | 277 | Encloser.class, |
emc@1975 | 278 | int.class, |
emc@1975 | 279 | String.class |
emc@1975 | 280 | }; |
emc@1975 | 281 | } |
emc@1975 | 282 | |
emc@1975 | 283 | public String message() { |
emc@1975 | 284 | return message; |
emc@1975 | 285 | } |
emc@1975 | 286 | }; |
emc@1975 | 287 | } |
emc@1808 | 288 | } |
emc@1808 | 289 | } |