<commit_before>/*
 * This file is 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 org.tresamigos.smv

import java.io.{InputStream, StringWriter, PrintWriter}
import java.util.zip.CRC32

import scala.util.control.NonFatal
import scala.tools.asm

/**
 * computes the CRC32 checksum for the code of the given class name.
 * The class must be reachable through the configured java class path.
 */
private[smv] case class ClassCRC(
  className: String,
  classLoader : ClassLoader = getClass.getClassLoader) {
  import ClassCRC._
  lazy val crc: Long = checksum(className, classLoader).getValue
}

object ClassCRC {
  private[smv] def cksum0(acc: CRC32, reader: asm.ClassReader): CRC32 = {
    val code = AsmUtil.asmTrace(reader).getBytes("UTF-8")
    acc.update(code)
    acc
  }

  def checksum(bytecode: Array[Byte]): CRC32 = cksum0(new CRC32, new asm.ClassReader(bytecode))

  /**
   * Compute a checksum of the bytecode of the class and all its
   * supertypes in linearized order, excluding java.lang.Object and
   * scala.Any
   */
  def checksum(className: String, classLoader: ClassLoader): CRC32 = {
    /** calculate CRC for a single class */
    def step(fqn: String, crc: CRC32): CRC32 = {
      println(fqn)
      val classResourcePath = fqn.replace('.', '/') + ".class"
      val is: InputStream = classLoader.getResourceAsStream(classResourcePath)

      try {
        cksum0(crc, new asm.ClassReader(is))
      } catch {
        case NonFatal(t) => throw new IllegalArgumentException("invalid class name for crc: " + fqn, t)
      } finally {
        if (is != null) is.close
      }
    }

    // basesOf returns the parents but not include the caller class, added to the end
    val bases = new SmvReflection(classLoader).basesOf(className).filter(s =>
      !s.startsWith("java.") && !s.startsWith("scala.") && !s.startsWith("org.tresamigos.smv.")
    ) :+ className

    bases.foldRight(new CRC32)((e, acc) => step(e, acc))
  }

}<commit_msg>Removed the debug leftover from ClassCRC fix<commit_after>/*
 * This file is 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 org.tresamigos.smv

import java.io.{InputStream, StringWriter, PrintWriter}
import java.util.zip.CRC32

import scala.util.control.NonFatal
import scala.tools.asm

/**
 * computes the CRC32 checksum for the code of the given class name.
 * The class must be reachable through the configured java class path.
 */
private[smv] case class ClassCRC(
  className: String,
  classLoader : ClassLoader = getClass.getClassLoader) {
  import ClassCRC._
  lazy val crc: Long = checksum(className, classLoader).getValue
}

object ClassCRC {
  private[smv] def cksum0(acc: CRC32, reader: asm.ClassReader): CRC32 = {
    val code = AsmUtil.asmTrace(reader).getBytes("UTF-8")
    acc.update(code)
    acc
  }

  def checksum(bytecode: Array[Byte]): CRC32 = cksum0(new CRC32, new asm.ClassReader(bytecode))

  /**
   * Compute a checksum of the bytecode of the class and all its
   * supertypes in linearized order, excluding java.lang.Object and
   * scala.Any
   */
  def checksum(className: String, classLoader: ClassLoader): CRC32 = {
    /** calculate CRC for a single class */
    def step(fqn: String, crc: CRC32): CRC32 = {
      val classResourcePath = fqn.replace('.', '/') + ".class"
      val is: InputStream = classLoader.getResourceAsStream(classResourcePath)

      try {
        cksum0(crc, new asm.ClassReader(is))
      } catch {
        case NonFatal(t) => throw new IllegalArgumentException("invalid class name for crc: " + fqn, t)
      } finally {
        if (is != null) is.close
      }
    }

    // basesOf returns the parents but not include the caller class, added to the end
    val bases = new SmvReflection(classLoader).basesOf(className).filter(s =>
      !s.startsWith("java.") && !s.startsWith("scala.") && !s.startsWith("org.tresamigos.smv.")
    ) :+ className

    bases.foldRight(new CRC32)((e, acc) => step(e, acc))
  }

}
{'fraction_non_alphanumeric': 0.10681176944780331, 'fraction_numerical': 0.01128577186618299, 'mean_word_length': 3.4873417721518987, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, 'https://': 0, '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': '2267379', 'n_tokens_mistral': 1538, 'n_tokens_neox': 1485, 'n_words': 507}