test/tools/javac/annotations/typeAnnotations/referenceinfos/Lambda.java

Thu, 31 Aug 2017 15:17:03 +0800

author
aoqi
date
Thu, 31 Aug 2017 15:17:03 +0800
changeset 2525
2eb010b6cb22
parent 2222
8832b6048e65
parent 0
959103a6100f
permissions
-rw-r--r--

merge

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 2013, 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 /*
aoqi@0 25 * @test
aoqi@0 26 * @bug 8008077 8029721
aoqi@0 27 * @summary Test population of reference info for lambda expressions
aoqi@0 28 * javac crash for annotated parameter type of lambda in a field
aoqi@0 29 * @compile -g Driver.java ReferenceInfoUtil.java Lambda.java
aoqi@0 30 * @run main Driver Lambda
aoqi@0 31 * @author Werner Dietl
aoqi@0 32 */
aoqi@0 33
aoqi@0 34 import static com.sun.tools.classfile.TypeAnnotation.TargetType.*;
aoqi@0 35
aoqi@0 36 public class Lambda {
aoqi@0 37
aoqi@0 38 @TADescriptions({
aoqi@0 39 @TADescription(annotation = "TA", type = METHOD_REFERENCE,
aoqi@0 40 offset = ReferenceInfoUtil.IGNORE_VALUE),
aoqi@0 41 @TADescription(annotation = "TB", type = METHOD_REFERENCE,
aoqi@0 42 offset = ReferenceInfoUtil.IGNORE_VALUE)
aoqi@0 43 })
aoqi@0 44 public String returnMethodRef1() {
aoqi@0 45 return
aoqi@0 46 "class Lambda {" +
aoqi@0 47 " public String getName() { return \"Lambda!\"; }" +
aoqi@0 48 "}" +
aoqi@0 49
aoqi@0 50 "class Test {" +
aoqi@0 51 " java.util.function.Function<Lambda, String> lambda() {" +
aoqi@0 52 " return @TA @TB Lambda::getName;" +
aoqi@0 53 " }" +
aoqi@0 54 "}";
aoqi@0 55 }
aoqi@0 56
aoqi@0 57 @TADescriptions({
aoqi@0 58 @TADescription(annotation = "TA", type = METHOD_REFERENCE,
aoqi@0 59 offset = ReferenceInfoUtil.IGNORE_VALUE),
aoqi@0 60 @TADescription(annotation = "TB", type = METHOD_REFERENCE,
aoqi@0 61 offset = ReferenceInfoUtil.IGNORE_VALUE,
aoqi@0 62 genericLocation = { 3, 0 }),
aoqi@0 63 @TADescription(annotation = "TC", type = METHOD_REFERENCE,
aoqi@0 64 offset = ReferenceInfoUtil.IGNORE_VALUE,
aoqi@0 65 genericLocation = { 3, 0 }),
aoqi@0 66 @TADescription(annotation = "TD", type = METHOD_REFERENCE,
aoqi@0 67 offset = ReferenceInfoUtil.IGNORE_VALUE,
aoqi@0 68 genericLocation = { 3, 1 }),
aoqi@0 69 @TADescription(annotation = "TE", type = METHOD_REFERENCE,
aoqi@0 70 offset = ReferenceInfoUtil.IGNORE_VALUE,
aoqi@0 71 genericLocation = { 3, 1 })
aoqi@0 72 })
aoqi@0 73 public String returnMethodRef2() {
aoqi@0 74 return
aoqi@0 75 "class Lambda<S, T> {" +
aoqi@0 76 " public String getName() { return \"Lambda!\"; }" +
aoqi@0 77 "}" +
aoqi@0 78
aoqi@0 79 "class Test {" +
aoqi@0 80 " java.util.function.Function<Lambda<Integer, Float>, String> lambda() {" +
aoqi@0 81 " return @TA Lambda<@TB @TC Integer, @TD @TE Float>::getName;" +
aoqi@0 82 " }" +
aoqi@0 83 "}";
aoqi@0 84 }
aoqi@0 85
aoqi@0 86 @TADescriptions({
aoqi@0 87 @TADescription(annotation = "CTA", type = METHOD_REFERENCE,
aoqi@0 88 offset = ReferenceInfoUtil.IGNORE_VALUE),
aoqi@0 89 @TADescription(annotation = "CTB", type = METHOD_REFERENCE,
aoqi@0 90 offset = ReferenceInfoUtil.IGNORE_VALUE,
aoqi@0 91 genericLocation = { 3, 0 }),
aoqi@0 92 @TADescription(annotation = "CTC", type = METHOD_REFERENCE,
aoqi@0 93 offset = ReferenceInfoUtil.IGNORE_VALUE,
aoqi@0 94 genericLocation = { 3, 1 })
aoqi@0 95 })
aoqi@0 96 public String returnMethodRef3() {
aoqi@0 97 return
aoqi@0 98 "class Lambda<S, T> {" +
aoqi@0 99 " public String getName() { return \"Lambda!\"; }" +
aoqi@0 100 "}" +
aoqi@0 101
aoqi@0 102 "@Target(ElementType.TYPE_USE)" +
aoqi@0 103 "@interface CTA {" +
aoqi@0 104 " String value();" +
aoqi@0 105 "}" +
aoqi@0 106
aoqi@0 107 "@Target(ElementType.TYPE_USE)" +
aoqi@0 108 "@interface CTB {" +
aoqi@0 109 " int age();" +
aoqi@0 110 "}" +
aoqi@0 111
aoqi@0 112 "@Target(ElementType.TYPE_USE)" +
aoqi@0 113 "@interface CTC {" +
aoqi@0 114 " String name();" +
aoqi@0 115 "}" +
aoqi@0 116
aoqi@0 117 "class Test {" +
aoqi@0 118 " java.util.function.Function<Lambda<Integer, Float>, String> lambda() {" +
aoqi@0 119 " return @CTA(\"x\") Lambda<@CTB(age = 5) Integer, @CTC(name = \"y\") Float>::getName;" +
aoqi@0 120 " }" +
aoqi@0 121 "}";
aoqi@0 122 }
aoqi@0 123
aoqi@0 124
aoqi@0 125 @TADescriptions({
aoqi@0 126 @TADescription(annotation = "TA", type = CONSTRUCTOR_REFERENCE,
aoqi@0 127 offset = ReferenceInfoUtil.IGNORE_VALUE),
aoqi@0 128 @TADescription(annotation = "TB", type = CONSTRUCTOR_REFERENCE,
aoqi@0 129 offset = ReferenceInfoUtil.IGNORE_VALUE)
aoqi@0 130 })
aoqi@0 131 public String returnConstructorRef1() {
aoqi@0 132 return
aoqi@0 133 "class Lambda {" +
aoqi@0 134 " Lambda() { }" +
aoqi@0 135 "}" +
aoqi@0 136
aoqi@0 137 "class Test {" +
aoqi@0 138 " Runnable lambda() {" +
aoqi@0 139 " return @TA @TB Lambda::new;" +
aoqi@0 140 " }" +
aoqi@0 141 "}";
aoqi@0 142 }
aoqi@0 143
aoqi@0 144 @TADescriptions({
aoqi@0 145 @TADescription(annotation = "TA", type = CONSTRUCTOR_REFERENCE,
aoqi@0 146 offset = ReferenceInfoUtil.IGNORE_VALUE),
aoqi@0 147 @TADescription(annotation = "TB", type = CONSTRUCTOR_REFERENCE,
aoqi@0 148 offset = ReferenceInfoUtil.IGNORE_VALUE,
aoqi@0 149 genericLocation = { 3, 0 }),
aoqi@0 150 @TADescription(annotation = "TC", type = CONSTRUCTOR_REFERENCE,
aoqi@0 151 offset = ReferenceInfoUtil.IGNORE_VALUE,
aoqi@0 152 genericLocation = { 3, 0 }),
aoqi@0 153 @TADescription(annotation = "TD", type = CONSTRUCTOR_REFERENCE,
aoqi@0 154 offset = ReferenceInfoUtil.IGNORE_VALUE,
aoqi@0 155 genericLocation = { 3, 1 }),
aoqi@0 156 @TADescription(annotation = "TE", type = CONSTRUCTOR_REFERENCE,
aoqi@0 157 offset = ReferenceInfoUtil.IGNORE_VALUE,
aoqi@0 158 genericLocation = { 3, 1 })
aoqi@0 159 })
aoqi@0 160 public String returnConstructorRef2() {
aoqi@0 161 return
aoqi@0 162 "class Lambda<S, T> {" +
aoqi@0 163 " Lambda() { }" +
aoqi@0 164 "}" +
aoqi@0 165
aoqi@0 166 "class Test {" +
aoqi@0 167 " Runnable lambda() {" +
aoqi@0 168 " return @TA Lambda<@TB @TC Integer, @TD @TE Float>::new;" +
aoqi@0 169 " }" +
aoqi@0 170 "}";
aoqi@0 171 }
aoqi@0 172
aoqi@0 173 @TADescriptions({
aoqi@0 174 @TADescription(annotation = "CTA", type = CONSTRUCTOR_REFERENCE,
aoqi@0 175 offset = ReferenceInfoUtil.IGNORE_VALUE),
aoqi@0 176 @TADescription(annotation = "CTB", type = CONSTRUCTOR_REFERENCE,
aoqi@0 177 offset = ReferenceInfoUtil.IGNORE_VALUE,
aoqi@0 178 genericLocation = { 3, 0 }),
aoqi@0 179 @TADescription(annotation = "CTC", type = CONSTRUCTOR_REFERENCE,
aoqi@0 180 offset = ReferenceInfoUtil.IGNORE_VALUE,
aoqi@0 181 genericLocation = { 3, 1 })
aoqi@0 182 })
aoqi@0 183 public String returnConstructorRef3() {
aoqi@0 184 return
aoqi@0 185 "class Lambda<S, T> {" +
aoqi@0 186 " Lambda() { }" +
aoqi@0 187 "}" +
aoqi@0 188
aoqi@0 189 "@Target(ElementType.TYPE_USE)" +
aoqi@0 190 "@interface CTA {" +
aoqi@0 191 " String value();" +
aoqi@0 192 "}" +
aoqi@0 193
aoqi@0 194 "@Target(ElementType.TYPE_USE)" +
aoqi@0 195 "@interface CTB {" +
aoqi@0 196 " int age();" +
aoqi@0 197 "}" +
aoqi@0 198
aoqi@0 199 "@Target(ElementType.TYPE_USE)" +
aoqi@0 200 "@interface CTC {" +
aoqi@0 201 " String name();" +
aoqi@0 202 "}" +
aoqi@0 203
aoqi@0 204 "class Test {" +
aoqi@0 205 " Runnable lambda() {" +
aoqi@0 206 " return @CTA(\"x\") Lambda<@CTB(age = 5) Integer, @CTC(name = \"y\") Float>::new;" +
aoqi@0 207 " }" +
aoqi@0 208 "}";
aoqi@0 209 }
aoqi@0 210
aoqi@0 211
aoqi@0 212 @TADescriptions({
aoqi@0 213 @TADescription(annotation = "TA", type = METHOD_REFERENCE_TYPE_ARGUMENT,
aoqi@0 214 offset = ReferenceInfoUtil.IGNORE_VALUE,
aoqi@0 215 typeIndex = 0),
aoqi@0 216 @TADescription(annotation = "TB", type = METHOD_REFERENCE_TYPE_ARGUMENT,
aoqi@0 217 offset = ReferenceInfoUtil.IGNORE_VALUE,
aoqi@0 218 typeIndex = 1)
aoqi@0 219 })
aoqi@0 220 public String returnMethodRefTA1() {
aoqi@0 221 return
aoqi@0 222 "interface Lambda {" +
aoqi@0 223 " <S, T> void generic(S p1, T p2);" +
aoqi@0 224 "}" +
aoqi@0 225
aoqi@0 226 "class LambdaImpl implements Lambda {" +
aoqi@0 227 " public <S, T> void generic(S p1, T p2) {}" +
aoqi@0 228 "}" +
aoqi@0 229
aoqi@0 230 "class Test {" +
aoqi@0 231 " Lambda lambda(LambdaImpl r) {" +
aoqi@0 232 " return r::<@TA Object, @TB Object>generic;" +
aoqi@0 233 " }" +
aoqi@0 234 "}";
aoqi@0 235 }
aoqi@0 236
aoqi@0 237 @TADescriptions({
aoqi@0 238 @TADescription(annotation = "TA", type = CONSTRUCTOR_REFERENCE_TYPE_ARGUMENT,
aoqi@0 239 offset = ReferenceInfoUtil.IGNORE_VALUE,
aoqi@0 240 typeIndex = 0),
aoqi@0 241 @TADescription(annotation = "TB", type = CONSTRUCTOR_REFERENCE_TYPE_ARGUMENT,
aoqi@0 242 offset = ReferenceInfoUtil.IGNORE_VALUE,
aoqi@0 243 typeIndex = 1)
aoqi@0 244 })
aoqi@0 245 public String returnConstructorRefTA2() {
aoqi@0 246 return
aoqi@0 247 "interface Lambda {" +
aoqi@0 248 " <S, T> void generic(S p1, T p2);" +
aoqi@0 249 "}" +
aoqi@0 250
aoqi@0 251 "class LambdaImpl implements Lambda {" +
aoqi@0 252 " <S, T> LambdaImpl(S p1, T p2) {}" +
aoqi@0 253 " public <S, T> void generic(S p1, T p2) {}" +
aoqi@0 254 "}" +
aoqi@0 255
aoqi@0 256 "class Test {" +
aoqi@0 257 " Lambda lambda() {" +
aoqi@0 258 " return LambdaImpl::<@TA Object, @TB Object>new;" +
aoqi@0 259 " }" +
aoqi@0 260 "}";
aoqi@0 261 }
aoqi@0 262
aoqi@0 263 @TADescriptions({
aoqi@0 264 @TADescription(annotation = "TA", type = METHOD_FORMAL_PARAMETER,
aoqi@0 265 paramIndex = 0),
aoqi@0 266 @TADescription(annotation = "TB", type = METHOD_FORMAL_PARAMETER,
aoqi@0 267 paramIndex = 1),
aoqi@0 268 @TADescription(annotation = "TC", type = METHOD_FORMAL_PARAMETER,
aoqi@0 269 paramIndex = 1, genericLocation = { 3, 0 }),
aoqi@0 270 @TADescription(annotation = "TD", type = LOCAL_VARIABLE,
aoqi@0 271 lvarOffset = ReferenceInfoUtil.IGNORE_VALUE,
aoqi@0 272 lvarLength = ReferenceInfoUtil.IGNORE_VALUE,
aoqi@0 273 lvarIndex = ReferenceInfoUtil.IGNORE_VALUE),
aoqi@0 274 @TADescription(annotation = "TE", type = CAST,
aoqi@0 275 offset = ReferenceInfoUtil.IGNORE_VALUE,
aoqi@0 276 typeIndex = 0)
aoqi@0 277 })
aoqi@0 278 public String returnLambdaExpr1() {
aoqi@0 279 return
aoqi@0 280 "interface LambdaInt {" +
aoqi@0 281 " void lambda(Object p1, List<Object> p2);" +
aoqi@0 282 "}" +
aoqi@0 283 "class Test {" +
aoqi@0 284 " LambdaInt getLambda() {" +
aoqi@0 285 " return (@TA Object x, @TB List<@TC Object> y) -> { @TD Object l = null; System.out.println((@TE Object) l); };" +
aoqi@0 286 " }" +
aoqi@0 287 "}";
aoqi@0 288 }
aoqi@0 289
aoqi@0 290 @TADescriptions({
aoqi@0 291 @TADescription(annotation = "TA", type = METHOD_FORMAL_PARAMETER,
aoqi@0 292 paramIndex = 0)})
aoqi@0 293 public String lambdaField1() {
aoqi@0 294 return
aoqi@0 295 "class Test {" +
aoqi@0 296 " java.util.function.IntUnaryOperator field = (@TA int y) -> 1;" +
aoqi@0 297 "}";
aoqi@0 298 }
aoqi@0 299
aoqi@0 300 @TADescriptions({
aoqi@0 301 @TADescription(annotation = "TA", type = METHOD_FORMAL_PARAMETER,
aoqi@0 302 paramIndex = 0)})
aoqi@0 303 public String lambdaField2() {
aoqi@0 304 return
aoqi@0 305 "class Test {" +
aoqi@0 306 " static java.util.function.IntUnaryOperator field = (@TA int y) -> 1;" +
aoqi@0 307 "}";
aoqi@0 308 }
aoqi@0 309 }

mercurial