1 /* src/vmcore/field.c - field functions
3 Copyright (C) 1996-2005, 2006, 2007, 2008
4 CACAOVM - Verein zur Foerderung der freien virtuellen Maschine CACAO
6 This file is part of CACAO.
8 This program is free software; you can redistribute it and/or
9 modify it under the terms of the GNU General Public License as
10 published by the Free Software Foundation; either version 2, or (at
11 your option) any later version.
13 This program is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
32 #include "mm/memory.h"
34 #include "native/llni.h"
39 #include "vm/builtin.h"
40 #include "vm/exceptions.hpp"
41 #include "vm/global.h"
42 #include "vm/primitive.hpp"
43 #include "vm/string.hpp"
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_BOEHM)
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->clazz->classloader);
367 c = Primitive_get_class_by_type(td->primitivetype);
374 /* field_free ******************************************************************
376 Frees a fields' resources.
378 *******************************************************************************/
380 void field_free(fieldinfo *f)
382 /* free memory for fields which have a value */
385 #if defined(ENABLE_GC_BOEHM)
386 if (f->type != TYPE_ADR)
388 FREE(f->value, imm_union);
392 /* field_get_annotations ******************************************************
394 Get a fields' unparsed annotations in a byte array.
397 f........the field of which the annotations should be returned
400 The unparsed annotations in a byte array (or NULL if there aren't any).
402 *******************************************************************************/
404 java_handle_bytearray_t *field_get_annotations(fieldinfo *f)
406 #if defined(ENABLE_ANNOTATIONS)
407 classinfo *c; /* declaring class */
408 int slot; /* slot of this field */
409 java_handle_bytearray_t *annotations; /* unparsed annotations */
410 java_handle_t *field_annotations; /* array of unparsed */
411 /* annotations of all fields of the declaring class */
414 slot = f - c->fields;
417 LLNI_classinfo_field_get(c, field_annotations, field_annotations);
419 /* the field_annotations array might be shorter then the field
420 * count if the fields above a certain index have no annotations.
422 if (field_annotations != NULL &&
423 array_length_get(field_annotations) > slot) {
424 annotations = (java_handle_bytearray_t*)array_objectarray_element_get(
425 (java_handle_objectarray_t*)field_annotations, slot);
435 /* field_printflags ************************************************************
439 *******************************************************************************/
442 void field_printflags(fieldinfo *f)
449 if (f->flags & ACC_PUBLIC) printf(" PUBLIC");
450 if (f->flags & ACC_PRIVATE) printf(" PRIVATE");
451 if (f->flags & ACC_PROTECTED) printf(" PROTECTED");
452 if (f->flags & ACC_STATIC) printf(" STATIC");
453 if (f->flags & ACC_FINAL) printf(" FINAL");
454 if (f->flags & ACC_SYNCHRONIZED) printf(" SYNCHRONIZED");
455 if (f->flags & ACC_VOLATILE) printf(" VOLATILE");
456 if (f->flags & ACC_TRANSIENT) printf(" TRANSIENT");
457 if (f->flags & ACC_NATIVE) printf(" NATIVE");
458 if (f->flags & ACC_INTERFACE) printf(" INTERFACE");
459 if (f->flags & ACC_ABSTRACT) printf(" ABSTRACT");
464 /* field_print *****************************************************************
468 *******************************************************************************/
471 void field_print(fieldinfo *f)
474 printf("(fieldinfo*)NULL");
478 utf_display_printable_ascii_classname(f->clazz->name);
480 utf_display_printable_ascii(f->name);
482 utf_display_printable_ascii(f->descriptor);
486 if (!(f->flags & ACC_STATIC)) {
487 printf(", offset: %d", f->offset);
493 /* field_println ***************************************************************
497 *******************************************************************************/
500 void field_println(fieldinfo *f)
507 /* field_fieldref_print ********************************************************
511 *******************************************************************************/
514 void field_fieldref_print(constant_FMIref *fr)
517 printf("(constant_FMIref *)NULL");
521 if (IS_FMIREF_RESOLVED(fr)) {
523 field_print(fr->p.field);
526 printf("<fieldref> ");
527 utf_display_printable_ascii_classname(fr->p.classref->name);
529 utf_display_printable_ascii(fr->name);
531 utf_display_printable_ascii(fr->descriptor);
536 /* field_fieldref_println ******************************************************
540 *******************************************************************************/
543 void field_fieldref_println(constant_FMIref *fr)
545 field_fieldref_print(fr);
551 * These are local overrides for various environment variables in Emacs.
552 * Please do not remove this and leave it at the end of the file, where
553 * Emacs will automagically detect them.
554 * ---------------------------------------------------------------------
557 * indent-tabs-mode: t
561 * vim:noexpandtab:sw=4:ts=4: