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 MethodHandleShortFieldAccessorImpl 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(short.class));
 38             if (setter != null) {
 39                 setter = setter.asType(MethodType.methodType(void.class, short.class));
 40             }
 41         } else {
 42             getter = getter.asType(MethodType.methodType(short.class, Object.class));
 43             if (setter != null) {
 44                 setter = setter.asType(MethodType.methodType(void.class, Object.class, short.class));
 45             }
 46         }
 47         return new MethodHandleShortFieldAccessorImpl(field, getter, setter, isReadOnly, isStatic);
 48     }
 49 
 50     MethodHandleShortFieldAccessorImpl(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 Short.valueOf(getShort(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         try {
 72             if (isStatic()) {
 73                 return (short) getter.invokeExact();
 74             } else {
 75                 return (short) getter.invokeExact(obj);
 76             }
 77         } catch (IllegalArgumentException|IllegalStateException|NullPointerException e) {
 78             throw e;
 79         } catch (ClassCastException e) {
 80             throw newGetIllegalArgumentException(obj);
 81         } catch (Throwable e) {
 82             throw new InternalError(e);
 83         }
 84     }
 85 
 86     public int getInt(Object obj) throws IllegalArgumentException {
 87         return getShort(obj);
 88     }
 89 
 90     public long getLong(Object obj) throws IllegalArgumentException {
 91         return getShort(obj);
 92     }
 93 
 94     public float getFloat(Object obj) throws IllegalArgumentException {
 95         return getShort(obj);
 96     }
 97 
 98     public double getDouble(Object obj) throws IllegalArgumentException {
 99         return getShort(obj);
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 Byte b) {
115             setShort(obj, b.byteValue());
116         }
117         else if (value instanceof Short s) {
118             setShort(obj, s.shortValue());
119         }
120         else {
121             throwSetIllegalArgumentException(value);
122         }
123     }
124 
125     public void setBoolean(Object obj, boolean z)
126         throws IllegalArgumentException, IllegalAccessException
127     {
128         throwSetIllegalArgumentException(z);
129     }
130 
131     public void setByte(Object obj, byte b)
132         throws IllegalArgumentException, IllegalAccessException
133     {
134         setShort(obj, b);
135     }
136 
137     public void setChar(Object obj, char c)
138         throws IllegalArgumentException, IllegalAccessException
139     {
140         throwSetIllegalArgumentException(c);
141     }
142 
143     public void setShort(Object obj, short s)
144         throws IllegalArgumentException, IllegalAccessException
145     {
146         if (isReadOnly()) {
147             ensureObj(obj);     // throw NPE if obj is null on instance field
148             throwFinalFieldIllegalAccessException(s);
149         }
150         try {
151             if (isStatic()) {
152                 setter.invokeExact(s);
153             } else {
154                 setter.invokeExact(obj, s);
155             }
156         } catch (IllegalArgumentException|IllegalStateException|NullPointerException e) {
157             throw e;
158         } catch (ClassCastException e) {
159             // receiver is of invalid type
160             throw newSetIllegalArgumentException(obj);
161         } catch (Throwable e) {
162             throw new InternalError(e);
163         }
164     }
165 
166     public void setInt(Object obj, int i)
167         throws IllegalArgumentException, IllegalAccessException
168     {
169         throwSetIllegalArgumentException(i);
170     }
171 
172     public void setLong(Object obj, long l)
173         throws IllegalArgumentException, IllegalAccessException
174     {
175         throwSetIllegalArgumentException(l);
176     }
177 
178     public void setFloat(Object obj, float f)
179         throws IllegalArgumentException, IllegalAccessException
180     {
181         throwSetIllegalArgumentException(f);
182     }
183 
184     public void setDouble(Object obj, double d)
185         throws IllegalArgumentException, IllegalAccessException
186     {
187         throwSetIllegalArgumentException(d);
188     }
189 }