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.invoke.MethodType;
 30 import java.lang.reflect.Field;
 31 import java.lang.reflect.Modifier;
 32 
 33 class MethodHandleBooleanFieldAccessorImpl extends MethodHandleFieldAccessorImpl {
 34     static FieldAccessorImpl fieldAccessor(Field field, MethodHandle getter, MethodHandle setter, boolean isReadOnly) {
 35         boolean isStatic = Modifier.isStatic(field.getModifiers());
 36         if (isStatic) {
 37             getter = getter.asType(MethodType.methodType(boolean.class));
 38             if (setter != null) {
 39                 setter = setter.asType(MethodType.methodType(void.class, boolean.class));
 40             }
 41         } else {
 42             getter = getter.asType(MethodType.methodType(boolean.class, Object.class));
 43             if (setter != null) {
 44                 setter = setter.asType(MethodType.methodType(void.class, Object.class, boolean.class));
 45             }
 46         }
 47         return new MethodHandleBooleanFieldAccessorImpl(field, getter, setter, isReadOnly, isStatic);
 48     }
 49 
 50     MethodHandleBooleanFieldAccessorImpl(Field field, MethodHandle getter, MethodHandle setter, boolean isReadOnly, boolean isStatic) {
 51         super(field, getter, setter, isReadOnly, isStatic);
 52     }
 53 
 54     public Object get(Object obj) throws IllegalArgumentException {
 55         return Boolean.valueOf(getBoolean(obj));
 56     }
 57 
 58     public boolean getBoolean(Object obj) throws IllegalArgumentException {
 59         try {
 60             if (isStatic()) {
 61                 return (boolean) getter.invokeExact();
 62             } else {
 63                 return (boolean) getter.invokeExact(obj);
 64             }
 65         } catch (IllegalArgumentException|NullPointerException e) {
 66             throw e;
 67         } catch (ClassCastException e) {
 68             throw newGetIllegalArgumentException(obj);
 69         } catch (Throwable e) {
 70             throw new InternalError(e);
 71         }
 72     }
 73 
 74     public byte getByte(Object obj) throws IllegalArgumentException {
 75         throw newGetByteIllegalArgumentException();
 76     }
 77 
 78     public char getChar(Object obj) throws IllegalArgumentException {
 79         throw newGetCharIllegalArgumentException();
 80     }
 81 
 82     public short getShort(Object obj) throws IllegalArgumentException {
 83         throw newGetShortIllegalArgumentException();
 84     }
 85 
 86     public int getInt(Object obj) throws IllegalArgumentException {
 87         throw newGetIntIllegalArgumentException();
 88     }
 89 
 90     public long getLong(Object obj) throws IllegalArgumentException {
 91         throw newGetLongIllegalArgumentException();
 92     }
 93 
 94     public float getFloat(Object obj) throws IllegalArgumentException {
 95         throw newGetFloatIllegalArgumentException();
 96     }
 97 
 98     public double getDouble(Object obj) throws IllegalArgumentException {
 99         throw newGetDoubleIllegalArgumentException();
100     }
101 
102     public void set(Object obj, Object value)
103             throws IllegalArgumentException, IllegalAccessException
104     {
105         ensureObj(obj);
106         if (isReadOnly()) {
107             throwFinalFieldIllegalAccessException(value);
108         }
109 
110         if (value == null) {
111             throwSetIllegalArgumentException(value);
112         }
113 
114         if (value instanceof Boolean b) {
115             setBoolean(obj, b.booleanValue());
116         } else {
117             throwSetIllegalArgumentException(value);
118         }
119     }
120 
121     public void setBoolean(Object obj, boolean z)
122         throws IllegalArgumentException, IllegalAccessException
123     {
124         if (isReadOnly()) {
125             ensureObj(obj);     // throw NPE if obj is null on instance field
126             throwFinalFieldIllegalAccessException(z);
127         }
128         try {
129             if (isStatic()) {
130                 setter.invokeExact(z);
131             } else {
132                 setter.invokeExact(obj, z);
133             }
134         } catch (IllegalArgumentException|NullPointerException e) {
135             throw e;
136         } catch (ClassCastException e) {
137             // receiver is of invalid type
138             throw newSetIllegalArgumentException(obj);
139         } catch (Throwable e) {
140             throw new InternalError(e);
141         }
142     }
143 
144     public void setByte(Object obj, byte b)
145         throws IllegalArgumentException, IllegalAccessException
146     {
147         throwSetIllegalArgumentException(b);
148     }
149 
150     public void setChar(Object obj, char c)
151         throws IllegalArgumentException, IllegalAccessException
152     {
153         throwSetIllegalArgumentException(c);
154     }
155 
156     public void setShort(Object obj, short s)
157         throws IllegalArgumentException, IllegalAccessException
158     {
159         throwSetIllegalArgumentException(s);
160     }
161 
162     public void setInt(Object obj, int i)
163         throws IllegalArgumentException, IllegalAccessException
164     {
165         throwSetIllegalArgumentException(i);
166     }
167 
168     public void setLong(Object obj, long l)
169         throws IllegalArgumentException, IllegalAccessException
170     {
171         throwSetIllegalArgumentException(l);
172     }
173 
174     public void setFloat(Object obj, float f)
175         throws IllegalArgumentException, IllegalAccessException
176     {
177         throwSetIllegalArgumentException(f);
178     }
179 
180     public void setDouble(Object obj, double d)
181         throws IllegalArgumentException, IllegalAccessException
182     {
183         throwSetIllegalArgumentException(d);
184     }
185 }