33 #include "code/dependencyContext.hpp"
34 #include "code/pcDesc.hpp"
35 #include "interpreter/interpreter.hpp"
36 #include "interpreter/linkResolver.hpp"
37 #include "logging/log.hpp"
38 #include "logging/logStream.hpp"
39 #include "memory/heapShared.inline.hpp"
40 #include "memory/metaspaceShared.hpp"
41 #include "memory/oopFactory.hpp"
42 #include "memory/resourceArea.hpp"
43 #include "memory/universe.hpp"
44 #include "oops/fieldStreams.hpp"
45 #include "oops/instanceKlass.hpp"
46 #include "oops/instanceMirrorKlass.hpp"
47 #include "oops/klass.hpp"
48 #include "oops/method.inline.hpp"
49 #include "oops/objArrayOop.inline.hpp"
50 #include "oops/oop.inline.hpp"
51 #include "oops/symbol.hpp"
52 #include "oops/typeArrayOop.inline.hpp"
53 #include "prims/jvmtiExport.hpp"
54 #include "prims/resolvedMethodTable.hpp"
55 #include "runtime/fieldDescriptor.inline.hpp"
56 #include "runtime/frame.inline.hpp"
57 #include "runtime/handles.inline.hpp"
58 #include "runtime/interfaceSupport.inline.hpp"
59 #include "runtime/java.hpp"
60 #include "runtime/javaCalls.hpp"
61 #include "runtime/jniHandles.inline.hpp"
62 #include "runtime/safepoint.hpp"
63 #include "runtime/safepointVerifiers.hpp"
64 #include "runtime/thread.inline.hpp"
65 #include "runtime/vframe.inline.hpp"
66 #include "utilities/align.hpp"
67 #include "utilities/preserveException.hpp"
68 #if INCLUDE_JVMCI
69 #include "jvmci/jvmciJavaClasses.hpp"
70 #endif
71
72 #define INJECTED_FIELD_COMPUTE_OFFSET(klass, name, signature, may_be_java) \
891 int computed_modifiers = k->compute_modifier_flags(CHECK);
892 k->set_modifier_flags(computed_modifiers);
893 // Class_klass has to be loaded because it is used to allocate
894 // the mirror.
895 if (SystemDictionary::Class_klass_loaded()) {
896 // Allocate mirror (java.lang.Class instance)
897 oop mirror_oop = InstanceMirrorKlass::cast(SystemDictionary::Class_klass())->allocate_instance(k, CHECK);
898 Handle mirror(THREAD, mirror_oop);
899 Handle comp_mirror;
900
901 // Setup indirection from mirror->klass
902 java_lang_Class::set_klass(mirror(), k);
903
904 InstanceMirrorKlass* mk = InstanceMirrorKlass::cast(mirror->klass());
905 assert(oop_size(mirror()) == mk->instance_size(k), "should have been set");
906
907 java_lang_Class::set_static_oop_field_count(mirror(), mk->compute_static_oop_field_count(mirror()));
908
909 // It might also have a component mirror. This mirror must already exist.
910 if (k->is_array_klass()) {
911 if (k->is_typeArray_klass()) {
912 BasicType type = TypeArrayKlass::cast(k)->element_type();
913 comp_mirror = Handle(THREAD, Universe::java_mirror(type));
914 } else {
915 assert(k->is_objArray_klass(), "Must be");
916 Klass* element_klass = ObjArrayKlass::cast(k)->element_klass();
917 assert(element_klass != NULL, "Must have an element klass");
918 comp_mirror = Handle(THREAD, element_klass->java_mirror());
919 }
920 assert(comp_mirror() != NULL, "must have a mirror");
921
922 // Two-way link between the array klass and its component mirror:
923 // (array_klass) k -> mirror -> component_mirror -> array_klass -> k
924 set_component_mirror(mirror(), comp_mirror());
925 // See below for ordering dependencies between field array_klass in component mirror
926 // and java_mirror in this klass.
927 } else {
928 assert(k->is_instance_klass(), "Must be");
929
930 initialize_mirror_fields(k, mirror, protection_domain, THREAD);
931 if (HAS_PENDING_EXCEPTION) {
932 // If any of the fields throws an exception like OOM remove the klass field
933 // from the mirror so GC doesn't follow it after the klass has been deallocated.
934 // This mirror looks like a primitive type, which logically it is because it
935 // it represents no class.
936 java_lang_Class::set_klass(mirror(), NULL);
937 return;
938 }
939 }
940
941 // set the classLoader field in the java_lang_Class instance
942 assert(oopDesc::equals(class_loader(), k->class_loader()), "should be same");
943 set_class_loader(mirror(), class_loader());
944
945 // Setup indirection from klass->mirror
946 // after any exceptions can happen during allocations.
947 k->set_java_mirror(mirror);
948
949 // Set the module field in the java_lang_Class instance. This must be done
950 // after the mirror is set.
951 set_mirror_module_field(k, mirror, module, THREAD);
952
953 if (comp_mirror() != NULL) {
954 // Set after k->java_mirror() is published, because compiled code running
955 // concurrently doesn't expect a k to have a null java_mirror.
956 release_set_array_klass(comp_mirror(), k);
957 }
958 } else {
959 assert(fixup_mirror_list() != NULL, "fixup_mirror_list not initialized");
960 fixup_mirror_list()->push(k);
961 }
962 }
963
964 #if INCLUDE_CDS_JAVA_HEAP
965 // Clears mirror fields. Static final fields with initial values are reloaded
966 // from constant pool. The object identity hash is in the object header and is
967 // not affected.
968 class ResetMirrorField: public FieldClosure {
969 private:
970 Handle _m;
971
972 public:
973 ResetMirrorField(Handle mirror) : _m(mirror) {}
974
975 void do_field(fieldDescriptor* fd) {
976 assert(DumpSharedSpaces, "dump time only");
977 assert(_m.not_null(), "Mirror cannot be NULL");
978
979 if (fd->is_static() && fd->has_initial_value()) {
980 initialize_static_field(fd, _m, Thread::current());
981 return;
982 }
983
1351 oop java_lang_Class::name(Handle java_class, TRAPS) {
1352 assert(_name_offset != 0, "must be set");
1353 oop o = java_class->obj_field(_name_offset);
1354 if (o == NULL) {
1355 o = StringTable::intern(java_lang_Class::as_external_name(java_class()), THREAD);
1356 java_class->obj_field_put(_name_offset, o);
1357 }
1358 return o;
1359 }
1360
1361 oop java_lang_Class::source_file(oop java_class) {
1362 assert(_source_file_offset != 0, "must be set");
1363 return java_class->obj_field(_source_file_offset);
1364 }
1365
1366 void java_lang_Class::set_source_file(oop java_class, oop source_file) {
1367 assert(_source_file_offset != 0, "must be set");
1368 java_class->obj_field_put(_source_file_offset, source_file);
1369 }
1370
1371 oop java_lang_Class::create_basic_type_mirror(const char* basic_type_name, BasicType type, TRAPS) {
1372 // This should be improved by adding a field at the Java level or by
1373 // introducing a new VM klass (see comment in ClassFileParser)
1374 oop java_class = InstanceMirrorKlass::cast(SystemDictionary::Class_klass())->allocate_instance(NULL, CHECK_0);
1375 if (type != T_VOID) {
1376 Klass* aklass = Universe::typeArrayKlassObj(type);
1377 assert(aklass != NULL, "correct bootstrap");
1378 release_set_array_klass(java_class, aklass);
1379 }
1380 #ifdef ASSERT
1381 InstanceMirrorKlass* mk = InstanceMirrorKlass::cast(SystemDictionary::Class_klass());
1382 assert(java_lang_Class::static_oop_field_count(java_class) == 0, "should have been zeroed by allocation");
1383 #endif
1384 return java_class;
1385 }
1386
1387
1388 Klass* java_lang_Class::as_Klass(oop java_class) {
1389 //%note memory_2
1390 assert(java_lang_Class::is_instance(java_class), "must be a Class object");
1395
1396 Klass* java_lang_Class::as_Klass_raw(oop java_class) {
1397 //%note memory_2
1398 assert(java_lang_Class::is_instance(java_class), "must be a Class object");
1399 Klass* k = ((Klass*)java_class->metadata_field_raw(_klass_offset));
1400 assert(k == NULL || k->is_klass(), "type check");
1401 return k;
1402 }
1403
1404
1405 void java_lang_Class::set_klass(oop java_class, Klass* klass) {
1406 assert(java_lang_Class::is_instance(java_class), "must be a Class object");
1407 java_class->metadata_field_put(_klass_offset, klass);
1408 }
1409
1410
1411 void java_lang_Class::print_signature(oop java_class, outputStream* st) {
1412 assert(java_lang_Class::is_instance(java_class), "must be a Class object");
1413 Symbol* name = NULL;
1414 bool is_instance = false;
1415 if (is_primitive(java_class)) {
1416 name = vmSymbols::type_signature(primitive_type(java_class));
1417 } else {
1418 Klass* k = as_Klass(java_class);
1419 is_instance = k->is_instance_klass();
1420 name = k->name();
1421 }
1422 if (name == NULL) {
1423 st->print("<null>");
1424 return;
1425 }
1426 if (is_instance) st->print("L");
1427 st->write((char*) name->base(), (int) name->utf8_length());
1428 if (is_instance) st->print(";");
1429 }
1430
1431 Symbol* java_lang_Class::as_signature(oop java_class, bool intern_if_not_found, TRAPS) {
1432 assert(java_lang_Class::is_instance(java_class), "must be a Class object");
1433 Symbol* name;
1434 if (is_primitive(java_class)) {
1435 name = vmSymbols::type_signature(primitive_type(java_class));
1436 // Because this can create a new symbol, the caller has to decrement
1437 // the refcount, so make adjustment here and below for symbols returned
1438 // that are not created or incremented due to a successful lookup.
1439 name->increment_refcount();
1440 } else {
1441 Klass* k = as_Klass(java_class);
1442 if (!k->is_instance_klass()) {
1443 name = k->name();
1444 name->increment_refcount();
1445 } else {
1446 ResourceMark rm;
1447 const char* sigstr = k->signature_name();
1448 int siglen = (int) strlen(sigstr);
1449 if (!intern_if_not_found) {
1450 name = SymbolTable::probe(sigstr, siglen);
1451 } else {
1452 name = SymbolTable::new_symbol(sigstr, siglen, THREAD);
1453 }
1454 }
1455 }
1456 return name;
1457 }
1458
1459 // Returns the Java name for this Java mirror (Resource allocated)
1460 // See Klass::external_name().
1461 // For primitive type Java mirrors, its type name is returned.
1462 const char* java_lang_Class::as_external_name(oop java_class) {
1463 assert(java_lang_Class::is_instance(java_class), "must be a Class object");
1464 const char* name = NULL;
1465 if (is_primitive(java_class)) {
1466 name = type2name(primitive_type(java_class));
1467 } else {
1513 }
1514 }
1515
1516
1517 oop java_lang_Class::primitive_mirror(BasicType t) {
1518 oop mirror = Universe::java_mirror(t);
1519 assert(mirror != NULL && mirror->is_a(SystemDictionary::Class_klass()), "must be a Class");
1520 assert(java_lang_Class::is_primitive(mirror), "must be primitive");
1521 return mirror;
1522 }
1523
1524 bool java_lang_Class::offsets_computed = false;
1525 int java_lang_Class::classRedefinedCount_offset = -1;
1526
1527 #define CLASS_FIELDS_DO(macro) \
1528 macro(classRedefinedCount_offset, k, "classRedefinedCount", int_signature, false) ; \
1529 macro(_class_loader_offset, k, "classLoader", classloader_signature, false); \
1530 macro(_component_mirror_offset, k, "componentType", class_signature, false); \
1531 macro(_module_offset, k, "module", module_signature, false); \
1532 macro(_name_offset, k, "name", string_signature, false); \
1533
1534 void java_lang_Class::compute_offsets() {
1535 if (offsets_computed) {
1536 return;
1537 }
1538
1539 offsets_computed = true;
1540
1541 InstanceKlass* k = SystemDictionary::Class_klass();
1542 CLASS_FIELDS_DO(FIELD_COMPUTE_OFFSET);
1543
1544 // Init lock is a C union with component_mirror. Only instanceKlass mirrors have
1545 // init_lock and only ArrayKlass mirrors have component_mirror. Since both are oops
1546 // GC treats them the same.
1547 _init_lock_offset = _component_mirror_offset;
1548
1549 CLASS_INJECTED_FIELDS(INJECTED_FIELD_COMPUTE_OFFSET);
1550 }
1551
1552 #if INCLUDE_CDS
3990 }
3991
3992 #if INCLUDE_CDS
3993 void java_lang_System::serialize_offsets(SerializeClosure* f) {
3994 SYSTEM_FIELDS_DO(FIELD_SERIALIZE_OFFSET);
3995 }
3996 #endif
3997
3998 int java_lang_System::in_offset_in_bytes() { return static_in_offset; }
3999 int java_lang_System::out_offset_in_bytes() { return static_out_offset; }
4000 int java_lang_System::err_offset_in_bytes() { return static_err_offset; }
4001
4002 int java_lang_Class::_klass_offset;
4003 int java_lang_Class::_array_klass_offset;
4004 int java_lang_Class::_oop_size_offset;
4005 int java_lang_Class::_static_oop_field_count_offset;
4006 int java_lang_Class::_class_loader_offset;
4007 int java_lang_Class::_module_offset;
4008 int java_lang_Class::_protection_domain_offset;
4009 int java_lang_Class::_component_mirror_offset;
4010 int java_lang_Class::_init_lock_offset;
4011 int java_lang_Class::_signers_offset;
4012 int java_lang_Class::_name_offset;
4013 int java_lang_Class::_source_file_offset;
4014 GrowableArray<Klass*>* java_lang_Class::_fixup_mirror_list = NULL;
4015 GrowableArray<Klass*>* java_lang_Class::_fixup_module_field_list = NULL;
4016 int java_lang_Throwable::backtrace_offset;
4017 int java_lang_Throwable::detailMessage_offset;
4018 int java_lang_Throwable::stackTrace_offset;
4019 int java_lang_Throwable::depth_offset;
4020 int java_lang_Throwable::static_unassigned_stacktrace_offset;
4021 int java_lang_reflect_AccessibleObject::override_offset;
4022 int java_lang_reflect_Method::clazz_offset;
4023 int java_lang_reflect_Method::name_offset;
4024 int java_lang_reflect_Method::returnType_offset;
4025 int java_lang_reflect_Method::parameterTypes_offset;
4026 int java_lang_reflect_Method::exceptionTypes_offset;
4027 int java_lang_reflect_Method::slot_offset;
4028 int java_lang_reflect_Method::modifiers_offset;
4029 int java_lang_reflect_Method::signature_offset;
|
33 #include "code/dependencyContext.hpp"
34 #include "code/pcDesc.hpp"
35 #include "interpreter/interpreter.hpp"
36 #include "interpreter/linkResolver.hpp"
37 #include "logging/log.hpp"
38 #include "logging/logStream.hpp"
39 #include "memory/heapShared.inline.hpp"
40 #include "memory/metaspaceShared.hpp"
41 #include "memory/oopFactory.hpp"
42 #include "memory/resourceArea.hpp"
43 #include "memory/universe.hpp"
44 #include "oops/fieldStreams.hpp"
45 #include "oops/instanceKlass.hpp"
46 #include "oops/instanceMirrorKlass.hpp"
47 #include "oops/klass.hpp"
48 #include "oops/method.inline.hpp"
49 #include "oops/objArrayOop.inline.hpp"
50 #include "oops/oop.inline.hpp"
51 #include "oops/symbol.hpp"
52 #include "oops/typeArrayOop.inline.hpp"
53 #include "oops/valueArrayKlass.hpp"
54 #include "prims/jvmtiExport.hpp"
55 #include "prims/resolvedMethodTable.hpp"
56 #include "runtime/fieldDescriptor.inline.hpp"
57 #include "runtime/frame.inline.hpp"
58 #include "runtime/handles.inline.hpp"
59 #include "runtime/interfaceSupport.inline.hpp"
60 #include "runtime/java.hpp"
61 #include "runtime/javaCalls.hpp"
62 #include "runtime/jniHandles.inline.hpp"
63 #include "runtime/safepoint.hpp"
64 #include "runtime/safepointVerifiers.hpp"
65 #include "runtime/thread.inline.hpp"
66 #include "runtime/vframe.inline.hpp"
67 #include "utilities/align.hpp"
68 #include "utilities/preserveException.hpp"
69 #if INCLUDE_JVMCI
70 #include "jvmci/jvmciJavaClasses.hpp"
71 #endif
72
73 #define INJECTED_FIELD_COMPUTE_OFFSET(klass, name, signature, may_be_java) \
892 int computed_modifiers = k->compute_modifier_flags(CHECK);
893 k->set_modifier_flags(computed_modifiers);
894 // Class_klass has to be loaded because it is used to allocate
895 // the mirror.
896 if (SystemDictionary::Class_klass_loaded()) {
897 // Allocate mirror (java.lang.Class instance)
898 oop mirror_oop = InstanceMirrorKlass::cast(SystemDictionary::Class_klass())->allocate_instance(k, CHECK);
899 Handle mirror(THREAD, mirror_oop);
900 Handle comp_mirror;
901
902 // Setup indirection from mirror->klass
903 java_lang_Class::set_klass(mirror(), k);
904
905 InstanceMirrorKlass* mk = InstanceMirrorKlass::cast(mirror->klass());
906 assert(oop_size(mirror()) == mk->instance_size(k), "should have been set");
907
908 java_lang_Class::set_static_oop_field_count(mirror(), mk->compute_static_oop_field_count(mirror()));
909
910 // It might also have a component mirror. This mirror must already exist.
911 if (k->is_array_klass()) {
912 if (k->is_valueArray_klass()) {
913 Klass* element_klass = (Klass*) ValueArrayKlass::cast(k)->element_klass();
914 if (element_klass->is_value()) {
915 ValueKlass* vk = ValueKlass::cast(InstanceKlass::cast(element_klass));
916 comp_mirror = Handle(THREAD, vk->value_mirror());
917 } else {
918 comp_mirror = Handle(THREAD, element_klass->java_mirror());
919 }
920 }
921 else if (k->is_typeArray_klass()) {
922 BasicType type = TypeArrayKlass::cast(k)->element_type();
923 comp_mirror = Handle(THREAD, Universe::java_mirror(type));
924 } else {
925 assert(k->is_objArray_klass(), "Must be");
926 Klass* element_klass = ObjArrayKlass::cast(k)->element_klass();
927 assert(element_klass != NULL, "Must have an element klass");
928 if (element_klass->is_value()) {
929 ValueKlass* vk = ValueKlass::cast(InstanceKlass::cast(element_klass));
930 comp_mirror = Handle(THREAD, vk->value_mirror());
931 } else {
932 comp_mirror = Handle(THREAD, element_klass->java_mirror());
933 }
934 }
935 assert(comp_mirror() != NULL, "must have a mirror");
936
937 // Two-way link between the array klass and its component mirror:
938 // (array_klass) k -> mirror -> component_mirror -> array_klass -> k
939 set_component_mirror(mirror(), comp_mirror());
940 // See below for ordering dependencies between field array_klass in component mirror
941 // and java_mirror in this klass.
942 } else {
943 assert(k->is_instance_klass(), "Must be");
944
945 initialize_mirror_fields(k, mirror, protection_domain, THREAD);
946 if (HAS_PENDING_EXCEPTION) {
947 // If any of the fields throws an exception like OOM remove the klass field
948 // from the mirror so GC doesn't follow it after the klass has been deallocated.
949 // This mirror looks like a primitive type, which logically it is because it
950 // it represents no class.
951 java_lang_Class::set_klass(mirror(), NULL);
952 return;
953 }
954 }
955
956 // set the classLoader field in the java_lang_Class instance
957 assert(oopDesc::equals(class_loader(), k->class_loader()), "should be same");
958 set_class_loader(mirror(), class_loader());
959
960 // Setup indirection from klass->mirror
961 // after any exceptions can happen during allocations.
962 k->set_java_mirror(mirror);
963
964 // Set the module field in the java_lang_Class instance. This must be done
965 // after the mirror is set.
966 set_mirror_module_field(k, mirror, module, THREAD);
967
968 if (comp_mirror() != NULL) {
969 // Set after k->java_mirror() is published, because compiled code running
970 // concurrently doesn't expect a k to have a null java_mirror.
971 release_set_array_klass(comp_mirror(), k);
972 }
973
974 if (k->is_value()) {
975 // create the secondary mirror for value class
976 oop value_mirror_oop = create_value_mirror(k, mirror, CHECK);
977 set_box_mirror(mirror(), mirror());
978 set_value_mirror(mirror(), value_mirror_oop);
979 }
980 } else {
981 assert(fixup_mirror_list() != NULL, "fixup_mirror_list not initialized");
982 fixup_mirror_list()->push(k);
983 }
984 }
985
986 // Create the secondary mirror for value type. Sets all the fields of this java.lang.Class
987 // instance with the same value as the primary mirror except signers.
988 // Class::setSigners and getSigners will use the primary mirror when passed to the JVM.
989 oop java_lang_Class::create_value_mirror(Klass* k, Handle mirror, TRAPS) {
990 // Allocate mirror (java.lang.Class instance)
991 oop mirror_oop = InstanceMirrorKlass::cast(SystemDictionary::Class_klass())->allocate_instance(k, CHECK_0);
992 Handle value_mirror(THREAD, mirror_oop);
993
994 java_lang_Class::set_klass(value_mirror(), k);
995 java_lang_Class::set_static_oop_field_count(value_mirror(), static_oop_field_count(mirror()));
996 // ## do we need to set init lock?
997 java_lang_Class::set_init_lock(value_mirror(), init_lock(mirror()));
998
999 if (k->is_array_klass()) {
1000 assert(component_mirror(mirror()) != NULL, "must have a mirror");
1001 set_component_mirror(value_mirror(), component_mirror(mirror()));
1002 }
1003
1004 set_protection_domain(value_mirror(), protection_domain(mirror()));
1005 set_class_loader(value_mirror(), class_loader(mirror()));
1006 // ## handle if java.base is not yet defined
1007 set_module(value_mirror(), module(mirror()));
1008 set_box_mirror(value_mirror(), mirror());
1009 set_value_mirror(value_mirror(), value_mirror());
1010 return value_mirror();
1011 }
1012
1013 #if INCLUDE_CDS_JAVA_HEAP
1014 // Clears mirror fields. Static final fields with initial values are reloaded
1015 // from constant pool. The object identity hash is in the object header and is
1016 // not affected.
1017 class ResetMirrorField: public FieldClosure {
1018 private:
1019 Handle _m;
1020
1021 public:
1022 ResetMirrorField(Handle mirror) : _m(mirror) {}
1023
1024 void do_field(fieldDescriptor* fd) {
1025 assert(DumpSharedSpaces, "dump time only");
1026 assert(_m.not_null(), "Mirror cannot be NULL");
1027
1028 if (fd->is_static() && fd->has_initial_value()) {
1029 initialize_static_field(fd, _m, Thread::current());
1030 return;
1031 }
1032
1400 oop java_lang_Class::name(Handle java_class, TRAPS) {
1401 assert(_name_offset != 0, "must be set");
1402 oop o = java_class->obj_field(_name_offset);
1403 if (o == NULL) {
1404 o = StringTable::intern(java_lang_Class::as_external_name(java_class()), THREAD);
1405 java_class->obj_field_put(_name_offset, o);
1406 }
1407 return o;
1408 }
1409
1410 oop java_lang_Class::source_file(oop java_class) {
1411 assert(_source_file_offset != 0, "must be set");
1412 return java_class->obj_field(_source_file_offset);
1413 }
1414
1415 void java_lang_Class::set_source_file(oop java_class, oop source_file) {
1416 assert(_source_file_offset != 0, "must be set");
1417 java_class->obj_field_put(_source_file_offset, source_file);
1418 }
1419
1420 oop java_lang_Class::value_mirror(oop java_class) {
1421 assert(_value_mirror_offset != 0, "must be set");
1422 return java_class->obj_field(_value_mirror_offset);
1423 }
1424
1425 void java_lang_Class::set_value_mirror(oop java_class, oop mirror) {
1426 assert(_value_mirror_offset != 0, "must be set");
1427 java_class->obj_field_put(_value_mirror_offset, mirror);
1428 }
1429
1430 oop java_lang_Class::box_mirror(oop java_class) {
1431 assert(_box_mirror_offset != 0, "must be set");
1432 return java_class->obj_field(_box_mirror_offset);
1433 }
1434
1435 void java_lang_Class::set_box_mirror(oop java_class, oop mirror) {
1436 assert(_box_mirror_offset != 0, "must be set");
1437 java_class->obj_field_put(_box_mirror_offset, mirror);
1438 }
1439
1440 oop java_lang_Class::create_basic_type_mirror(const char* basic_type_name, BasicType type, TRAPS) {
1441 // This should be improved by adding a field at the Java level or by
1442 // introducing a new VM klass (see comment in ClassFileParser)
1443 oop java_class = InstanceMirrorKlass::cast(SystemDictionary::Class_klass())->allocate_instance(NULL, CHECK_0);
1444 if (type != T_VOID) {
1445 Klass* aklass = Universe::typeArrayKlassObj(type);
1446 assert(aklass != NULL, "correct bootstrap");
1447 release_set_array_klass(java_class, aklass);
1448 }
1449 #ifdef ASSERT
1450 InstanceMirrorKlass* mk = InstanceMirrorKlass::cast(SystemDictionary::Class_klass());
1451 assert(java_lang_Class::static_oop_field_count(java_class) == 0, "should have been zeroed by allocation");
1452 #endif
1453 return java_class;
1454 }
1455
1456
1457 Klass* java_lang_Class::as_Klass(oop java_class) {
1458 //%note memory_2
1459 assert(java_lang_Class::is_instance(java_class), "must be a Class object");
1464
1465 Klass* java_lang_Class::as_Klass_raw(oop java_class) {
1466 //%note memory_2
1467 assert(java_lang_Class::is_instance(java_class), "must be a Class object");
1468 Klass* k = ((Klass*)java_class->metadata_field_raw(_klass_offset));
1469 assert(k == NULL || k->is_klass(), "type check");
1470 return k;
1471 }
1472
1473
1474 void java_lang_Class::set_klass(oop java_class, Klass* klass) {
1475 assert(java_lang_Class::is_instance(java_class), "must be a Class object");
1476 java_class->metadata_field_put(_klass_offset, klass);
1477 }
1478
1479
1480 void java_lang_Class::print_signature(oop java_class, outputStream* st) {
1481 assert(java_lang_Class::is_instance(java_class), "must be a Class object");
1482 Symbol* name = NULL;
1483 bool is_instance = false;
1484 bool is_value = false;
1485 if (is_primitive(java_class)) {
1486 name = vmSymbols::type_signature(primitive_type(java_class));
1487 } else {
1488 Klass* k = as_Klass(java_class);
1489 is_instance = k->is_instance_klass();
1490 is_value = k->is_value();
1491 name = k->name();
1492 }
1493 if (name == NULL) {
1494 st->print("<null>");
1495 return;
1496 }
1497 if (is_instance) {
1498 if (is_value && (java_class == value_mirror(java_class))) {
1499 st->print("Q");
1500 } else {
1501 st->print("L");
1502 }
1503 }
1504 st->write((char*) name->base(), (int) name->utf8_length());
1505 if (is_instance) st->print(";");
1506 }
1507
1508 Symbol* java_lang_Class::as_signature(oop java_class, bool intern_if_not_found, TRAPS) {
1509 assert(java_lang_Class::is_instance(java_class), "must be a Class object");
1510 Symbol* name;
1511 if (is_primitive(java_class)) {
1512 name = vmSymbols::type_signature(primitive_type(java_class));
1513 // Because this can create a new symbol, the caller has to decrement
1514 // the refcount, so make adjustment here and below for symbols returned
1515 // that are not created or incremented due to a successful lookup.
1516 name->increment_refcount();
1517 } else {
1518 Klass* k = as_Klass(java_class);
1519 if (!k->is_instance_klass()) {
1520 name = k->name();
1521 name->increment_refcount();
1522 } else {
1523 ResourceMark rm;
1524 const char* sigstr;
1525 if (k->is_value()) {
1526 char c = (java_class == value_mirror(java_class)) ? 'Q' : 'L';
1527 sigstr = InstanceKlass::cast(k)->signature_name_of(c);
1528 } else {
1529 sigstr = k->signature_name();
1530 }
1531 int siglen = (int) strlen(sigstr);
1532 if (!intern_if_not_found) {
1533 name = SymbolTable::probe(sigstr, siglen);
1534 } else {
1535 name = SymbolTable::new_symbol(sigstr, siglen, THREAD);
1536 }
1537 }
1538 }
1539 return name;
1540 }
1541
1542 // Returns the Java name for this Java mirror (Resource allocated)
1543 // See Klass::external_name().
1544 // For primitive type Java mirrors, its type name is returned.
1545 const char* java_lang_Class::as_external_name(oop java_class) {
1546 assert(java_lang_Class::is_instance(java_class), "must be a Class object");
1547 const char* name = NULL;
1548 if (is_primitive(java_class)) {
1549 name = type2name(primitive_type(java_class));
1550 } else {
1596 }
1597 }
1598
1599
1600 oop java_lang_Class::primitive_mirror(BasicType t) {
1601 oop mirror = Universe::java_mirror(t);
1602 assert(mirror != NULL && mirror->is_a(SystemDictionary::Class_klass()), "must be a Class");
1603 assert(java_lang_Class::is_primitive(mirror), "must be primitive");
1604 return mirror;
1605 }
1606
1607 bool java_lang_Class::offsets_computed = false;
1608 int java_lang_Class::classRedefinedCount_offset = -1;
1609
1610 #define CLASS_FIELDS_DO(macro) \
1611 macro(classRedefinedCount_offset, k, "classRedefinedCount", int_signature, false) ; \
1612 macro(_class_loader_offset, k, "classLoader", classloader_signature, false); \
1613 macro(_component_mirror_offset, k, "componentType", class_signature, false); \
1614 macro(_module_offset, k, "module", module_signature, false); \
1615 macro(_name_offset, k, "name", string_signature, false); \
1616 macro(_box_mirror_offset, k, "boxType", class_signature, false); \
1617 macro(_value_mirror_offset, k, "valueType", class_signature, false); \
1618
1619 void java_lang_Class::compute_offsets() {
1620 if (offsets_computed) {
1621 return;
1622 }
1623
1624 offsets_computed = true;
1625
1626 InstanceKlass* k = SystemDictionary::Class_klass();
1627 CLASS_FIELDS_DO(FIELD_COMPUTE_OFFSET);
1628
1629 // Init lock is a C union with component_mirror. Only instanceKlass mirrors have
1630 // init_lock and only ArrayKlass mirrors have component_mirror. Since both are oops
1631 // GC treats them the same.
1632 _init_lock_offset = _component_mirror_offset;
1633
1634 CLASS_INJECTED_FIELDS(INJECTED_FIELD_COMPUTE_OFFSET);
1635 }
1636
1637 #if INCLUDE_CDS
4075 }
4076
4077 #if INCLUDE_CDS
4078 void java_lang_System::serialize_offsets(SerializeClosure* f) {
4079 SYSTEM_FIELDS_DO(FIELD_SERIALIZE_OFFSET);
4080 }
4081 #endif
4082
4083 int java_lang_System::in_offset_in_bytes() { return static_in_offset; }
4084 int java_lang_System::out_offset_in_bytes() { return static_out_offset; }
4085 int java_lang_System::err_offset_in_bytes() { return static_err_offset; }
4086
4087 int java_lang_Class::_klass_offset;
4088 int java_lang_Class::_array_klass_offset;
4089 int java_lang_Class::_oop_size_offset;
4090 int java_lang_Class::_static_oop_field_count_offset;
4091 int java_lang_Class::_class_loader_offset;
4092 int java_lang_Class::_module_offset;
4093 int java_lang_Class::_protection_domain_offset;
4094 int java_lang_Class::_component_mirror_offset;
4095 int java_lang_Class::_box_mirror_offset;
4096 int java_lang_Class::_value_mirror_offset;
4097 int java_lang_Class::_init_lock_offset;
4098 int java_lang_Class::_signers_offset;
4099 int java_lang_Class::_name_offset;
4100 int java_lang_Class::_source_file_offset;
4101 GrowableArray<Klass*>* java_lang_Class::_fixup_mirror_list = NULL;
4102 GrowableArray<Klass*>* java_lang_Class::_fixup_module_field_list = NULL;
4103 int java_lang_Throwable::backtrace_offset;
4104 int java_lang_Throwable::detailMessage_offset;
4105 int java_lang_Throwable::stackTrace_offset;
4106 int java_lang_Throwable::depth_offset;
4107 int java_lang_Throwable::static_unassigned_stacktrace_offset;
4108 int java_lang_reflect_AccessibleObject::override_offset;
4109 int java_lang_reflect_Method::clazz_offset;
4110 int java_lang_reflect_Method::name_offset;
4111 int java_lang_reflect_Method::returnType_offset;
4112 int java_lang_reflect_Method::parameterTypes_offset;
4113 int java_lang_reflect_Method::exceptionTypes_offset;
4114 int java_lang_reflect_Method::slot_offset;
4115 int java_lang_reflect_Method::modifiers_offset;
4116 int java_lang_reflect_Method::signature_offset;
|