<commit_before>/*
 * @author: <NAME>
 * Date: Mar 6, 2003
 * Time: 12:10:11 PM
 */
package com.intellij.ide.plugins.cl;

import com.intellij.ide.plugins.PluginManager;
import com.intellij.openapi.extensions.PluginId;
import com.intellij.util.lang.UrlClassLoader;
import org.jetbrains.annotations.Nullable;
import sun.misc.CompoundEnumeration;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Method;
import java.net.URL;
import java.util.Enumeration;
import java.util.List;

public class PluginClassLoader extends UrlClassLoader {
  private final ClassLoader[] myParents;
  private final PluginId myPluginId;
  private final File myLibDirectory;

  public PluginClassLoader(List<URL> urls, ClassLoader[] parents, PluginId pluginId, File pluginRoot) {
    super(urls, null, true, true);
    myParents = parents;
    myPluginId = pluginId;

    //noinspection HardCodedStringLiteral
    final File file = new File(pluginRoot, "lib");
    myLibDirectory = file.exists()? file : null;
  }


  // changed sequence in which classes are searched, this is essential if plugin uses library, a different version of which
  // is used in IDEA.
  public synchronized Class _loadClass(final String name, final boolean resolve) {
    Class c = findLoadedClass(name);
    if (c == null) {
      c = _findClass(name);
      if (c == null) {
        for (ClassLoader parent : myParents) {
          try {
            c = parent instanceof UrlClassLoader ? ((UrlClassLoader)parent)._loadClass(name, false) : parent.loadClass(name);
            if (c != null) break;
          }
          catch (ClassNotFoundException ignoreAndContinue) {
            // Ignore and continue
          }
        }
        if (c == null) return null;
      }
      else {
        PluginManager.addPluginClass(c.getName(), myPluginId);
      }
    }

    if (resolve) {
      resolveClass(c);
    }


    return c;
  }

  public URL findResource(final String name) {
    final long started = myDebugTime ? System.nanoTime():0;
    
    try {
      final URL resource = findResourceImpl(name);
      if (resource != null) {
        return resource;
      }

      for (ClassLoader parent : myParents) {
        final URL parentResource = fetchResource(parent, name);
        if (parentResource != null) {
          return parentResource;
        }
      }
      return null;
    }<commit_msg>[max] reduced lock's scope (IDEADEV-16375)<commit_after>/*
 * @author: <NAME>
 * Date: Mar 6, 2003
 * Time: 12:10:11 PM
 */
package com.intellij.ide.plugins.cl;

import com.intellij.ide.plugins.PluginManager;
import com.intellij.openapi.extensions.PluginId;
import com.intellij.util.lang.UrlClassLoader;
import org.jetbrains.annotations.Nullable;
import sun.misc.CompoundEnumeration;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Method;
import java.net.URL;
import java.util.Enumeration;
import java.util.List;

@SuppressWarnings({"UseOfSystemOutOrSystemErr"})
public class PluginClassLoader extends UrlClassLoader {
  private final ClassLoader[] myParents;
  private final PluginId myPluginId;
  private final File myLibDirectory;

  public PluginClassLoader(List<URL> urls, ClassLoader[] parents, PluginId pluginId, File pluginRoot) {
    super(urls, null, true, true);
    myParents = parents;
    myPluginId = pluginId;

    //noinspection HardCodedStringLiteral
    final File file = new File(pluginRoot, "lib");
    myLibDirectory = file.exists()? file : null;
  }


  // changed sequence in which classes are searched, this is essential if plugin uses library, a different version of which
  // is used in IDEA.
  public Class _loadClass(final String name, final boolean resolve) {
    Class c = loadClassInsideSelf(name);

    if (c == null) {
      c = loadClassFromParents(name);
    }

    if (c != null && resolve) {
      resolveClass(c);
    }

    return c;
  }

  @Nullable
  private Class loadClassFromParents(final String name) {
    for (ClassLoader parent : myParents) {
      try {
        Class c = parent instanceof UrlClassLoader ? ((UrlClassLoader)parent)._loadClass(name, false) : parent.loadClass(name);
        if (c != null) return c;
      }
      catch (ClassNotFoundException ignoreAndContinue) {
        // Ignore and continue
      }
    }

    return null;
  }

  @Nullable
  private synchronized Class loadClassInsideSelf(final String name) {
    Class c = findLoadedClass(name);
    if (c != null) return c;

    c = _findClass(name);
    if (c != null) {
      PluginManager.addPluginClass(c.getName(), myPluginId);
    }

    return c;
  }

  public URL findResource(final String name) {
    final long started = myDebugTime ? System.nanoTime():0;
    
    try {
      final URL resource = findResourceImpl(name);
      if (resource != null) {
        return resource;
      }

      for (ClassLoader parent : myParents) {
        final URL parentResource = fetchResource(parent, name);
        if (parentResource != null) {
          return parentResource;
        }
      }
      return null;
    }
{'fraction_non_alphanumeric': 0.1009012539184953, 'fraction_numerical': 0.005681818181818182, 'mean_word_length': 3.02602523659306, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4669458', 'n_tokens_mistral': 1557, 'n_tokens_neox': 1491, 'n_words': 421}