xref: /aosp_15_r20/art/test/2280-const-method-handle-validation/src/PlainPut.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 PlainPut {
22 
23     private final int finalField = 2;
24     private static int STATIC_FIELD;
25     private static final int STATIC_FINAL_FIELD = 1;
26 
unreachable()27     private static void unreachable() {
28         throw new AssertionError("unreachable!");
29     }
30 
31     @ConstantMethodHandle(
32         kind = ConstantMethodHandle.INSTANCE_PUT,
33         owner = "PlainGet",
34         fieldOrMethodName = "STATIC_FIELD",
35         descriptor = "I")
forStaticField()36     private static MethodHandle forStaticField() {
37         unreachable();
38         return null;
39     }
40 
41     @ConstantMethodHandle(
42         kind = ConstantMethodHandle.INSTANCE_PUT,
43         owner = "PlainGet",
44         fieldOrMethodName = "finalField",
45         descriptor = "I")
forFinalField()46     private static MethodHandle forFinalField() {
47         unreachable();
48         return null;
49     }
50 
51     @ConstantMethodHandle(
52         kind = ConstantMethodHandle.INSTANCE_PUT,
53         owner = "PlainGet",
54         fieldOrMethodName = "STATIC_FINAL_FIELD",
55         descriptor = "I")
forStaticFinalField()56     private static MethodHandle forStaticFinalField() {
57         unreachable();
58         return null;
59     }
60 
61     @ConstantMethodHandle(
62         kind = ConstantMethodHandle.INSTANCE_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.INSTANCE_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             forStaticField();
84             unreachable();
85         } catch (IncompatibleClassChangeError expected) {}
86 
87         try {
88             forFinalField();
89             unreachable();
90         } catch (IncompatibleClassChangeError expected) {}
91 
92         try {
93             forStaticFinalField();
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