1 /* src/vmcore/field.c - field functions
3 Copyright (C) 1996-2005, 2006, 2007 R. Grafl, A. Krall, C. Kruegel,
4 C. Oates, R. Obermaisser, M. Platter, M. Probst, S. Ring,
5 E. Steiner, C. Thalinger, D. Thuernbeck, P. Tomsich, C. Ullrich,
6 J. Wenninger, Institut f. Computersprachen - TU Wien
8 This file is part of CACAO.
10 This program is free software; you can redistribute it and/or
11 modify it under the terms of the GNU General Public License as
12 published by the Free Software Foundation; either version 2, or (at
13 your option) any later version.
15 This program is distributed in the hope that it will be useful, but
16 WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
34 #include "mm/memory.h"
39 #include "vm/builtin.h"
40 #include "vm/exceptions.h"
41 #include "vm/global.h"
42 #include "vm/primitive.h"
43 #include "vm/stringlocal.h"
46 #include "vmcore/annotation.h"
47 #include "vmcore/class.h"
48 #include "vmcore/descriptor.h"
49 #include "vmcore/field.h"
50 #include "vmcore/loader.h"
51 #include "vmcore/options.h"
52 #include "vmcore/references.h"
53 #include "vmcore/suck.h"
54 #include "vmcore/utf8.h"
57 /* field_load ******************************************************************
59 Load everything about a class field from the class file and fill a
62 *******************************************************************************/
64 #define field_load_NOVALUE 0xffffffff /* must be bigger than any u2 value! */
66 bool field_load(classbuffer *cb, fieldinfo *f, descriptor_pool *descpool)
70 u4 pindex = field_load_NOVALUE; /* constantvalue_index */
79 /* Get access flags. */
81 if (!suck_check_classbuffer_size(cb, 2 + 2 + 2))
84 f->flags = suck_u2(cb);
88 if (!(u = class_getconstant(c, suck_u2(cb), CONSTANT_Utf8)))
95 if (!(u = class_getconstant(c, suck_u2(cb), CONSTANT_Utf8)))
101 if (!descriptor_pool_add(descpool, u, NULL))
104 /* descriptor_pool_add accepts method descriptors, so we have to
105 check against them here before the call of
106 descriptor_to_basic_type below. */
108 if (u->text[0] == '(') {
109 exceptions_throw_classformaterror(c, "Method descriptor used for field");
113 #ifdef ENABLE_VERIFIER
116 if (!is_valid_name_utf(f->name) || f->name->text[0] == '<') {
117 exceptions_throw_classformaterror(c,
118 "Illegal Field name \"%s\"",
123 /* check flag consistency */
124 i = f->flags & (ACC_PUBLIC | ACC_PRIVATE | ACC_PROTECTED);
126 if ((i != 0 && i != ACC_PUBLIC && i != ACC_PRIVATE && i != ACC_PROTECTED) ||
127 ((f->flags & (ACC_FINAL | ACC_VOLATILE)) == (ACC_FINAL | ACC_VOLATILE))) {
128 exceptions_throw_classformaterror(c,
129 "Illegal field modifiers: 0x%X",
134 if (c->flags & ACC_INTERFACE) {
135 if (((f->flags & (ACC_STATIC | ACC_PUBLIC | ACC_FINAL))
136 != (ACC_STATIC | ACC_PUBLIC | ACC_FINAL)) ||
137 f->flags & ACC_TRANSIENT) {
138 exceptions_throw_classformaterror(c,
139 "Illegal field modifiers: 0x%X",
145 #endif /* ENABLE_VERIFIER */
149 f->type = descriptor_to_basic_type(f->descriptor);
151 /* For static-fields allocate memory for the value and set the
154 if (f->flags & ACC_STATIC) {
160 f->value = NEW(imm_union);
164 #if defined(ENABLE_GC_CACAO)
165 f->value = NEW(imm_union);
167 f->value = GCNEW_UNCOLLECTABLE(imm_union, 1);
172 vm_abort("field_load: invalid field type %d", f->type);
175 /* Set the field to zero, for float and double fields set the
176 correct 0.0 value. */
195 /* For instance-fields set the offset to 0. */
199 /* For final fields, which are not static, we need a value
202 if (f->flags & ACC_FINAL) {
203 f->value = NEW(imm_union);
210 c->flags |= ACC_CLASS_HAS_POINTERS;
215 /* read attributes */
217 if (!suck_check_classbuffer_size(cb, 2))
220 attrnum = suck_u2(cb);
222 for (i = 0; i < attrnum; i++) {
223 if (!suck_check_classbuffer_size(cb, 2))
226 if (!(u = class_getconstant(c, suck_u2(cb), CONSTANT_Utf8)))
229 if (u == utf_ConstantValue) {
230 if (!suck_check_classbuffer_size(cb, 4 + 2))
233 /* check attribute length */
235 if (suck_u4(cb) != 2) {
236 exceptions_throw_classformaterror(c, "Wrong size for VALUE attribute");
240 /* constant value attribute */
242 if (pindex != field_load_NOVALUE) {
243 exceptions_throw_classformaterror(c, "Multiple ConstantValue attributes");
247 /* index of value in constantpool */
249 pindex = suck_u2(cb);
251 /* initialize field with value from constantpool */
255 constant_integer *ci;
257 if (!(ci = class_getconstant(c, pindex, CONSTANT_Integer)))
260 f->value->i = ci->value;
267 if (!(cl = class_getconstant(c, pindex, CONSTANT_Long)))
270 f->value->l = cl->value;
277 if (!(cf = class_getconstant(c, pindex, CONSTANT_Float)))
280 f->value->f = cf->value;
287 if (!(cd = class_getconstant(c, pindex, CONSTANT_Double)))
290 f->value->d = cd->value;
295 if (!(u = class_getconstant(c, pindex, CONSTANT_String)))
298 /* Create Java-string from compressed UTF8-string. */
300 f->value->a = literalstring_new(u);
304 vm_abort("field_load: invalid field type %d", f->type);
307 #if defined(ENABLE_JAVASE)
308 else if (u == utf_Signature) {
311 if (!loader_load_attribute_signature(cb, &(f->signature)))
315 #if defined(ENABLE_ANNOTATIONS)
316 else if (u == utf_RuntimeVisibleAnnotations) {
317 /* RuntimeVisibleAnnotations */
318 if (!annotation_load_field_attribute_runtimevisibleannotations(cb, f))
321 else if (u == utf_RuntimeInvisibleAnnotations) {
322 /* RuntimeInvisibleAnnotations */
323 if (!annotation_load_field_attribute_runtimeinvisibleannotations(cb, f))
329 /* unknown attribute */
331 if (!loader_skip_attribute_body(cb))
336 /* everything was ok */
342 /* field_get_type **************************************************************
344 Returns the type of the field as class.
346 *******************************************************************************/
348 classinfo *field_get_type(fieldinfo *f)
356 if (td->type == TYPE_ADR) {
357 assert(td->classref);
359 u = td->classref->name;
361 /* load the class of the field-type with the field's
364 c = load_class_from_classloader(u, f->class->classloader);
367 c = primitive_class_get_by_type(td->decltype);
374 /* field_free ******************************************************************
376 Frees a fields' resources.
378 *******************************************************************************/
380 void field_free(fieldinfo *f)
386 /* field_get_annotations ******************************************************
388 Gets a fields' annotations (or NULL if none).
390 *******************************************************************************/
392 java_handle_bytearray_t *field_get_annotations(fieldinfo *f)
394 #if defined(ENABLE_ANNOTATIONS)
397 java_handle_bytearray_t *annotations;
401 slot = f - c->fields;
403 a = (java_handle_t*)c->field_annotations;
405 if (c->field_annotations != NULL && array_length_get(a) > slot) {
406 annotations = (java_handle_bytearray_t*)
407 array_objectarray_element_get(
408 c->field_annotations, slot);
418 /* field_printflags ************************************************************
422 *******************************************************************************/
425 void field_printflags(fieldinfo *f)
432 if (f->flags & ACC_PUBLIC) printf(" PUBLIC");
433 if (f->flags & ACC_PRIVATE) printf(" PRIVATE");
434 if (f->flags & ACC_PROTECTED) printf(" PROTECTED");
435 if (f->flags & ACC_STATIC) printf(" STATIC");
436 if (f->flags & ACC_FINAL) printf(" FINAL");
437 if (f->flags & ACC_SYNCHRONIZED) printf(" SYNCHRONIZED");
438 if (f->flags & ACC_VOLATILE) printf(" VOLATILE");
439 if (f->flags & ACC_TRANSIENT) printf(" TRANSIENT");
440 if (f->flags & ACC_NATIVE) printf(" NATIVE");
441 if (f->flags & ACC_INTERFACE) printf(" INTERFACE");
442 if (f->flags & ACC_ABSTRACT) printf(" ABSTRACT");
447 /* field_print *****************************************************************
451 *******************************************************************************/
454 void field_print(fieldinfo *f)
457 printf("(fieldinfo*)NULL");
461 utf_display_printable_ascii_classname(f->class->name);
463 utf_display_printable_ascii(f->name);
465 utf_display_printable_ascii(f->descriptor);
472 /* field_println ***************************************************************
476 *******************************************************************************/
479 void field_println(fieldinfo *f)
486 /* field_fieldref_print ********************************************************
490 *******************************************************************************/
493 void field_fieldref_print(constant_FMIref *fr)
496 printf("(constant_FMIref *)NULL");
500 if (IS_FMIREF_RESOLVED(fr)) {
502 field_print(fr->p.field);
505 printf("<fieldref> ");
506 utf_display_printable_ascii_classname(fr->p.classref->name);
508 utf_display_printable_ascii(fr->name);
510 utf_display_printable_ascii(fr->descriptor);
515 /* field_fieldref_println ******************************************************
519 *******************************************************************************/
522 void field_fieldref_println(constant_FMIref *fr)
524 field_fieldref_print(fr);
530 * These are local overrides for various environment variables in Emacs.
531 * Please do not remove this and leave it at the end of the file, where
532 * Emacs will automagically detect them.
533 * ---------------------------------------------------------------------
536 * indent-tabs-mode: t