* Mainline merge as of 2006-02-16 (@111136).
[official-gcc.git] / libjava / java / lang / reflect / natField.cc
blob33a5717f4b3805b05775f9ec30c1f83536cf51d1
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
9 details. */
11 #include <config.h>
13 #include <stdlib.h>
15 #include <jvm.h>
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>
32 jint
33 java::lang::reflect::Field::getModifiers ()
35 return _Jv_FromReflectedField (this)->getModifiers ();
38 jstring
39 java::lang::reflect::Field::getName ()
41 if (name == NULL)
42 name = _Jv_NewStringUtf8Const (_Jv_FromReflectedField (this)->name);
43 return name;
46 jclass
47 java::lang::reflect::Field::getType ()
49 if (type == NULL)
51 jfieldID fld = _Jv_FromReflectedField (this);
52 JvSynchronize sync (declaringClass);
53 _Jv_Linker::resolve_field (fld, declaringClass->getClassLoaderInternal ());
54 type = fld->type;
56 return type;
59 static void*
60 getAddr (java::lang::reflect::Field* field, jclass caller, jobject obj,
61 jboolean checkFinal)
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.
74 if (checkFinal
75 && field->getModifiers() & java::lang::reflect::Modifier::FINAL)
76 throw new java::lang::IllegalAccessException(JvNewStringUTF
77 ("Field is final"));
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);
91 return fld->u.addr;
93 else
95 if (obj == NULL)
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 ());
103 static jboolean
104 getBoolean (jclass cls, void* addr)
106 if (cls == JvPrimClass (boolean))
107 return * (jboolean *) addr;
108 throw new java::lang::IllegalArgumentException;
111 static jchar
112 getChar (jclass cls, void* addr)
114 if (cls == JvPrimClass (char))
115 return * (jchar *) addr;
116 throw new java::lang::IllegalArgumentException;
119 static jbyte
120 getByte (jclass cls, void* addr)
122 if (cls == JvPrimClass (byte))
123 return * (jbyte *) addr;
124 throw new java::lang::IllegalArgumentException;
127 static jshort
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;
137 static jint
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;
151 static jlong
152 getLong (jclass cls, void* addr)
154 if (cls == JvPrimClass (long))
155 return * (jlong *) addr;
156 return ::getInt(cls, addr);
159 static jfloat
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);
169 static jdouble
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);
181 jboolean
182 java::lang::reflect::Field::getBoolean (jclass caller, jobject obj)
184 return ::getBoolean (this->getType(), getAddr (this, caller, obj, false));
187 jchar
188 java::lang::reflect::Field::getChar (jclass caller, jobject obj)
190 return ::getChar (this->getType(), getAddr (this, caller, obj, false));
193 jbyte
194 java::lang::reflect::Field::getByte (jclass caller, jobject obj)
196 return ::getByte (this->getType(), getAddr (this, caller, obj, false));
199 jshort
200 java::lang::reflect::Field::getShort (jclass caller, jobject obj)
202 return ::getShort (this->getType(), getAddr (this, caller, obj, false));
205 jint
206 java::lang::reflect::Field::getInt (jclass caller, jobject obj)
208 return ::getInt (this->getType(), getAddr (this, caller, obj, false));
211 jlong
212 java::lang::reflect::Field::getLong (jclass caller, jobject obj)
214 return ::getLong (this->getType(), getAddr (this, caller, obj, false));
217 jfloat
218 java::lang::reflect::Field::getFloat (jclass caller, jobject obj)
220 return ::getFloat (this->getType(), getAddr (this, caller, obj, false));
223 jdouble
224 java::lang::reflect::Field::getDouble (jclass caller, jobject obj)
226 return ::getDouble (this->getType(), getAddr (this, caller, obj, false));
229 jobject
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;
255 else
256 return java::lang::Boolean::FALSE;
258 throw new java::lang::IllegalArgumentException;
261 static void
262 setBoolean (jclass type, void *addr, jboolean value)
264 if (type == JvPrimClass (boolean))
265 * (jboolean *) addr = value;
266 else
267 throw new java::lang::IllegalArgumentException;
270 static void
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;
283 else
284 throw new java::lang::IllegalArgumentException;
287 static void
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;
302 else
303 throw new java::lang::IllegalArgumentException;
306 static void
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;
319 else
320 throw new java::lang::IllegalArgumentException;
323 static void
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;
334 else
335 throw new java::lang::IllegalArgumentException;
338 static void
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;
347 else
348 throw new java::lang::IllegalArgumentException;
351 static void
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;
358 else
359 throw new java::lang::IllegalArgumentException;
362 static void
363 setDouble (jclass type, void *addr, jdouble value)
365 if (type == JvPrimClass (double))
366 * (jdouble *) addr = value;
367 else
368 throw new java::lang::IllegalArgumentException;
371 void
372 java::lang::reflect::Field::setBoolean (jclass caller, jobject obj, jboolean b,
373 jboolean checkFinal)
375 ::setBoolean (this->getType(), getAddr (this, caller, obj, checkFinal), b);
378 void
379 java::lang::reflect::Field::setChar (jclass caller, jobject obj, jchar c,
380 jboolean checkFinal)
382 ::setChar (this->getType(), getAddr (this, caller, obj, checkFinal), c);
385 void
386 java::lang::reflect::Field::setByte (jclass caller, jobject obj, jbyte b,
387 jboolean checkFinal)
389 ::setByte (this->getType(), getAddr (this, caller, obj, checkFinal), b);
392 void
393 java::lang::reflect::Field::setShort (jclass caller, jobject obj, jshort s,
394 jboolean checkFinal)
396 ::setShort (this->getType(), getAddr (this, caller, obj, checkFinal), s);
399 void
400 java::lang::reflect::Field::setInt (jclass caller, jobject obj, jint i,
401 jboolean checkFinal)
403 ::setInt (this->getType(), getAddr (this, caller, obj, checkFinal), i);
406 void
407 java::lang::reflect::Field::setLong (jclass caller, jobject obj, jlong l,
408 jboolean checkFinal)
410 ::setLong (this->getType(), getAddr (this, caller, obj, checkFinal), l);
413 void
414 java::lang::reflect::Field::setFloat (jclass caller, jobject obj, jfloat f,
415 jboolean checkFinal)
417 ::setFloat (this->getType(), getAddr (this, caller, obj, checkFinal), f);
420 void
421 java::lang::reflect::Field::setDouble (jclass caller, jobject obj, jdouble d,
422 jboolean checkFinal)
424 ::setDouble (this->getType(), getAddr (this, caller, obj, checkFinal), d);
427 void
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;