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 MethodHandleObjectFieldAccessorImpl 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(Object.class));
 38             if (setter != null) {
 39                 setter = setter.asType(MethodType.methodType(void.class, Object.class));
 40             }
 41         } else {
 42             getter = getter.asType(MethodType.methodType(Object.class, Object.class));
 43             if (setter != null) {
 44                 setter = setter.asType(MethodType.methodType(void.class, Object.class, Object.class));
 45             }
 46         }
 47         return new MethodHandleObjectFieldAccessorImpl(field, getter, setter, isReadOnly, isStatic);
 48     }
 49 
 50     MethodHandleObjectFieldAccessorImpl(Field field, MethodHandle getter, MethodHandle setter, boolean isReadOnly, boolean isStatic) {
 51         super(field, getter, setter, isReadOnly, isStatic);
 52     }
 53 
 54     @Override
 55     public Object get(Object obj) throws IllegalArgumentException {
 56         try {
 57             return isStatic() ? getter.invokeExact() : getter.invokeExact(obj);
 58         } catch (IllegalArgumentException|NullPointerException e) {
 59             throw e;
 60         } catch (ClassCastException e) {
 61             throw newGetIllegalArgumentException(obj);
 62         } catch (Throwable e) {
 63             throw new InternalError(e);
 64         }
 65     }
 66 
 67     public boolean getBoolean(Object obj) throws IllegalArgumentException {
 68         throw newGetBooleanIllegalArgumentException();
 69     }
 70 
 71     public byte getByte(Object obj) throws IllegalArgumentException {
 72         throw newGetByteIllegalArgumentException();
 73     }
 74 
 75     public char getChar(Object obj) throws IllegalArgumentException {
 76         throw newGetCharIllegalArgumentException();
 77     }
 78 
 79     public short getShort(Object obj) throws IllegalArgumentException {
 80         throw newGetShortIllegalArgumentException();
 81     }
 82 
 83     public int getInt(Object obj) throws IllegalArgumentException {
 84         throw newGetIntIllegalArgumentException();
 85     }
 86 
 87     public long getLong(Object obj) throws IllegalArgumentException {
 88         throw newGetLongIllegalArgumentException();
 89     }
 90 
 91     public float getFloat(Object obj) throws IllegalArgumentException {
 92         throw newGetFloatIllegalArgumentException();
 93     }
 94 
 95     public double getDouble(Object obj) throws IllegalArgumentException {
 96         throw newGetDoubleIllegalArgumentException();
 97     }
 98 
 99     @Override
100     public void set(Object obj, Object value) throws IllegalAccessException {
101         ensureObj(obj);
102         if (isReadOnly()) {
103             throwFinalFieldIllegalAccessException(value);
104         }
105         try {
106             if (isStatic()) {
107                 setter.invokeExact(value);
108             } else {
109                 setter.invokeExact(obj, value);
110             }
111         } catch (IllegalArgumentException|NullPointerException e) {
112             throw e;
113         } catch (ClassCastException e) {
114             // already ensure the receiver type.  So this CCE is due to the value.
115             throwSetIllegalArgumentException(value);
116         } catch (Throwable e) {
117             throw new InternalError(e);
118         }
119     }
120 
121     public void setBoolean(Object obj, boolean z)
122         throws IllegalArgumentException, IllegalAccessException
123     {
124         throwSetIllegalArgumentException(z);
125     }
126 
127     public void setByte(Object obj, byte b)
128         throws IllegalArgumentException, IllegalAccessException
129     {
130         throwSetIllegalArgumentException(b);
131     }
132 
133     public void setChar(Object obj, char c)
134         throws IllegalArgumentException, IllegalAccessException
135     {
136         throwSetIllegalArgumentException(c);
137     }
138 
139     public void setShort(Object obj, short s)
140         throws IllegalArgumentException, IllegalAccessException
141     {
142         throwSetIllegalArgumentException(s);
143     }
144 
145     public void setInt(Object obj, int i)
146         throws IllegalArgumentException, IllegalAccessException
147     {
148         throwSetIllegalArgumentException(i);
149     }
150 
151     public void setLong(Object obj, long l)
152         throws IllegalArgumentException, IllegalAccessException
153     {
154         throwSetIllegalArgumentException(l);
155     }
156 
157     public void setFloat(Object obj, float f)
158         throws IllegalArgumentException, IllegalAccessException
159     {
160         throwSetIllegalArgumentException(f);
161     }
162 
163     public void setDouble(Object obj, double d)
164         throws IllegalArgumentException, IllegalAccessException
165     {
166         throwSetIllegalArgumentException(d);
167     }
168 }