1 /*
 2  * Copyright (c) 2021, Oracle and/or its affiliates. All rights reserved.
 3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 4  *
 5  * This code is free software; you can redistribute it and/or modify it
 6  * under the terms of the GNU General Public License version 2 only, as
 7  * published by the Free Software Foundation.  Oracle designates this
 8  * particular file as subject to the "Classpath" exception as provided
 9  * by Oracle in the LICENSE file that accompanied this code.
10  *
11  * This code is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14  * version 2 for more details (a copy is included in the LICENSE file that
15  * accompanied this code).
16  *
17  * You should have received a copy of the GNU General Public License version
18  * 2 along with this work; if not, write to the Free Software Foundation,
19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20  *
21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22  * or visit www.oracle.com if you need additional information or have any
23  * questions.
24  */
25 
26 package jdk.internal.reflect;
27 
28 import java.lang.invoke.MethodHandle;
29 import java.lang.reflect.Field;
30 
31 abstract class MethodHandleFieldAccessorImpl extends FieldAccessorImpl {
32     private static final int IS_READ_ONLY_BIT = 0x0001;
33     private static final int IS_STATIC_BIT = 0x0002;
34     private static final int NONZERO_BIT = 0x8000;
35 
36     private final int fieldFlags;
37     protected final MethodHandle getter;
38     protected final MethodHandle setter;
39 
40     protected MethodHandleFieldAccessorImpl(Field field, MethodHandle getter, MethodHandle setter, boolean isReadOnly, boolean isStatic) {
41         super(field);
42         this.fieldFlags = (isReadOnly ? IS_READ_ONLY_BIT : 0) |
43                           (isStatic ? IS_STATIC_BIT : 0) |
44                           NONZERO_BIT;
45         this.getter = getter;
46         this.setter = setter;
47     }
48 
49     protected final boolean isReadOnly() {
50         return (fieldFlags & IS_READ_ONLY_BIT) == IS_READ_ONLY_BIT;
51     }
52 
53     protected final boolean isStatic() {
54         return (fieldFlags & IS_STATIC_BIT) == IS_STATIC_BIT;
55     }
56 
57     protected final void ensureObj(Object o) {
58         if (!isStatic()) {
59             // for compatibility, check the receiver object first
60             // throw NullPointerException if o is null
61             if (!field.getDeclaringClass().isAssignableFrom(o.getClass())) {
62                 throwSetIllegalArgumentException(o);
63             }
64         }
65     }
66 
67     /**
68      * IllegalArgumentException because Field::get on the specified object, which
69      * is not an instance of the class or interface declaring the underlying method
70      */
71     protected IllegalArgumentException newGetIllegalArgumentException(Class<?> type) {
72         return new IllegalArgumentException(getMessage(true, type.getName()));
73     }
74 
75     /**
76      * IllegalArgumentException because Field::set on the specified object, which
77      * is not an instance of the class or interface declaring the underlying method
78      */
79     protected IllegalArgumentException newSetIllegalArgumentException(Class<?> type) {
80         return new IllegalArgumentException(getMessage(false, type.getName()));
81     }
82 
83 
84 }