package org.jetbrains.plugins.groovy.util;

import com.intellij.util.containers.ConcurrentHashMap;
import org.jetbrains.annotations.NotNull;

/**
 * @author Sergey Evdokimov
 */
public class ClassInstanceCache {

  private static final ConcurrentHashMap<String, Object> CACHE = new ConcurrentHashMap<String, Object>();

  private ClassInstanceCache() {
  }

  @SuppressWarnings("unchecked")
  public static <T> T getInstance(@NotNull String className, ClassLoader classLoader) {
    Object res = CACHE.get(className);
    if (res == null) {
      try {
        res = classLoader.loadClass(className).newInstance();
      }
      catch (Exception e) {
        throw new RuntimeException(e);
      }

      Object oldValue = CACHE.putIfAbsent(className, res);
      if (oldValue != null) {
        res = oldValue;
      }
    }

    return (T)res;
  }
}

@@ -37,16 +37,117 @@
 .util;%0A%0A
+import com.intellij.ide.plugins.IdeaPluginDescriptor;%0Aimport com.intellij.ide.plugins.PluginManager;%0A
 import c
@@ -193,16 +193,16 @@
 ashMap;%0A
-
 import o
@@ -452,16 +452,636 @@
  %7B%0A  %7D%0A%0A
+  private static Object createInstance(@NotNull String className) %7B%0A    try %7B%0A      try %7B%0A        return Class.forName(className).newInstance();%0A      %7D%0A      catch (ClassNotFoundException e) %7B%0A        for (IdeaPluginDescriptor descriptor : PluginManager.getPlugins()) %7B%0A          try %7B%0A            return descriptor.getPluginClassLoader().loadClass(className).newInstance();%0A          %7D%0A          catch (ClassNotFoundException ignored) %7B%0A%0A          %7D%0A        %7D%0A%0A        throw new RuntimeException(%22Class not found: %22 + className);%0A      %7D%0A    %7D%0A    catch (Exception e) %7B%0A      throw new RuntimeException(e);%0A    %7D%0A  %7D%0A%0A
   @Suppr
@@ -1265,158 +1265,40 @@
     
-try %7B%0A        res = classLoader.loadClass(className).newInstance();%0A      %7D%0A      catch (Exception e) %7B%0A        throw new RuntimeException(e);%0A      %7D
+res = createInstance(className);
 %0A%0A  
@@ -1442,14 +1442,17 @@
 (T)res;%0A
-
   %7D%0A
+  %0A
 %7D%0A

Improve class loading in GroovyMethodInfo. (cf9bf0e)