Lambda.java revision 2942:08092deced3f
1139823Simp/*
21541Srgrimes * Copyright (c) 2013, 2015, Oracle and/or its affiliates. All rights reserved.
3169465Srwatson * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4169465Srwatson *
51541Srgrimes * This code is free software; you can redistribute it and/or modify it
61541Srgrimes * under the terms of the GNU General Public License version 2 only, as
71541Srgrimes * published by the Free Software Foundation.
81541Srgrimes *
91541Srgrimes * This code is distributed in the hope that it will be useful, but WITHOUT
101541Srgrimes * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
111541Srgrimes * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
121541Srgrimes * version 2 for more details (a copy is included in the LICENSE file that
131541Srgrimes * accompanied this code).
141541Srgrimes *
151541Srgrimes * You should have received a copy of the GNU General Public License version
161541Srgrimes * 2 along with this work; if not, write to the Free Software Foundation,
171541Srgrimes * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
181541Srgrimes *
191541Srgrimes * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
201541Srgrimes * or visit www.oracle.com if you need additional information or have any
211541Srgrimes * questions.
221541Srgrimes */
231541Srgrimes
241541Srgrimes/*
251541Srgrimes * @test
261541Srgrimes * @bug 8008077 8029721 8042451 8043974
271541Srgrimes * @summary Test population of reference info for lambda expressions
281541Srgrimes *          javac crash for annotated parameter type of lambda in a field
291541Srgrimes * @modules jdk.jdeps/com.sun.tools.classfile
301541Srgrimes * @ignore 8057687 emit correct byte code an attributes for type annotations
3150477Speter * @compile -g Driver.java ReferenceInfoUtil.java Lambda.java
321541Srgrimes * @run main Driver Lambda
331541Srgrimes * @author Werner Dietl
342169Spaul */
35169465Srwatson
362169Spaulimport static com.sun.tools.classfile.TypeAnnotation.TargetType.*;
371541Srgrimes
381541Srgrimespublic class Lambda {
39169465Srwatson
401541Srgrimes    @TADescription(annotation = "TA", type = METHOD_REFERENCE,
411541Srgrimes                offset = ReferenceInfoUtil.IGNORE_VALUE)
421541Srgrimes    @TADescription(annotation = "TB", type = METHOD_REFERENCE,
431541Srgrimes                offset = ReferenceInfoUtil.IGNORE_VALUE)
441541Srgrimes    public String returnMethodRef1() {
451541Srgrimes        return
461541Srgrimes                "class Lambda {" +
471541Srgrimes                "  public String getName() { return \"Lambda!\"; }" +
48137971Srwatson                "}" +
491541Srgrimes
501541Srgrimes                "class %TEST_CLASS_NAME% {" +
511541Srgrimes                "  java.util.function.Function<Lambda, String> lambda() {" +
521541Srgrimes                "    return @TA @TB Lambda::getName;" +
531541Srgrimes                "  }" +
541541Srgrimes                "}";
551541Srgrimes    }
561541Srgrimes
571541Srgrimes    @TADescription(annotation = "TA", type = METHOD_REFERENCE,
581541Srgrimes                offset = ReferenceInfoUtil.IGNORE_VALUE)
591541Srgrimes    @TADescription(annotation = "TB", type = METHOD_REFERENCE,
6052904Sshin                offset = ReferenceInfoUtil.IGNORE_VALUE,
6152904Sshin                genericLocation = { 3, 0 })
6252904Sshin    @TADescription(annotation = "TC", type = METHOD_REFERENCE,
6352904Sshin                offset = ReferenceInfoUtil.IGNORE_VALUE,
6452904Sshin                genericLocation = { 3, 0 })
6552904Sshin    @TADescription(annotation = "TD", type = METHOD_REFERENCE,
6652904Sshin                offset = ReferenceInfoUtil.IGNORE_VALUE,
6752904Sshin                genericLocation = { 3, 1 })
6852904Sshin    @TADescription(annotation = "TE", type = METHOD_REFERENCE,
6952904Sshin                offset = ReferenceInfoUtil.IGNORE_VALUE,
7052904Sshin                genericLocation = { 3, 1})
7152904Sshin    public String returnMethodRef2() {
7252904Sshin        return
7352904Sshin                "class Lambda<S, T> {" +
741541Srgrimes                "  public String getName() { return \"Lambda!\"; }" +
7552904Sshin                "}" +
761541Srgrimes
771541Srgrimes                "class %TEST_CLASS_NAME% {" +
781541Srgrimes                "  java.util.function.Function<Lambda<Integer, Float>, String> lambda() {" +
791541Srgrimes                "    return @TA Lambda<@TB @TC Integer, @TD @TE Float>::getName;" +
80169465Srwatson                "  }" +
81169465Srwatson                "}";
82169465Srwatson    }
83169465Srwatson
841541Srgrimes    @TADescription(annotation = "CTA", type = METHOD_REFERENCE,
8512296Sphk                offset = ReferenceInfoUtil.IGNORE_VALUE)
8645439Sjulian    @TADescription(annotation = "CTB", type = METHOD_REFERENCE,
8745439Sjulian                offset = ReferenceInfoUtil.IGNORE_VALUE,
8845439Sjulian                genericLocation = { 3, 0 })
8945439Sjulian    @TADescription(annotation = "CTC", type = METHOD_REFERENCE,
9045439Sjulian                offset = ReferenceInfoUtil.IGNORE_VALUE,
9145439Sjulian                genericLocation = { 3, 1    })
9245439Sjulian    public String returnMethodRef3() {
9352952Sjlemon        return
9445439Sjulian                "class Lambda<S, T> {" +
9545439Sjulian                "  public String getName() { return \"Lambda!\"; }" +
9645439Sjulian                "}" +
9745439Sjulian
981541Srgrimes                "@Target(ElementType.TYPE_USE)" +
991541Srgrimes                "@interface CTA {" +
1001541Srgrimes                "  String value();" +
1011541Srgrimes                "}" +
1021541Srgrimes
1031541Srgrimes                "@Target(ElementType.TYPE_USE)" +
1041541Srgrimes                "@interface CTB {" +
105171656Sdes                "  int age();" +
1061541Srgrimes                "}" +
1071541Srgrimes
1081541Srgrimes                "@Target(ElementType.TYPE_USE)" +
1091541Srgrimes                "@interface CTC {" +
1101541Srgrimes                "  String name();" +
1112169Spaul                "}" +
1122169Spaul
113                "class %TEST_CLASS_NAME% {" +
114                "  java.util.function.Function<Lambda<Integer, Float>, String> lambda() {" +
115                "    return @CTA(\"x\") Lambda<@CTB(age = 5) Integer, @CTC(name = \"y\") Float>::getName;" +
116                "  }" +
117                "}";
118    }
119
120
121    @TADescription(annotation = "TA", type = CONSTRUCTOR_REFERENCE,
122                offset = ReferenceInfoUtil.IGNORE_VALUE)
123    @TADescription(annotation = "TB", type = CONSTRUCTOR_REFERENCE,
124                offset = ReferenceInfoUtil.IGNORE_VALUE)
125    public String returnConstructorRef1() {
126        return
127                "class Lambda {" +
128                "  Lambda() { }" +
129                "}" +
130
131                "class %TEST_CLASS_NAME% {" +
132                "  Runnable lambda() {" +
133                "    return @TA @TB Lambda::new;" +
134                "  }" +
135                "}";
136    }
137
138    @TADescription(annotation = "TA", type = CONSTRUCTOR_REFERENCE,
139                offset = ReferenceInfoUtil.IGNORE_VALUE)
140    @TADescription(annotation = "TB", type = CONSTRUCTOR_REFERENCE,
141                offset = ReferenceInfoUtil.IGNORE_VALUE,
142                genericLocation = { 3, 0 })
143    @TADescription(annotation = "TC", type = CONSTRUCTOR_REFERENCE,
144                offset = ReferenceInfoUtil.IGNORE_VALUE,
145                genericLocation = { 3, 0 })
146    @TADescription(annotation = "TD", type = CONSTRUCTOR_REFERENCE,
147                offset = ReferenceInfoUtil.IGNORE_VALUE,
148                genericLocation = { 3, 1 })
149    @TADescription(annotation = "TE", type = CONSTRUCTOR_REFERENCE,
150                offset = ReferenceInfoUtil.IGNORE_VALUE,
151                genericLocation = { 3, 1    })
152    public String returnConstructorRef2() {
153        return
154                "class Lambda<S, T> {" +
155                "  Lambda() { }" +
156                "}" +
157
158                "class %TEST_CLASS_NAME% {" +
159                "  Runnable lambda() {" +
160                "    return @TA Lambda<@TB @TC Integer, @TD @TE Float>::new;" +
161                "  }" +
162                "}";
163    }
164
165    @TADescription(annotation = "CTA", type = CONSTRUCTOR_REFERENCE,
166                offset = ReferenceInfoUtil.IGNORE_VALUE)
167    @TADescription(annotation = "CTB", type = CONSTRUCTOR_REFERENCE,
168                offset = ReferenceInfoUtil.IGNORE_VALUE,
169                genericLocation = { 3, 0 })
170    @TADescription(annotation = "CTC", type = CONSTRUCTOR_REFERENCE,
171                offset = ReferenceInfoUtil.IGNORE_VALUE,
172                genericLocation = { 3, 1    })
173    public String returnConstructorRef3() {
174        return
175                "class Lambda<S, T> {" +
176                "  Lambda() { }" +
177                "}" +
178
179                "@Target(ElementType.TYPE_USE)" +
180                "@interface CTA {" +
181                "  String value();" +
182                "}" +
183
184                "@Target(ElementType.TYPE_USE)" +
185                "@interface CTB {" +
186                "  int age();" +
187                "}" +
188
189                "@Target(ElementType.TYPE_USE)" +
190                "@interface CTC {" +
191                "  String name();" +
192                "}" +
193
194                "class %TEST_CLASS_NAME% {" +
195                "  Runnable lambda() {" +
196                "    return @CTA(\"x\") Lambda<@CTB(age = 5) Integer, @CTC(name = \"y\") Float>::new;" +
197                "  }" +
198                "}";
199    }
200
201
202    @TADescription(annotation = "TA", type = METHOD_REFERENCE_TYPE_ARGUMENT,
203                 offset = ReferenceInfoUtil.IGNORE_VALUE,
204                 typeIndex = 0)
205    @TADescription(annotation = "TB", type = METHOD_REFERENCE_TYPE_ARGUMENT,
206                 offset = ReferenceInfoUtil.IGNORE_VALUE,
207                 typeIndex = 1)
208    public String returnMethodRefTA1() {
209        return
210                "interface Lambda {" +
211                "  <S, T> void generic(S p1, T p2);" +
212                "}" +
213
214                "class LambdaImpl implements Lambda {" +
215                "  public <S, T> void generic(S p1, T p2) {}" +
216                "}" +
217
218                "class %TEST_CLASS_NAME% {" +
219                "  Lambda lambda(LambdaImpl r) {" +
220                "    return r::<@TA Object, @TB Object>generic;" +
221                "  }" +
222                "}";
223    }
224
225    @TADescription(annotation = "TA", type = CONSTRUCTOR_REFERENCE_TYPE_ARGUMENT,
226                 offset = ReferenceInfoUtil.IGNORE_VALUE,
227                 typeIndex = 0)
228    @TADescription(annotation = "TB", type = CONSTRUCTOR_REFERENCE_TYPE_ARGUMENT,
229                 offset = ReferenceInfoUtil.IGNORE_VALUE,
230                 typeIndex = 1)
231    public String returnConstructorRefTA2() {
232        return
233                "interface Lambda {" +
234                "  <S, T> void generic(S p1, T p2);" +
235                "}" +
236
237                "class LambdaImpl implements Lambda {" +
238                "  <S, T> LambdaImpl(S p1, T p2) {}" +
239                "  public <S, T> void generic(S p1, T p2) {}" +
240                "}" +
241
242                "class %TEST_CLASS_NAME% {" +
243                "  Lambda lambda() {" +
244                "    return LambdaImpl::<@TA Object, @TB Object>new;" +
245                "  }" +
246                "}";
247    }
248
249    @TADescription(annotation = "TA", type = METHOD_FORMAL_PARAMETER,
250                paramIndex = 0)
251    @TADescription(annotation = "TB", type = METHOD_FORMAL_PARAMETER,
252                paramIndex = 1)
253    @TADescription(annotation = "TC", type = METHOD_FORMAL_PARAMETER,
254                paramIndex = 1, genericLocation = { 3, 0 })
255    @TADescription(annotation = "TD", type = LOCAL_VARIABLE,
256                lvarOffset = ReferenceInfoUtil.IGNORE_VALUE,
257                lvarLength = ReferenceInfoUtil.IGNORE_VALUE,
258                lvarIndex = ReferenceInfoUtil.IGNORE_VALUE)
259    @TADescription(annotation = "TE", type = CAST,
260                offset = ReferenceInfoUtil.IGNORE_VALUE,
261                typeIndex = 0)
262    public String returnLambdaExpr1() {
263        return
264                "interface LambdaInt {" +
265                "  void lambda(Object p1, List<Object> p2);" +
266                "}" +
267                "class %TEST_CLASS_NAME% {" +
268                "  LambdaInt getLambda() {" +
269                "    return (@TA Object x, @TB List<@TC Object> y) -> { @TD Object l = null; System.out.println((@TE Object) l); };" +
270                "  }" +
271                "}";
272    }
273
274    @TADescription(annotation = "TA", type = METHOD_FORMAL_PARAMETER,
275            paramIndex = 0)
276    public String lambdaField1() {
277        return
278            "class %TEST_CLASS_NAME% {" +
279                " java.util.function.IntUnaryOperator field = (@TA int y) -> 1;" +
280            "}";
281    }
282
283    @TADescription(annotation = "TA", type = METHOD_FORMAL_PARAMETER,
284            paramIndex = 0)
285    public String lambdaField2() {
286        return
287            "class %TEST_CLASS_NAME% {" +
288                " static java.util.function.IntUnaryOperator field = (@TA int y) -> 1;" +
289            "}";
290    }
291
292    @TADescription(annotation = "RTAs", type = METHOD_REFERENCE,
293            offset = ReferenceInfoUtil.IGNORE_VALUE)
294    public String returnMethodRefRepeatableAnnotation1() {
295        return
296                "class Lambda {" +
297                        "  public String getName() { return \"Lambda!\"; }" +
298                        "}" +
299
300                        "class %TEST_CLASS_NAME% {" +
301                        "  java.util.function.Function<Lambda, String> lambda() {" +
302                        "    return @RTA @RTA Lambda::getName;" +
303                        "  }" +
304                        "}";
305    }
306
307    @TADescription(annotation = "RTAs", type = METHOD_REFERENCE,
308            offset = ReferenceInfoUtil.IGNORE_VALUE)
309    @TADescription(annotation = "RTBs", type = METHOD_REFERENCE,
310            offset = ReferenceInfoUtil.IGNORE_VALUE,
311            genericLocation = { 3, 0 })
312    @TADescription(annotation = "RTCs", type = METHOD_REFERENCE,
313            offset = ReferenceInfoUtil.IGNORE_VALUE,
314            genericLocation = { 3, 0 })
315    @TADescription(annotation = "RTDs", type = METHOD_REFERENCE,
316            offset = ReferenceInfoUtil.IGNORE_VALUE,
317            genericLocation = { 3, 1 })
318    @TADescription(annotation = "RTEs", type = METHOD_REFERENCE,
319            offset = ReferenceInfoUtil.IGNORE_VALUE,
320            genericLocation = { 3, 1})
321    public String returnMethodRefRepeatableAnnotation2() {
322        return
323                "class Lambda<S, T> {" +
324                        "  public String getName() { return \"Lambda!\"; }" +
325                        "}" +
326
327                        "class %TEST_CLASS_NAME% {" +
328                        "  java.util.function.Function<Lambda<Integer, Float>, String> lambda() {" +
329                        "    return @RTA @RTA Lambda<@RTB @RTB @RTC @RTC Integer, @RTD @RTD @RTE @RTE Float>::getName;" +
330                        "  }" +
331                        "}";
332    }
333
334    @TADescription(annotation = "RTAs", type = CONSTRUCTOR_REFERENCE,
335            offset = ReferenceInfoUtil.IGNORE_VALUE)
336    public String returnConstructorRefRepeatable1() {
337        return
338                "class Lambda {" +
339                        "  Lambda() { }" +
340                        "}" +
341
342                        "class %TEST_CLASS_NAME% {" +
343                        "  Runnable lambda() {" +
344                        "    return @RTA @RTA Lambda::new;" +
345                        "  }" +
346                        "}";
347    }
348
349    @TADescription(annotation = "RTAs", type = CONSTRUCTOR_REFERENCE,
350            offset = ReferenceInfoUtil.IGNORE_VALUE)
351    @TADescription(annotation = "RTBs", type = CONSTRUCTOR_REFERENCE,
352            offset = ReferenceInfoUtil.IGNORE_VALUE,
353            genericLocation = { 3, 0 })
354    @TADescription(annotation = "RTCs", type = CONSTRUCTOR_REFERENCE,
355            offset = ReferenceInfoUtil.IGNORE_VALUE,
356            genericLocation = { 3, 1    })
357    public String returnConstructorRefRepeatable2() {
358        return
359                "class Lambda<S, T> {" +
360                        "  Lambda() { }" +
361                        "}" +
362
363                        "class %TEST_CLASS_NAME% {" +
364                        "  Runnable lambda() {" +
365                        "    return @RTA @RTA Lambda<@RTB @RTB Integer, @RTC @RTC Float>::new;" +
366                        "  }" +
367                        "}";
368    }
369
370    @TADescription(annotation = "RTAs", type = METHOD_REFERENCE_TYPE_ARGUMENT,
371            offset = ReferenceInfoUtil.IGNORE_VALUE,
372            typeIndex = 0)
373    @TADescription(annotation = "RTBs", type = METHOD_REFERENCE_TYPE_ARGUMENT,
374            offset = ReferenceInfoUtil.IGNORE_VALUE,
375            typeIndex = 1)
376    public String returnMethodRefTARepeatableAnnotation1() {
377        return
378                "interface Lambda {" +
379                        "  <S, T> void generic(S p1, T p2);" +
380                        "}" +
381
382                        "class LambdaImpl implements Lambda {" +
383                        "  public <S, T> void generic(S p1, T p2) {}" +
384                        "}" +
385
386                        "class %TEST_CLASS_NAME% {" +
387                        "  Lambda lambda(LambdaImpl r) {" +
388                        "    return r::<@RTA @RTA Object, @RTB @RTB Object>generic;" +
389                        "  }" +
390                        "}";
391    }
392
393    @TADescription(annotation = "RTAs", type = CONSTRUCTOR_REFERENCE_TYPE_ARGUMENT,
394            offset = ReferenceInfoUtil.IGNORE_VALUE,
395            typeIndex = 0)
396    @TADescription(annotation = "RTBs", type = CONSTRUCTOR_REFERENCE_TYPE_ARGUMENT,
397            offset = ReferenceInfoUtil.IGNORE_VALUE,
398            typeIndex = 1)
399    public String returnConstructorRefTARepeatableAnnotation2() {
400        return
401                "interface Lambda {" +
402                        "  <S, T> void generic(S p1, T p2);" +
403                        "}" +
404
405                        "class LambdaImpl implements Lambda {" +
406                        "  <S, T> LambdaImpl(S p1, T p2) {}" +
407                        "  public <S, T> void generic(S p1, T p2) {}" +
408                        "}" +
409
410                        "class %TEST_CLASS_NAME% {" +
411                        "  Lambda lambda() {" +
412                        "    return LambdaImpl::<@RTA @RTA Object, @RTB @RTB Object>new;" +
413                        "  }" +
414                        "}";
415    }
416
417    @TADescription(annotation = "RTAs", type = METHOD_FORMAL_PARAMETER,
418            paramIndex = 0)
419    @TADescription(annotation = "RTBs", type = METHOD_FORMAL_PARAMETER,
420            paramIndex = 1)
421    @TADescription(annotation = "RTCs", type = METHOD_FORMAL_PARAMETER,
422            paramIndex = 1, genericLocation = { 3, 0 })
423    @TADescription(annotation = "RTDs", type = LOCAL_VARIABLE,
424            lvarOffset = ReferenceInfoUtil.IGNORE_VALUE,
425            lvarLength = ReferenceInfoUtil.IGNORE_VALUE,
426            lvarIndex = ReferenceInfoUtil.IGNORE_VALUE)
427    @TADescription(annotation = "RTEs", type = CAST,
428            offset = ReferenceInfoUtil.IGNORE_VALUE,
429            typeIndex = 0)
430    public String returnLambdaExprRepeatableAnnotation1() {
431        return
432                "interface LambdaInt {" +
433                        "  void lambda(Object p1, List<Object> p2);" +
434                        "}" +
435                        "class %TEST_CLASS_NAME% {" +
436                        "  LambdaInt getLambda() {" +
437                        "    return (@RTA @RTA Object x, @RTB @RTB List<@RTC @RTC Object> y) ->" +
438                        " { @RTD @RTD Object l = null; System.out.println((@RTE @RTE Object) l); };" +
439                        "  }" +
440                        "}";
441    }
442
443    @TADescription(annotation = "RTAs", type = METHOD_FORMAL_PARAMETER,
444            paramIndex = 0)
445    public String lambdaFieldRepeatableAnnotation1() {
446        return
447                "class %TEST_CLASS_NAME% {" +
448                        " java.util.function.IntUnaryOperator field = (@RTA @RTA int y) -> 1;" +
449                        "}";
450    }
451
452    @TADescription(annotation = "RTAs", type = METHOD_FORMAL_PARAMETER,
453            paramIndex = 0)
454    public String lambdaFieldRepeatableAnnotation2() {
455        return
456                "class %TEST_CLASS_NAME% {" +
457                        " static java.util.function.IntUnaryOperator field = (@RTA @RTA int y) -> 1;" +
458                        "}";
459    }
460}
461