xref: /aosp_15_r20/art/test/2280-const-method-handle-validation/src/StaticPut.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 StaticPut {
22 
23     private int instanceField;
24     private final int finalInstanceField = 1;
25     private static final int STATIC_FINAL_FIELD = 2;
26 
unreachable()27     private static void unreachable() {
28         throw new AssertionError("unreachable!");
29     }
30 
31     @ConstantMethodHandle(
32         kind = ConstantMethodHandle.STATIC_PUT,
33         owner = "StaticPut",
34         fieldOrMethodName = "instanceField",
35         descriptor = "I")
forInstanceField()36     private static MethodHandle forInstanceField() {
37         unreachable();
38         return null;
39     }
40 
41     @ConstantMethodHandle(
42         kind = ConstantMethodHandle.STATIC_PUT,
43         owner = "StaticPut",
44         fieldOrMethodName = "finalInstanceField",
45         descriptor = "I")
forFinalInstanceField()46     private static MethodHandle forFinalInstanceField() {
47         unreachable();
48         return null;
49     }
50 
51     @ConstantMethodHandle(
52         kind = ConstantMethodHandle.STATIC_PUT,
53         owner = "StaticPut",
54         fieldOrMethodName = "STATIC_FINAL_FIELD",
55         descriptor = "I")
forFinalStaticField()56     private static MethodHandle forFinalStaticField() {
57         unreachable();
58         return null;
59     }
60 
61     @ConstantMethodHandle(
62         kind = ConstantMethodHandle.STATIC_PUT,
63         owner = "Main",
64         fieldOrMethodName = "privateField",
65         descriptor = "I")
inaccessibleInstanceField()66     private static MethodHandle inaccessibleInstanceField() {
67         unreachable();
68         return null;
69     }
70 
71     @ConstantMethodHandle(
72         kind = ConstantMethodHandle.STATIC_PUT,
73         owner = "Main",
74         fieldOrMethodName = "PRIVATE_STATIC_FIELD",
75         descriptor = "I")
inaccessibleStaticField()76     private static MethodHandle inaccessibleStaticField() {
77         unreachable();
78         return null;
79     }
80 
runTests()81     public static void runTests() {
82         try {
83             forInstanceField();
84             unreachable();
85         } catch (IncompatibleClassChangeError expected) {}
86 
87         try {
88             forFinalInstanceField();
89             unreachable();
90         } catch (IncompatibleClassChangeError expected) {}
91 
92         try {
93             forFinalStaticField();
94             unreachable();
95         } catch (IncompatibleClassChangeError expected) {}
96 
97         try {
98             inaccessibleInstanceField();
99             unreachable();
100         } catch (IncompatibleClassChangeError expected) {}
101 
102         try {
103             inaccessibleStaticField();
104             unreachable();
105         } catch (IncompatibleClassChangeError expected) {}
106     }
107 
108 }
109