<commit_before>package org.micromanager.utils;

import java.awt.Dimension;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.Rectangle;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.lang.reflect.Array;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.jar.JarEntry;
import java.util.jar.JarInputStream;
import java.util.prefs.Preferences;
import java.util.prefs.BackingStoreException;

public class JavaUtils {

   private static final String BACKING_STORE_AVAIL = "BackingStoreAvail";

   /**
    * Add directories and JARs to the classpath, and return the classes found
    * in the process.
    *
    * This method does two things that are really completely separate tasks.
    *
    * First, it adds the given directory and all JARs directly in directory to
    * the search path of the system class loader. If recursionLevel is greater
    * than 0, it does the same with subdirectories, up to that level of
    * nesting.
    *
    * Second, it finds all classes in the directories and JARs that were added
    * to the class path in the first step, and returns them. Classes are found
    * anywhere within the JARs, or as .class files directly within directories.
    *
    * There is no support for .class files contained in a hierarchy of
    * directories representing the package names (they will be loadable because
    * the directory is in the search path, but they will not be included in the
    * returned list unless they are within the recursionLevel).
    *
    * On most errors, an empty list is returned and the error is logged.
    *
    * @param directory The directory to search for classes
    * @param recursionLevel Nesting level for searching subdirectories
    * @return The discovered classes
    */
   public static List<Class<?>> findAndLoadClasses(File directory, int recursionLevel) {
      List<Class<?>> classes = new ArrayList<Class<?>>();
      if (!directory.exists()) {
         return classes;
      }

      final URL directoryURL;
      try {
         directoryURL = directory.toURI().toURL();
      }
      catch (MalformedURLException e) {
         ReportingUtils.logError(e, "Failed to search for classes");
         return classes;
      }

      try {
         addURL(directoryURL);
      }
      catch (IOException ignore) {
         // Logged by addURL()
      }

      File[] files = directory.listFiles();
      for (File file : files) {
         final String fileName = file.getName();
         if (file.isDirectory() && recursionLevel > 0) {
            classes.addAll(findAndLoadClasses(file, recursionLevel - 1));
         } else if (fileName.endsWith(".class")) {
            final String className = stripFilenameExtension(fileName);
            try {
               classes.add(Class.forName(className));
            }
            catch (ClassNotFoundException e) {
               ReportingUtils.logError(e, "Failed to load class: " +
                     className + " (expected in " + fileName + ")");
            }
         } else if (file.getName().endsWith(".jar")) {
            try {
               addURL(new URL("jar:file:" + file.getAbsolutePath() + "!/"));
               JarInputStream jarFile = new JarInputStream(new FileInputStream(file));
               for (JarEntry jarEntry = jarFile.getNextJarEntry();
                       jarEntry != null;
                       jarEntry = jarFile.getNextJarEntry()) {
                  final String classFileName = jarEntry.getName();
                  if (classFileName.endsWith(".class")) {
                     final String className = stripFilenameExtension(classFileName).replace("/", ".");
                     try {
                        classes.add(Class.forName(className));
                     } catch (ClassNotFoundException e) {
                        ReportingUtils.logError(e, "Failed to load class: " +
                              className + " (expected in " +
                              file.getAbsolutePath() + " based on JAR entry");
                     }
                  }
               }
            } catch (Exception e) {
               ReportingUtils.logError(e);
            }
         }
      }

      return classes;
   }

   private static String stripFilenameExtension(String filename) {
      int i = filename.lastIndexOf('.');<commit_msg>mmstudio: catch an exception in the JavaUtils class loading function<commit_after>package org.micromanager.utils;

import java.awt.Dimension;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.Rectangle;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.lang.reflect.Array;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.jar.JarEntry;
import java.util.jar.JarInputStream;
import java.util.prefs.Preferences;
import java.util.prefs.BackingStoreException;

public class JavaUtils {

   private static final String BACKING_STORE_AVAIL = "BackingStoreAvail";

   /**
    * Add directories and JARs to the classpath, and return the classes found
    * in the process.
    *
    * This method does two things that are really completely separate tasks.
    *
    * First, it adds the given directory and all JARs directly in directory to
    * the search path of the system class loader. If recursionLevel is greater
    * than 0, it does the same with subdirectories, up to that level of
    * nesting.
    *
    * Second, it finds all classes in the directories and JARs that were added
    * to the class path in the first step, and returns them. Classes are found
    * anywhere within the JARs, or as .class files directly within directories.
    *
    * There is no support for .class files contained in a hierarchy of
    * directories representing the package names (they will be loadable because
    * the directory is in the search path, but they will not be included in the
    * returned list unless they are within the recursionLevel).
    *
    * On most errors, an empty list is returned and the error is logged.
    *
    * @param directory The directory to search for classes
    * @param recursionLevel Nesting level for searching subdirectories
    * @return The discovered classes
    */
   public static List<Class<?>> findAndLoadClasses(File directory, int recursionLevel) {
      List<Class<?>> classes = new ArrayList<Class<?>>();
      if (!directory.exists()) {
         return classes;
      }

      final URL directoryURL;
      try {
         directoryURL = directory.toURI().toURL();
      }
      catch (MalformedURLException e) {
         ReportingUtils.logError(e, "Failed to search for classes");
         return classes;
      }

      try {
         addURL(directoryURL);
      }
      catch (IOException ignore) {
         // Logged by addURL()
      }

      File[] files = directory.listFiles();
      for (File file : files) {
         final String fileName = file.getName();
         if (file.isDirectory() && recursionLevel > 0) {
            classes.addAll(findAndLoadClasses(file, recursionLevel - 1));
         } else if (fileName.endsWith(".class")) {
            final String className = stripFilenameExtension(fileName);
            try {
               classes.add(Class.forName(className));
            }
            catch (ClassNotFoundException e) {
               ReportingUtils.logError(e, "Failed to load class: " +
                     className + " (expected in " + fileName + ")");
            }
         } else if (file.getName().endsWith(".jar")) {
            try {
               addURL(new URL("jar:file:" + file.getAbsolutePath() + "!/"));
               JarInputStream jarFile = new JarInputStream(new FileInputStream(file));
               for (JarEntry jarEntry = jarFile.getNextJarEntry();
                       jarEntry != null;
                       jarEntry = jarFile.getNextJarEntry()) {
                  final String classFileName = jarEntry.getName();
                  if (classFileName.endsWith(".class")) {
                     final String className = stripFilenameExtension(classFileName).replace("/", ".");
                     try {
                        classes.add(Class.forName(className));
                     } catch (ClassNotFoundException e) {
                        ReportingUtils.logError(e, "Failed to load class: " +
                              className + " (expected in " +
                              file.getAbsolutePath() + " based on JAR entry");
                     } catch (NoClassDefFoundError ncfe) {
                        ReportingUtils.logError(ncfe, "Failed to load class: " +
                              className + " (expected in " +
                              file.getAbsolutePath() + " based on JAR entry");
                     }
                  }
               }
            } catch (Exception e) {
               ReportingUtils.logError(e);
            }
         }
      }

      return classes;
   }

   private static String stripFilenameExtension(String filename) {
      int i = filename.lastIndexOf('.');
{'fraction_non_alphanumeric': 0.09049818217549375, 'fraction_numerical': 0.0005895647047263437, 'mean_word_length': 2.3185523312683403, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 17, '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': '6607872', 'n_tokens_mistral': 2631, 'n_tokens_neox': 2566, 'n_words': 808}