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

changeset 0
959103a6100f
child 2525
2eb010b6cb22
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/test/tools/javac/annotations/typeAnnotations/referenceinfos/Lambda.java	Wed Apr 27 01:34:52 2016 +0800
     1.3 @@ -0,0 +1,309 @@
     1.4 +/*
     1.5 + * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.
    1.11 + *
    1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.14 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.15 + * version 2 for more details (a copy is included in the LICENSE file that
    1.16 + * accompanied this code).
    1.17 + *
    1.18 + * You should have received a copy of the GNU General Public License version
    1.19 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.21 + *
    1.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.23 + * or visit www.oracle.com if you need additional information or have any
    1.24 + * questions.
    1.25 + */
    1.26 +
    1.27 +/*
    1.28 + * @test
    1.29 + * @bug 8008077 8029721
    1.30 + * @summary Test population of reference info for lambda expressions
    1.31 + * javac crash for annotated parameter type of lambda in a field
    1.32 + * @compile -g Driver.java ReferenceInfoUtil.java Lambda.java
    1.33 + * @run main Driver Lambda
    1.34 + * @author Werner Dietl
    1.35 + */
    1.36 +
    1.37 +import static com.sun.tools.classfile.TypeAnnotation.TargetType.*;
    1.38 +
    1.39 +public class Lambda {
    1.40 +
    1.41 +    @TADescriptions({
    1.42 +        @TADescription(annotation = "TA", type = METHOD_REFERENCE,
    1.43 +                offset = ReferenceInfoUtil.IGNORE_VALUE),
    1.44 +        @TADescription(annotation = "TB", type = METHOD_REFERENCE,
    1.45 +                offset = ReferenceInfoUtil.IGNORE_VALUE)
    1.46 +    })
    1.47 +    public String returnMethodRef1() {
    1.48 +        return
    1.49 +                "class Lambda {" +
    1.50 +                "  public String getName() { return \"Lambda!\"; }" +
    1.51 +                "}" +
    1.52 +
    1.53 +                "class Test {" +
    1.54 +                "  java.util.function.Function<Lambda, String> lambda() {" +
    1.55 +                "    return @TA @TB Lambda::getName;" +
    1.56 +                "  }" +
    1.57 +                "}";
    1.58 +    }
    1.59 +
    1.60 +    @TADescriptions({
    1.61 +        @TADescription(annotation = "TA", type = METHOD_REFERENCE,
    1.62 +                offset = ReferenceInfoUtil.IGNORE_VALUE),
    1.63 +        @TADescription(annotation = "TB", type = METHOD_REFERENCE,
    1.64 +                offset = ReferenceInfoUtil.IGNORE_VALUE,
    1.65 +                genericLocation = { 3, 0 }),
    1.66 +        @TADescription(annotation = "TC", type = METHOD_REFERENCE,
    1.67 +                offset = ReferenceInfoUtil.IGNORE_VALUE,
    1.68 +                genericLocation = { 3, 0 }),
    1.69 +        @TADescription(annotation = "TD", type = METHOD_REFERENCE,
    1.70 +                offset = ReferenceInfoUtil.IGNORE_VALUE,
    1.71 +                genericLocation = { 3, 1 }),
    1.72 +        @TADescription(annotation = "TE", type = METHOD_REFERENCE,
    1.73 +                offset = ReferenceInfoUtil.IGNORE_VALUE,
    1.74 +                genericLocation = { 3, 1 })
    1.75 +    })
    1.76 +    public String returnMethodRef2() {
    1.77 +        return
    1.78 +                "class Lambda<S, T> {" +
    1.79 +                "  public String getName() { return \"Lambda!\"; }" +
    1.80 +                "}" +
    1.81 +
    1.82 +                "class Test {" +
    1.83 +                "  java.util.function.Function<Lambda<Integer, Float>, String> lambda() {" +
    1.84 +                "    return @TA Lambda<@TB @TC Integer, @TD @TE Float>::getName;" +
    1.85 +                "  }" +
    1.86 +                "}";
    1.87 +    }
    1.88 +
    1.89 +    @TADescriptions({
    1.90 +        @TADescription(annotation = "CTA", type = METHOD_REFERENCE,
    1.91 +                offset = ReferenceInfoUtil.IGNORE_VALUE),
    1.92 +        @TADescription(annotation = "CTB", type = METHOD_REFERENCE,
    1.93 +                offset = ReferenceInfoUtil.IGNORE_VALUE,
    1.94 +                genericLocation = { 3, 0 }),
    1.95 +        @TADescription(annotation = "CTC", type = METHOD_REFERENCE,
    1.96 +                offset = ReferenceInfoUtil.IGNORE_VALUE,
    1.97 +                genericLocation = { 3, 1 })
    1.98 +    })
    1.99 +    public String returnMethodRef3() {
   1.100 +        return
   1.101 +                "class Lambda<S, T> {" +
   1.102 +                "  public String getName() { return \"Lambda!\"; }" +
   1.103 +                "}" +
   1.104 +
   1.105 +                "@Target(ElementType.TYPE_USE)" +
   1.106 +                "@interface CTA {" +
   1.107 +                "  String value();" +
   1.108 +                "}" +
   1.109 +
   1.110 +                "@Target(ElementType.TYPE_USE)" +
   1.111 +                "@interface CTB {" +
   1.112 +                "  int age();" +
   1.113 +                "}" +
   1.114 +
   1.115 +                "@Target(ElementType.TYPE_USE)" +
   1.116 +                "@interface CTC {" +
   1.117 +                "  String name();" +
   1.118 +                "}" +
   1.119 +
   1.120 +                "class Test {" +
   1.121 +                "  java.util.function.Function<Lambda<Integer, Float>, String> lambda() {" +
   1.122 +                "    return @CTA(\"x\") Lambda<@CTB(age = 5) Integer, @CTC(name = \"y\") Float>::getName;" +
   1.123 +                "  }" +
   1.124 +                "}";
   1.125 +    }
   1.126 +
   1.127 +
   1.128 +    @TADescriptions({
   1.129 +        @TADescription(annotation = "TA", type = CONSTRUCTOR_REFERENCE,
   1.130 +                offset = ReferenceInfoUtil.IGNORE_VALUE),
   1.131 +        @TADescription(annotation = "TB", type = CONSTRUCTOR_REFERENCE,
   1.132 +                offset = ReferenceInfoUtil.IGNORE_VALUE)
   1.133 +    })
   1.134 +    public String returnConstructorRef1() {
   1.135 +        return
   1.136 +                "class Lambda {" +
   1.137 +                "  Lambda() { }" +
   1.138 +                "}" +
   1.139 +
   1.140 +                "class Test {" +
   1.141 +                "  Runnable lambda() {" +
   1.142 +                "    return @TA @TB Lambda::new;" +
   1.143 +                "  }" +
   1.144 +                "}";
   1.145 +    }
   1.146 +
   1.147 +    @TADescriptions({
   1.148 +        @TADescription(annotation = "TA", type = CONSTRUCTOR_REFERENCE,
   1.149 +                offset = ReferenceInfoUtil.IGNORE_VALUE),
   1.150 +        @TADescription(annotation = "TB", type = CONSTRUCTOR_REFERENCE,
   1.151 +                offset = ReferenceInfoUtil.IGNORE_VALUE,
   1.152 +                genericLocation = { 3, 0 }),
   1.153 +        @TADescription(annotation = "TC", type = CONSTRUCTOR_REFERENCE,
   1.154 +                offset = ReferenceInfoUtil.IGNORE_VALUE,
   1.155 +                genericLocation = { 3, 0 }),
   1.156 +        @TADescription(annotation = "TD", type = CONSTRUCTOR_REFERENCE,
   1.157 +                offset = ReferenceInfoUtil.IGNORE_VALUE,
   1.158 +                genericLocation = { 3, 1 }),
   1.159 +        @TADescription(annotation = "TE", type = CONSTRUCTOR_REFERENCE,
   1.160 +                offset = ReferenceInfoUtil.IGNORE_VALUE,
   1.161 +                genericLocation = { 3, 1 })
   1.162 +    })
   1.163 +    public String returnConstructorRef2() {
   1.164 +        return
   1.165 +                "class Lambda<S, T> {" +
   1.166 +                "  Lambda() { }" +
   1.167 +                "}" +
   1.168 +
   1.169 +                "class Test {" +
   1.170 +                "  Runnable lambda() {" +
   1.171 +                "    return @TA Lambda<@TB @TC Integer, @TD @TE Float>::new;" +
   1.172 +                "  }" +
   1.173 +                "}";
   1.174 +    }
   1.175 +
   1.176 +    @TADescriptions({
   1.177 +        @TADescription(annotation = "CTA", type = CONSTRUCTOR_REFERENCE,
   1.178 +                offset = ReferenceInfoUtil.IGNORE_VALUE),
   1.179 +        @TADescription(annotation = "CTB", type = CONSTRUCTOR_REFERENCE,
   1.180 +                offset = ReferenceInfoUtil.IGNORE_VALUE,
   1.181 +                genericLocation = { 3, 0 }),
   1.182 +        @TADescription(annotation = "CTC", type = CONSTRUCTOR_REFERENCE,
   1.183 +                offset = ReferenceInfoUtil.IGNORE_VALUE,
   1.184 +                genericLocation = { 3, 1 })
   1.185 +    })
   1.186 +    public String returnConstructorRef3() {
   1.187 +        return
   1.188 +                "class Lambda<S, T> {" +
   1.189 +                "  Lambda() { }" +
   1.190 +                "}" +
   1.191 +
   1.192 +                "@Target(ElementType.TYPE_USE)" +
   1.193 +                "@interface CTA {" +
   1.194 +                "  String value();" +
   1.195 +                "}" +
   1.196 +
   1.197 +                "@Target(ElementType.TYPE_USE)" +
   1.198 +                "@interface CTB {" +
   1.199 +                "  int age();" +
   1.200 +                "}" +
   1.201 +
   1.202 +                "@Target(ElementType.TYPE_USE)" +
   1.203 +                "@interface CTC {" +
   1.204 +                "  String name();" +
   1.205 +                "}" +
   1.206 +
   1.207 +                "class Test {" +
   1.208 +                "  Runnable lambda() {" +
   1.209 +                "    return @CTA(\"x\") Lambda<@CTB(age = 5) Integer, @CTC(name = \"y\") Float>::new;" +
   1.210 +                "  }" +
   1.211 +                "}";
   1.212 +    }
   1.213 +
   1.214 +
   1.215 +    @TADescriptions({
   1.216 +        @TADescription(annotation = "TA", type = METHOD_REFERENCE_TYPE_ARGUMENT,
   1.217 +                 offset = ReferenceInfoUtil.IGNORE_VALUE,
   1.218 +                 typeIndex = 0),
   1.219 +        @TADescription(annotation = "TB", type = METHOD_REFERENCE_TYPE_ARGUMENT,
   1.220 +                 offset = ReferenceInfoUtil.IGNORE_VALUE,
   1.221 +                 typeIndex = 1)
   1.222 +    })
   1.223 +    public String returnMethodRefTA1() {
   1.224 +        return
   1.225 +                "interface Lambda {" +
   1.226 +                "  <S, T> void generic(S p1, T p2);" +
   1.227 +                "}" +
   1.228 +
   1.229 +                "class LambdaImpl implements Lambda {" +
   1.230 +                "  public <S, T> void generic(S p1, T p2) {}" +
   1.231 +                "}" +
   1.232 +
   1.233 +                "class Test {" +
   1.234 +                "  Lambda lambda(LambdaImpl r) {" +
   1.235 +                "    return r::<@TA Object, @TB Object>generic;" +
   1.236 +                "  }" +
   1.237 +                "}";
   1.238 +    }
   1.239 +
   1.240 +    @TADescriptions({
   1.241 +        @TADescription(annotation = "TA", type = CONSTRUCTOR_REFERENCE_TYPE_ARGUMENT,
   1.242 +                 offset = ReferenceInfoUtil.IGNORE_VALUE,
   1.243 +                 typeIndex = 0),
   1.244 +        @TADescription(annotation = "TB", type = CONSTRUCTOR_REFERENCE_TYPE_ARGUMENT,
   1.245 +                 offset = ReferenceInfoUtil.IGNORE_VALUE,
   1.246 +                 typeIndex = 1)
   1.247 +    })
   1.248 +    public String returnConstructorRefTA2() {
   1.249 +        return
   1.250 +                "interface Lambda {" +
   1.251 +                "  <S, T> void generic(S p1, T p2);" +
   1.252 +                "}" +
   1.253 +
   1.254 +                "class LambdaImpl implements Lambda {" +
   1.255 +                "  <S, T> LambdaImpl(S p1, T p2) {}" +
   1.256 +                "  public <S, T> void generic(S p1, T p2) {}" +
   1.257 +                "}" +
   1.258 +
   1.259 +                "class Test {" +
   1.260 +                "  Lambda lambda() {" +
   1.261 +                "    return LambdaImpl::<@TA Object, @TB Object>new;" +
   1.262 +                "  }" +
   1.263 +                "}";
   1.264 +    }
   1.265 +
   1.266 +    @TADescriptions({
   1.267 +        @TADescription(annotation = "TA", type = METHOD_FORMAL_PARAMETER,
   1.268 +                paramIndex = 0),
   1.269 +        @TADescription(annotation = "TB", type = METHOD_FORMAL_PARAMETER,
   1.270 +                paramIndex = 1),
   1.271 +        @TADescription(annotation = "TC", type = METHOD_FORMAL_PARAMETER,
   1.272 +                paramIndex = 1, genericLocation = { 3, 0 }),
   1.273 +        @TADescription(annotation = "TD", type = LOCAL_VARIABLE,
   1.274 +                lvarOffset = ReferenceInfoUtil.IGNORE_VALUE,
   1.275 +                lvarLength = ReferenceInfoUtil.IGNORE_VALUE,
   1.276 +                lvarIndex = ReferenceInfoUtil.IGNORE_VALUE),
   1.277 +        @TADescription(annotation = "TE", type = CAST,
   1.278 +                offset = ReferenceInfoUtil.IGNORE_VALUE,
   1.279 +                typeIndex = 0)
   1.280 +    })
   1.281 +    public String returnLambdaExpr1() {
   1.282 +        return
   1.283 +                "interface LambdaInt {" +
   1.284 +                "  void lambda(Object p1, List<Object> p2);" +
   1.285 +                "}" +
   1.286 +                "class Test {" +
   1.287 +                "  LambdaInt getLambda() {" +
   1.288 +                "    return (@TA Object x, @TB List<@TC Object> y) -> { @TD Object l = null; System.out.println((@TE Object) l); };" +
   1.289 +                "  }" +
   1.290 +                "}";
   1.291 +    }
   1.292 +
   1.293 +    @TADescriptions({
   1.294 +        @TADescription(annotation = "TA", type = METHOD_FORMAL_PARAMETER,
   1.295 +            paramIndex = 0)})
   1.296 +    public String lambdaField1() {
   1.297 +        return
   1.298 +            "class Test {" +
   1.299 +                " java.util.function.IntUnaryOperator field = (@TA int y) -> 1;" +
   1.300 +            "}";
   1.301 +    }
   1.302 +
   1.303 +    @TADescriptions({
   1.304 +        @TADescription(annotation = "TA", type = METHOD_FORMAL_PARAMETER,
   1.305 +            paramIndex = 0)})
   1.306 +    public String lambdaField2() {
   1.307 +        return
   1.308 +            "class Test {" +
   1.309 +                " static java.util.function.IntUnaryOperator field = (@TA int y) -> 1;" +
   1.310 +            "}";
   1.311 +    }
   1.312 +}

mercurial