xref: /aosp_15_r20/art/test/2280-const-method-handle-validation/src/InvokeInterface.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 InvokeInterface {
22 
23     private int instanceField;
24 
unreachable()25     private static void unreachable() {
26         throw new AssertionError("unreachable!");
27     }
28 
29     @ConstantMethodHandle(
30         kind = ConstantMethodHandle.INVOKE_INTERFACE,
31         owner = "InvokeInterface",
32         fieldOrMethodName = "unreachable",
33         descriptor = "()V")
forStaticMethod()34     private static MethodHandle forStaticMethod() {
35         unreachable();
36         return null;
37     }
38 
39     @ConstantMethodHandle(
40         kind = ConstantMethodHandle.INVOKE_INTERFACE,
41         owner = "java/lang/Object",
42         fieldOrMethodName = "hashCode",
43         descriptor = "()I")
forInstanceMethod()44     private static MethodHandle forInstanceMethod() {
45         unreachable();
46         return null;
47     }
48 
49     @ConstantMethodHandle(
50         kind = ConstantMethodHandle.INVOKE_INTERFACE,
51         owner = "Main$I",
52         fieldOrMethodName = "privateMethod",
53         descriptor = "()V",
54         ownerIsInterface = true)
inaccessiblePrivateInterfaceMethod()55     private static MethodHandle inaccessiblePrivateInterfaceMethod() {
56         unreachable();
57         return null;
58     }
59 
runTests()60     public static void runTests() {
61         try {
62             InvokeInterfaceForStaticMethod.runTests();
63             unreachable();
64         } catch (IncompatibleClassChangeError | ClassFormatError expected) {}
65 
66         try {
67             InvokeInterfaceForInstanceMethod.runTests();
68             unreachable();
69         } catch (IncompatibleClassChangeError | ClassFormatError expected) {}
70 
71         try {
72             InvokeInterfaceForConstructor.runTests();
73             unreachable();
74         } catch (IncompatibleClassChangeError | ClassFormatError expected) {}
75 
76         try {
77             InvokeInterfaceForClassInitializer.runTests();
78             unreachable();
79         } catch (IncompatibleClassChangeError | ClassFormatError expected) {}
80 
81         try {
82             inaccessiblePrivateInterfaceMethod();
83             unreachable();
84         } catch (IncompatibleClassChangeError expected) {}
85     }
86 
87 }
88