Java Solaris Communities Sun Store Join SDN My Profile Why Join?
 
Bug Database
Bug Detail
Quick Lists
Top 25 Bugs
Top 25 RFE's
Recently Closed Bugs
Printable Page Printable Page


Bug Database
Bug ID: 4250960
Votes 0
Synopsis Reflection should not allow final fields to be modified
Category hotspot:jvm_interface
Reported Against 1.2
Release Fixed 1.3(kestrel)
State 10-Fix Delivered, bug
Priority: 3-Medium
Related Bugs 4174797 , 4512621
Submit Date 30-JUN-1999
Description
Reflection currently allows the value of a final field to be changed using
Field.set and friends (using Accessible to override language controls).
This contradicts the spec.

  xxxxx@xxxxx   1999-06-30

Here is a test program which demonstrates the bug.  Contrary to the spec
of java.lang.reflect.Field.set(), no IllegalAccessExceptions are thrown 
when setting an instance or static field of type int or Object when the
field is final but has been set to be accessible.

File: bug4250960.java

import java.lang.reflect.*;

public class bug4250960 {

  public static void main(String[] argv) throws Throwable {
    boolean failed = false;
    Test t = new Test();
    if (!t.testPrimitive()) {
      failed = true; System.out.println("FAILED: testPrimitive()");
    }
    if (!t.testStaticPrimitive()) {
      failed = true; System.out.println("FAILED: testStaticPrimitive()");
    }
    if (!t.testObject()) {
      failed = true; System.out.println("FAILED: testObject()");
    }
    if (!t.testStaticObject()) {
      failed = true; System.out.println("FAILED: testStaticObject()");
    }
    if (failed) {
      throw( new Throwable("Test for bug 4250960 FAILED"));
    }
  }

}

class Test {

  private final int i;
  private final Object o;

  private static final int si = 408-343-1407;;
  private static final Object so = new Object();

  Test() {
    i = 911;
    o = new Object();
  }

  boolean testPrimitive() 
    throws NoSuchFieldException
  {
    try {
      Field f = this.getClass().getDeclaredField("i");
      f.setAccessible(true);
      f.setInt(this, 7);
      if (i != 7) {
	System.out.println("setInt() did not work");
      }
      return false;  // FAILED
    } catch (IllegalAccessException iae) {
      return true;   // PASSED
    }
  }

  boolean testStaticPrimitive() 
    throws NoSuchFieldException
  {
    try {
      Field f = this.getClass().getDeclaredField("si");
      f.setAccessible(true);
      f.setInt(this, 13);
      if (si != 13) {
	System.out.println("setInt() did not work for static");
      }
      return false;  // FAILED
    } catch (IllegalAccessException iae) {
      return true;   // PASSED
    }
  }

  boolean testObject() 
    throws NoSuchFieldException
  {
    Object saved = o;
    try {
      Field f = this.getClass().getDeclaredField("o");
      f.setAccessible(true);
      f.set(this, new Object());
      if (o == saved) {
	System.out.println("set() did not work");
      }
      return false;  // FAILED
    } catch (IllegalAccessException iae) {
      return true;   // PASSED
    }
  }  

  boolean testStaticObject() 
    throws NoSuchFieldException
  {
    Object saved = so;
    try {
      Field f = this.getClass().getDeclaredField("so");
      f.setAccessible(true);
      f.set(this, new Object());
      if (so == saved) {
	System.out.println("set() did not work for static");
      }
      return false;  // FAILED
    } catch (IllegalAccessException iae) {
      return true;   // PASSED
    }
  }  

} 
Work Around
N/A
Evaluation
Can be fixed as soon as serialization no longer relies on this.

  xxxxx@xxxxx   1999-09-01
Comments
  
  Include a link with my name & email   


PLEASE NOTE: JDK6 is formerly known as Project Mustang