src/share/vm/prims/jvm.cpp

Print this page




1440   JVMWrapper("JVM_GetClassSignature");
1441   JvmtiVMObjectAllocEventCollector oam;
1442   ResourceMark rm(THREAD);
1443   // Return null for arrays and primatives
1444   if (!java_lang_Class::is_primitive(JNIHandles::resolve(cls))) {
1445     Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve(cls));
1446     if (k->oop_is_instance()) {
1447       Symbol* sym = InstanceKlass::cast(k)->generic_signature();
1448       if (sym == NULL) return NULL;
1449       Handle str = java_lang_String::create_from_symbol(sym, CHECK_NULL);
1450       return (jstring) JNIHandles::make_local(env, str());
1451     }
1452   }
1453   return NULL;
1454 JVM_END
1455 
1456 
1457 JVM_ENTRY(jbyteArray, JVM_GetClassAnnotations(JNIEnv *env, jclass cls))
1458   assert (cls != NULL, "illegal class");
1459   JVMWrapper("JVM_GetClassAnnotations");
1460   ResourceMark rm(THREAD);
1461   // Return null for arrays and primitives
1462   if (!java_lang_Class::is_primitive(JNIHandles::resolve(cls))) {
1463     Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve(cls));
1464     if (k->oop_is_instance()) {
1465       typeArrayOop a = Annotations::make_java_array(InstanceKlass::cast(k)->class_annotations(), CHECK_NULL);
1466       return (jbyteArray) JNIHandles::make_local(env, a);
1467     }
1468   }
1469   return NULL;
1470 JVM_END
1471 
1472 
1473 JVM_ENTRY(jbyteArray, JVM_GetFieldAnnotations(JNIEnv *env, jobject field))
1474   assert(field != NULL, "illegal field");
1475   JVMWrapper("JVM_GetFieldAnnotations");
1476 
1477   // some of this code was adapted from from jni_FromReflectedField
1478 
1479   // field is a handle to a java.lang.reflect.Field object
1480   oop reflected = JNIHandles::resolve_non_null(field);
1481   oop mirror    = java_lang_reflect_Field::clazz(reflected);
1482   Klass* k    = java_lang_Class::as_Klass(mirror);
1483   int slot      = java_lang_reflect_Field::slot(reflected);
1484   int modifiers = java_lang_reflect_Field::modifiers(reflected);
1485 
1486   fieldDescriptor fd;
1487   KlassHandle kh(THREAD, k);
1488   intptr_t offset = InstanceKlass::cast(kh())->field_offset(slot);
1489 
1490   if (modifiers & JVM_ACC_STATIC) {
1491     // for static fields we only look in the current class
1492     if (!InstanceKlass::cast(kh())->find_local_field_from_offset(offset, true, &fd)) {
1493       assert(false, "cannot find static field");
1494       return NULL;  // robustness
1495     }
1496   } else {
1497     // for instance fields we start with the current class and work
1498     // our way up through the superclass chain
1499     if (!InstanceKlass::cast(kh())->find_field_from_offset(offset, false, &fd)) {
1500       assert(false, "cannot find instance field");
1501       return NULL;  // robustness

1502     }












1503   }
1504 
1505   return (jbyteArray) JNIHandles::make_local(env, Annotations::make_java_array(fd.annotations(), THREAD));
1506 JVM_END
1507 
1508 
1509 static Method* jvm_get_method_common(jobject method) {
1510   // some of this code was adapted from from jni_FromReflectedMethod
1511 
1512   oop reflected = JNIHandles::resolve_non_null(method);
1513   oop mirror    = NULL;
1514   int slot      = 0;
1515 
1516   if (reflected->klass() == SystemDictionary::reflect_Constructor_klass()) {
1517     mirror = java_lang_reflect_Constructor::clazz(reflected);
1518     slot   = java_lang_reflect_Constructor::slot(reflected);
1519   } else {
1520     assert(reflected->klass() == SystemDictionary::reflect_Method_klass(),
1521            "wrong type");
1522     mirror = java_lang_reflect_Method::clazz(reflected);
1523     slot   = java_lang_reflect_Method::slot(reflected);
1524   }
1525   Klass* k = java_lang_Class::as_Klass(mirror);
1526 
1527   Method* m = InstanceKlass::cast(k)->method_with_idnum(slot);
1528   if (m == NULL) {
1529     assert(false, "cannot find method");
1530     return NULL;  // robustness
1531   }
1532 
1533   return m;
1534 }
1535 
1536 
1537 JVM_ENTRY(jbyteArray, JVM_GetMethodAnnotations(JNIEnv *env, jobject method))
1538   JVMWrapper("JVM_GetMethodAnnotations");
1539 
1540   // method is a handle to a java.lang.reflect.Method object
1541   Method* m = jvm_get_method_common(method);




1542   return (jbyteArray) JNIHandles::make_local(env,
1543     Annotations::make_java_array(m->annotations(), THREAD));
1544 JVM_END
1545 
1546 
1547 JVM_ENTRY(jbyteArray, JVM_GetMethodDefaultAnnotationValue(JNIEnv *env, jobject method))
1548   JVMWrapper("JVM_GetMethodDefaultAnnotationValue");
1549 
1550   // method is a handle to a java.lang.reflect.Method object
1551   Method* m = jvm_get_method_common(method);




1552   return (jbyteArray) JNIHandles::make_local(env,
1553     Annotations::make_java_array(m->annotation_default(), THREAD));
1554 JVM_END
1555 
1556 
1557 JVM_ENTRY(jbyteArray, JVM_GetMethodParameterAnnotations(JNIEnv *env, jobject method))
1558   JVMWrapper("JVM_GetMethodParameterAnnotations");
1559 
1560   // method is a handle to a java.lang.reflect.Method object
1561   Method* m = jvm_get_method_common(method);




1562   return (jbyteArray) JNIHandles::make_local(env,
1563     Annotations::make_java_array(m->parameter_annotations(), THREAD));
1564 JVM_END
1565 
1566 /* Type use annotations support (JDK 1.8) */
1567 
1568 JVM_ENTRY(jbyteArray, JVM_GetClassTypeAnnotations(JNIEnv *env, jclass cls))
1569   assert (cls != NULL, "illegal class");
1570   JVMWrapper("JVM_GetClassTypeAnnotations");
1571   ResourceMark rm(THREAD);
1572   // Return null for arrays and primitives
1573   if (!java_lang_Class::is_primitive(JNIHandles::resolve(cls))) {
1574     Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve(cls));
1575     if (k->oop_is_instance()) {
1576       AnnotationArray* type_annotations = InstanceKlass::cast(k)->class_type_annotations();
1577       if (type_annotations != NULL) {
1578         typeArrayOop a = Annotations::make_java_array(type_annotations, CHECK_NULL);
1579         return (jbyteArray) JNIHandles::make_local(env, a);
1580       }
1581     }
1582   }
1583   return NULL;
































1584 JVM_END
1585 
1586 static void bounds_check(constantPoolHandle cp, jint index, TRAPS) {
1587   if (!cp->is_within_bounds(index)) {
1588     THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), "Constant pool index out of bounds");
1589   }
1590 }
1591 
1592 JVM_ENTRY(jobjectArray, JVM_GetMethodParameters(JNIEnv *env, jobject method))
1593 {
1594   JVMWrapper("JVM_GetMethodParameters");
1595   // method is a handle to a java.lang.reflect.Method object
1596   Method* method_ptr = jvm_get_method_common(method);
1597   methodHandle mh (THREAD, method_ptr);
1598   Handle reflected_method (THREAD, JNIHandles::resolve_non_null(method));
1599   const int num_params = mh->method_parameters_length();
1600 
1601   if (0 != num_params) {
1602     // make sure all the symbols are properly formatted
1603     for (int i = 0; i < num_params; i++) {




1440   JVMWrapper("JVM_GetClassSignature");
1441   JvmtiVMObjectAllocEventCollector oam;
1442   ResourceMark rm(THREAD);
1443   // Return null for arrays and primatives
1444   if (!java_lang_Class::is_primitive(JNIHandles::resolve(cls))) {
1445     Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve(cls));
1446     if (k->oop_is_instance()) {
1447       Symbol* sym = InstanceKlass::cast(k)->generic_signature();
1448       if (sym == NULL) return NULL;
1449       Handle str = java_lang_String::create_from_symbol(sym, CHECK_NULL);
1450       return (jstring) JNIHandles::make_local(env, str());
1451     }
1452   }
1453   return NULL;
1454 JVM_END
1455 
1456 
1457 JVM_ENTRY(jbyteArray, JVM_GetClassAnnotations(JNIEnv *env, jclass cls))
1458   assert (cls != NULL, "illegal class");
1459   JVMWrapper("JVM_GetClassAnnotations");
1460 
1461   // Return null for arrays and primitives
1462   if (!java_lang_Class::is_primitive(JNIHandles::resolve(cls))) {
1463     Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve(cls));
1464     if (k->oop_is_instance()) {
1465       typeArrayOop a = Annotations::make_java_array(InstanceKlass::cast(k)->class_annotations(), CHECK_NULL);
1466       return (jbyteArray) JNIHandles::make_local(env, a);
1467     }
1468   }
1469   return NULL;
1470 JVM_END
1471 
1472 
1473 static bool jvm_get_field_common(jobject field, fieldDescriptor& fd, TRAPS) {



1474   // some of this code was adapted from from jni_FromReflectedField
1475 

1476   oop reflected = JNIHandles::resolve_non_null(field);
1477   oop mirror    = java_lang_reflect_Field::clazz(reflected);
1478   Klass* k    = java_lang_Class::as_Klass(mirror);
1479   int slot      = java_lang_reflect_Field::slot(reflected);
1480   int modifiers = java_lang_reflect_Field::modifiers(reflected);
1481 

1482   KlassHandle kh(THREAD, k);
1483   intptr_t offset = InstanceKlass::cast(kh())->field_offset(slot);
1484 
1485   if (modifiers & JVM_ACC_STATIC) {
1486     // for static fields we only look in the current class
1487     if (!InstanceKlass::cast(kh())->find_local_field_from_offset(offset, true, &fd)) {
1488       assert(false, "cannot find static field");
1489       return false;
1490     }
1491   } else {
1492     // for instance fields we start with the current class and work
1493     // our way up through the superclass chain
1494     if (!InstanceKlass::cast(kh())->find_field_from_offset(offset, false, &fd)) {
1495       assert(false, "cannot find instance field");
1496       return false;
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;
1511   }
1512 
1513   return (jbyteArray) JNIHandles::make_local(env, Annotations::make_java_array(fd.annotations(), THREAD));
1514 JVM_END
1515 
1516 
1517 static Method* jvm_get_method_common(jobject method) {
1518   // some of this code was adapted from from jni_FromReflectedMethod
1519 
1520   oop reflected = JNIHandles::resolve_non_null(method);
1521   oop mirror    = NULL;
1522   int slot      = 0;
1523 
1524   if (reflected->klass() == SystemDictionary::reflect_Constructor_klass()) {
1525     mirror = java_lang_reflect_Constructor::clazz(reflected);
1526     slot   = java_lang_reflect_Constructor::slot(reflected);
1527   } else {
1528     assert(reflected->klass() == SystemDictionary::reflect_Method_klass(),
1529            "wrong type");
1530     mirror = java_lang_reflect_Method::clazz(reflected);
1531     slot   = java_lang_reflect_Method::slot(reflected);
1532   }
1533   Klass* k = java_lang_Class::as_Klass(mirror);
1534 
1535   Method* m = InstanceKlass::cast(k)->method_with_idnum(slot);
1536   assert(m != NULL, "cannot find method");
1537   return m;  // caller has to deal with NULL in product mode




1538 }
1539 
1540 
1541 JVM_ENTRY(jbyteArray, JVM_GetMethodAnnotations(JNIEnv *env, jobject method))
1542   JVMWrapper("JVM_GetMethodAnnotations");
1543 
1544   // method is a handle to a java.lang.reflect.Method object
1545   Method* m = jvm_get_method_common(method);
1546   if (m == NULL) {
1547     return NULL;
1548   }
1549 
1550   return (jbyteArray) JNIHandles::make_local(env,
1551     Annotations::make_java_array(m->annotations(), THREAD));
1552 JVM_END
1553 
1554 
1555 JVM_ENTRY(jbyteArray, JVM_GetMethodDefaultAnnotationValue(JNIEnv *env, jobject method))
1556   JVMWrapper("JVM_GetMethodDefaultAnnotationValue");
1557 
1558   // method is a handle to a java.lang.reflect.Method object
1559   Method* m = jvm_get_method_common(method);
1560   if (m == NULL) {
1561     return NULL;
1562   }
1563 
1564   return (jbyteArray) JNIHandles::make_local(env,
1565     Annotations::make_java_array(m->annotation_default(), THREAD));
1566 JVM_END
1567 
1568 
1569 JVM_ENTRY(jbyteArray, JVM_GetMethodParameterAnnotations(JNIEnv *env, jobject method))
1570   JVMWrapper("JVM_GetMethodParameterAnnotations");
1571 
1572   // method is a handle to a java.lang.reflect.Method object
1573   Method* m = jvm_get_method_common(method);
1574   if (m == NULL) {
1575     return NULL;
1576   }
1577 
1578   return (jbyteArray) JNIHandles::make_local(env,
1579     Annotations::make_java_array(m->parameter_annotations(), THREAD));
1580 JVM_END
1581 
1582 /* Type use annotations support (JDK 1.8) */
1583 
1584 JVM_ENTRY(jbyteArray, JVM_GetClassTypeAnnotations(JNIEnv *env, jclass cls))
1585   assert (cls != NULL, "illegal class");
1586   JVMWrapper("JVM_GetClassTypeAnnotations");
1587   ResourceMark rm(THREAD);
1588   // Return null for arrays and primitives
1589   if (!java_lang_Class::is_primitive(JNIHandles::resolve(cls))) {
1590     Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve(cls));
1591     if (k->oop_is_instance()) {
1592       AnnotationArray* type_annotations = InstanceKlass::cast(k)->class_type_annotations();
1593       if (type_annotations != NULL) {
1594         typeArrayOop a = Annotations::make_java_array(type_annotations, CHECK_NULL);
1595         return (jbyteArray) JNIHandles::make_local(env, a);
1596       }
1597     }
1598   }
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));
1632 JVM_END
1633 
1634 static void bounds_check(constantPoolHandle cp, jint index, TRAPS) {
1635   if (!cp->is_within_bounds(index)) {
1636     THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), "Constant pool index out of bounds");
1637   }
1638 }
1639 
1640 JVM_ENTRY(jobjectArray, JVM_GetMethodParameters(JNIEnv *env, jobject method))
1641 {
1642   JVMWrapper("JVM_GetMethodParameters");
1643   // method is a handle to a java.lang.reflect.Method object
1644   Method* method_ptr = jvm_get_method_common(method);
1645   methodHandle mh (THREAD, method_ptr);
1646   Handle reflected_method (THREAD, JNIHandles::resolve_non_null(method));
1647   const int num_params = mh->method_parameters_length();
1648 
1649   if (0 != num_params) {
1650     // make sure all the symbols are properly formatted
1651     for (int i = 0; i < num_params; i++) {