java.net implemented.
[cacao.git] / src / native / vm / Field.c
1 /* class: java/lang/reflect/Field */
2
3
4 /*
5  * Class:     java/lang/reflect/Field
6  * Method:    get
7  * Signature: (Ljava/lang/Object;)Ljava/lang/Object;
8  */
9 JNIEXPORT struct java_lang_Object* JNICALL Java_java_lang_reflect_Field_get ( JNIEnv *env ,  struct java_lang_reflect_Field* this, struct java_lang_Object* obj)
10 {
11   jfieldID target_fid;   /* the JNI-fieldid of the wrapping object */
12   jfieldID fid;          /* the JNI-fieldid of the field containing the value */
13   jobject o;             /* the object for wrapping the primitive type */
14   classinfo *c = (classinfo *) this->clazz;
15   int st = (this->modifiers & ACC_STATIC); /* true if field is static */
16
17   /* get the fieldid of the field represented by this Field-object */
18   fid = class_findfield_approx((classinfo *) this->clazz,javastring_toutf(this->name, false));
19
20   /* The fieldid is used to retrieve the value, for primitive types a new 
21      object for wrapping the primitive type is created.  */
22   if (st || obj)
23     switch (this->slot) {
24       case 'I' : {
25                    /* create wrapping class */
26                    c = class_java_lang_Integer;
27                    o = builtin_new(c);
28                    /* get fieldid to store the value */
29                    target_fid = env->GetFieldID(env,c,"value","I");
30                    if (!target_fid) break;
31                                    
32                    if (st)
33                         /* static field */
34                         SetIntField(env,o,target_fid, env->GetStaticIntField(env, c, fid));
35                    else
36                         /* instance field */
37                         SetIntField(env,o,target_fid, env->GetIntField(env,(jobject) obj, fid));
38
39                    /* return the wrapped object */                 
40                    return (java_lang_Object*) o;
41                  }
42       case 'J' : {
43                    c = class_java_lang_Long;
44                    o = builtin_new(c);
45                    target_fid = env->GetFieldID(env,c,"value","J");
46                    if (!target_fid) break;
47                                    
48                    if (st)
49                         SetLongField(env,o,target_fid, env->GetStaticLongField(env, c, fid));
50                    else
51                         SetLongField(env,o,target_fid, env->GetLongField(env,(jobject)  obj, fid));
52
53                    return (java_lang_Object*) o;
54                  }
55       case 'F' : {
56                    c = class_java_lang_Float;
57                    o = builtin_new(c);
58                    target_fid = env->GetFieldID(env,c,"value","F");
59                    if (!target_fid) break;
60                                    
61                    if (st)
62                         SetFloatField(env,o,target_fid, env->GetStaticFloatField(env, c, fid));
63                    else
64                         SetFloatField(env,o,target_fid, env->GetFloatField(env, (jobject) obj, fid));
65
66                    return (java_lang_Object*) o;
67                  }
68       case 'D' : {
69                    c = class_java_lang_Double;
70                    o = builtin_new(c);
71                    target_fid = env->GetFieldID(env,c,"value","D");
72                    if (!target_fid) break;
73                                    
74                    if (st)
75                         SetDoubleField(env,o,target_fid, env->GetStaticDoubleField(env, c, fid));
76                    else
77                         SetDoubleField(env,o,target_fid, env->GetDoubleField(env, (jobject) obj, fid));
78
79                    return (java_lang_Object*) o;
80                  }
81       case 'B' : {
82                    c = class_java_lang_Byte;
83                    o = builtin_new(c);
84                    target_fid = env->GetFieldID(env,c,"value","B");
85                    if (!target_fid) break;
86                                    
87                    if (st)
88                         SetByteField(env,o,target_fid, env->GetStaticByteField(env, c, fid));
89                    else
90                         SetByteField(env,o,target_fid, env->GetByteField(env, (jobject) obj, fid));
91
92                    return (java_lang_Object*) o;
93                  }
94       case 'C' : {
95                    c = class_java_lang_Character;
96                    o = builtin_new(c);
97                    target_fid = env->GetFieldID(env,c,"value","C");
98                    if (!target_fid) break;
99                                    
100                    if (st)
101                         SetCharField(env,o,target_fid, env->GetStaticCharField(env, c, fid));
102                    else
103                         SetCharField(env,o,target_fid, env->GetCharField(env, (jobject) obj, fid));
104
105                    return (java_lang_Object*) o;
106                  }
107       case 'S' : {
108                    c = class_java_lang_Short;
109                    o = builtin_new(c);
110                    target_fid = env->GetFieldID(env,c,"value","S");
111                    if (!target_fid) break;
112                                    
113                    if (st)
114                         SetShortField(env,o,target_fid, env->GetStaticShortField(env, c, fid));
115                    else
116                         SetShortField(env,o,target_fid, env->GetShortField(env, (jobject) obj, fid));
117
118                    return (java_lang_Object*) o;
119                  }
120       case 'Z' : {
121                    c = class_java_lang_Boolean;
122                    o = builtin_new(c);
123                    target_fid = env->GetFieldID(env,c,"value","Z");
124                    if (!target_fid) break;
125                                    
126                    if (st)
127                         SetBooleanField(env,o,target_fid, env->GetStaticBooleanField(env, c, fid));
128                    else
129                         SetBooleanField(env,o,target_fid, env->GetBooleanField(env, (jobject) obj, fid));
130
131                    return (java_lang_Object*) o;
132                  }
133       case '[' : 
134       case 'L' : {
135                    if (st)
136                         /* static field */
137                         return (java_lang_Object*) env->GetStaticObjectField(env, c, fid);
138                    else
139                         /* instance field */
140                         return (java_lang_Object*) env->GetObjectField(env, (jobject) obj, fid);
141
142                  }
143   }
144
145   exceptionptr = native_new_and_init(class_java_lang_IllegalArgumentException);
146 }
147
148 /*
149  * Class:     java/lang/reflect/Field
150  * Method:    getBoolean
151  * Signature: (Ljava/lang/Object;)Z
152  */
153 JNIEXPORT s4 JNICALL Java_java_lang_reflect_Field_getBoolean ( JNIEnv *env ,  struct java_lang_reflect_Field* this, struct java_lang_Object* obj)
154 {
155   jfieldID fid;
156
157   if (this->clazz && obj) {
158
159       /* get the fieldid represented by the field-object */
160       fid = class_findfield_approx ((classinfo *) this->clazz,javastring_toutf(this->name, false));
161
162       if (fid) 
163           /* call the JNI-function to retrieve the field */ 
164           return env->GetBooleanField (env, (jobject) obj, fid);
165   }
166
167   /* raise exception */
168   exceptionptr = native_new_and_init(class_java_lang_IllegalArgumentException);
169 }
170
171 /*
172  * Class:     java/lang/reflect/Field
173  * Method:    getByte
174  * Signature: (Ljava/lang/Object;)B
175  */
176 JNIEXPORT s4 JNICALL Java_java_lang_reflect_Field_getByte ( JNIEnv *env ,  struct java_lang_reflect_Field* this, struct java_lang_Object* obj)
177 {
178   jfieldID fid;
179
180   if (this->clazz && obj) {
181
182       fid = class_findfield_approx ((classinfo *) this->clazz,javastring_toutf(this->name, false));
183
184       if (fid) 
185           return env->GetByteField (env, (jobject) obj, fid);
186   }
187
188   exceptionptr = native_new_and_init(class_java_lang_IllegalArgumentException);
189 }
190
191 /*
192  * Class:     java/lang/reflect/Field
193  * Method:    getChar
194  * Signature: (Ljava/lang/Object;)C
195  */
196 JNIEXPORT s4 JNICALL Java_java_lang_reflect_Field_getChar ( JNIEnv *env ,  struct java_lang_reflect_Field* this, struct java_lang_Object* obj)
197 {
198   jfieldID fid;
199
200   if (this->clazz && obj) {
201
202       fid = class_findfield_approx ((classinfo *) this->clazz,javastring_toutf(this->name, false));
203
204       if (fid) 
205           return env->GetCharField (env, (jobject) obj, fid);
206   }
207
208   exceptionptr = native_new_and_init(class_java_lang_IllegalArgumentException);
209 }
210
211 /*
212  * Class:     java/lang/reflect/Field
213  * Method:    getDouble
214  * Signature: (Ljava/lang/Object;)D
215  */
216 JNIEXPORT double JNICALL Java_java_lang_reflect_Field_getDouble ( JNIEnv *env ,  struct java_lang_reflect_Field* this, struct java_lang_Object* obj)
217 {
218   jfieldID fid;
219
220   if (this->clazz && obj) {
221
222       fid = class_findfield_approx ((classinfo *) this->clazz,javastring_toutf(this->name, false));
223
224       if (fid) 
225           return env->GetDoubleField (env, (jobject) obj, fid);
226   }
227
228   exceptionptr = native_new_and_init(class_java_lang_IllegalArgumentException);
229 }
230
231 /*
232  * Class:     java/lang/reflect/Field
233  * Method:    getFloat
234  * Signature: (Ljava/lang/Object;)F
235  */
236 JNIEXPORT float JNICALL Java_java_lang_reflect_Field_getFloat ( JNIEnv *env ,  struct java_lang_reflect_Field* this, struct java_lang_Object* obj)
237 {
238   jfieldID fid;
239
240   if (this->clazz && obj) {
241
242       fid = class_findfield_approx ((classinfo *) this->clazz,javastring_toutf(this->name, false));
243
244       if (fid) 
245           return env->GetFloatField (env, (jobject) obj, fid);
246   }
247
248   exceptionptr = native_new_and_init(class_java_lang_IllegalArgumentException);
249 }
250
251 /*
252  * Class:     java/lang/reflect/Field
253  * Method:    getInt
254  * Signature: (Ljava/lang/Object;)I
255  */
256 JNIEXPORT s4 JNICALL Java_java_lang_reflect_Field_getInt ( JNIEnv *env ,  struct java_lang_reflect_Field* this, struct java_lang_Object* obj)
257 {
258   jfieldID fid;
259
260   if (this->clazz && obj) {
261
262       fid = class_findfield_approx ((classinfo *) this->clazz,javastring_toutf(this->name, false));
263
264       if (fid) 
265           return env->GetIntField (env, (jobject) obj, fid);
266   }
267
268   exceptionptr = native_new_and_init(class_java_lang_IllegalArgumentException);
269 }
270
271 /*
272  * Class:     java/lang/reflect/Field
273  * Method:    getLong
274  * Signature: (Ljava/lang/Object;)J
275  */
276 JNIEXPORT s8 JNICALL Java_java_lang_reflect_Field_getLong ( JNIEnv *env ,  struct java_lang_reflect_Field* this, struct java_lang_Object* obj)
277 {
278   jfieldID fid;
279
280   if (this->clazz && obj) {
281
282       fid = class_findfield_approx ((classinfo *) this->clazz,javastring_toutf(this->name, false));
283
284       if (fid) 
285           return env->GetLongField (env, (jobject) obj, fid);
286   }
287 }
288
289 /*
290  * Class:     java/lang/reflect/Field
291  * Method:    getShort
292  * Signature: (Ljava/lang/Object;)S
293  */
294 JNIEXPORT s4 JNICALL Java_java_lang_reflect_Field_getShort ( JNIEnv *env ,  struct java_lang_reflect_Field* this, struct java_lang_Object* obj)
295 {
296   jfieldID fid;
297
298   if (this->clazz && obj) {
299
300       fid = class_findfield_approx ((classinfo *) this->clazz,javastring_toutf(this->name, false));
301
302       if (fid) 
303           return env->GetShortField (env, (jobject) obj, fid);
304   }
305
306   exceptionptr = native_new_and_init(class_java_lang_IllegalArgumentException);
307 }
308
309 /*
310  * Class:     java/lang/reflect/Field
311  * Method:    set
312  * Signature: (Ljava/lang/Object;Ljava/lang/Object;)V
313  */
314 JNIEXPORT void JNICALL Java_java_lang_reflect_Field_set ( JNIEnv *env ,  struct java_lang_reflect_Field* this, struct java_lang_Object* obj, struct java_lang_Object* val)
315 {
316   jfieldID source_fid;  /* the field containing the value to be written */
317   jfieldID fid;         /* the field to be written */
318   classinfo *c;
319   int st = (this->modifiers & ACC_STATIC); /* true if the field is static */
320
321   fid = class_findfield_approx((classinfo *) this->clazz,javastring_toutf(this->name, false));
322
323   if (val && (st || obj)) {
324
325     c = val->header.vftbl -> class;
326
327     /* The fieldid is used to set the new value, for primitive types the value
328        has to be retrieved from the wrapping object */  
329     switch (this->slot) {
330       case 'I' : {
331                    /* illegal argument specified */
332                    if (c != class_java_lang_Integer) break;
333                    /* determine the field to read the value */
334                    source_fid = env->GetFieldID(env,c,"value","I");
335                    if (!source_fid) break;
336                                    
337                    /* set the new value */
338                    if (st)
339                         /* static field */
340                         env->SetStaticIntField(env, c, fid, GetIntField(env,(jobject) val,source_fid));
341                    else
342                         /* instance field */
343                         env->SetIntField(env, (jobject) obj, fid, GetIntField(env,(jobject) val,source_fid));
344
345                    return;
346                  }
347       case 'J' : {
348                    if (c != class_java_lang_Long) break;
349                    source_fid = env->GetFieldID(env,c,"value","J");
350                    if (!source_fid) break;
351                                    
352                    if (st)
353                         env->SetStaticLongField(env, c, fid, GetLongField(env,(jobject) val,source_fid));
354                    else
355                         env->SetLongField(env, (jobject) obj, fid, GetLongField(env,(jobject) val,source_fid));
356
357                    return;
358                  }
359       case 'F' : {
360                    if (c != class_java_lang_Float) break;
361                    source_fid = env->GetFieldID(env,c,"value","F");
362                    if (!source_fid) break;
363                                    
364                    if (st)
365                         env->SetStaticFloatField(env, c, fid, GetFloatField(env,(jobject) val,source_fid));
366                    else
367                         env->SetFloatField(env, (jobject) obj, fid, GetFloatField(env,(jobject) val,source_fid));
368
369                    return;
370                  }
371       case 'D' : {
372                    if (c != class_java_lang_Double) break;
373                    source_fid = env->GetFieldID(env,c,"value","D");
374                    if (!source_fid) break;
375                                    
376                    if (st)
377                         env->SetStaticDoubleField(env, c, fid, GetDoubleField(env,(jobject) val,source_fid));
378                    else
379                         env->SetDoubleField(env, (jobject) obj, fid, GetDoubleField(env,(jobject) val,source_fid));
380
381                    return;
382                  }
383       case 'B' : {
384                    if (c != class_java_lang_Byte) break;
385                    source_fid = env->GetFieldID(env,c,"value","B");
386                    if (!source_fid) break;
387                                    
388                    if (st)
389                         env->SetStaticByteField(env, c, fid, GetByteField(env,(jobject) val,source_fid));
390                    else
391                         env->SetByteField(env, (jobject) obj, fid, GetByteField(env,(jobject) val,source_fid));
392
393                    return;
394                  }
395       case 'C' : {
396                    if (c != class_java_lang_Character) break;
397                    source_fid = env->GetFieldID(env,c,"value","C");
398                    if (!source_fid) break;
399                                    
400                    if (st)
401                         env->SetStaticCharField(env, c, fid, GetCharField(env,(jobject) val,source_fid));
402                    else
403                         env->SetCharField(env, (jobject) obj, fid, GetCharField(env,(jobject) val,source_fid));
404
405                    return;
406                  }
407       case 'S' : {
408                    if (c != class_java_lang_Short) break;
409                    source_fid = env->GetFieldID(env,c,"value","S");
410                    if (!source_fid) break;
411                                    
412                    if (st)
413                         env->SetStaticShortField(env, c, fid, GetShortField(env,(jobject) val,source_fid));
414                    else
415                         env->SetShortField(env, (jobject) obj, fid, GetShortField(env,(jobject) val,source_fid));
416
417                    return;
418                  }
419       case 'Z' : {
420                    if (c != class_java_lang_Boolean) break;
421                    source_fid = env->GetFieldID(env,c,"value","Z");
422                    if (!source_fid) break;
423                                    
424                    if (st)
425                         env->SetStaticBooleanField(env, c, fid, GetBooleanField(env,(jobject) val,source_fid));
426                    else
427                         env->SetBooleanField(env, (jobject) obj, fid, GetBooleanField(env,(jobject) val,source_fid));
428
429                    return;
430                  }
431       case '[' :
432       case 'L' : {
433                    if (st)
434                         env->SetStaticObjectField(env, c, fid, (jobject) val);
435                    else
436                         env->SetObjectField(env, (jobject) obj, fid, (jobject) val);
437
438                    return;
439                  }
440     }
441   }
442
443   exceptionptr = native_new_and_init(class_java_lang_IllegalArgumentException);
444 }
445
446 /*
447  * Class:     java/lang/reflect/Field
448  * Method:    setBoolean
449  * Signature: (Ljava/lang/Object;Z)V
450  */
451 JNIEXPORT void JNICALL Java_java_lang_reflect_Field_setBoolean ( JNIEnv *env ,  struct java_lang_reflect_Field* this, struct java_lang_Object* obj, s4 val)
452 {
453   jfieldID fid;
454
455   if (this->clazz && obj) {
456
457       fid = class_findfield_approx ((classinfo *) this->clazz,javastring_toutf(this->name, false));
458
459       if (fid) { 
460           env->SetBooleanField (env, (jobject) obj, fid, val);
461           return;
462       }
463   }
464
465   exceptionptr = native_new_and_init(class_java_lang_IllegalArgumentException);
466 }
467
468 /*
469  * Class:     java/lang/reflect/Field
470  * Method:    setByte
471  * Signature: (Ljava/lang/Object;B)V
472  */
473 JNIEXPORT void JNICALL Java_java_lang_reflect_Field_setByte ( JNIEnv *env ,  struct java_lang_reflect_Field* this, struct java_lang_Object* obj, s4 val)
474 {
475   jfieldID fid;
476
477   if (this->clazz && obj) {
478
479       fid = class_findfield_approx ((classinfo *) this->clazz,javastring_toutf(this->name, false));
480
481       if (fid) {
482           env->SetByteField (env, (jobject) obj, fid, val);
483           return;
484       }
485   }
486
487   exceptionptr = native_new_and_init(class_java_lang_IllegalArgumentException);
488 }
489
490 /*
491  * Class:     java/lang/reflect/Field
492  * Method:    setChar
493  * Signature: (Ljava/lang/Object;C)V
494  */
495 JNIEXPORT void JNICALL Java_java_lang_reflect_Field_setChar ( JNIEnv *env ,  struct java_lang_reflect_Field* this, struct java_lang_Object* obj, s4 val)
496 {
497   jfieldID fid;
498
499   if (this->clazz && obj) {
500
501       fid = class_findfield_approx ((classinfo *) this->clazz,javastring_toutf(this->name, false));
502
503       if (fid) {
504           env->SetCharField (env, (jobject) obj, fid, val);
505           return;
506       }
507   }
508
509   exceptionptr = native_new_and_init(class_java_lang_IllegalArgumentException);
510 }
511
512 /*
513  * Class:     java/lang/reflect/Field
514  * Method:    setDouble
515  * Signature: (Ljava/lang/Object;D)V
516  */
517 JNIEXPORT void JNICALL Java_java_lang_reflect_Field_setDouble ( JNIEnv *env ,  struct java_lang_reflect_Field* this, struct java_lang_Object* obj, double val)
518 {
519   jfieldID fid;
520
521   if (this->clazz && obj) {
522
523       fid = class_findfield_approx ((classinfo *) this->clazz,javastring_toutf(this->name, false));
524
525       if (fid) {
526           env->SetDoubleField (env, (jobject) obj, fid, val);
527           return;
528       } 
529   }
530
531   exceptionptr = native_new_and_init(class_java_lang_IllegalArgumentException);
532 }
533
534 /*
535  * Class:     java/lang/reflect/Field
536  * Method:    setFloat
537  * Signature: (Ljava/lang/Object;F)V
538  */
539 JNIEXPORT void JNICALL Java_java_lang_reflect_Field_setFloat ( JNIEnv *env ,  struct java_lang_reflect_Field* this, struct java_lang_Object* obj, float val)
540 {
541   jfieldID fid;
542
543   if (this->clazz && obj) {
544
545       fid = class_findfield_approx ((classinfo *) this->clazz,javastring_toutf(this->name, false));
546
547       if (fid) {
548           env->SetFloatField (env, (jobject) obj, fid, val);
549           return;
550       }
551   }
552
553   exceptionptr = native_new_and_init(class_java_lang_IllegalArgumentException);
554 }
555
556 /*
557  * Class:     java/lang/reflect/Field
558  * Method:    setInt
559  * Signature: (Ljava/lang/Object;I)V
560  */
561 JNIEXPORT void JNICALL Java_java_lang_reflect_Field_setInt ( JNIEnv *env ,  struct java_lang_reflect_Field* this, struct java_lang_Object* obj, s4 val)
562 {
563   jfieldID fid;
564
565   if (this->clazz && obj) {
566
567       fid = class_findfield_approx ((classinfo *) this->clazz,javastring_toutf(this->name, false));
568
569       if (fid) {
570           env->SetIntField (env, (jobject) obj, fid, val);
571           return;
572       }
573   }
574
575   exceptionptr = native_new_and_init(class_java_lang_IllegalArgumentException);
576 }
577
578 /*
579  * Class:     java/lang/reflect/Field
580  * Method:    setLong
581  * Signature: (Ljava/lang/Object;J)V
582  */
583 JNIEXPORT void JNICALL Java_java_lang_reflect_Field_setLong ( JNIEnv *env ,  struct java_lang_reflect_Field* this, struct java_lang_Object* obj, s8 val)
584 {
585   jfieldID fid;
586
587   if (this->clazz && obj) {
588
589       fid = class_findfield_approx ((classinfo *) this->clazz,javastring_toutf(this->name, false));
590
591       if (fid) {
592           env->SetLongField (env, (jobject) obj, fid, val);
593           return;
594       }
595   }
596
597   exceptionptr = native_new_and_init(class_java_lang_IllegalArgumentException);
598 }
599
600 /*
601  * Class:     java/lang/reflect/Field
602  * Method:    setShort
603  * Signature: (Ljava/lang/Object;S)V
604  */
605 JNIEXPORT void JNICALL Java_java_lang_reflect_Field_setShort ( JNIEnv *env ,  struct java_lang_reflect_Field* this, struct java_lang_Object* obj, s4 val)
606 {
607   jfieldID fid;
608
609   if (this->clazz && obj) {
610
611       fid = class_findfield_approx ((classinfo *) this->clazz,javastring_toutf(this->name, false));
612
613       if (fid) {
614           env->SetShortField (env, (jobject) obj, fid, val);
615           return;
616       }
617   }
618
619   exceptionptr = native_new_and_init(class_java_lang_IllegalArgumentException);
620 }
621
622
623
624
625
626