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"
36 #include "native/llni.h"
41 #include "vm/builtin.h"
42 #include "vm/exceptions.h"
43 #include "vm/global.h"
44 #include "vm/primitive.h"
45 #include "vm/stringlocal.h"
48 #include "vmcore/annotation.h"
49 #include "vmcore/class.h"
50 #include "vmcore/descriptor.h"
51 #include "vmcore/field.h"
52 #include "vmcore/loader.h"
53 #include "vmcore/options.h"
54 #include "vmcore/references.h"
55 #include "vmcore/suck.h"
56 #include "vmcore/utf8.h"
59 /* field_load ******************************************************************
61 Load everything about a class field from the class file and fill a
64 *******************************************************************************/
66 #define field_load_NOVALUE 0xffffffff /* must be bigger than any u2 value! */
68 bool field_load(classbuffer *cb, fieldinfo *f, descriptor_pool *descpool)
72 u4 pindex = field_load_NOVALUE; /* constantvalue_index */
81 /* Get access flags. */
83 if (!suck_check_classbuffer_size(cb, 2 + 2 + 2))
86 f->flags = suck_u2(cb);
90 if (!(u = class_getconstant(c, suck_u2(cb), CONSTANT_Utf8)))
97 if (!(u = class_getconstant(c, suck_u2(cb), CONSTANT_Utf8)))
101 f->parseddesc = NULL;
103 if (!descriptor_pool_add(descpool, u, NULL))
106 /* descriptor_pool_add accepts method descriptors, so we have to
107 check against them here before the call of
108 descriptor_to_basic_type below. */
110 if (u->text[0] == '(') {
111 exceptions_throw_classformaterror(c, "Method descriptor used for field");
115 #ifdef ENABLE_VERIFIER
118 if (!is_valid_name_utf(f->name) || f->name->text[0] == '<') {
119 exceptions_throw_classformaterror(c,
120 "Illegal Field name \"%s\"",
125 /* check flag consistency */
126 i = f->flags & (ACC_PUBLIC | ACC_PRIVATE | ACC_PROTECTED);
128 if ((i != 0 && i != ACC_PUBLIC && i != ACC_PRIVATE && i != ACC_PROTECTED) ||
129 ((f->flags & (ACC_FINAL | ACC_VOLATILE)) == (ACC_FINAL | ACC_VOLATILE))) {
130 exceptions_throw_classformaterror(c,
131 "Illegal field modifiers: 0x%X",
136 if (c->flags & ACC_INTERFACE) {
137 if (((f->flags & (ACC_STATIC | ACC_PUBLIC | ACC_FINAL))
138 != (ACC_STATIC | ACC_PUBLIC | ACC_FINAL)) ||
139 f->flags & ACC_TRANSIENT) {
140 exceptions_throw_classformaterror(c,
141 "Illegal field modifiers: 0x%X",
147 #endif /* ENABLE_VERIFIER */
151 f->type = descriptor_to_basic_type(f->descriptor);
153 /* For static-fields allocate memory for the value and set the
156 if (f->flags & ACC_STATIC) {
162 f->value = NEW(imm_union);
166 #if !defined(ENABLE_GC_BOEHM)
167 f->value = NEW(imm_union);
169 f->value = GCNEW_UNCOLLECTABLE(imm_union, 1);
174 vm_abort("field_load: invalid field type %d", f->type);
177 /* Set the field to zero, for float and double fields set the
178 correct 0.0 value. */
197 /* For instance-fields set the offset to 0. */
201 /* For final fields, which are not static, we need a value
204 if (f->flags & ACC_FINAL) {
205 f->value = NEW(imm_union);
212 c->flags |= ACC_CLASS_HAS_POINTERS;
217 /* read attributes */
219 if (!suck_check_classbuffer_size(cb, 2))
222 attrnum = suck_u2(cb);
224 for (i = 0; i < attrnum; i++) {
225 if (!suck_check_classbuffer_size(cb, 2))
228 if (!(u = class_getconstant(c, suck_u2(cb), CONSTANT_Utf8)))
231 if (u == utf_ConstantValue) {
232 if (!suck_check_classbuffer_size(cb, 4 + 2))
235 /* check attribute length */
237 if (suck_u4(cb) != 2) {
238 exceptions_throw_classformaterror(c, "Wrong size for VALUE attribute");
242 /* constant value attribute */
244 if (pindex != field_load_NOVALUE) {
245 exceptions_throw_classformaterror(c, "Multiple ConstantValue attributes");
249 /* index of value in constantpool */
251 pindex = suck_u2(cb);
253 /* initialize field with value from constantpool */
257 constant_integer *ci;
259 if (!(ci = class_getconstant(c, pindex, CONSTANT_Integer)))
262 f->value->i = ci->value;
269 if (!(cl = class_getconstant(c, pindex, CONSTANT_Long)))
272 f->value->l = cl->value;
279 if (!(cf = class_getconstant(c, pindex, CONSTANT_Float)))
282 f->value->f = cf->value;
289 if (!(cd = class_getconstant(c, pindex, CONSTANT_Double)))
292 f->value->d = cd->value;
297 if (!(u = class_getconstant(c, pindex, CONSTANT_String)))
300 /* Create Java-string from compressed UTF8-string. */
302 f->value->a = literalstring_new(u);
306 vm_abort("field_load: invalid field type %d", f->type);
309 #if defined(ENABLE_JAVASE)
310 else if (u == utf_Signature) {
313 if (!loader_load_attribute_signature(cb, &(f->signature)))
317 #if defined(ENABLE_ANNOTATIONS)
318 else if (u == utf_RuntimeVisibleAnnotations) {
319 /* RuntimeVisibleAnnotations */
320 if (!annotation_load_field_attribute_runtimevisibleannotations(cb, f))
323 else if (u == utf_RuntimeInvisibleAnnotations) {
324 /* RuntimeInvisibleAnnotations */
325 if (!annotation_load_field_attribute_runtimeinvisibleannotations(cb, f))
331 /* unknown attribute */
333 if (!loader_skip_attribute_body(cb))
338 /* everything was ok */
344 /* field_get_type **************************************************************
346 Returns the type of the field as class.
348 *******************************************************************************/
350 classinfo *field_get_type(fieldinfo *f)
358 if (td->type == TYPE_ADR) {
359 assert(td->classref);
361 u = td->classref->name;
363 /* load the class of the field-type with the field's
366 c = load_class_from_classloader(u, f->class->classloader);
369 c = primitive_class_get_by_type(td->decltype);
376 /* field_free ******************************************************************
378 Frees a fields' resources.
380 *******************************************************************************/
382 void field_free(fieldinfo *f)
384 /* free memory for fields which have a value */
387 #if defined(ENABLE_GC_BOEHM)
388 if (f->type != TYPE_ADR)
390 FREE(f->value, imm_union);
394 /* field_get_annotations ******************************************************
396 Get a fields' unparsed annotations in a byte array.
399 f........the field of which the annotations should be returned
402 The unparsed annotations in a byte array (or NULL if there aren't any).
404 *******************************************************************************/
406 java_handle_bytearray_t *field_get_annotations(fieldinfo *f)
408 #if defined(ENABLE_ANNOTATIONS)
409 classinfo *c; /* declaring class */
410 int slot; /* slot of this field */
411 java_handle_bytearray_t *annotations; /* unparsed annotations */
412 java_handle_t *field_annotations; /* array of unparsed */
413 /* annotations of all fields of the declaring class */
416 slot = f - c->fields;
419 LLNI_classinfo_field_get(c, field_annotations, field_annotations);
421 /* the field_annotations array might be shorter then the field
422 * count if the fields above a certain index have no annotations.
424 if (field_annotations != NULL &&
425 array_length_get(field_annotations) > slot) {
426 annotations = (java_handle_bytearray_t*)array_objectarray_element_get(
427 (java_handle_objectarray_t*)field_annotations, slot);
437 /* field_printflags ************************************************************
441 *******************************************************************************/
444 void field_printflags(fieldinfo *f)
451 if (f->flags & ACC_PUBLIC) printf(" PUBLIC");
452 if (f->flags & ACC_PRIVATE) printf(" PRIVATE");
453 if (f->flags & ACC_PROTECTED) printf(" PROTECTED");
454 if (f->flags & ACC_STATIC) printf(" STATIC");
455 if (f->flags & ACC_FINAL) printf(" FINAL");
456 if (f->flags & ACC_SYNCHRONIZED) printf(" SYNCHRONIZED");
457 if (f->flags & ACC_VOLATILE) printf(" VOLATILE");
458 if (f->flags & ACC_TRANSIENT) printf(" TRANSIENT");
459 if (f->flags & ACC_NATIVE) printf(" NATIVE");
460 if (f->flags & ACC_INTERFACE) printf(" INTERFACE");
461 if (f->flags & ACC_ABSTRACT) printf(" ABSTRACT");
466 /* field_print *****************************************************************
470 *******************************************************************************/
473 void field_print(fieldinfo *f)
476 printf("(fieldinfo*)NULL");
480 utf_display_printable_ascii_classname(f->class->name);
482 utf_display_printable_ascii(f->name);
484 utf_display_printable_ascii(f->descriptor);
491 /* field_println ***************************************************************
495 *******************************************************************************/
498 void field_println(fieldinfo *f)
505 /* field_fieldref_print ********************************************************
509 *******************************************************************************/
512 void field_fieldref_print(constant_FMIref *fr)
515 printf("(constant_FMIref *)NULL");
519 if (IS_FMIREF_RESOLVED(fr)) {
521 field_print(fr->p.field);
524 printf("<fieldref> ");
525 utf_display_printable_ascii_classname(fr->p.classref->name);
527 utf_display_printable_ascii(fr->name);
529 utf_display_printable_ascii(fr->descriptor);
534 /* field_fieldref_println ******************************************************
538 *******************************************************************************/
541 void field_fieldref_println(constant_FMIref *fr)
543 field_fieldref_print(fr);
549 * These are local overrides for various environment variables in Emacs.
550 * Please do not remove this and leave it at the end of the file, where
551 * Emacs will automagically detect them.
552 * ---------------------------------------------------------------------
555 * indent-tabs-mode: t
559 * vim:noexpandtab:sw=4:ts=4: