comparison src/share/vm/classfile/classFileParser.cpp @ 6222:6d8f36bcef55

6711908: JVM needs direct access to some annotations Summary: Add annotation extraction code to class file parser. Reviewed-by: twisti, jrose, kvn Contributed-by: michael.haupt@oracle.com
author jrose
date Thu, 12 Jul 2012 00:39:53 -0700
parents 71afdabfd05b
children 9c9fb30d2b3b
comparison
equal deleted inserted replaced
6207:ae9241bbce4a 6222:6d8f36bcef55
315 } 315 }
316 void set_in_error(bool clean) { _in_error = clean; } 316 void set_in_error(bool clean) { _in_error = clean; }
317 }; 317 };
318 318
319 bool inline valid_cp_range(int index, int length) { return (index > 0 && index < length); } 319 bool inline valid_cp_range(int index, int length) { return (index > 0 && index < length); }
320
321 inline Symbol* check_symbol_at(constantPoolHandle cp, int index) {
322 if (valid_cp_range(index, cp->length()) && cp->tag_at(index).is_utf8())
323 return cp->symbol_at(index);
324 else
325 return NULL;
326 }
320 327
321 constantPoolHandle ClassFileParser::parse_constant_pool(Handle class_loader, TRAPS) { 328 constantPoolHandle ClassFileParser::parse_constant_pool(Handle class_loader, TRAPS) {
322 ClassFileStream* cfs = stream(); 329 ClassFileStream* cfs = stream();
323 constantPoolHandle nullHandle; 330 constantPoolHandle nullHandle;
324 331
900 bool is_static, u2 signature_index, 907 bool is_static, u2 signature_index,
901 u2* constantvalue_index_addr, 908 u2* constantvalue_index_addr,
902 bool* is_synthetic_addr, 909 bool* is_synthetic_addr,
903 u2* generic_signature_index_addr, 910 u2* generic_signature_index_addr,
904 typeArrayHandle* field_annotations, 911 typeArrayHandle* field_annotations,
912 ClassFileParser::FieldAnnotationCollector* parsed_annotations,
905 TRAPS) { 913 TRAPS) {
906 ClassFileStream* cfs = stream(); 914 ClassFileStream* cfs = stream();
907 assert(attributes_count > 0, "length should be greater than 0"); 915 assert(attributes_count > 0, "length should be greater than 0");
908 u2 constantvalue_index = 0; 916 u2 constantvalue_index = 0;
909 u2 generic_signature_index = 0; 917 u2 generic_signature_index = 0;
1140 1148
1141 u2 constantvalue_index = 0; 1149 u2 constantvalue_index = 0;
1142 bool is_synthetic = false; 1150 bool is_synthetic = false;
1143 u2 generic_signature_index = 0; 1151 u2 generic_signature_index = 0;
1144 bool is_static = access_flags.is_static(); 1152 bool is_static = access_flags.is_static();
1153 FieldAnnotationCollector parsed_annotations;
1145 1154
1146 u2 attributes_count = cfs->get_u2_fast(); 1155 u2 attributes_count = cfs->get_u2_fast();
1147 if (attributes_count > 0) { 1156 if (attributes_count > 0) {
1148 parse_field_attributes(cp, attributes_count, is_static, signature_index, 1157 parse_field_attributes(cp, attributes_count, is_static, signature_index,
1149 &constantvalue_index, &is_synthetic, 1158 &constantvalue_index, &is_synthetic,
1150 &generic_signature_index, &field_annotations, 1159 &generic_signature_index, &field_annotations,
1160 &parsed_annotations,
1151 CHECK_(nullHandle)); 1161 CHECK_(nullHandle));
1152 if (field_annotations.not_null()) { 1162 if (field_annotations.not_null()) {
1153 if (fields_annotations->is_null()) { 1163 if (fields_annotations->is_null()) {
1154 objArrayOop md = oopFactory::new_system_objArray(length, CHECK_(nullHandle)); 1164 objArrayOop md = oopFactory::new_system_objArray(length, CHECK_(nullHandle));
1155 *fields_annotations = objArrayHandle(THREAD, md); 1165 *fields_annotations = objArrayHandle(THREAD, md);
1171 field->initialize(access_flags.as_short(), 1181 field->initialize(access_flags.as_short(),
1172 name_index, 1182 name_index,
1173 signature_index, 1183 signature_index,
1174 constantvalue_index, 1184 constantvalue_index,
1175 0); 1185 0);
1186 if (parsed_annotations.has_any_annotations())
1187 parsed_annotations.apply_to(field);
1176 1188
1177 BasicType type = cp->basic_type_for_signature_at(signature_index); 1189 BasicType type = cp->basic_type_for_signature_at(signature_index);
1178 1190
1179 // Remember how many oops we encountered and compute allocation type 1191 // Remember how many oops we encountered and compute allocation type
1180 FieldAllocationType atype = fac->update(is_static, type); 1192 FieldAllocationType atype = fac->update(is_static, type);
1636 vmSymbols::java_lang_ClassFormatError(), 1648 vmSymbols::java_lang_ClassFormatError(),
1637 "%s \"%s\" in class %s has illegal signature \"%s\"", type, 1649 "%s \"%s\" in class %s has illegal signature \"%s\"", type,
1638 name->as_C_string(), _class_name->as_C_string(), sig->as_C_string()); 1650 name->as_C_string(), _class_name->as_C_string(), sig->as_C_string());
1639 } 1651 }
1640 1652
1653 // Skip an annotation. Return >=limit if there is any problem.
1654 int ClassFileParser::skip_annotation(u1* buffer, int limit, int index) {
1655 // annotation := atype:u2 do(nmem:u2) {member:u2 value}
1656 // value := switch (tag:u1) { ... }
1657 index += 2; // skip atype
1658 if ((index += 2) >= limit) return limit; // read nmem
1659 int nmem = Bytes::get_Java_u2(buffer+index-2);
1660 while (--nmem >= 0 && index < limit) {
1661 index += 2; // skip member
1662 index = skip_annotation_value(buffer, limit, index);
1663 }
1664 return index;
1665 }
1666
1667 // Skip an annotation value. Return >=limit if there is any problem.
1668 int ClassFileParser::skip_annotation_value(u1* buffer, int limit, int index) {
1669 // value := switch (tag:u1) {
1670 // case B, C, I, S, Z, D, F, J, c: con:u2;
1671 // case e: e_class:u2 e_name:u2;
1672 // case s: s_con:u2;
1673 // case [: do(nval:u2) {value};
1674 // case @: annotation;
1675 // case s: s_con:u2;
1676 // }
1677 if ((index += 1) >= limit) return limit; // read tag
1678 u1 tag = buffer[index-1];
1679 switch (tag) {
1680 case 'B': case 'C': case 'I': case 'S': case 'Z':
1681 case 'D': case 'F': case 'J': case 'c': case 's':
1682 index += 2; // skip con or s_con
1683 break;
1684 case 'e':
1685 index += 4; // skip e_class, e_name
1686 break;
1687 case '[':
1688 {
1689 if ((index += 2) >= limit) return limit; // read nval
1690 int nval = Bytes::get_Java_u2(buffer+index-2);
1691 while (--nval >= 0 && index < limit) {
1692 index = skip_annotation_value(buffer, limit, index);
1693 }
1694 }
1695 break;
1696 case '@':
1697 index = skip_annotation(buffer, limit, index);
1698 break;
1699 default:
1700 assert(false, "annotation tag");
1701 return limit; // bad tag byte
1702 }
1703 return index;
1704 }
1705
1706 // Sift through annotations, looking for those significant to the VM:
1707 void ClassFileParser::parse_annotations(u1* buffer, int limit,
1708 constantPoolHandle cp,
1709 ClassFileParser::AnnotationCollector* coll,
1710 TRAPS) {
1711 // annotations := do(nann:u2) {annotation}
1712 int index = 0;
1713 if ((index += 2) >= limit) return; // read nann
1714 int nann = Bytes::get_Java_u2(buffer+index-2);
1715 enum { // initial annotation layout
1716 atype_off = 0, // utf8 such as 'Ljava/lang/annotation/Retention;'
1717 count_off = 2, // u2 such as 1 (one value)
1718 member_off = 4, // utf8 such as 'value'
1719 tag_off = 6, // u1 such as 'c' (type) or 'e' (enum)
1720 e_tag_val = 'e',
1721 e_type_off = 7, // utf8 such as 'Ljava/lang/annotation/RetentionPolicy;'
1722 e_con_off = 9, // utf8 payload, such as 'SOURCE', 'CLASS', 'RUNTIME'
1723 e_size = 11, // end of 'e' annotation
1724 c_tag_val = 'c',
1725 c_con_off = 7, // utf8 payload, such as 'I' or 'Ljava/lang/String;'
1726 c_size = 9, // end of 'c' annotation
1727 min_size = 6 // smallest possible size (zero members)
1728 };
1729 while ((--nann) >= 0 && (index-2 + min_size <= limit)) {
1730 int index0 = index;
1731 index = skip_annotation(buffer, limit, index);
1732 u1* abase = buffer + index0;
1733 int atype = Bytes::get_Java_u2(abase + atype_off);
1734 int count = Bytes::get_Java_u2(abase + count_off);
1735 Symbol* aname = check_symbol_at(cp, atype);
1736 if (aname == NULL) break; // invalid annotation name
1737 Symbol* member = NULL;
1738 if (count >= 1) {
1739 int member_index = Bytes::get_Java_u2(abase + member_off);
1740 member = check_symbol_at(cp, member_index);
1741 if (member == NULL) break; // invalid member name
1742 }
1743
1744 // Here is where parsing particular annotations will take place.
1745 AnnotationCollector::ID id = coll->annotation_index(aname);
1746 if (id == AnnotationCollector::_unknown) continue;
1747 coll->set_annotation(id);
1748 // If there are no values, just set the bit and move on:
1749 if (count == 0) continue;
1750
1751 // For the record, here is how annotation payloads can be collected.
1752 // Suppose we want to capture @Retention.value. Here is how:
1753 //if (id == AnnotationCollector::_class_Retention) {
1754 // Symbol* payload = NULL;
1755 // if (count == 1
1756 // && e_size == (index0 - index) // match size
1757 // && e_tag_val == *(abase + tag_off)
1758 // && (check_symbol_at(cp, Bytes::get_Java_u2(abase + e_type_off))
1759 // == vmSymbols::RetentionPolicy_signature())
1760 // && member == vmSymbols::value_name()) {
1761 // payload = check_symbol_at(cp, Bytes::get_Java_u2(abase + e_con_off));
1762 // }
1763 // check_property(payload != NULL,
1764 // "Invalid @Retention annotation at offset %u in class file %s",
1765 // index0, CHECK);
1766 // if (payload != NULL) {
1767 // payload->increment_refcount();
1768 // coll->_class_RetentionPolicy = payload;
1769 // }
1770 //}
1771 }
1772 }
1773
1774 ClassFileParser::AnnotationCollector::ID ClassFileParser::AnnotationCollector::annotation_index(Symbol* name) {
1775 vmSymbols::SID sid = vmSymbols::find_sid(name);
1776 switch (sid) {
1777 case vmSymbols::VM_SYMBOL_ENUM_NAME(java_lang_invoke_ForceInline_signature):
1778 if (_location != _in_method) break; // only allow for methods
1779 return _method_ForceInline;
1780 default: break;
1781 }
1782 return AnnotationCollector::_unknown;
1783 }
1784
1785 void ClassFileParser::FieldAnnotationCollector::apply_to(FieldInfo* f) {
1786 fatal("no field annotations yet");
1787 }
1788
1789 void ClassFileParser::MethodAnnotationCollector::apply_to(methodHandle m) {
1790 if (has_annotation(_method_ForceInline))
1791 m->set_force_inline(true);
1792 }
1793
1794 void ClassFileParser::ClassAnnotationCollector::apply_to(instanceKlassHandle k) {
1795 fatal("no class annotations yet");
1796 }
1797
1798
1641 #define MAX_ARGS_SIZE 255 1799 #define MAX_ARGS_SIZE 255
1642 #define MAX_CODE_SIZE 65535 1800 #define MAX_CODE_SIZE 65535
1643 #define INITIAL_MAX_LVT_NUMBER 256 1801 #define INITIAL_MAX_LVT_NUMBER 256
1644 1802
1645 // Note: the parse_method below is big and clunky because all parsing of the code and exceptions 1803 // Note: the parse_method below is big and clunky because all parsing of the code and exceptions
1646 // attribute is inlined. This is curbersome to avoid since we inline most of the parts in the 1804 // attribute is inlined. This is cumbersome to avoid since we inline most of the parts in the
1647 // methodOop to save footprint, so we only know the size of the resulting methodOop when the 1805 // methodOop to save footprint, so we only know the size of the resulting methodOop when the
1648 // entire method attribute is parsed. 1806 // entire method attribute is parsed.
1649 // 1807 //
1650 // The promoted_flags parameter is used to pass relevant access_flags 1808 // The promoted_flags parameter is used to pass relevant access_flags
1651 // from the method back up to the containing klass. These flag values 1809 // from the method back up to the containing klass. These flag values
1731 bool parsed_checked_exceptions_attribute = false; 1889 bool parsed_checked_exceptions_attribute = false;
1732 bool parsed_stackmap_attribute = false; 1890 bool parsed_stackmap_attribute = false;
1733 // stackmap attribute - JDK1.5 1891 // stackmap attribute - JDK1.5
1734 typeArrayHandle stackmap_data; 1892 typeArrayHandle stackmap_data;
1735 u2 generic_signature_index = 0; 1893 u2 generic_signature_index = 0;
1894 MethodAnnotationCollector parsed_annotations;
1736 u1* runtime_visible_annotations = NULL; 1895 u1* runtime_visible_annotations = NULL;
1737 int runtime_visible_annotations_length = 0; 1896 int runtime_visible_annotations_length = 0;
1738 u1* runtime_invisible_annotations = NULL; 1897 u1* runtime_invisible_annotations = NULL;
1739 int runtime_invisible_annotations_length = 0; 1898 int runtime_invisible_annotations_length = 0;
1740 u1* runtime_visible_parameter_annotations = NULL; 1899 u1* runtime_visible_parameter_annotations = NULL;
1957 generic_signature_index = cfs->get_u2_fast(); 2116 generic_signature_index = cfs->get_u2_fast();
1958 } else if (method_attribute_name == vmSymbols::tag_runtime_visible_annotations()) { 2117 } else if (method_attribute_name == vmSymbols::tag_runtime_visible_annotations()) {
1959 runtime_visible_annotations_length = method_attribute_length; 2118 runtime_visible_annotations_length = method_attribute_length;
1960 runtime_visible_annotations = cfs->get_u1_buffer(); 2119 runtime_visible_annotations = cfs->get_u1_buffer();
1961 assert(runtime_visible_annotations != NULL, "null visible annotations"); 2120 assert(runtime_visible_annotations != NULL, "null visible annotations");
2121 parse_annotations(runtime_visible_annotations, runtime_visible_annotations_length, cp, &parsed_annotations, CHECK_(nullHandle));
1962 cfs->skip_u1(runtime_visible_annotations_length, CHECK_(nullHandle)); 2122 cfs->skip_u1(runtime_visible_annotations_length, CHECK_(nullHandle));
1963 } else if (PreserveAllAnnotations && method_attribute_name == vmSymbols::tag_runtime_invisible_annotations()) { 2123 } else if (PreserveAllAnnotations && method_attribute_name == vmSymbols::tag_runtime_invisible_annotations()) {
1964 runtime_invisible_annotations_length = method_attribute_length; 2124 runtime_invisible_annotations_length = method_attribute_length;
1965 runtime_invisible_annotations = cfs->get_u1_buffer(); 2125 runtime_invisible_annotations = cfs->get_u1_buffer();
1966 assert(runtime_invisible_annotations != NULL, "null invisible annotations"); 2126 assert(runtime_invisible_annotations != NULL, "null invisible annotations");
2134 } 2294 }
2135 } 2295 }
2136 clear_hashtable(lvt_Hash); 2296 clear_hashtable(lvt_Hash);
2137 } 2297 }
2138 2298
2299 if (parsed_annotations.has_any_annotations())
2300 parsed_annotations.apply_to(m);
2139 *method_annotations = assemble_annotations(runtime_visible_annotations, 2301 *method_annotations = assemble_annotations(runtime_visible_annotations,
2140 runtime_visible_annotations_length, 2302 runtime_visible_annotations_length,
2141 runtime_invisible_annotations, 2303 runtime_invisible_annotations,
2142 runtime_invisible_annotations_length, 2304 runtime_invisible_annotations_length,
2143 CHECK_(nullHandle)); 2305 CHECK_(nullHandle));
2312 return typeArrayHandle(THREAD, Universe::the_empty_int_array()); 2474 return typeArrayHandle(THREAD, Universe::the_empty_int_array());
2313 } 2475 }
2314 } 2476 }
2315 2477
2316 2478
2317 void ClassFileParser::parse_classfile_sourcefile_attribute(constantPoolHandle cp, instanceKlassHandle k, TRAPS) { 2479 void ClassFileParser::parse_classfile_sourcefile_attribute(constantPoolHandle cp, TRAPS) {
2318 ClassFileStream* cfs = stream(); 2480 ClassFileStream* cfs = stream();
2319 cfs->guarantee_more(2, CHECK); // sourcefile_index 2481 cfs->guarantee_more(2, CHECK); // sourcefile_index
2320 u2 sourcefile_index = cfs->get_u2_fast(); 2482 u2 sourcefile_index = cfs->get_u2_fast();
2321 check_property( 2483 check_property(
2322 valid_cp_range(sourcefile_index, cp->length()) && 2484 valid_cp_range(sourcefile_index, cp->length()) &&
2323 cp->tag_at(sourcefile_index).is_utf8(), 2485 cp->tag_at(sourcefile_index).is_utf8(),
2324 "Invalid SourceFile attribute at constant pool index %u in class file %s", 2486 "Invalid SourceFile attribute at constant pool index %u in class file %s",
2325 sourcefile_index, CHECK); 2487 sourcefile_index, CHECK);
2326 k->set_source_file_name(cp->symbol_at(sourcefile_index)); 2488 set_class_sourcefile(cp->symbol_at(sourcefile_index));
2327 } 2489 }
2328 2490
2329 2491
2330 2492
2331 void ClassFileParser::parse_classfile_source_debug_extension_attribute(constantPoolHandle cp, 2493 void ClassFileParser::parse_classfile_source_debug_extension_attribute(constantPoolHandle cp,
2332 instanceKlassHandle k,
2333 int length, TRAPS) { 2494 int length, TRAPS) {
2334 ClassFileStream* cfs = stream(); 2495 ClassFileStream* cfs = stream();
2335 u1* sde_buffer = cfs->get_u1_buffer(); 2496 u1* sde_buffer = cfs->get_u1_buffer();
2336 assert(sde_buffer != NULL, "null sde buffer"); 2497 assert(sde_buffer != NULL, "null sde buffer");
2337 2498
2338 // Don't bother storing it if there is no way to retrieve it 2499 // Don't bother storing it if there is no way to retrieve it
2339 if (JvmtiExport::can_get_source_debug_extension()) { 2500 if (JvmtiExport::can_get_source_debug_extension()) {
2340 // Optimistically assume that only 1 byte UTF format is used 2501 // Optimistically assume that only 1 byte UTF format is used
2341 // (common case) 2502 // (common case)
2342 TempNewSymbol sde_symbol = SymbolTable::new_symbol((const char*)sde_buffer, length, CHECK); 2503 TempNewSymbol sde_symbol = SymbolTable::new_symbol((const char*)sde_buffer, length, CHECK);
2343 k->set_source_debug_extension(sde_symbol);
2344 // Note that set_source_debug_extension() increments the reference count 2504 // Note that set_source_debug_extension() increments the reference count
2345 // for its copy of the Symbol*, so use a TempNewSymbol here. 2505 // for its copy of the Symbol*, so use a TempNewSymbol here.
2506 set_class_sde_symbol(sde_symbol);
2346 } 2507 }
2347 // Got utf8 string, set stream position forward 2508 // Got utf8 string, set stream position forward
2348 cfs->skip_u1(length, CHECK); 2509 cfs->skip_u1(length, CHECK);
2349 } 2510 }
2350 2511
2356 u2 ClassFileParser::parse_classfile_inner_classes_attribute(u1* inner_classes_attribute_start, 2517 u2 ClassFileParser::parse_classfile_inner_classes_attribute(u1* inner_classes_attribute_start,
2357 bool parsed_enclosingmethod_attribute, 2518 bool parsed_enclosingmethod_attribute,
2358 u2 enclosing_method_class_index, 2519 u2 enclosing_method_class_index,
2359 u2 enclosing_method_method_index, 2520 u2 enclosing_method_method_index,
2360 constantPoolHandle cp, 2521 constantPoolHandle cp,
2361 instanceKlassHandle k, TRAPS) { 2522 TRAPS) {
2362 ClassFileStream* cfs = stream(); 2523 ClassFileStream* cfs = stream();
2363 u1* current_mark = cfs->current(); 2524 u1* current_mark = cfs->current();
2364 u2 length = 0; 2525 u2 length = 0;
2365 if (inner_classes_attribute_start != NULL) { 2526 if (inner_classes_attribute_start != NULL) {
2366 cfs->set_current(inner_classes_attribute_start); 2527 cfs->set_current(inner_classes_attribute_start);
2447 inner_classes->short_at_put(index++, enclosing_method_method_index); 2608 inner_classes->short_at_put(index++, enclosing_method_method_index);
2448 } 2609 }
2449 assert(index == size, "wrong size"); 2610 assert(index == size, "wrong size");
2450 2611
2451 // Update instanceKlass with inner class info. 2612 // Update instanceKlass with inner class info.
2452 k->set_inner_classes(inner_classes()); 2613 set_class_inner_classes(inner_classes);
2453 2614
2454 // Restore buffer's current position. 2615 // Restore buffer's current position.
2455 cfs->set_current(current_mark); 2616 cfs->set_current(current_mark);
2456 2617
2457 return length; 2618 return length;
2458 } 2619 }
2459 2620
2460 void ClassFileParser::parse_classfile_synthetic_attribute(constantPoolHandle cp, instanceKlassHandle k, TRAPS) { 2621 void ClassFileParser::parse_classfile_synthetic_attribute(constantPoolHandle cp, TRAPS) {
2461 k->set_is_synthetic(); 2622 set_class_synthetic_flag(true);
2462 } 2623 }
2463 2624
2464 void ClassFileParser::parse_classfile_signature_attribute(constantPoolHandle cp, instanceKlassHandle k, TRAPS) { 2625 void ClassFileParser::parse_classfile_signature_attribute(constantPoolHandle cp, TRAPS) {
2465 ClassFileStream* cfs = stream(); 2626 ClassFileStream* cfs = stream();
2466 u2 signature_index = cfs->get_u2(CHECK); 2627 u2 signature_index = cfs->get_u2(CHECK);
2467 check_property( 2628 check_property(
2468 valid_cp_range(signature_index, cp->length()) && 2629 valid_cp_range(signature_index, cp->length()) &&
2469 cp->tag_at(signature_index).is_utf8(), 2630 cp->tag_at(signature_index).is_utf8(),
2470 "Invalid constant pool index %u in Signature attribute in class file %s", 2631 "Invalid constant pool index %u in Signature attribute in class file %s",
2471 signature_index, CHECK); 2632 signature_index, CHECK);
2472 k->set_generic_signature(cp->symbol_at(signature_index)); 2633 set_class_generic_signature(cp->symbol_at(signature_index));
2473 } 2634 }
2474 2635
2475 void ClassFileParser::parse_classfile_bootstrap_methods_attribute(constantPoolHandle cp, instanceKlassHandle k, 2636 void ClassFileParser::parse_classfile_bootstrap_methods_attribute(constantPoolHandle cp,
2476 u4 attribute_byte_length, TRAPS) { 2637 u4 attribute_byte_length, TRAPS) {
2477 ClassFileStream* cfs = stream(); 2638 ClassFileStream* cfs = stream();
2478 u1* current_start = cfs->current(); 2639 u1* current_start = cfs->current();
2479 2640
2480 cfs->guarantee_more(2, CHECK); // length 2641 cfs->guarantee_more(2, CHECK); // length
2542 2703
2543 cp->set_operands(operands()); 2704 cp->set_operands(operands());
2544 } 2705 }
2545 2706
2546 2707
2547 void ClassFileParser::parse_classfile_attributes(constantPoolHandle cp, instanceKlassHandle k, TRAPS) { 2708 void ClassFileParser::parse_classfile_attributes(constantPoolHandle cp,
2709 ClassFileParser::ClassAnnotationCollector* parsed_annotations,
2710 TRAPS) {
2548 ClassFileStream* cfs = stream(); 2711 ClassFileStream* cfs = stream();
2549 // Set inner classes attribute to default sentinel 2712 // Set inner classes attribute to default sentinel
2550 k->set_inner_classes(Universe::the_empty_short_array()); 2713 set_class_inner_classes(typeArrayHandle(THREAD, Universe::the_empty_short_array()));
2551 cfs->guarantee_more(2, CHECK); // attributes_count 2714 cfs->guarantee_more(2, CHECK); // attributes_count
2552 u2 attributes_count = cfs->get_u2_fast(); 2715 u2 attributes_count = cfs->get_u2_fast();
2553 bool parsed_sourcefile_attribute = false; 2716 bool parsed_sourcefile_attribute = false;
2554 bool parsed_innerclasses_attribute = false; 2717 bool parsed_innerclasses_attribute = false;
2555 bool parsed_enclosingmethod_attribute = false; 2718 bool parsed_enclosingmethod_attribute = false;
2581 if (parsed_sourcefile_attribute) { 2744 if (parsed_sourcefile_attribute) {
2582 classfile_parse_error("Multiple SourceFile attributes in class file %s", CHECK); 2745 classfile_parse_error("Multiple SourceFile attributes in class file %s", CHECK);
2583 } else { 2746 } else {
2584 parsed_sourcefile_attribute = true; 2747 parsed_sourcefile_attribute = true;
2585 } 2748 }
2586 parse_classfile_sourcefile_attribute(cp, k, CHECK); 2749 parse_classfile_sourcefile_attribute(cp, CHECK);
2587 } else if (tag == vmSymbols::tag_source_debug_extension()) { 2750 } else if (tag == vmSymbols::tag_source_debug_extension()) {
2588 // Check for SourceDebugExtension tag 2751 // Check for SourceDebugExtension tag
2589 parse_classfile_source_debug_extension_attribute(cp, k, (int)attribute_length, CHECK); 2752 parse_classfile_source_debug_extension_attribute(cp, (int)attribute_length, CHECK);
2590 } else if (tag == vmSymbols::tag_inner_classes()) { 2753 } else if (tag == vmSymbols::tag_inner_classes()) {
2591 // Check for InnerClasses tag 2754 // Check for InnerClasses tag
2592 if (parsed_innerclasses_attribute) { 2755 if (parsed_innerclasses_attribute) {
2593 classfile_parse_error("Multiple InnerClasses attributes in class file %s", CHECK); 2756 classfile_parse_error("Multiple InnerClasses attributes in class file %s", CHECK);
2594 } else { 2757 } else {
2603 if (attribute_length != 0) { 2766 if (attribute_length != 0) {
2604 classfile_parse_error( 2767 classfile_parse_error(
2605 "Invalid Synthetic classfile attribute length %u in class file %s", 2768 "Invalid Synthetic classfile attribute length %u in class file %s",
2606 attribute_length, CHECK); 2769 attribute_length, CHECK);
2607 } 2770 }
2608 parse_classfile_synthetic_attribute(cp, k, CHECK); 2771 parse_classfile_synthetic_attribute(cp, CHECK);
2609 } else if (tag == vmSymbols::tag_deprecated()) { 2772 } else if (tag == vmSymbols::tag_deprecated()) {
2610 // Check for Deprecatd tag - 4276120 2773 // Check for Deprecatd tag - 4276120
2611 if (attribute_length != 0) { 2774 if (attribute_length != 0) {
2612 classfile_parse_error( 2775 classfile_parse_error(
2613 "Invalid Deprecated classfile attribute length %u in class file %s", 2776 "Invalid Deprecated classfile attribute length %u in class file %s",
2618 if (attribute_length != 2) { 2781 if (attribute_length != 2) {
2619 classfile_parse_error( 2782 classfile_parse_error(
2620 "Wrong Signature attribute length %u in class file %s", 2783 "Wrong Signature attribute length %u in class file %s",
2621 attribute_length, CHECK); 2784 attribute_length, CHECK);
2622 } 2785 }
2623 parse_classfile_signature_attribute(cp, k, CHECK); 2786 parse_classfile_signature_attribute(cp, CHECK);
2624 } else if (tag == vmSymbols::tag_runtime_visible_annotations()) { 2787 } else if (tag == vmSymbols::tag_runtime_visible_annotations()) {
2625 runtime_visible_annotations_length = attribute_length; 2788 runtime_visible_annotations_length = attribute_length;
2626 runtime_visible_annotations = cfs->get_u1_buffer(); 2789 runtime_visible_annotations = cfs->get_u1_buffer();
2627 assert(runtime_visible_annotations != NULL, "null visible annotations"); 2790 assert(runtime_visible_annotations != NULL, "null visible annotations");
2791 parse_annotations(runtime_visible_annotations,
2792 runtime_visible_annotations_length,
2793 cp,
2794 parsed_annotations,
2795 CHECK);
2628 cfs->skip_u1(runtime_visible_annotations_length, CHECK); 2796 cfs->skip_u1(runtime_visible_annotations_length, CHECK);
2629 } else if (PreserveAllAnnotations && tag == vmSymbols::tag_runtime_invisible_annotations()) { 2797 } else if (PreserveAllAnnotations && tag == vmSymbols::tag_runtime_invisible_annotations()) {
2630 runtime_invisible_annotations_length = attribute_length; 2798 runtime_invisible_annotations_length = attribute_length;
2631 runtime_invisible_annotations = cfs->get_u1_buffer(); 2799 runtime_invisible_annotations = cfs->get_u1_buffer();
2632 assert(runtime_invisible_annotations != NULL, "null invisible annotations"); 2800 assert(runtime_invisible_annotations != NULL, "null invisible annotations");
2656 } else if (tag == vmSymbols::tag_bootstrap_methods() && 2824 } else if (tag == vmSymbols::tag_bootstrap_methods() &&
2657 _major_version >= Verifier::INVOKEDYNAMIC_MAJOR_VERSION) { 2825 _major_version >= Verifier::INVOKEDYNAMIC_MAJOR_VERSION) {
2658 if (parsed_bootstrap_methods_attribute) 2826 if (parsed_bootstrap_methods_attribute)
2659 classfile_parse_error("Multiple BootstrapMethods attributes in class file %s", CHECK); 2827 classfile_parse_error("Multiple BootstrapMethods attributes in class file %s", CHECK);
2660 parsed_bootstrap_methods_attribute = true; 2828 parsed_bootstrap_methods_attribute = true;
2661 parse_classfile_bootstrap_methods_attribute(cp, k, attribute_length, CHECK); 2829 parse_classfile_bootstrap_methods_attribute(cp, attribute_length, CHECK);
2662 } else { 2830 } else {
2663 // Unknown attribute 2831 // Unknown attribute
2664 cfs->skip_u1(attribute_length, CHECK); 2832 cfs->skip_u1(attribute_length, CHECK);
2665 } 2833 }
2666 } else { 2834 } else {
2671 typeArrayHandle annotations = assemble_annotations(runtime_visible_annotations, 2839 typeArrayHandle annotations = assemble_annotations(runtime_visible_annotations,
2672 runtime_visible_annotations_length, 2840 runtime_visible_annotations_length,
2673 runtime_invisible_annotations, 2841 runtime_invisible_annotations,
2674 runtime_invisible_annotations_length, 2842 runtime_invisible_annotations_length,
2675 CHECK); 2843 CHECK);
2676 k->set_class_annotations(annotations()); 2844 set_class_annotations(annotations);
2677 2845
2678 if (parsed_innerclasses_attribute || parsed_enclosingmethod_attribute) { 2846 if (parsed_innerclasses_attribute || parsed_enclosingmethod_attribute) {
2679 u2 num_of_classes = parse_classfile_inner_classes_attribute( 2847 u2 num_of_classes = parse_classfile_inner_classes_attribute(
2680 inner_classes_attribute_start, 2848 inner_classes_attribute_start,
2681 parsed_innerclasses_attribute, 2849 parsed_innerclasses_attribute,
2682 enclosing_method_class_index, 2850 enclosing_method_class_index,
2683 enclosing_method_method_index, 2851 enclosing_method_method_index,
2684 cp, k, CHECK); 2852 cp, CHECK);
2685 if (parsed_innerclasses_attribute &&_need_verify && _major_version >= JAVA_1_5_VERSION) { 2853 if (parsed_innerclasses_attribute &&_need_verify && _major_version >= JAVA_1_5_VERSION) {
2686 guarantee_property( 2854 guarantee_property(
2687 inner_classes_attribute_length == sizeof(num_of_classes) + 4 * sizeof(u2) * num_of_classes, 2855 inner_classes_attribute_length == sizeof(num_of_classes) + 4 * sizeof(u2) * num_of_classes,
2688 "Wrong InnerClasses attribute length in class file %s", CHECK); 2856 "Wrong InnerClasses attribute length in class file %s", CHECK);
2689 } 2857 }
2693 guarantee_property(parsed_bootstrap_methods_attribute, 2861 guarantee_property(parsed_bootstrap_methods_attribute,
2694 "Missing BootstrapMethods attribute in class file %s", CHECK); 2862 "Missing BootstrapMethods attribute in class file %s", CHECK);
2695 } 2863 }
2696 } 2864 }
2697 2865
2866 void ClassFileParser::apply_parsed_class_attributes(instanceKlassHandle k) {
2867 if (_synthetic_flag)
2868 k->set_is_synthetic();
2869 if (_sourcefile != NULL) {
2870 _sourcefile->increment_refcount();
2871 k->set_source_file_name(_sourcefile);
2872 }
2873 if (_generic_signature != NULL) {
2874 _generic_signature->increment_refcount();
2875 k->set_generic_signature(_generic_signature);
2876 }
2877 k->set_source_debug_extension(_sde_symbol); // increment_refcount inside
2878 k->set_inner_classes(_inner_classes());
2879 k->set_class_annotations(_annotations());
2880 }
2698 2881
2699 typeArrayHandle ClassFileParser::assemble_annotations(u1* runtime_visible_annotations, 2882 typeArrayHandle ClassFileParser::assemble_annotations(u1* runtime_visible_annotations,
2700 int runtime_visible_annotations_length, 2883 int runtime_visible_annotations_length,
2701 u1* runtime_invisible_annotations, 2884 u1* runtime_invisible_annotations,
2702 int runtime_invisible_annotations_length, TRAPS) { 2885 int runtime_invisible_annotations_length, TRAPS) {
2743 NULL, 2926 NULL,
2744 jt->get_thread_stat()->perf_recursion_counts_addr(), 2927 jt->get_thread_stat()->perf_recursion_counts_addr(),
2745 jt->get_thread_stat()->perf_timers_addr(), 2928 jt->get_thread_stat()->perf_timers_addr(),
2746 PerfClassTraceTime::PARSE_CLASS); 2929 PerfClassTraceTime::PARSE_CLASS);
2747 2930
2748 _has_finalizer = _has_empty_finalizer = _has_vanilla_constructor = false; 2931 init_parsed_class_attributes();
2749 _max_bootstrap_specifier_index = -1;
2750 2932
2751 if (JvmtiExport::should_post_class_file_load_hook()) { 2933 if (JvmtiExport::should_post_class_file_load_hook()) {
2752 // Get the cached class file bytes (if any) from the class that 2934 // Get the cached class file bytes (if any) from the class that
2753 // is being redefined or retransformed. We use jvmti_thread_state() 2935 // is being redefined or retransformed. We use jvmti_thread_state()
2754 // instead of JvmtiThreadState::state_for(jt) so we don't allocate 2936 // instead of JvmtiThreadState::state_for(jt) so we don't allocate
2976 CHECK_(nullHandle)); 3158 CHECK_(nullHandle));
2977 3159
2978 objArrayHandle methods_annotations(THREAD, methods_annotations_oop); 3160 objArrayHandle methods_annotations(THREAD, methods_annotations_oop);
2979 objArrayHandle methods_parameter_annotations(THREAD, methods_parameter_annotations_oop); 3161 objArrayHandle methods_parameter_annotations(THREAD, methods_parameter_annotations_oop);
2980 objArrayHandle methods_default_annotations(THREAD, methods_default_annotations_oop); 3162 objArrayHandle methods_default_annotations(THREAD, methods_default_annotations_oop);
3163
3164 // Additional attributes
3165 ClassAnnotationCollector parsed_annotations;
3166 parse_classfile_attributes(cp, &parsed_annotations, CHECK_(nullHandle));
3167
3168 // Make sure this is the end of class file stream
3169 guarantee_property(cfs->at_eos(), "Extra bytes at the end of class file %s", CHECK_(nullHandle));
2981 3170
2982 // We check super class after class file is parsed and format is checked 3171 // We check super class after class file is parsed and format is checked
2983 if (super_class_index > 0 && super_klass.is_null()) { 3172 if (super_class_index > 0 && super_klass.is_null()) {
2984 Symbol* sk = cp->klass_name_at(super_class_index); 3173 Symbol* sk = cp->klass_name_at(super_class_index);
2985 if (access_flags.is_interface()) { 3174 if (access_flags.is_interface()) {
3465 // super class exists and this class inherited miranda methods 3654 // super class exists and this class inherited miranda methods
3466 ) { 3655 ) {
3467 this_klass->set_has_miranda_methods(); // then set a flag 3656 this_klass->set_has_miranda_methods(); // then set a flag
3468 } 3657 }
3469 3658
3470 // Additional attributes 3659 // Fill in field values obtained by parse_classfile_attributes
3471 parse_classfile_attributes(cp, this_klass, CHECK_(nullHandle)); 3660 if (parsed_annotations.has_any_annotations())
3472 3661 parsed_annotations.apply_to(this_klass);
3473 // Make sure this is the end of class file stream 3662 apply_parsed_class_attributes(this_klass);
3474 guarantee_property(cfs->at_eos(), "Extra bytes at the end of class file %s", CHECK_(nullHandle));
3475 3663
3476 // VerifyOops believes that once this has been set, the object is completely loaded. 3664 // VerifyOops believes that once this has been set, the object is completely loaded.
3477 // Compute transitive closure of interfaces this class implements 3665 // Compute transitive closure of interfaces this class implements
3478 this_klass->set_transitive_interfaces(transitive_interfaces()); 3666 this_klass->set_transitive_interfaces(transitive_interfaces());
3479 3667
3484 klassItable::setup_itable_offset_table(this_klass); 3672 klassItable::setup_itable_offset_table(this_klass);
3485 3673
3486 // Do final class setup 3674 // Do final class setup
3487 fill_oop_maps(this_klass, nonstatic_oop_map_count, nonstatic_oop_offsets, nonstatic_oop_counts); 3675 fill_oop_maps(this_klass, nonstatic_oop_map_count, nonstatic_oop_offsets, nonstatic_oop_counts);
3488 3676
3677 // Fill in has_finalizer, has_vanilla_constructor, and layout_helper
3489 set_precomputed_flags(this_klass); 3678 set_precomputed_flags(this_klass);
3490 3679
3491 // reinitialize modifiers, using the InnerClasses attribute 3680 // reinitialize modifiers, using the InnerClasses attribute
3492 int computed_modifiers = this_klass->compute_modifier_flags(CHECK_(nullHandle)); 3681 int computed_modifiers = this_klass->compute_modifier_flags(CHECK_(nullHandle));
3493 this_klass->set_modifier_flags(computed_modifiers); 3682 this_klass->set_modifier_flags(computed_modifiers);