1 /* src/vm/field.cpp - field functions
3 Copyright (C) 1996-2005, 2006, 2007, 2008, 2010
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.hpp"
34 #include "native/llni.h"
38 #include "vm/annotation.hpp"
39 #include "vm/array.hpp"
40 #include "vm/jit/builtin.hpp"
41 #include "vm/class.hpp"
42 #include "vm/descriptor.hpp"
43 #include "vm/exceptions.hpp"
44 #include "vm/field.hpp"
45 #include "vm/global.h"
46 #include "vm/globals.hpp"
47 #include "vm/loader.hpp"
48 #include "vm/options.h"
49 #include "vm/primitive.hpp"
50 #include "vm/references.h"
51 #include "vm/string.hpp"
52 #include "vm/suck.hpp"
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 = (utf*) class_getconstant(c, suck_u2(cb), CONSTANT_Utf8)))
95 if (!(u = (utf*) 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 = (utf*) 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 = (constant_integer*) class_getconstant(c, pindex, CONSTANT_Integer)))
260 f->value->i = ci->value;
267 if (!(cl = (constant_long*) class_getconstant(c, pindex, CONSTANT_Long)))
270 f->value->l = cl->value;
277 if (!(cf = (constant_float*) class_getconstant(c, pindex, CONSTANT_Float)))
280 f->value->f = cf->value;
287 if (!(cd = (constant_double*) class_getconstant(c, pindex, CONSTANT_Double)))
290 f->value->d = cd->value;
295 if (!(u = (utf*) class_getconstant(c, pindex, CONSTANT_String)))
298 /* Create Java-string from compressed UTF8-string. */
300 if (!(class_java_lang_String->flags & CLASS_LINKED))
301 linker_create_string_later(reinterpret_cast<java_object_t**>(&f->value->a), u);
303 f->value->a = literalstring_new(u);
307 vm_abort("field_load: invalid field type %d", f->type);
310 #if defined(ENABLE_JAVASE)
311 else if (u == utf_Signature) {
314 if (!loader_load_attribute_signature(cb, &(f->signature)))
318 #if defined(ENABLE_ANNOTATIONS)
319 else if (u == utf_RuntimeVisibleAnnotations) {
320 /* RuntimeVisibleAnnotations */
321 if (!annotation_load_field_attribute_runtimevisibleannotations(cb, f))
324 else if (u == utf_RuntimeInvisibleAnnotations) {
325 /* RuntimeInvisibleAnnotations */
326 if (!annotation_load_field_attribute_runtimeinvisibleannotations(cb, f))
332 /* unknown attribute */
334 if (!loader_skip_attribute_body(cb))
339 /* everything was ok */
345 /* field_get_type **************************************************************
347 Returns the type of the field as class.
349 *******************************************************************************/
351 classinfo *field_get_type(fieldinfo *f)
359 if (td->type == TYPE_ADR) {
360 assert(td->classref);
362 u = td->classref->name;
364 /* load the class of the field-type with the field's
367 c = load_class_from_classloader(u, f->clazz->classloader);
370 c = Primitive::get_class_by_type(td->primitivetype);
377 /* field_free ******************************************************************
379 Frees a fields' resources.
381 *******************************************************************************/
383 void field_free(fieldinfo *f)
385 /* free memory for fields which have a value */
388 #if defined(ENABLE_GC_BOEHM)
389 if (f->type != TYPE_ADR)
391 FREE(f->value, imm_union);
395 /* field_get_annotations ******************************************************
397 Get a fields' unparsed annotations in a byte array.
400 f........the field of which the annotations should be returned
403 The unparsed annotations in a byte array (or NULL if there aren't any).
405 *******************************************************************************/
407 java_handle_bytearray_t *field_get_annotations(fieldinfo *f)
409 #if defined(ENABLE_ANNOTATIONS)
410 classinfo *c; /* declaring class */
411 int slot; /* slot of this field */
412 java_handle_t *field_annotations; /* array of unparsed */
413 /* annotations of all fields of the declaring class */
416 slot = f - c->fields;
418 LLNI_classinfo_field_get(c, field_annotations, field_annotations);
420 ObjectArray oa(field_annotations);
422 /* the field_annotations array might be shorter then the field
423 * count if the fields above a certain index have no annotations.
425 if (field_annotations != NULL && oa.get_length() > slot) {
426 return (java_handle_bytearray_t*) oa.get_element(slot);
436 /* field_printflags ************************************************************
440 *******************************************************************************/
443 void field_printflags(fieldinfo *f)
450 if (f->flags & ACC_PUBLIC) printf(" PUBLIC");
451 if (f->flags & ACC_PRIVATE) printf(" PRIVATE");
452 if (f->flags & ACC_PROTECTED) printf(" PROTECTED");
453 if (f->flags & ACC_STATIC) printf(" STATIC");
454 if (f->flags & ACC_FINAL) printf(" FINAL");
455 if (f->flags & ACC_SYNCHRONIZED) printf(" SYNCHRONIZED");
456 if (f->flags & ACC_VOLATILE) printf(" VOLATILE");
457 if (f->flags & ACC_TRANSIENT) printf(" TRANSIENT");
458 if (f->flags & ACC_NATIVE) printf(" NATIVE");
459 if (f->flags & ACC_INTERFACE) printf(" INTERFACE");
460 if (f->flags & ACC_ABSTRACT) printf(" ABSTRACT");
465 /* field_print *****************************************************************
469 *******************************************************************************/
472 void field_print(fieldinfo *f)
475 printf("(fieldinfo*)NULL");
479 utf_display_printable_ascii_classname(f->clazz->name);
481 utf_display_printable_ascii(f->name);
483 utf_display_printable_ascii(f->descriptor);
487 if (!(f->flags & ACC_STATIC)) {
488 printf(", offset: %d", f->offset);
494 /* field_println ***************************************************************
498 *******************************************************************************/
501 void field_println(fieldinfo *f)
508 /* field_fieldref_print ********************************************************
512 *******************************************************************************/
515 void field_fieldref_print(constant_FMIref *fr)
518 printf("(constant_FMIref *)NULL");
522 if (IS_FMIREF_RESOLVED(fr)) {
524 field_print(fr->p.field);
527 printf("<fieldref> ");
528 utf_display_printable_ascii_classname(fr->p.classref->name);
530 utf_display_printable_ascii(fr->name);
532 utf_display_printable_ascii(fr->descriptor);
537 /* field_fieldref_println ******************************************************
541 *******************************************************************************/
544 void field_fieldref_println(constant_FMIref *fr)
546 field_fieldref_print(fr);
552 * These are local overrides for various environment variables in Emacs.
553 * Please do not remove this and leave it at the end of the file, where
554 * Emacs will automagically detect them.
555 * ---------------------------------------------------------------------
558 * indent-tabs-mode: t
562 * vim:noexpandtab:sw=4:ts=4: