xref: /aosp_15_r20/art/test/2280-const-method-handle-validation/src/InvokeStatic.java (revision 795d594fd825385562da6b089ea9b2033f3abf5a)
1 /*
2  * Copyright (C) 2024 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 import java.lang.invoke.MethodHandle;
18 
19 import annotations.ConstantMethodHandle;
20 
21 public class InvokeStatic {
22     private int instanceField;
23 
unreachable()24     private static void unreachable() {
25         throw new AssertionError("unreachable!");
26     }
27 
method()28     public void method() {}
29 
30     @ConstantMethodHandle(
31         kind = ConstantMethodHandle.INVOKE_STATIC,
32         owner = "InvokeStatic",
33         fieldOrMethodName = "method",
34         descriptor = "()V")
forInstanceMethod()35     private static MethodHandle forInstanceMethod() {
36         unreachable();
37         return null;
38     }
39 
40     @ConstantMethodHandle(
41         kind = ConstantMethodHandle.INVOKE_STATIC,
42         owner = "java/util/List",
43         fieldOrMethodName = "size",
44         descriptor = "()I")
forInterfaceMethod()45     private static MethodHandle forInterfaceMethod() {
46         unreachable();
47         return null;
48     }
49 
50     @ConstantMethodHandle(
51         kind = ConstantMethodHandle.INVOKE_STATIC,
52         owner = "Main",
53         fieldOrMethodName = "instanceMethod",
54         descriptor = "()V")
inaccessibleInstanceMethod()55     private static MethodHandle inaccessibleInstanceMethod() {
56         unreachable();
57         return null;
58     }
59 
60     @ConstantMethodHandle(
61         kind = ConstantMethodHandle.INVOKE_STATIC,
62         owner = "Main",
63         fieldOrMethodName = "staticMethod",
64         descriptor = "()V")
inaccessibleStaticMethod()65     private static MethodHandle inaccessibleStaticMethod() {
66         unreachable();
67         return null;
68     }
69 
runTests()70     public static void runTests() {
71         try {
72             forInstanceMethod();
73             unreachable();
74         } catch (IncompatibleClassChangeError expected) {}
75 
76         try {
77             forInterfaceMethod();
78             unreachable();
79         } catch (IncompatibleClassChangeError expected) {}
80 
81         try {
82             InvokeStaticForConstructor.runTests();
83             unreachable();
84         } catch (IncompatibleClassChangeError | ClassFormatError expected) {}
85 
86         try {
87             InvokeStaticForClassInitializer.runTests();
88             unreachable();
89         } catch (IncompatibleClassChangeError | ClassFormatError expected) {}
90 
91         try {
92             inaccessibleInstanceMethod();
93             unreachable();
94         } catch (IncompatibleClassChangeError expected) {}
95 
96         try {
97             inaccessibleStaticMethod();
98             unreachable();
99         } catch (IncompatibleClassChangeError expected) {}
100     }
101 
102 }
103