From: l00803608 <liuyulong35@huawei.com>
Subject:  Fix an error caused by anonymous when AppCDS generates jsa
---
 hotspot/src/share/vm/classfile/dictionary.hpp       |  1 +
 hotspot/src/share/vm/classfile/systemDictionary.cpp | 13 ++++++++++++-
 hotspot/src/share/vm/classfile/systemDictionary.hpp |  1 +
 hotspot/src/share/vm/memory/metaspaceShared.cpp     |  8 ++++----
 hotspot/src/share/vm/prims/unsafe.cpp               |  6 ------
 5 files changed, 18 insertions(+), 11 deletions(-)

diff --git a/hotspot/src/share/vm/classfile/dictionary.hpp b/hotspot/src/share/vm/classfile/dictionary.hpp
index 8a88fa2e4..e5d98184b 100644
--- a/hotspot/src/share/vm/classfile/dictionary.hpp
+++ b/hotspot/src/share/vm/classfile/dictionary.hpp
@@ -394,6 +394,7 @@ class SymbolPropertyEntry : public HashtableEntry<Symbol*, mtSymbol> {
 // MethodHandle.invoke(S)T, for all signatures (S)T.
 class SymbolPropertyTable : public Hashtable<Symbol*, mtSymbol> {
   friend class VMStructs;
+  friend class SystemDictionary;
 private:
   SymbolPropertyEntry* bucket(int i) {
     return (SymbolPropertyEntry*) Hashtable<Symbol*, mtSymbol>::bucket(i);
diff --git a/hotspot/src/share/vm/classfile/systemDictionary.cpp b/hotspot/src/share/vm/classfile/systemDictionary.cpp
index c8f66e830..59ab829c7 100644
--- a/hotspot/src/share/vm/classfile/systemDictionary.cpp
+++ b/hotspot/src/share/vm/classfile/systemDictionary.cpp
@@ -1031,7 +1031,6 @@ Klass* SystemDictionary::parse_stream(Symbol* class_name,
     // as the host_klass
     assert(EnableInvokeDynamic, "");
     guarantee(host_klass->class_loader() == class_loader(), "should be the same");
-    guarantee(!DumpSharedSpaces, "must not create anonymous classes when dumping");
     loader_data = ClassLoaderData::anonymous_class_loader_data(class_loader(), CHECK_NULL);
     loader_data->record_dependency(host_klass(), CHECK_NULL);
   } else {
@@ -1498,6 +1497,18 @@ instanceKlassHandle SystemDictionary::load_shared_class(instanceKlassHandle ik,
   }
   return ik;
 }
+
+void SystemDictionary::clear_invoke_method_table() {
+  SymbolPropertyEntry* spe = NULL;
+  for (int index = 0; index < _invoke_method_table->table_size(); index++) {
+    SymbolPropertyEntry* p = _invoke_method_table->bucket(index);
+    while (p != NULL) {
+      spe = p;
+      p = p->next();
+      _invoke_method_table->free_entry(spe);
+    }
+  }
+}
 #endif // INCLUDE_CDS
 
 instanceKlassHandle SystemDictionary::load_instance_class(Symbol* class_name, Handle class_loader, TRAPS) {
diff --git a/hotspot/src/share/vm/classfile/systemDictionary.hpp b/hotspot/src/share/vm/classfile/systemDictionary.hpp
index e39c1de62..83ca3794b 100644
--- a/hotspot/src/share/vm/classfile/systemDictionary.hpp
+++ b/hotspot/src/share/vm/classfile/systemDictionary.hpp
@@ -652,6 +652,7 @@ public:
   static bool is_ext_class_loader(Handle class_loader);
   static bool is_app_class_loader(Handle class_loader);
   static bool is_builtin_loader(Handle class_loader);
+  static void clear_invoke_method_table();
 
 protected:
   static Klass* find_shared_class(Symbol* class_name);
diff --git a/hotspot/src/share/vm/memory/metaspaceShared.cpp b/hotspot/src/share/vm/memory/metaspaceShared.cpp
index eea79cc09..1576f4fbc 100644
--- a/hotspot/src/share/vm/memory/metaspaceShared.cpp
+++ b/hotspot/src/share/vm/memory/metaspaceShared.cpp
@@ -793,6 +793,10 @@ void MetaspaceShared::preload_and_dump(TRAPS) {
   link_and_cleanup_shared_classes(CATCH);
   tty->print_cr("Rewriting and linking classes: done");
 
+  tty->print("clear _invoke_method_table ...");
+  SystemDictionary::clear_invoke_method_table();
+  tty->print_cr(" done");
+
   // At this point, many classes have been loaded.
   // Gather systemDictionary classes in a global array and do everything to
   // that so we don't have to walk the SystemDictionary again.
@@ -829,10 +833,6 @@ int MetaspaceShared::preload_and_dump(const char * class_list_path,
       TempNewSymbol class_name_symbol = SymbolTable::new_permanent_symbol(class_name, THREAD);
       guarantee(!HAS_PENDING_EXCEPTION, "Exception creating a symbol.");
 
-      // If preload_and_dump has anonymous class failed ,pls del this class_name in classlist
-      if (TraceClassLoading) {
-        tty->print_cr("preload_and_dump start: %s", class_name);
-      }
       Handle loader = UseAppCDS ? SystemDictionary::java_system_loader() : Handle();
       Klass* klass = SystemDictionary::resolve_or_null(class_name_symbol,
                                                        loader,
diff --git a/hotspot/src/share/vm/prims/unsafe.cpp b/hotspot/src/share/vm/prims/unsafe.cpp
index d6c33dd33..f1bd5799d 100644
--- a/hotspot/src/share/vm/prims/unsafe.cpp
+++ b/hotspot/src/share/vm/prims/unsafe.cpp
@@ -1040,12 +1040,6 @@ Unsafe_DefineAnonymousClass_impl(JNIEnv *env,
                                  HeapWord* *temp_alloc,
                                  TRAPS) {
 
-  if (DumpSharedSpaces) {
-    tty->print_cr("failed: must not create anonymous classes when dumping.");
-    tty->print_cr("Please delete the last class_name prefixed with \"preload_and_dump start\" from -XX:SharedClassListFile to avoid anonymous classes.");
-    JVM_Halt(0);
-  }
-
   if (UsePerfData) {
     ClassLoader::unsafe_defineClassCallCounter()->inc();
   }
-- 
2.19.1

