8057919: Class.getSimpleName() should work for non-JLS compliant class names
authorvlivanov
Tue, 14 Apr 2015 18:03:43 +0300
changeset 30222 bfe6be3c4ef8
parent 30221 70dab4a0cd45
child 30223 82ab7b6b4927
8057919: Class.getSimpleName() should work for non-JLS compliant class names Reviewed-by: dholmes, jrose
hotspot/make/aix/makefiles/mapfile-vers-debug
hotspot/make/aix/makefiles/mapfile-vers-product
hotspot/make/bsd/makefiles/mapfile-vers-darwin-debug
hotspot/make/bsd/makefiles/mapfile-vers-darwin-product
hotspot/make/bsd/makefiles/mapfile-vers-debug
hotspot/make/bsd/makefiles/mapfile-vers-product
hotspot/make/linux/makefiles/mapfile-vers-debug
hotspot/make/linux/makefiles/mapfile-vers-product
hotspot/make/solaris/makefiles/mapfile-vers
hotspot/src/share/vm/oops/instanceKlass.cpp
hotspot/src/share/vm/oops/instanceKlass.hpp
hotspot/src/share/vm/prims/jvm.cpp
hotspot/src/share/vm/prims/jvm.h
--- a/hotspot/make/aix/makefiles/mapfile-vers-debug	Mon Apr 13 18:49:59 2015 -0700
+++ b/hotspot/make/aix/makefiles/mapfile-vers-debug	Tue Apr 14 18:03:43 2015 +0300
@@ -19,7 +19,7 @@
 # Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 # or visit www.oracle.com if you need additional information or have any
 # questions.
-#  
+#
 #
 
 # Define public interface.
@@ -107,6 +107,7 @@
                 JVM_GetClassTypeAnnotations;
                 JVM_GetDeclaredClasses;
                 JVM_GetDeclaringClass;
+                JVM_GetSimpleBinaryName;
                 JVM_GetEnclosingMethodInfo;
                 JVM_GetFieldIxModifiers;
                 JVM_GetFieldTypeAnnotations;
--- a/hotspot/make/aix/makefiles/mapfile-vers-product	Mon Apr 13 18:49:59 2015 -0700
+++ b/hotspot/make/aix/makefiles/mapfile-vers-product	Tue Apr 14 18:03:43 2015 +0300
@@ -19,7 +19,7 @@
 # Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 # or visit www.oracle.com if you need additional information or have any
 # questions.
-#  
+#
 #
 
 # Define public interface.
@@ -107,6 +107,7 @@
                 JVM_GetClassTypeAnnotations;
                 JVM_GetDeclaredClasses;
                 JVM_GetDeclaringClass;
+                JVM_GetSimpleBinaryName;
                 JVM_GetEnclosingMethodInfo;
                 JVM_GetFieldIxModifiers;
                 JVM_GetInheritedAccessControlContext;
--- a/hotspot/make/bsd/makefiles/mapfile-vers-darwin-debug	Mon Apr 13 18:49:59 2015 -0700
+++ b/hotspot/make/bsd/makefiles/mapfile-vers-darwin-debug	Tue Apr 14 18:03:43 2015 +0300
@@ -105,6 +105,7 @@
                 _JVM_GetClassTypeAnnotations
                 _JVM_GetDeclaredClasses
                 _JVM_GetDeclaringClass
+                _JVM_GetSimpleBinaryName
                 _JVM_GetEnclosingMethodInfo
                 _JVM_GetFieldIxModifiers
                 _JVM_GetFieldTypeAnnotations
--- a/hotspot/make/bsd/makefiles/mapfile-vers-darwin-product	Mon Apr 13 18:49:59 2015 -0700
+++ b/hotspot/make/bsd/makefiles/mapfile-vers-darwin-product	Tue Apr 14 18:03:43 2015 +0300
@@ -105,6 +105,7 @@
                 _JVM_GetClassTypeAnnotations
                 _JVM_GetDeclaredClasses
                 _JVM_GetDeclaringClass
+                _JVM_GetSimpleBinaryName
                 _JVM_GetEnclosingMethodInfo
                 _JVM_GetFieldIxModifiers
                 _JVM_GetFieldTypeAnnotations
--- a/hotspot/make/bsd/makefiles/mapfile-vers-debug	Mon Apr 13 18:49:59 2015 -0700
+++ b/hotspot/make/bsd/makefiles/mapfile-vers-debug	Tue Apr 14 18:03:43 2015 +0300
@@ -19,7 +19,7 @@
 # Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 # or visit www.oracle.com if you need additional information or have any
 # questions.
-#  
+#
 #
 
 # Define public interface.
@@ -107,6 +107,7 @@
                 JVM_GetClassTypeAnnotations;
                 JVM_GetDeclaredClasses;
                 JVM_GetDeclaringClass;
+                JVM_GetSimpleBinaryName;
                 JVM_GetEnclosingMethodInfo;
                 JVM_GetFieldIxModifiers;
                 JVM_GetFieldTypeAnnotations;
--- a/hotspot/make/bsd/makefiles/mapfile-vers-product	Mon Apr 13 18:49:59 2015 -0700
+++ b/hotspot/make/bsd/makefiles/mapfile-vers-product	Tue Apr 14 18:03:43 2015 +0300
@@ -19,7 +19,7 @@
 # Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 # or visit www.oracle.com if you need additional information or have any
 # questions.
-#  
+#
 #
 
 # Define public interface.
@@ -107,6 +107,7 @@
                 JVM_GetClassTypeAnnotations;
                 JVM_GetDeclaredClasses;
                 JVM_GetDeclaringClass;
+                JVM_GetSimpleBinaryName;
                 JVM_GetEnclosingMethodInfo;
                 JVM_GetFieldIxModifiers;
                 JVM_GetFieldTypeAnnotations;
--- a/hotspot/make/linux/makefiles/mapfile-vers-debug	Mon Apr 13 18:49:59 2015 -0700
+++ b/hotspot/make/linux/makefiles/mapfile-vers-debug	Tue Apr 14 18:03:43 2015 +0300
@@ -19,7 +19,7 @@
 # Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 # or visit www.oracle.com if you need additional information or have any
 # questions.
-#  
+#
 #
 
 # Define public interface.
@@ -107,6 +107,7 @@
                 JVM_GetClassTypeAnnotations;
                 JVM_GetDeclaredClasses;
                 JVM_GetDeclaringClass;
+                JVM_GetSimpleBinaryName;
                 JVM_GetEnclosingMethodInfo;
                 JVM_GetFieldIxModifiers;
                 JVM_GetFieldTypeAnnotations;
--- a/hotspot/make/linux/makefiles/mapfile-vers-product	Mon Apr 13 18:49:59 2015 -0700
+++ b/hotspot/make/linux/makefiles/mapfile-vers-product	Tue Apr 14 18:03:43 2015 +0300
@@ -19,7 +19,7 @@
 # Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 # or visit www.oracle.com if you need additional information or have any
 # questions.
-#  
+#
 #
 
 # Define public interface.
@@ -107,6 +107,7 @@
                 JVM_GetClassTypeAnnotations;
                 JVM_GetDeclaredClasses;
                 JVM_GetDeclaringClass;
+                JVM_GetSimpleBinaryName;
                 JVM_GetEnclosingMethodInfo;
                 JVM_GetFieldIxModifiers;
                 JVM_GetFieldTypeAnnotations;
--- a/hotspot/make/solaris/makefiles/mapfile-vers	Mon Apr 13 18:49:59 2015 -0700
+++ b/hotspot/make/solaris/makefiles/mapfile-vers	Tue Apr 14 18:03:43 2015 +0300
@@ -19,7 +19,7 @@
 # Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 # or visit www.oracle.com if you need additional information or have any
 # questions.
-#  
+#
 #
 
 # Define public interface.
@@ -30,7 +30,7 @@
                 JNI_CreateJavaVM;
                 JNI_GetCreatedJavaVMs;
                 JNI_GetDefaultJavaVMInitArgs;
-        
+
                 # JVM
                 JVM_ActiveProcessorCount;
                 JVM_ArrayCopy;
@@ -107,6 +107,7 @@
                 JVM_GetClassTypeAnnotations;
                 JVM_GetDeclaredClasses;
                 JVM_GetDeclaringClass;
+                JVM_GetSimpleBinaryName;
                 JVM_GetEnclosingMethodInfo;
                 JVM_GetFieldIxModifiers;
                 JVM_GetFieldTypeAnnotations;
--- a/hotspot/src/share/vm/oops/instanceKlass.cpp	Mon Apr 13 18:49:59 2015 -0700
+++ b/hotspot/src/share/vm/oops/instanceKlass.cpp	Tue Apr 14 18:03:43 2015 +0300
@@ -2712,6 +2712,57 @@
   return false;
 }
 
+bool InstanceKlass::find_inner_classes_attr(instanceKlassHandle k, int* ooff, int* noff, TRAPS) {
+  constantPoolHandle i_cp(THREAD, k->constants());
+  for (InnerClassesIterator iter(k); !iter.done(); iter.next()) {
+    int ioff = iter.inner_class_info_index();
+    if (ioff != 0) {
+      // Check to see if the name matches the class we're looking for
+      // before attempting to find the class.
+      if (i_cp->klass_name_at_matches(k, ioff)) {
+        Klass* inner_klass = i_cp->klass_at(ioff, CHECK_false);
+        if (k() == inner_klass) {
+          *ooff = iter.outer_class_info_index();
+          *noff = iter.inner_name_index();
+          return true;
+        }
+      }
+    }
+  }
+  return false;
+}
+
+Klass* InstanceKlass::compute_enclosing_class_impl(instanceKlassHandle k, bool* inner_is_member, TRAPS) {
+  instanceKlassHandle outer_klass;
+  *inner_is_member = false;
+  int ooff = 0, noff = 0;
+  if (find_inner_classes_attr(k, &ooff, &noff, THREAD)) {
+    constantPoolHandle i_cp(THREAD, k->constants());
+    if (ooff != 0) {
+      Klass* ok = i_cp->klass_at(ooff, CHECK_NULL);
+      outer_klass = instanceKlassHandle(THREAD, ok);
+      *inner_is_member = true;
+    }
+    if (outer_klass.is_null()) {
+      // It may be anonymous; try for that.
+      int encl_method_class_idx = k->enclosing_method_class_index();
+      if (encl_method_class_idx != 0) {
+        Klass* ok = i_cp->klass_at(encl_method_class_idx, CHECK_NULL);
+        outer_klass = instanceKlassHandle(THREAD, ok);
+        *inner_is_member = false;
+      }
+    }
+  }
+
+  // If no inner class attribute found for this class.
+  if (outer_klass.is_null())  return NULL;
+
+  // Throws an exception if outer klass has not declared k as an inner klass
+  // We need evidence that each klass knows about the other, or else
+  // the system could allow a spoof of an inner class to gain access rights.
+  Reflection::check_for_inner_class(outer_klass, k, *inner_is_member, CHECK_NULL);
+  return outer_klass();
+}
 
 jint InstanceKlass::compute_modifier_flags(TRAPS) const {
   jint access = access_flags().as_int();
--- a/hotspot/src/share/vm/oops/instanceKlass.hpp	Mon Apr 13 18:49:59 2015 -0700
+++ b/hotspot/src/share/vm/oops/instanceKlass.hpp	Tue Apr 14 18:03:43 2015 +0300
@@ -404,13 +404,17 @@
   bool is_same_class_package(oop classloader2, Symbol* classname2);
   static bool is_same_class_package(oop class_loader1, Symbol* class_name1, oop class_loader2, Symbol* class_name2);
 
-  // find an enclosing class (defined where original code was, in jvm.cpp!)
+  // find an enclosing class
   Klass* compute_enclosing_class(bool* inner_is_member, TRAPS) {
     instanceKlassHandle self(THREAD, this);
     return compute_enclosing_class_impl(self, inner_is_member, THREAD);
   }
   static Klass* compute_enclosing_class_impl(instanceKlassHandle self,
-                                               bool* inner_is_member, TRAPS);
+                                             bool* inner_is_member, TRAPS);
+
+  // Find InnerClasses attribute for k and return outer_class_info_index & inner_name_index.
+  static bool find_inner_classes_attr(instanceKlassHandle k,
+                                      int* ooff, int* noff, TRAPS);
 
   // tell if two classes have the same enclosing class (at package level)
   bool is_same_package_member(Klass* class2, TRAPS) {
--- a/hotspot/src/share/vm/prims/jvm.cpp	Mon Apr 13 18:49:59 2015 -0700
+++ b/hotspot/src/share/vm/prims/jvm.cpp	Tue Apr 14 18:03:43 2015 +0300
@@ -1465,63 +1465,26 @@
 }
 JVM_END
 
-// should be in InstanceKlass.cpp, but is here for historical reasons
-Klass* InstanceKlass::compute_enclosing_class_impl(instanceKlassHandle k,
-                                                     bool* inner_is_member,
-                                                     TRAPS) {
-  Thread* thread = THREAD;
-  InnerClassesIterator iter(k);
-  if (iter.length() == 0) {
-    // No inner class info => no declaring class
+JVM_ENTRY(jstring, JVM_GetSimpleBinaryName(JNIEnv *env, jclass cls))
+{
+  oop mirror = JNIHandles::resolve_non_null(cls);
+  if (java_lang_Class::is_primitive(mirror) ||
+      !java_lang_Class::as_Klass(mirror)->oop_is_instance()) {
     return NULL;
   }
-
-  constantPoolHandle i_cp(thread, k->constants());
-
-  bool found = false;
-  Klass* ok;
-  instanceKlassHandle outer_klass;
-  *inner_is_member = false;
-
-  // Find inner_klass attribute
-  for (; !iter.done() && !found; iter.next()) {
-    int ioff = iter.inner_class_info_index();
-    int ooff = iter.outer_class_info_index();
-    int noff = iter.inner_name_index();
-    if (ioff != 0) {
-      // Check to see if the name matches the class we're looking for
-      // before attempting to find the class.
-      if (i_cp->klass_name_at_matches(k, ioff)) {
-        Klass* inner_klass = i_cp->klass_at(ioff, CHECK_NULL);
-        found = (k() == inner_klass);
-        if (found && ooff != 0) {
-          ok = i_cp->klass_at(ooff, CHECK_NULL);
-          outer_klass = instanceKlassHandle(thread, ok);
-          *inner_is_member = true;
-        }
-      }
+  instanceKlassHandle k(THREAD, InstanceKlass::cast(java_lang_Class::as_Klass(mirror)));
+  int ooff = 0, noff = 0;
+  if (InstanceKlass::find_inner_classes_attr(k, &ooff, &noff, THREAD)) {
+    if (noff != 0) {
+      constantPoolHandle i_cp(thread, k->constants());
+      Symbol* name = i_cp->symbol_at(noff);
+      Handle str = java_lang_String::create_from_symbol(name, CHECK_NULL);
+      return (jstring) JNIHandles::make_local(env, str());
     }
   }
-
-  if (found && outer_klass.is_null()) {
-    // It may be anonymous; try for that.
-    int encl_method_class_idx = k->enclosing_method_class_index();
-    if (encl_method_class_idx != 0) {
-      ok = i_cp->klass_at(encl_method_class_idx, CHECK_NULL);
-      outer_klass = instanceKlassHandle(thread, ok);
-      *inner_is_member = false;
-    }
-  }
-
-  // If no inner class attribute found for this class.
-  if (outer_klass.is_null())  return NULL;
-
-  // Throws an exception if outer klass has not declared k as an inner klass
-  // We need evidence that each klass knows about the other, or else
-  // the system could allow a spoof of an inner class to gain access rights.
-  Reflection::check_for_inner_class(outer_klass, k, *inner_is_member, CHECK_NULL);
-  return outer_klass();
+  return NULL;
 }
+JVM_END
 
 JVM_ENTRY(jstring, JVM_GetClassSignature(JNIEnv *env, jclass cls))
   assert (cls != NULL, "illegal class");
--- a/hotspot/src/share/vm/prims/jvm.h	Mon Apr 13 18:49:59 2015 -0700
+++ b/hotspot/src/share/vm/prims/jvm.h	Tue Apr 14 18:03:43 2015 +0300
@@ -426,6 +426,9 @@
 JNIEXPORT jclass JNICALL
 JVM_GetDeclaringClass(JNIEnv *env, jclass ofClass);
 
+JNIEXPORT jstring JNICALL
+JVM_GetSimpleBinaryName(JNIEnv *env, jclass ofClass);
+
 /* Generics support (JDK 1.5) */
 JNIEXPORT jstring JNICALL
 JVM_GetClassSignature(JNIEnv *env, jclass cls);