From 033509daea7672c110fbe3ad80b445623deb0848 Mon Sep 17 00:00:00 2001
From: Per Bothner <per@bothner.com>
Date: Wed, 23 Jan 2002 11:42:18 -0800
Subject: [PATCH] natField.cc (setAddr): New function.

	* java/lang/reflect/natField.cc (setAddr):  New function.
	Calls getAddr and then checks that the field isn't final.
	(setBoolean. setChar, setByte, setShort, setInt, setLong, setFloat,
	setDouble, set):  Use setAddr instead of getAddr, to check for FINAL.
	(set):  Call setAddr before check that new value has right type,
	to better match specified semantics.

From-SVN: r49153
---
 libjava/ChangeLog                     |  9 +++++++++
 libjava/java/lang/reflect/natField.cc | 28 ++++++++++++++++++---------
 2 files changed, 28 insertions(+), 9 deletions(-)

diff --git a/libjava/ChangeLog b/libjava/ChangeLog
index afaeacbacff1..bc20e9637f3a 100644
--- a/libjava/ChangeLog
+++ b/libjava/ChangeLog
@@ -1,3 +1,12 @@
+2002-01-23  Per Bothner  <per@bothner.com>
+
+	* java/lang/reflect/natField.cc (setAddr):  New function.
+	Calls getAddr and then checks that the field isn't final.
+	(setBoolean. setChar, setByte, setShort, setInt, setLong, setFloat,
+	setDouble, set):  Use setAddr instead of getAddr, to check for FINAL.
+	(set):  Call setAddr before check that new value has right type,
+	to better match specified semantics.
+
 2002-01-22  Tom Tromey  <tromey@redhat.com>
 
 	* java/awt/TextField.java: Replaced with Classpath version.
diff --git a/libjava/java/lang/reflect/natField.cc b/libjava/java/lang/reflect/natField.cc
index 3ca9293a8166..b0ae48d3383c 100644
--- a/libjava/java/lang/reflect/natField.cc
+++ b/libjava/java/lang/reflect/natField.cc
@@ -16,6 +16,7 @@ details.  */
 #include <java/lang/reflect/Field.h>
 #include <java/lang/reflect/Modifier.h>
 #include <java/lang/IllegalArgumentException.h>
+#include <java/lang/IllegalAccessException.h>
 #include <java/lang/NullPointerException.h>
 #include <java/lang/Byte.h>
 #include <java/lang/Short.h>
@@ -252,6 +253,15 @@ java::lang::reflect::Field::get (jclass caller, jobject obj)
   throw new java::lang::IllegalArgumentException;
 }
 
+static void*
+setAddr (java::lang::reflect::Field* field, jclass caller, jobject obj)
+{
+  void *addr = getAddr(field, caller, obj);
+  if  (field->getModifiers() & java::lang::reflect::Modifier::FINAL)
+    throw new java::lang::IllegalAccessException();
+  return addr;
+}
+
 static void
 setBoolean (jclass type, void *addr, jboolean value)
 {
@@ -366,63 +376,63 @@ void
 java::lang::reflect::Field::setBoolean (jclass caller, jobject obj, jboolean b)
 {
   jfieldID fld = _Jv_FromReflectedField (this);
-  ::setBoolean (fld->type, getAddr (this, caller, obj), b);
+  ::setBoolean (fld->type, setAddr (this, caller, obj), b);
 }
 
 void
 java::lang::reflect::Field::setChar (jclass caller, jobject obj, jchar c)
 {
   jfieldID fld = _Jv_FromReflectedField (this);
-  ::setChar (fld->type, getAddr (this, caller, obj), c);
+  ::setChar (fld->type, setAddr (this, caller, obj), c);
 }
 
 void
 java::lang::reflect::Field::setByte (jclass caller, jobject obj, jbyte b)
 {
   jfieldID fld = _Jv_FromReflectedField (this);
-  ::setByte (fld->type, getAddr (this, caller, obj), b);
+  ::setByte (fld->type, setAddr (this, caller, obj), b);
 }
 
 void
 java::lang::reflect::Field::setShort (jclass caller, jobject obj, jshort s)
 {
   jfieldID fld = _Jv_FromReflectedField (this);
-  ::setShort (fld->type, getAddr (this, caller, obj), s);
+  ::setShort (fld->type, setAddr (this, caller, obj), s);
 }
 
 void
 java::lang::reflect::Field::setInt (jclass caller, jobject obj, jint i)
 {
   jfieldID fld = _Jv_FromReflectedField (this);
-  ::setInt (fld->type, getAddr (this, caller, obj), i);
+  ::setInt (fld->type, setAddr (this, caller, obj), i);
 }
 
 void
 java::lang::reflect::Field::setLong (jclass caller, jobject obj, jlong l)
 {
   jfieldID fld = _Jv_FromReflectedField (this);
-  ::setLong (fld->type, getAddr (this, caller, obj), l);
+  ::setLong (fld->type, setAddr (this, caller, obj), l);
 }
 void
 java::lang::reflect::Field::setFloat (jclass caller, jobject obj, jfloat f)
 {
   jfieldID fld = _Jv_FromReflectedField (this);
-  ::setFloat (fld->type, getAddr (this, caller, obj), f);
+  ::setFloat (fld->type, setAddr (this, caller, obj), f);
 }
 
 void
 java::lang::reflect::Field::setDouble (jclass caller, jobject obj, jdouble d)
 {
   jfieldID fld = _Jv_FromReflectedField (this);
-  ::setDouble (fld->type, getAddr (this, caller, obj), d);
+  ::setDouble (fld->type, setAddr (this, caller, obj), d);
 }
 
 void
 java::lang::reflect::Field::set (jclass caller, jobject object, jobject value,
 				 jclass type)
 {
+  void* addr = setAddr (this, caller, object);
   if (value != NULL && ! _Jv_IsInstanceOf (value, type))
     throw new java::lang::IllegalArgumentException;
-  void* addr = getAddr (this, caller, object);
   * (jobject*) addr = value;
 }
-- 
GitLab