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 MethodHandleDoubleFieldAccessorImpl 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(double.class));
 38             if (setter != null) {
 39                 setter = setter.asType(MethodType.methodType(void.class, double.class));
 40             }
 41         } else {
 42             getter = getter.asType(MethodType.methodType(double.class, Object.class));
 43             if (setter != null) {
 44                 setter = setter.asType(MethodType.methodType(void.class, Object.class, double.class));
 45             }
 46         }
 47         return new MethodHandleDoubleFieldAccessorImpl(field, getter, setter, isReadOnly, isStatic);
 48     }
 49 
 50     MethodHandleDoubleFieldAccessorImpl(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 Double.valueOf(getDouble(obj));
 56     }
 57 
 58     public boolean getBoolean(Object obj) throws IllegalArgumentException {
 59         throw newGetBooleanIllegalArgumentException();
 60     }
 61 
 62     public byte getByte(Object obj) throws IllegalArgumentException {
 63         throw newGetByteIllegalArgumentException();
 64     }
 65 
 66     public char getChar(Object obj) throws IllegalArgumentException {
 67         throw newGetCharIllegalArgumentException();
 68     }
 69 
 70     public short getShort(Object obj) throws IllegalArgumentException {
 71         throw newGetShortIllegalArgumentException();
 72     }
 73 
 74     public int getInt(Object obj) throws IllegalArgumentException {
 75         throw newGetIntIllegalArgumentException();
 76     }
 77 
 78     public long getLong(Object obj) throws IllegalArgumentException {
 79         throw newGetLongIllegalArgumentException();
 80     }
 81 
 82     public float getFloat(Object obj) throws IllegalArgumentException {
 83         throw newGetFloatIllegalArgumentException();
 84     }
 85 
 86     public double getDouble(Object obj) throws IllegalArgumentException {
 87         try {
 88             if (isStatic()) {
 89                 return (double) getter.invokeExact();
 90             } else {
 91                 return (double) getter.invokeExact(obj);
 92             }
 93         } catch (IllegalArgumentException|NullPointerException e) {
 94             throw e;
 95         } catch (ClassCastException e) {
 96             throw newGetIllegalArgumentException(obj.getClass());
 97         } catch (Throwable e) {
 98             throw new InternalError(e);
 99         }
100     }
101 
102     public void set(Object obj, Object value)
103             throws IllegalArgumentException, IllegalAccessException
104     {
105         if (isReadOnly()) {
106             ensureObj(obj);     // throw NPE if obj is null on instance field
107             throwFinalFieldIllegalAccessException(value);
108         }
109 
110         if (value == null) {
111             throwSetIllegalArgumentException(value);
112         }
113 
114         if (value instanceof Byte b) {
115             setDouble(obj, b.byteValue());
116         }
117         else if (value instanceof Short s) {
118             setDouble(obj, s.shortValue());
119         }
120         else if (value instanceof Character c) {
121             setDouble(obj, c.charValue());
122         }
123         else if (value instanceof Integer i) {
124             setDouble(obj, i.intValue());
125         }
126         else if (value instanceof Long l) {
127             setDouble(obj, l.longValue());
128         }
129         else if (value instanceof Float f) {
130             setDouble(obj, f.floatValue());
131         }
132         else if (value instanceof Double d) {
133             setDouble(obj, d.doubleValue());
134         }
135         else {
136             throwSetIllegalArgumentException(value);
137         }
138     }
139 
140     public void setBoolean(Object obj, boolean z)
141         throws IllegalArgumentException, IllegalAccessException
142     {
143         throwSetIllegalArgumentException(z);
144     }
145 
146     public void setByte(Object obj, byte b)
147         throws IllegalArgumentException, IllegalAccessException
148     {
149         setDouble(obj, b);
150     }
151 
152     public void setChar(Object obj, char c)
153         throws IllegalArgumentException, IllegalAccessException
154     {
155         setDouble(obj, c);
156     }
157 
158     public void setShort(Object obj, short s)
159         throws IllegalArgumentException, IllegalAccessException
160     {
161         setDouble(obj, s);
162     }
163 
164     public void setInt(Object obj, int i)
165         throws IllegalArgumentException, IllegalAccessException
166     {
167         setDouble(obj, i);
168     }
169 
170     public void setLong(Object obj, long l)
171         throws IllegalArgumentException, IllegalAccessException
172     {
173         setDouble(obj, l);
174     }
175 
176     public void setFloat(Object obj, float f)
177         throws IllegalArgumentException, IllegalAccessException
178     {
179         setDouble(obj, f);
180     }
181 
182     public void setDouble(Object obj, double d)
183         throws IllegalArgumentException, IllegalAccessException
184     {
185         if (isReadOnly()) {
186             ensureObj(obj);     // throw NPE if obj is null on instance field
187             throwFinalFieldIllegalAccessException(d);
188         }
189         try {
190             if (isStatic()) {
191                 setter.invokeExact(d);
192             } else {
193                 setter.invokeExact(obj, d);
194             }
195         } catch (IllegalArgumentException|NullPointerException e) {
196             throw e;
197         } catch (ClassCastException e) {
198             throw newSetIllegalArgumentException(obj.getClass());
199         } catch (Throwable e) {
200             throw new InternalError(e);
201         }
202     }
203 }