test/jdk/java/lang/constant/access_test/pkg1/MethodTypeDescriptorAccessTest.java
changeset 55502 bdaec4628ea9
equal deleted inserted replaced
55501:c9590e526d19 55502:bdaec4628ea9
       
     1 /*
       
     2  * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
       
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
       
     4  *
       
     5  * This code is free software; you can redistribute it and/or modify it
       
     6  * under the terms of the GNU General Public License version 2 only, as
       
     7  * published by the Free Software Foundation.
       
     8  *
       
     9  * This code is distributed in the hope that it will be useful, but WITHOUT
       
    10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       
    12  * version 2 for more details (a copy is included in the LICENSE file that
       
    13  * accompanied this code).
       
    14  *
       
    15  * You should have received a copy of the GNU General Public License version
       
    16  * 2 along with this work; if not, write to the Free Software Foundation,
       
    17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
       
    18  *
       
    19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
       
    20  * or visit www.oracle.com if you need additional information or have any
       
    21  * questions.
       
    22  */
       
    23 
       
    24 /*
       
    25  * @test
       
    26  * @bug 8226709
       
    27  * @summary MethodTypeDesc::resolveConstantDesc needs access check per the specification
       
    28  * @compile ../pkg2/PublicClass.java ../pkg2/NonPublicClass.java
       
    29  * @run main pkg1.MethodTypeDescriptorAccessTest
       
    30  */
       
    31 
       
    32 package pkg1;
       
    33 
       
    34 import java.lang.invoke.*;
       
    35 import java.lang.invoke.MethodType;
       
    36 import java.lang.constant.*;
       
    37 
       
    38 import static java.lang.invoke.MethodHandles.*;
       
    39 import static java.lang.invoke.MethodHandles.Lookup.*;
       
    40 import static java.lang.invoke.MethodType.*;
       
    41 
       
    42 public class MethodTypeDescriptorAccessTest {
       
    43     public static void main(String... args) throws Throwable {
       
    44         new MethodTypeDescriptorAccessTest().test();
       
    45     }
       
    46 
       
    47     void test() {
       
    48         Lookup selfLookup = MethodHandles.lookup();
       
    49         //first test PublicClass
       
    50         String descriptorpub = "(Lpkg2/PublicClass;)Lpkg2/PublicClass;";
       
    51         MethodTypeDesc mtdpub = MethodTypeDesc.ofDescriptor(descriptorpub);
       
    52         checkValidAccess(mtdpub, selfLookup);
       
    53 
       
    54         // test NonPublicClass in the return type
       
    55         String descriptornp = "()Lpkg2/NonPublicClass;";
       
    56         MethodTypeDesc mtdnp = MethodTypeDesc.ofDescriptor(descriptornp);
       
    57         checkInvalidAccess(mtdnp, selfLookup);
       
    58 
       
    59         // test NonPublicClass in the parameters
       
    60         descriptornp = "(Lpkg2/NonPublicClass;)I";
       
    61         mtdnp = MethodTypeDesc.ofDescriptor(descriptornp);
       
    62         checkInvalidAccess(mtdnp, selfLookup);
       
    63 
       
    64         MethodType mt = MethodType.fromMethodDescriptorString("(Lpkg2/NonPublicClass;)I", selfLookup.lookupClass().getClassLoader());
       
    65     }
       
    66 
       
    67     private void checkValidAccess(MethodTypeDesc mtd, Lookup lookup) {
       
    68         try {
       
    69             MethodType mt = (MethodType)mtd.resolveConstantDesc(lookup);
       
    70         } catch (ReflectiveOperationException unexpected) {
       
    71             throw new Error("resolveConstantDesc() threw ReflectiveOperationException unexpectedly with cause " +
       
    72                     unexpected.getCause() + " for " + mtd);
       
    73         }
       
    74     }
       
    75 
       
    76     private void checkInvalidAccess(MethodTypeDesc mtd, Lookup lookup) {
       
    77         try {
       
    78             MethodType mt = (MethodType)mtd.resolveConstantDesc(lookup);
       
    79             throw new Error("resolveConstantDesc() succeeded unexpectedly " + mtd);
       
    80         } catch (ReflectiveOperationException expected) {
       
    81             if (expected.getClass() != IllegalAccessException.class) {
       
    82                 throw new Error("resolveConstantDesc() threw unexpected ReflectiveOperationException with cause " +
       
    83                         expected.getCause() + " for " + mtd);
       
    84             }
       
    85         }
       
    86     }
       
    87 
       
    88 }