< prev index next >

src/java.base/share/classes/java/lang/reflect/Field.java

Print this page

        

@@ -165,11 +165,18 @@
      */
     @Override
     @CallerSensitive
     public void setAccessible(boolean flag) {
         AccessibleObject.checkPermission();
-        if (flag) checkCanSetAccessible(Reflection.getCallerClass());
+
+        if (clazz.isInlineClass()) {
+            throw new InaccessibleObjectException("cannot make a field accessible of inline class "
+                    + clazz.getName());
+        }
+        if (flag) {
+            checkCanSetAccessible(Reflection.getCallerClass());
+        }
         setAccessible0(flag);
     }
 
     @Override
     void checkCanSetAccessible(Class<?> caller) {

@@ -771,10 +778,12 @@
     @CallerSensitive
     @ForceInline // to ensure Reflection.getCallerClass optimization
     public void set(Object obj, Object value)
         throws IllegalArgumentException, IllegalAccessException
     {
+        ensureNotValueClass();
+
         if (!override) {
             Class<?> caller = Reflection.getCallerClass();
             checkAccess(caller, obj);
         }
         getFieldAccessor(obj).set(obj, value);

@@ -807,10 +816,12 @@
     @CallerSensitive
     @ForceInline // to ensure Reflection.getCallerClass optimization
     public void setBoolean(Object obj, boolean z)
         throws IllegalArgumentException, IllegalAccessException
     {
+        ensureNotValueClass();
+
         if (!override) {
             Class<?> caller = Reflection.getCallerClass();
             checkAccess(caller, obj);
         }
         getFieldAccessor(obj).setBoolean(obj, z);

@@ -843,10 +854,12 @@
     @CallerSensitive
     @ForceInline // to ensure Reflection.getCallerClass optimization
     public void setByte(Object obj, byte b)
         throws IllegalArgumentException, IllegalAccessException
     {
+        ensureNotValueClass();
+
         if (!override) {
             Class<?> caller = Reflection.getCallerClass();
             checkAccess(caller, obj);
         }
         getFieldAccessor(obj).setByte(obj, b);

@@ -879,10 +892,12 @@
     @CallerSensitive
     @ForceInline // to ensure Reflection.getCallerClass optimization
     public void setChar(Object obj, char c)
         throws IllegalArgumentException, IllegalAccessException
     {
+        ensureNotValueClass();
+
         if (!override) {
             Class<?> caller = Reflection.getCallerClass();
             checkAccess(caller, obj);
         }
         getFieldAccessor(obj).setChar(obj, c);

@@ -915,10 +930,12 @@
     @CallerSensitive
     @ForceInline // to ensure Reflection.getCallerClass optimization
     public void setShort(Object obj, short s)
         throws IllegalArgumentException, IllegalAccessException
     {
+        ensureNotValueClass();
+
         if (!override) {
             Class<?> caller = Reflection.getCallerClass();
             checkAccess(caller, obj);
         }
         getFieldAccessor(obj).setShort(obj, s);

@@ -951,10 +968,12 @@
     @CallerSensitive
     @ForceInline // to ensure Reflection.getCallerClass optimization
     public void setInt(Object obj, int i)
         throws IllegalArgumentException, IllegalAccessException
     {
+        ensureNotValueClass();
+
         if (!override) {
             Class<?> caller = Reflection.getCallerClass();
             checkAccess(caller, obj);
         }
         getFieldAccessor(obj).setInt(obj, i);

@@ -987,10 +1006,12 @@
     @CallerSensitive
     @ForceInline // to ensure Reflection.getCallerClass optimization
     public void setLong(Object obj, long l)
         throws IllegalArgumentException, IllegalAccessException
     {
+        ensureNotValueClass();
+
         if (!override) {
             Class<?> caller = Reflection.getCallerClass();
             checkAccess(caller, obj);
         }
         getFieldAccessor(obj).setLong(obj, l);

@@ -1023,10 +1044,12 @@
     @CallerSensitive
     @ForceInline // to ensure Reflection.getCallerClass optimization
     public void setFloat(Object obj, float f)
         throws IllegalArgumentException, IllegalAccessException
     {
+        ensureNotValueClass();
+
         if (!override) {
             Class<?> caller = Reflection.getCallerClass();
             checkAccess(caller, obj);
         }
         getFieldAccessor(obj).setFloat(obj, f);

@@ -1059,10 +1082,12 @@
     @CallerSensitive
     @ForceInline // to ensure Reflection.getCallerClass optimization
     public void setDouble(Object obj, double d)
         throws IllegalArgumentException, IllegalAccessException
     {
+        ensureNotValueClass();
+
         if (!override) {
             Class<?> caller = Reflection.getCallerClass();
             checkAccess(caller, obj);
         }
         getFieldAccessor(obj).setDouble(obj, d);

@@ -1075,10 +1100,20 @@
         checkAccess(caller, clazz,
                     Modifier.isStatic(modifiers) ? null : obj.getClass(),
                     modifiers);
     }
 
+    /*
+     * Ensure the declaring class is not an inline class.
+     */
+    private void ensureNotValueClass() throws IllegalAccessException {
+        if (clazz.isInlineClass()) {
+            throw new IllegalAccessException("cannot set field \"" + this + "\" of inline class "
+                + clazz.getName());
+        }
+    }
+
     // security check is done before calling this method
     private FieldAccessor getFieldAccessor(Object obj)
         throws IllegalAccessException
     {
         boolean ov = override;
< prev index next >