1.1 --- a/src/share/vm/classfile/classFileParser.cpp Thu Dec 04 14:34:11 2014 +0100 1.2 +++ b/src/share/vm/classfile/classFileParser.cpp Thu Dec 11 11:13:13 2014 +0100 1.3 @@ -3058,21 +3058,39 @@ 1.4 } 1.5 } 1.6 1.7 -// Transfer ownership of metadata allocated to the InstanceKlass. 1.8 -void ClassFileParser::apply_parsed_class_metadata( 1.9 - instanceKlassHandle this_klass, 1.10 - int java_fields_count, TRAPS) { 1.11 - // Assign annotations if needed 1.12 - if (_annotations != NULL || _type_annotations != NULL || 1.13 - _fields_annotations != NULL || _fields_type_annotations != NULL) { 1.14 +// Create the Annotations object that will 1.15 +// hold the annotations array for the Klass. 1.16 +void ClassFileParser::create_combined_annotations(TRAPS) { 1.17 + if (_annotations == NULL && 1.18 + _type_annotations == NULL && 1.19 + _fields_annotations == NULL && 1.20 + _fields_type_annotations == NULL) { 1.21 + // Don't create the Annotations object unnecessarily. 1.22 + return; 1.23 + } 1.24 + 1.25 Annotations* annotations = Annotations::allocate(_loader_data, CHECK); 1.26 annotations->set_class_annotations(_annotations); 1.27 annotations->set_class_type_annotations(_type_annotations); 1.28 annotations->set_fields_annotations(_fields_annotations); 1.29 annotations->set_fields_type_annotations(_fields_type_annotations); 1.30 - this_klass->set_annotations(annotations); 1.31 - } 1.32 - 1.33 + 1.34 + // This is the Annotations object that will be 1.35 + // assigned to InstanceKlass being constructed. 1.36 + _combined_annotations = annotations; 1.37 + 1.38 + // The annotations arrays below has been transfered the 1.39 + // _combined_annotations so these fields can now be cleared. 1.40 + _annotations = NULL; 1.41 + _type_annotations = NULL; 1.42 + _fields_annotations = NULL; 1.43 + _fields_type_annotations = NULL; 1.44 +} 1.45 + 1.46 +// Transfer ownership of metadata allocated to the InstanceKlass. 1.47 +void ClassFileParser::apply_parsed_class_metadata( 1.48 + instanceKlassHandle this_klass, 1.49 + int java_fields_count, TRAPS) { 1.50 _cp->set_pool_holder(this_klass()); 1.51 this_klass->set_constants(_cp); 1.52 this_klass->set_fields(_fields, java_fields_count); 1.53 @@ -3080,6 +3098,7 @@ 1.54 this_klass->set_inner_classes(_inner_classes); 1.55 this_klass->set_local_interfaces(_local_interfaces); 1.56 this_klass->set_transitive_interfaces(_transitive_interfaces); 1.57 + this_klass->set_annotations(_combined_annotations); 1.58 1.59 // Clear out these fields so they don't get deallocated by the destructor 1.60 clear_class_metadata(); 1.61 @@ -3939,6 +3958,10 @@ 1.62 ClassAnnotationCollector parsed_annotations; 1.63 parse_classfile_attributes(&parsed_annotations, CHECK_(nullHandle)); 1.64 1.65 + // Finalize the Annotations metadata object, 1.66 + // now that all annotation arrays have been created. 1.67 + create_combined_annotations(CHECK_(nullHandle)); 1.68 + 1.69 // Make sure this is the end of class file stream 1.70 guarantee_property(cfs->at_eos(), "Extra bytes at the end of class file %s", CHECK_(nullHandle)); 1.71 1.72 @@ -4239,10 +4262,27 @@ 1.73 InstanceKlass::deallocate_interfaces(_loader_data, _super_klass(), 1.74 _local_interfaces, _transitive_interfaces); 1.75 1.76 - MetadataFactory::free_array<u1>(_loader_data, _annotations); 1.77 - MetadataFactory::free_array<u1>(_loader_data, _type_annotations); 1.78 - Annotations::free_contents(_loader_data, _fields_annotations); 1.79 - Annotations::free_contents(_loader_data, _fields_type_annotations); 1.80 + if (_combined_annotations != NULL) { 1.81 + // After all annotations arrays have been created, they are installed into the 1.82 + // Annotations object that will be assigned to the InstanceKlass being created. 1.83 + 1.84 + // Deallocate the Annotations object and the installed annotations arrays. 1.85 + _combined_annotations->deallocate_contents(_loader_data); 1.86 + 1.87 + // If the _combined_annotations pointer is non-NULL, 1.88 + // then the other annotations fields should have been cleared. 1.89 + assert(_annotations == NULL, "Should have been cleared"); 1.90 + assert(_type_annotations == NULL, "Should have been cleared"); 1.91 + assert(_fields_annotations == NULL, "Should have been cleared"); 1.92 + assert(_fields_type_annotations == NULL, "Should have been cleared"); 1.93 + } else { 1.94 + // If the annotations arrays were not installed into the Annotations object, 1.95 + // then they have to be deallocated explicitly. 1.96 + MetadataFactory::free_array<u1>(_loader_data, _annotations); 1.97 + MetadataFactory::free_array<u1>(_loader_data, _type_annotations); 1.98 + Annotations::free_contents(_loader_data, _fields_annotations); 1.99 + Annotations::free_contents(_loader_data, _fields_type_annotations); 1.100 + } 1.101 1.102 clear_class_metadata(); 1.103