<commit_before>/*
 * Copyright 2014 <NAME> (http://frugalmechanic.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package fm.common

import java.lang.annotation.Annotation
import java.lang.reflect.Modifier
import java.net.{URLConnection, URLDecoder}
import java.io.{File, InputStream}
import java.nio.file.Path
import java.util.jar.{JarFile, JarEntry}
import scala.collection.JavaConverters._

import Implicits._

/**
 * This contains utility methods for scanning Classes or Files on the classpath.
 * 
 * Originally we used the classpath scanning functionality in the Spring Framework
 * and then later switched to the Reflections library (https://code.google.com/p/reflections/)
 * to avoid the dependency on Spring.  At some point we ran into issues with the Reflections
 * library not properly detecting classes so I ended up writing this as a replacement.
 */
object ClassUtil extends Logging {

  /**
   * Check if a class exists.
   */<commit_msg>Added findLoadedClass to ClassUtil that uses reflection to call findLoadedClass on the ClassLoader<commit_after>/*
 * Copyright 2014 <NAME> (http://frugalmechanic.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package fm.common

import java.lang.annotation.Annotation
import java.lang.reflect.{Method, Modifier}
import java.net.{URLConnection, URLDecoder}
import java.io.{File, InputStream}
import java.nio.file.Path
import java.util.jar.{JarFile, JarEntry}
import scala.collection.JavaConverters._

import Implicits._

/**
 * This contains utility methods for scanning Classes or Files on the classpath.
 * 
 * Originally we used the classpath scanning functionality in the Spring Framework
 * and then later switched to the Reflections library (https://code.google.com/p/reflections/)
 * to avoid the dependency on Spring.  At some point we ran into issues with the Reflections
 * library not properly detecting classes so I ended up writing this as a replacement.
 */
object ClassUtil extends Logging {

  /**
   * Check if a class is loaded
   */
  def isClassLoaded(cls: String, classLoader: ClassLoader = defaultClassLoader): Boolean = {
    findLoadedClass(cls, classLoader).isDefined
  }
  
  def findLoadedClass(cls: String, classLoader: ClassLoader = defaultClassLoader): Option[Class[_]] = {
    val findLoadedClass: Method = classOf[ClassLoader].getDeclaredMethod("findLoadedClass", classOf[String])
    findLoadedClass.setAccessible(true)
    val res: Object = findLoadedClass.invoke(classLoader, cls)
    if (null == res) None else Some(res.asInstanceOf[Class[_]])
  }
  
  /**
   * Check if a class exists.
   */
{'fraction_non_alphanumeric': 0.08204419889502762, 'fraction_numerical': 0.004419889502762431, 'mean_word_length': 4.172857142857143, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, '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': '5095118', 'n_tokens_mistral': 977, 'n_tokens_neox': 951, 'n_words': 402}