1 // natField.cc - Implementation of java.lang.reflect.Field native methods.
3 /* Copyright (C) 1998, 1999, 2000, 2001, 2003, 2004 Free Software Foundation
5 This file is part of libgcj.
7 This software is copyrighted work licensed under the terms of the
8 Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
16 #include <java-stack.h>
17 #include <java/lang/reflect/Field.h>
18 #include <java/lang/reflect/Modifier.h>
19 #include <java/lang/ArrayIndexOutOfBoundsException.h>
20 #include <java/lang/IllegalArgumentException.h>
21 #include <java/lang/IllegalAccessException.h>
22 #include <java/lang/NullPointerException.h>
23 #include <java/lang/Byte.h>
24 #include <java/lang/Short.h>
25 #include <java/lang/Integer.h>
26 #include <java/lang/Long.h>
27 #include <java/lang/Float.h>
28 #include <java/lang/Double.h>
29 #include <java/lang/Boolean.h>
30 #include <java/lang/Character.h>
33 java::lang::reflect::Field::getModifiers ()
35 return _Jv_FromReflectedField (this)->getModifiers ();
39 java::lang::reflect::Field::getName ()
42 name
= _Jv_NewStringUtf8Const (_Jv_FromReflectedField (this)->name
);
47 java::lang::reflect::Field::getType ()
51 jfieldID fld
= _Jv_FromReflectedField (this);
52 JvSynchronize
sync (declaringClass
);
53 _Jv_Linker::resolve_field (fld
, declaringClass
->getClassLoaderInternal ());
60 getAddr (java::lang::reflect::Field
* field
, jclass caller
, jobject obj
,
63 // FIXME: we know CALLER is NULL here. At one point we planned to
64 // have the compiler insert the caller as a hidden argument in some
65 // calls. However, we never implemented that, so we have to find
66 // the caller by hand instead.
68 using namespace java::lang::reflect
;
70 jfieldID fld
= _Jv_FromReflectedField (field
);
71 _Jv_ushort flags
= fld
->getModifiers();
73 // Setting a final field is usually not allowed.
75 && field
->getModifiers() & java::lang::reflect::Modifier::FINAL
)
76 throw new java::lang::IllegalAccessException(JvNewStringUTF
79 // Check accessibility, if required.
80 if (! (Modifier::isPublic (flags
) || field
->isAccessible()))
82 caller
= _Jv_StackTrace::GetCallingClass (&Field::class$
);
83 if (! _Jv_CheckAccess (caller
, field
->getDeclaringClass(), flags
))
84 throw new java::lang::IllegalAccessException
;
87 if (flags
& Modifier::STATIC
)
89 jclass fldClass
= field
->getDeclaringClass ();
90 JvInitClass(fldClass
);
96 throw new java::lang::NullPointerException
;
97 if (! _Jv_IsInstanceOf (obj
, field
->getDeclaringClass()))
98 throw new java::lang::IllegalArgumentException
;
99 return (void*) ((char*) obj
+ fld
->getOffset ());
104 getBoolean (jclass cls
, void* addr
)
106 if (cls
== JvPrimClass (boolean
))
107 return * (jboolean
*) addr
;
108 throw new java::lang::IllegalArgumentException
;
112 getChar (jclass cls
, void* addr
)
114 if (cls
== JvPrimClass (char))
115 return * (jchar
*) addr
;
116 throw new java::lang::IllegalArgumentException
;
120 getByte (jclass cls
, void* addr
)
122 if (cls
== JvPrimClass (byte
))
123 return * (jbyte
*) addr
;
124 throw new java::lang::IllegalArgumentException
;
128 getShort (jclass cls
, void* addr
)
130 if (cls
== JvPrimClass (short))
131 return * (jshort
*) addr
;
132 if (cls
== JvPrimClass (byte
))
133 return * (jbyte
*) addr
;
134 throw new java::lang::IllegalArgumentException
;
138 getInt (jclass cls
, void* addr
)
140 if (cls
== JvPrimClass (int))
141 return * (jint
*) addr
;
142 if (cls
== JvPrimClass (short))
143 return * (jshort
*) addr
;
144 if (cls
== JvPrimClass (char))
145 return * (jchar
*) addr
;
146 if (cls
== JvPrimClass (byte
))
147 return * (jbyte
*) addr
;
148 throw new java::lang::IllegalArgumentException
;
152 getLong (jclass cls
, void* addr
)
154 if (cls
== JvPrimClass (long))
155 return * (jlong
*) addr
;
156 return ::getInt(cls
, addr
);
160 getFloat (jclass cls
, void* addr
)
162 if (cls
== JvPrimClass (float))
163 return * (jfloat
*) addr
;
164 if (cls
== JvPrimClass (long))
165 return * (jlong
*) addr
;
166 return ::getInt(cls
, addr
);
170 getDouble (jclass cls
, void* addr
)
172 if (cls
== JvPrimClass (double))
173 return * (jdouble
*) addr
;
174 if (cls
== JvPrimClass (float))
175 return * (jfloat
*) addr
;
176 if (cls
== JvPrimClass (long))
177 return * (jlong
*) addr
;
178 return ::getInt(cls
, addr
);
182 java::lang::reflect::Field::getBoolean (jclass caller
, jobject obj
)
184 return ::getBoolean (this->getType(), getAddr (this, caller
, obj
, false));
188 java::lang::reflect::Field::getChar (jclass caller
, jobject obj
)
190 return ::getChar (this->getType(), getAddr (this, caller
, obj
, false));
194 java::lang::reflect::Field::getByte (jclass caller
, jobject obj
)
196 return ::getByte (this->getType(), getAddr (this, caller
, obj
, false));
200 java::lang::reflect::Field::getShort (jclass caller
, jobject obj
)
202 return ::getShort (this->getType(), getAddr (this, caller
, obj
, false));
206 java::lang::reflect::Field::getInt (jclass caller
, jobject obj
)
208 return ::getInt (this->getType(), getAddr (this, caller
, obj
, false));
212 java::lang::reflect::Field::getLong (jclass caller
, jobject obj
)
214 return ::getLong (this->getType(), getAddr (this, caller
, obj
, false));
218 java::lang::reflect::Field::getFloat (jclass caller
, jobject obj
)
220 return ::getFloat (this->getType(), getAddr (this, caller
, obj
, false));
224 java::lang::reflect::Field::getDouble (jclass caller
, jobject obj
)
226 return ::getDouble (this->getType(), getAddr (this, caller
, obj
, false));
230 java::lang::reflect::Field::get (jclass caller
, jobject obj
)
232 jclass type
= this->getType();
233 void* addr
= getAddr (this, caller
, obj
, false);
234 if (! type
->isPrimitive ())
235 return * (jobject
*) addr
;
236 if (type
== JvPrimClass (double))
237 return new java::lang::Double (* (jdouble
*) addr
);
238 if (type
== JvPrimClass (float))
239 return new java::lang::Float (* (jfloat
*) addr
);
240 if (type
== JvPrimClass (long))
241 return new java::lang::Long (* (jlong
*) addr
);
242 if (type
== JvPrimClass (int))
243 return new java::lang::Integer (* (jint
*) addr
);
244 if (type
== JvPrimClass (short))
245 return new java::lang::Short (* (jshort
*) addr
);
246 if (type
== JvPrimClass (byte
))
247 return new java::lang::Byte (* (jbyte
*) addr
);
248 if (type
== JvPrimClass (char))
249 return new java::lang::Character (* (jchar
*) addr
);
250 if (type
== JvPrimClass (boolean
))
252 _Jv_InitClass (&java::lang::Boolean::class$
);
253 if (* (jboolean
*) addr
)
254 return java::lang::Boolean::TRUE
;
256 return java::lang::Boolean::FALSE
;
258 throw new java::lang::IllegalArgumentException
;
262 setBoolean (jclass type
, void *addr
, jboolean value
)
264 if (type
== JvPrimClass (boolean
))
265 * (jboolean
*) addr
= value
;
267 throw new java::lang::IllegalArgumentException
;
271 setChar (jclass type
, void *addr
, jchar value
)
273 if (type
== JvPrimClass (char))
274 * (jchar
*) addr
= value
;
275 else if (type
== JvPrimClass (int))
276 * (jint
*) addr
= value
;
277 else if (type
== JvPrimClass (long))
278 * (jlong
*) addr
= value
;
279 else if (type
== JvPrimClass (float))
280 * (jfloat
*) addr
= value
;
281 else if (type
== JvPrimClass (double))
282 * (jdouble
*) addr
= value
;
284 throw new java::lang::IllegalArgumentException
;
288 setByte (jclass type
, void *addr
, jbyte value
)
290 if (type
== JvPrimClass (byte
))
291 * (jbyte
*) addr
= value
;
292 else if (type
== JvPrimClass (short))
293 * (jshort
*) addr
= value
;
294 else if (type
== JvPrimClass (int))
295 * (jint
*) addr
= value
;
296 else if (type
== JvPrimClass (long))
297 * (jlong
*) addr
= value
;
298 else if (type
== JvPrimClass (float))
299 * (jfloat
*) addr
= value
;
300 else if (type
== JvPrimClass (double))
301 * (jdouble
*) addr
= value
;
303 throw new java::lang::IllegalArgumentException
;
307 setShort (jclass type
, void *addr
, jshort value
)
309 if (type
== JvPrimClass (short))
310 * (jshort
*) addr
= value
;
311 else if (type
== JvPrimClass (int))
312 * (jint
*) addr
= value
;
313 else if (type
== JvPrimClass (long))
314 * (jlong
*) addr
= value
;
315 else if (type
== JvPrimClass (float))
316 * (jfloat
*) addr
= value
;
317 else if (type
== JvPrimClass (double))
318 * (jdouble
*) addr
= value
;
320 throw new java::lang::IllegalArgumentException
;
324 setInt (jclass type
, void *addr
, jint value
)
326 if (type
== JvPrimClass (int))
327 * (jint
*) addr
= value
;
328 else if (type
== JvPrimClass (long))
329 * (jlong
*) addr
= value
;
330 else if (type
== JvPrimClass (float))
331 * (jfloat
*) addr
= value
;
332 else if (type
== JvPrimClass (double))
333 * (jdouble
*) addr
= value
;
335 throw new java::lang::IllegalArgumentException
;
339 setLong (jclass type
, void *addr
, jlong value
)
341 if (type
== JvPrimClass (long))
342 * (jlong
*) addr
= value
;
343 else if (type
== JvPrimClass (float))
344 * (jfloat
*) addr
= value
;
345 else if (type
== JvPrimClass (double))
346 * (jdouble
*) addr
= value
;
348 throw new java::lang::IllegalArgumentException
;
352 setFloat (jclass type
, void *addr
, jfloat value
)
354 if (type
== JvPrimClass (float))
355 * (jfloat
*) addr
= value
;
356 else if (type
== JvPrimClass (double))
357 * (jdouble
*) addr
= value
;
359 throw new java::lang::IllegalArgumentException
;
363 setDouble (jclass type
, void *addr
, jdouble value
)
365 if (type
== JvPrimClass (double))
366 * (jdouble
*) addr
= value
;
368 throw new java::lang::IllegalArgumentException
;
372 java::lang::reflect::Field::setBoolean (jclass caller
, jobject obj
, jboolean b
,
375 ::setBoolean (this->getType(), getAddr (this, caller
, obj
, checkFinal
), b
);
379 java::lang::reflect::Field::setChar (jclass caller
, jobject obj
, jchar c
,
382 ::setChar (this->getType(), getAddr (this, caller
, obj
, checkFinal
), c
);
386 java::lang::reflect::Field::setByte (jclass caller
, jobject obj
, jbyte b
,
389 ::setByte (this->getType(), getAddr (this, caller
, obj
, checkFinal
), b
);
393 java::lang::reflect::Field::setShort (jclass caller
, jobject obj
, jshort s
,
396 ::setShort (this->getType(), getAddr (this, caller
, obj
, checkFinal
), s
);
400 java::lang::reflect::Field::setInt (jclass caller
, jobject obj
, jint i
,
403 ::setInt (this->getType(), getAddr (this, caller
, obj
, checkFinal
), i
);
407 java::lang::reflect::Field::setLong (jclass caller
, jobject obj
, jlong l
,
410 ::setLong (this->getType(), getAddr (this, caller
, obj
, checkFinal
), l
);
414 java::lang::reflect::Field::setFloat (jclass caller
, jobject obj
, jfloat f
,
417 ::setFloat (this->getType(), getAddr (this, caller
, obj
, checkFinal
), f
);
421 java::lang::reflect::Field::setDouble (jclass caller
, jobject obj
, jdouble d
,
424 ::setDouble (this->getType(), getAddr (this, caller
, obj
, checkFinal
), d
);
428 java::lang::reflect::Field::set (jclass caller
, jobject object
, jobject value
,
429 jclass type
, jboolean checkFinal
)
431 void* addr
= getAddr (this, caller
, object
, checkFinal
);
432 if (value
!= NULL
&& ! _Jv_IsInstanceOf (value
, type
))
433 throw new java::lang::IllegalArgumentException
;
434 * (jobject
*) addr
= value
;