xref: /aosp_15_r20/art/test/2280-const-method-handle-validation/src/InvokeSpecial.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 InvokeSpecial {
22 
23     private int instanceField;
24 
unreachable()25     private static void unreachable() {
26         throw new AssertionError("unreachable!");
27     }
28 
method()29     public void method() {}
30 
31     @ConstantMethodHandle(
32         kind = ConstantMethodHandle.INVOKE_SPECIAL,
33         owner = "InvokeSpecial",
34         fieldOrMethodName = "unreachable",
35         descriptor = "()V")
forStaticMethod()36     private static MethodHandle forStaticMethod() {
37         unreachable();
38         return null;
39     }
40 
41     @ConstantMethodHandle(
42         kind = ConstantMethodHandle.INVOKE_SPECIAL,
43         owner = "java/util/List",
44         fieldOrMethodName = "size",
45         descriptor = "()I")
forInterfaceMethod()46     private static MethodHandle forInterfaceMethod() {
47         unreachable();
48         return null;
49     }
50 
51     @ConstantMethodHandle(
52         kind = ConstantMethodHandle.INVOKE_SPECIAL,
53         owner = "Main",
54         fieldOrMethodName = "instanceMethod",
55         descriptor = "()V")
inaccessiblePrivateMethod()56     private static MethodHandle inaccessiblePrivateMethod() {
57         unreachable();
58         return null;
59     }
60 
61     @ConstantMethodHandle(
62         kind = ConstantMethodHandle.INVOKE_SPECIAL,
63         owner = "Main",
64         fieldOrMethodName = "staticMethod",
65         descriptor = "()V")
inaccessibleStaticMethod()66     private static MethodHandle inaccessibleStaticMethod() {
67         unreachable();
68         return null;
69     }
70 
runTests()71     public static void runTests() {
72         try {
73             forStaticMethod();
74             unreachable();
75         } catch (IncompatibleClassChangeError expected) {}
76 
77         // TODO(b/297147201): runtime crashes here.
78         /*
79         try {
80             forInterfaceMethod();
81             unreachable();
82         } catch (IncompatibleClassChangeError expected) {}
83         */
84 
85         // TODO(b/297147201): runtime does not throw exception here.
86         /*
87         try {
88             InvokeSpecialForConstructor.runTests();
89             unreachable();
90         } catch (IncompatibleClassChangeError | ClassFormatError expected) {}
91         */
92 
93         try {
94             InvokeSpecialForClassInitializer.runTests();
95             unreachable();
96         } catch (IncompatibleClassChangeError | ClassFormatError expected) {}
97 
98         try {
99             inaccessibleStaticMethod();
100             unreachable();
101         } catch (IncompatibleClassChangeError expected) {}
102     }
103 
104 }
105