comparison src/share/vm/prims/jvm.cpp @ 8818:1916ca1dec2f

8009382: Add JVM_Get{Field|Method}TypeAnnotations Reviewed-by: dcubed, rbackman Contributed-by: Joel Borggren-Franck <joel.franck@oracle.com>
author rbackman
date Tue, 26 Mar 2013 15:00:34 +0100
parents 2c7663baeb67
children cd9ad42dfde0
comparison
equal deleted inserted replaced
8817:b8deb3205b51 8818:1916ca1dec2f
1455 1455
1456 1456
1457 JVM_ENTRY(jbyteArray, JVM_GetClassAnnotations(JNIEnv *env, jclass cls)) 1457 JVM_ENTRY(jbyteArray, JVM_GetClassAnnotations(JNIEnv *env, jclass cls))
1458 assert (cls != NULL, "illegal class"); 1458 assert (cls != NULL, "illegal class");
1459 JVMWrapper("JVM_GetClassAnnotations"); 1459 JVMWrapper("JVM_GetClassAnnotations");
1460 ResourceMark rm(THREAD); 1460
1461 // Return null for arrays and primitives 1461 // Return null for arrays and primitives
1462 if (!java_lang_Class::is_primitive(JNIHandles::resolve(cls))) { 1462 if (!java_lang_Class::is_primitive(JNIHandles::resolve(cls))) {
1463 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve(cls)); 1463 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve(cls));
1464 if (k->oop_is_instance()) { 1464 if (k->oop_is_instance()) {
1465 typeArrayOop a = Annotations::make_java_array(InstanceKlass::cast(k)->class_annotations(), CHECK_NULL); 1465 typeArrayOop a = Annotations::make_java_array(InstanceKlass::cast(k)->class_annotations(), CHECK_NULL);
1468 } 1468 }
1469 return NULL; 1469 return NULL;
1470 JVM_END 1470 JVM_END
1471 1471
1472 1472
1473 JVM_ENTRY(jbyteArray, JVM_GetFieldAnnotations(JNIEnv *env, jobject field)) 1473 static bool jvm_get_field_common(jobject field, fieldDescriptor& fd, TRAPS) {
1474 assert(field != NULL, "illegal field");
1475 JVMWrapper("JVM_GetFieldAnnotations");
1476
1477 // some of this code was adapted from from jni_FromReflectedField 1474 // some of this code was adapted from from jni_FromReflectedField
1478 1475
1479 // field is a handle to a java.lang.reflect.Field object
1480 oop reflected = JNIHandles::resolve_non_null(field); 1476 oop reflected = JNIHandles::resolve_non_null(field);
1481 oop mirror = java_lang_reflect_Field::clazz(reflected); 1477 oop mirror = java_lang_reflect_Field::clazz(reflected);
1482 Klass* k = java_lang_Class::as_Klass(mirror); 1478 Klass* k = java_lang_Class::as_Klass(mirror);
1483 int slot = java_lang_reflect_Field::slot(reflected); 1479 int slot = java_lang_reflect_Field::slot(reflected);
1484 int modifiers = java_lang_reflect_Field::modifiers(reflected); 1480 int modifiers = java_lang_reflect_Field::modifiers(reflected);
1485 1481
1486 fieldDescriptor fd;
1487 KlassHandle kh(THREAD, k); 1482 KlassHandle kh(THREAD, k);
1488 intptr_t offset = InstanceKlass::cast(kh())->field_offset(slot); 1483 intptr_t offset = InstanceKlass::cast(kh())->field_offset(slot);
1489 1484
1490 if (modifiers & JVM_ACC_STATIC) { 1485 if (modifiers & JVM_ACC_STATIC) {
1491 // for static fields we only look in the current class 1486 // for static fields we only look in the current class
1492 if (!InstanceKlass::cast(kh())->find_local_field_from_offset(offset, true, &fd)) { 1487 if (!InstanceKlass::cast(kh())->find_local_field_from_offset(offset, true, &fd)) {
1493 assert(false, "cannot find static field"); 1488 assert(false, "cannot find static field");
1494 return NULL; // robustness 1489 return false;
1495 } 1490 }
1496 } else { 1491 } else {
1497 // for instance fields we start with the current class and work 1492 // for instance fields we start with the current class and work
1498 // our way up through the superclass chain 1493 // our way up through the superclass chain
1499 if (!InstanceKlass::cast(kh())->find_field_from_offset(offset, false, &fd)) { 1494 if (!InstanceKlass::cast(kh())->find_field_from_offset(offset, false, &fd)) {
1500 assert(false, "cannot find instance field"); 1495 assert(false, "cannot find instance field");
1501 return NULL; // robustness 1496 return false;
1502 } 1497 }
1498 }
1499 return true;
1500 }
1501
1502 JVM_ENTRY(jbyteArray, JVM_GetFieldAnnotations(JNIEnv *env, jobject field))
1503 // field is a handle to a java.lang.reflect.Field object
1504 assert(field != NULL, "illegal field");
1505 JVMWrapper("JVM_GetFieldAnnotations");
1506
1507 fieldDescriptor fd;
1508 bool gotFd = jvm_get_field_common(field, fd, CHECK_NULL);
1509 if (!gotFd) {
1510 return NULL;
1503 } 1511 }
1504 1512
1505 return (jbyteArray) JNIHandles::make_local(env, Annotations::make_java_array(fd.annotations(), THREAD)); 1513 return (jbyteArray) JNIHandles::make_local(env, Annotations::make_java_array(fd.annotations(), THREAD));
1506 JVM_END 1514 JVM_END
1507 1515
1523 slot = java_lang_reflect_Method::slot(reflected); 1531 slot = java_lang_reflect_Method::slot(reflected);
1524 } 1532 }
1525 Klass* k = java_lang_Class::as_Klass(mirror); 1533 Klass* k = java_lang_Class::as_Klass(mirror);
1526 1534
1527 Method* m = InstanceKlass::cast(k)->method_with_idnum(slot); 1535 Method* m = InstanceKlass::cast(k)->method_with_idnum(slot);
1528 if (m == NULL) { 1536 assert(m != NULL, "cannot find method");
1529 assert(false, "cannot find method"); 1537 return m; // caller has to deal with NULL in product mode
1530 return NULL; // robustness
1531 }
1532
1533 return m;
1534 } 1538 }
1535 1539
1536 1540
1537 JVM_ENTRY(jbyteArray, JVM_GetMethodAnnotations(JNIEnv *env, jobject method)) 1541 JVM_ENTRY(jbyteArray, JVM_GetMethodAnnotations(JNIEnv *env, jobject method))
1538 JVMWrapper("JVM_GetMethodAnnotations"); 1542 JVMWrapper("JVM_GetMethodAnnotations");
1539 1543
1540 // method is a handle to a java.lang.reflect.Method object 1544 // method is a handle to a java.lang.reflect.Method object
1541 Method* m = jvm_get_method_common(method); 1545 Method* m = jvm_get_method_common(method);
1546 if (m == NULL) {
1547 return NULL;
1548 }
1549
1542 return (jbyteArray) JNIHandles::make_local(env, 1550 return (jbyteArray) JNIHandles::make_local(env,
1543 Annotations::make_java_array(m->annotations(), THREAD)); 1551 Annotations::make_java_array(m->annotations(), THREAD));
1544 JVM_END 1552 JVM_END
1545 1553
1546 1554
1547 JVM_ENTRY(jbyteArray, JVM_GetMethodDefaultAnnotationValue(JNIEnv *env, jobject method)) 1555 JVM_ENTRY(jbyteArray, JVM_GetMethodDefaultAnnotationValue(JNIEnv *env, jobject method))
1548 JVMWrapper("JVM_GetMethodDefaultAnnotationValue"); 1556 JVMWrapper("JVM_GetMethodDefaultAnnotationValue");
1549 1557
1550 // method is a handle to a java.lang.reflect.Method object 1558 // method is a handle to a java.lang.reflect.Method object
1551 Method* m = jvm_get_method_common(method); 1559 Method* m = jvm_get_method_common(method);
1560 if (m == NULL) {
1561 return NULL;
1562 }
1563
1552 return (jbyteArray) JNIHandles::make_local(env, 1564 return (jbyteArray) JNIHandles::make_local(env,
1553 Annotations::make_java_array(m->annotation_default(), THREAD)); 1565 Annotations::make_java_array(m->annotation_default(), THREAD));
1554 JVM_END 1566 JVM_END
1555 1567
1556 1568
1557 JVM_ENTRY(jbyteArray, JVM_GetMethodParameterAnnotations(JNIEnv *env, jobject method)) 1569 JVM_ENTRY(jbyteArray, JVM_GetMethodParameterAnnotations(JNIEnv *env, jobject method))
1558 JVMWrapper("JVM_GetMethodParameterAnnotations"); 1570 JVMWrapper("JVM_GetMethodParameterAnnotations");
1559 1571
1560 // method is a handle to a java.lang.reflect.Method object 1572 // method is a handle to a java.lang.reflect.Method object
1561 Method* m = jvm_get_method_common(method); 1573 Method* m = jvm_get_method_common(method);
1574 if (m == NULL) {
1575 return NULL;
1576 }
1577
1562 return (jbyteArray) JNIHandles::make_local(env, 1578 return (jbyteArray) JNIHandles::make_local(env,
1563 Annotations::make_java_array(m->parameter_annotations(), THREAD)); 1579 Annotations::make_java_array(m->parameter_annotations(), THREAD));
1564 JVM_END 1580 JVM_END
1565 1581
1566 /* Type use annotations support (JDK 1.8) */ 1582 /* Type use annotations support (JDK 1.8) */
1579 return (jbyteArray) JNIHandles::make_local(env, a); 1595 return (jbyteArray) JNIHandles::make_local(env, a);
1580 } 1596 }
1581 } 1597 }
1582 } 1598 }
1583 return NULL; 1599 return NULL;
1600 JVM_END
1601
1602 JVM_ENTRY(jbyteArray, JVM_GetMethodTypeAnnotations(JNIEnv *env, jobject method))
1603 assert (method != NULL, "illegal method");
1604 JVMWrapper("JVM_GetMethodTypeAnnotations");
1605
1606 // method is a handle to a java.lang.reflect.Method object
1607 Method* m = jvm_get_method_common(method);
1608 if (m == NULL) {
1609 return NULL;
1610 }
1611
1612 AnnotationArray* type_annotations = m->type_annotations();
1613 if (type_annotations != NULL) {
1614 typeArrayOop a = Annotations::make_java_array(type_annotations, CHECK_NULL);
1615 return (jbyteArray) JNIHandles::make_local(env, a);
1616 }
1617
1618 return NULL;
1619 JVM_END
1620
1621 JVM_ENTRY(jbyteArray, JVM_GetFieldTypeAnnotations(JNIEnv *env, jobject field))
1622 assert (field != NULL, "illegal field");
1623 JVMWrapper("JVM_GetFieldTypeAnnotations");
1624
1625 fieldDescriptor fd;
1626 bool gotFd = jvm_get_field_common(field, fd, CHECK_NULL);
1627 if (!gotFd) {
1628 return NULL;
1629 }
1630
1631 return (jbyteArray) JNIHandles::make_local(env, Annotations::make_java_array(fd.type_annotations(), THREAD));
1584 JVM_END 1632 JVM_END
1585 1633
1586 static void bounds_check(constantPoolHandle cp, jint index, TRAPS) { 1634 static void bounds_check(constantPoolHandle cp, jint index, TRAPS) {
1587 if (!cp->is_within_bounds(index)) { 1635 if (!cp->is_within_bounds(index)) {
1588 THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), "Constant pool index out of bounds"); 1636 THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), "Constant pool index out of bounds");