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"
38 #include "mm/memory.h"
40 #include "vm/builtin.h"
41 #include "vm/exceptions.h"
42 #include "vm/global.h"
43 #include "vm/primitive.h"
44 #include "vm/stringlocal.h"
47 #include "vmcore/annotation.h"
48 #include "vmcore/class.h"
49 #include "vmcore/descriptor.h"
50 #include "vmcore/field.h"
51 #include "vmcore/loader.h"
52 #include "vmcore/options.h"
53 #include "vmcore/references.h"
54 #include "vmcore/suck.h"
55 #include "vmcore/utf8.h"
58 /* field_load ******************************************************************
60 Load everything about a class field from the class file and fill a
63 *******************************************************************************/
65 #define field_load_NOVALUE 0xffffffff /* must be bigger than any u2 value! */
67 bool field_load(classbuffer *cb, fieldinfo *f, descriptor_pool *descpool)
71 u4 pindex = field_load_NOVALUE; /* constantvalue_index */
80 /* Get access flags. */
82 if (!suck_check_classbuffer_size(cb, 2 + 2 + 2))
85 f->flags = suck_u2(cb);
89 if (!(u = class_getconstant(c, suck_u2(cb), CONSTANT_Utf8)))
96 if (!(u = class_getconstant(c, suck_u2(cb), CONSTANT_Utf8)))
100 f->parseddesc = NULL;
102 if (!descriptor_pool_add(descpool, u, NULL))
105 /* descriptor_pool_add accepts method descriptors, so we have to
106 check against them here before the call of
107 descriptor_to_basic_type below. */
109 if (u->text[0] == '(') {
110 exceptions_throw_classformaterror(c, "Method descriptor used for field");
114 #ifdef ENABLE_VERIFIER
117 if (!is_valid_name_utf(f->name) || f->name->text[0] == '<') {
118 exceptions_throw_classformaterror(c,
119 "Illegal Field name \"%s\"",
124 /* check flag consistency */
125 i = f->flags & (ACC_PUBLIC | ACC_PRIVATE | ACC_PROTECTED);
127 if ((i != 0 && i != ACC_PUBLIC && i != ACC_PRIVATE && i != ACC_PROTECTED) ||
128 ((f->flags & (ACC_FINAL | ACC_VOLATILE)) == (ACC_FINAL | ACC_VOLATILE))) {
129 exceptions_throw_classformaterror(c,
130 "Illegal field modifiers: 0x%X",
135 if (c->flags & ACC_INTERFACE) {
136 if (((f->flags & (ACC_STATIC | ACC_PUBLIC | ACC_FINAL))
137 != (ACC_STATIC | ACC_PUBLIC | ACC_FINAL)) ||
138 f->flags & ACC_TRANSIENT) {
139 exceptions_throw_classformaterror(c,
140 "Illegal field modifiers: 0x%X",
146 #endif /* ENABLE_VERIFIER */
150 f->type = descriptor_to_basic_type(f->descriptor);
152 /* For static-fields allocate memory for the value and set the
155 if (f->flags & ACC_STATIC) {
161 f->value = NEW(imm_union);
165 #if defined(ENABLE_GC_CACAO)
166 f->value = NEW(imm_union);
168 f->value = GCNEW_UNCOLLECTABLE(imm_union, 1);
173 vm_abort("field_load: invalid field type %d", f->type);
176 /* Set the field to zero, for float and double fields set the
177 correct 0.0 value. */
196 /* For instance-fields set the offset to 0. */
200 /* For final fields, which are not static, we need a value
203 if (f->flags & ACC_FINAL) {
204 f->value = NEW(imm_union);
211 c->flags |= ACC_CLASS_HAS_POINTERS;
216 /* read attributes */
218 if (!suck_check_classbuffer_size(cb, 2))
221 attrnum = suck_u2(cb);
223 for (i = 0; i < attrnum; i++) {
224 if (!suck_check_classbuffer_size(cb, 2))
227 if (!(u = class_getconstant(c, suck_u2(cb), CONSTANT_Utf8)))
230 if (u == utf_ConstantValue) {
231 if (!suck_check_classbuffer_size(cb, 4 + 2))
234 /* check attribute length */
236 if (suck_u4(cb) != 2) {
237 exceptions_throw_classformaterror(c, "Wrong size for VALUE attribute");
241 /* constant value attribute */
243 if (pindex != field_load_NOVALUE) {
244 exceptions_throw_classformaterror(c, "Multiple ConstantValue attributes");
248 /* index of value in constantpool */
250 pindex = suck_u2(cb);
252 /* initialize field with value from constantpool */
256 constant_integer *ci;
258 if (!(ci = class_getconstant(c, pindex, CONSTANT_Integer)))
261 f->value->i = ci->value;
268 if (!(cl = class_getconstant(c, pindex, CONSTANT_Long)))
271 f->value->l = cl->value;
278 if (!(cf = class_getconstant(c, pindex, CONSTANT_Float)))
281 f->value->f = cf->value;
288 if (!(cd = class_getconstant(c, pindex, CONSTANT_Double)))
291 f->value->d = cd->value;
296 if (!(u = class_getconstant(c, pindex, CONSTANT_String)))
299 /* Create Java-string from compressed UTF8-string. */
301 f->value->a = literalstring_new(u);
305 vm_abort("field_load: invalid field type %d", f->type);
308 #if defined(ENABLE_JAVASE)
309 else if (u == utf_Signature) {
312 if (!loader_load_attribute_signature(cb, &(f->signature)))
316 #if defined(ENABLE_ANNOTATIONS)
317 else if (u == utf_RuntimeVisibleAnnotations) {
318 /* RuntimeVisibleAnnotations */
319 if (!annotation_load_field_attribute_runtimevisibleannotations(cb, f))
322 else if (u == utf_RuntimeInvisibleAnnotations) {
323 /* RuntimeInvisibleAnnotations */
324 if (!annotation_load_field_attribute_runtimeinvisibleannotations(cb, f))
330 /* unknown attribute */
332 if (!loader_skip_attribute_body(cb))
337 /* everything was ok */
343 /* field_get_type **************************************************************
345 Returns the type of the field as class.
347 *******************************************************************************/
349 classinfo *field_get_type(fieldinfo *f)
357 if (td->type == TYPE_ADR) {
358 assert(td->classref);
360 u = td->classref->name;
362 /* load the class of the field-type with the field's
365 c = load_class_from_classloader(u, f->class->classloader);
368 c = primitive_class_get_by_type(td->decltype);
375 /* field_free ******************************************************************
377 Frees a fields' resources.
379 *******************************************************************************/
381 void field_free(fieldinfo *f)
387 /* field_get_annotations ******************************************************
389 Gets a fields' annotations (or NULL if none).
391 *******************************************************************************/
393 java_handle_bytearray_t *field_get_annotations(fieldinfo *f)
395 #if defined(ENABLE_ANNOTATIONS)
398 annotation_bytearray_t *ba;
399 java_handle_bytearray_t *annotations;
402 slot = f - c->fields;
405 if (c->field_annotations != NULL && c->field_annotations->size > slot) {
406 ba = c->field_annotations->data[slot];
409 annotations = builtin_newarray_byte(ba->size);
411 if (annotations != NULL) {
412 MCOPY(annotations->data, ba->data, uint8_t, ba->size);
424 /* field_printflags ************************************************************
428 *******************************************************************************/
431 void field_printflags(fieldinfo *f)
438 if (f->flags & ACC_PUBLIC) printf(" PUBLIC");
439 if (f->flags & ACC_PRIVATE) printf(" PRIVATE");
440 if (f->flags & ACC_PROTECTED) printf(" PROTECTED");
441 if (f->flags & ACC_STATIC) printf(" STATIC");
442 if (f->flags & ACC_FINAL) printf(" FINAL");
443 if (f->flags & ACC_SYNCHRONIZED) printf(" SYNCHRONIZED");
444 if (f->flags & ACC_VOLATILE) printf(" VOLATILE");
445 if (f->flags & ACC_TRANSIENT) printf(" TRANSIENT");
446 if (f->flags & ACC_NATIVE) printf(" NATIVE");
447 if (f->flags & ACC_INTERFACE) printf(" INTERFACE");
448 if (f->flags & ACC_ABSTRACT) printf(" ABSTRACT");
453 /* field_print *****************************************************************
457 *******************************************************************************/
460 void field_print(fieldinfo *f)
463 printf("(fieldinfo*)NULL");
467 utf_display_printable_ascii_classname(f->class->name);
469 utf_display_printable_ascii(f->name);
471 utf_display_printable_ascii(f->descriptor);
478 /* field_println ***************************************************************
482 *******************************************************************************/
485 void field_println(fieldinfo *f)
492 /* field_fieldref_print ********************************************************
496 *******************************************************************************/
499 void field_fieldref_print(constant_FMIref *fr)
502 printf("(constant_FMIref *)NULL");
506 if (IS_FMIREF_RESOLVED(fr)) {
508 field_print(fr->p.field);
511 printf("<fieldref> ");
512 utf_display_printable_ascii_classname(fr->p.classref->name);
514 utf_display_printable_ascii(fr->name);
516 utf_display_printable_ascii(fr->descriptor);
521 /* field_fieldref_println ******************************************************
525 *******************************************************************************/
528 void field_fieldref_println(constant_FMIref *fr)
530 field_fieldref_print(fr);
536 * These are local overrides for various environment variables in Emacs.
537 * Please do not remove this and leave it at the end of the file, where
538 * Emacs will automagically detect them.
539 * ---------------------------------------------------------------------
542 * indent-tabs-mode: t