JDK-5059911 : (reflect) Final fields can be modified via Field.set - expect IllegalAccessExcep
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.lang:reflect
  • Affected Version: 5.0
  • Priority: P3
  • Status: Closed
  • Resolution: Not an Issue
  • OS: linux,windows_xp
  • CPU: x86
  • Submitted: 2004-06-08
  • Updated: 2012-09-28
  • Resolved: 2004-06-09
Related Reports
Duplicate :  
Description

Name: gm110360			Date: 06/08/2004


FULL PRODUCT VERSION :
Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0-beta2-b51)
Java HotSpot(TM) Client VM (build 1.5.0-beta2-b51, mixed mode, sharing)

ADDITIONAL OS VERSION INFORMATION :
Microsoft Windows XP [Version 5.1.2600]

A DESCRIPTION OF THE PROBLEM :
A private final int variable of a class can be modified via reflection

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
just execute the source below

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
an IllegalAccessException should be thrown
ACTUAL -
the value can be set without exception

ERROR MESSAGES/STACK TRACES THAT OCCUR :
---none---

REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------
import java.lang.reflect.*;

public class Test
{
	public static void main(String[] tooralooraloo) throws Throwable
	{
		Change change = new Change(42);
		System.out.println("before: "+change);
		
		Field f = change.getClass().getDeclaredField("number");
		f.setAccessible(true);
		
		f.set(change, new Integer(666) );
		System.out.println("after: "+change);
	}
}

class Change
{
	private final int number;
	
	Change(int x)
	{
		number = x;
	}
	
	public String toString()
	{
		return number + "";
	}
}
---------- END SOURCE ----------

CUSTOMER SUBMITTED WORKAROUND :
---none---

Release Regression From : 1.4.2
The above release value was the last known release where this 
bug was known to work. Since then there has been a regression.

(Incident Review ID: 276697) 
======================================================================

Comments
EVALUATION This is the expected behaviour. After much deliberation, bug 5044412 modified the specification of Field.set to allow modification of the field if it is non-static and setAccessible(true) has succeeded. The specification of Field.set contains more details. In making this change, we reverted to the behaviour which existed prior to jdk1.3. -- iag@sfbay 2004-06-09
09-06-2004