<commit_before>/*
 * Copyright (C) 2011 <NAME>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.openmole.core.tools.obj

import _root_.groovy.lang.GroovyShell
import org.openmole.core.exception.{ InternalProcessingError, UserBadDataError }
import scala.annotation.tailrec
import scala.collection.mutable
import scala.collection.mutable.ListBuffer
import java.lang.reflect.{ Type ⇒ JType, Array ⇒ _, _ }
import scala.reflect.Manifest.{ classType, intersectionType, arrayType, wildcardType }
import scala.reflect.ClassManifest

object ClassUtils {

  implicit class ClassDecorator[T](c: Class[T]) {
    def equivalence = classEquivalence(c).asInstanceOf[Class[T]]

    def listSuperClasses = {
      new Iterator[Class[_]] {
        var cur: Class[_] = c

        override def hasNext = cur != null

        override def next: Class[_] = {
          val ret = cur
          cur = cur.getSuperclass
          ret
        }
      }.toIterable
    }

    def listSuperClassesAndInterfaces = listIndexedSuperClassesAndInterfaces.unzip._1

    def listIndexedSuperClassesAndInterfaces = {
      val toExplore = new ListBuffer[Class[_]]
      toExplore += c

      val ret = new ListBuffer[(Class[_], Int)]
      var index = 0

      while (!toExplore.isEmpty) {
        val current = toExplore.remove(0)
        ret += current -> index
        val superClass = current.getSuperclass
        if (superClass != null) toExplore += superClass
        for (inter ← current.getInterfaces) toExplore += inter
        index += 1
      }
      ret
    }

    def listImplementedInterfaces = {
      val toExplore = new ListBuffer[Class[_]]
      toExplore += c

      val ret = new ListBuffer[Class[_]]

      while (!toExplore.isEmpty) {
        val current = toExplore.remove(0)

        val superClass = current.getSuperclass
        if (superClass != null) toExplore += superClass

        for (inter ← current.getInterfaces) {
          toExplore += inter
          ret += inter
        }
      }

      ret
    }

    def allDeclaredFields = listSuperClassesAndInterfaces.flatMap(_.getDeclaredFields)

    def fromArray = c.getComponentType

    def toManifest = classType[T](c)

  }

  implicit class ObjectClassDecorator(o: Any) {
    def allRelatedObjects: Seq[Any] = {
      val objects = mutable.HashSet[Any]()
      val toCrawl = mutable.Stack[Any]()

      toCrawl push o

      while (!toCrawl.isEmpty) {
        val cur = toCrawl pop ()
        cur match {
          case null                        ⇒
          case r if r.getClass.isPrimitive ⇒ objects += o
          case r: java.lang.Number         ⇒ objects += o
          case r: AnyRef ⇒
            if (!objects.contains(r)) {
              objects += r
              for {
                f ← r.getClass.allDeclaredFields
              } {
                val access = f.isAccessible
                f.setAccessible(true)
                val v = try f.get(r) finally f.setAccessible(access)
                toCrawl push v
              }
            }
          case o ⇒ throw new InternalProcessingError("Unexpected type " + o.getClass)
        }
      }

      objects.toVector
    }
  }
<commit_msg>[Core] fix: avoid crawling into the Class class.<commit_after>/*
 * Copyright (C) 2011 <NAME>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.openmole.core.tools.obj

import _root_.groovy.lang.GroovyShell
import org.openmole.core.exception.{ InternalProcessingError, UserBadDataError }
import scala.annotation.tailrec
import scala.collection.mutable
import scala.collection.mutable.ListBuffer
import java.lang.reflect.{ Type ⇒ JType, Array ⇒ _, _ }
import scala.reflect.Manifest.{ classType, intersectionType, arrayType, wildcardType }
import scala.reflect.ClassManifest

object ClassUtils {

  implicit class ClassDecorator[T](c: Class[T]) {
    def equivalence = classEquivalence(c).asInstanceOf[Class[T]]

    def listSuperClasses = {
      new Iterator[Class[_]] {
        var cur: Class[_] = c

        override def hasNext = cur != null

        override def next: Class[_] = {
          val ret = cur
          cur = cur.getSuperclass
          ret
        }
      }.toIterable
    }

    def listSuperClassesAndInterfaces = listIndexedSuperClassesAndInterfaces.unzip._1

    def listIndexedSuperClassesAndInterfaces = {
      val toExplore = new ListBuffer[Class[_]]
      toExplore += c

      val ret = new ListBuffer[(Class[_], Int)]
      var index = 0

      while (!toExplore.isEmpty) {
        val current = toExplore.remove(0)
        ret += current -> index
        val superClass = current.getSuperclass
        if (superClass != null) toExplore += superClass
        for (inter ← current.getInterfaces) toExplore += inter
        index += 1
      }
      ret
    }

    def listImplementedInterfaces = {
      val toExplore = new ListBuffer[Class[_]]
      toExplore += c

      val ret = new ListBuffer[Class[_]]

      while (!toExplore.isEmpty) {
        val current = toExplore.remove(0)

        val superClass = current.getSuperclass
        if (superClass != null) toExplore += superClass

        for (inter ← current.getInterfaces) {
          toExplore += inter
          ret += inter
        }
      }

      ret
    }

    def allDeclaredFields = listSuperClassesAndInterfaces.flatMap(_.getDeclaredFields)

    def fromArray = c.getComponentType

    def toManifest = classType[T](c)

  }

  implicit class ObjectClassDecorator(o: Any) {
    def allRelatedObjects: Seq[Any] = {
      val objects = mutable.HashSet[Any]()
      val toCrawl = mutable.Stack[Any]()

      toCrawl push o

      while (!toCrawl.isEmpty) {
        val cur = toCrawl pop ()
        cur match {
          case null                        ⇒
          case r if r.getClass.isPrimitive ⇒ objects += o
          case r: java.lang.Number         ⇒ objects += o
          case r: java.lang.Class => objects += o
          case r: AnyRef ⇒
            if (!objects.contains(r)) {
              objects += r
              for {
                f ← r.getClass.allDeclaredFields
              } {
                val access = f.isAccessible
                f.setAccessible(true)
                val v = try f.get(r) finally f.setAccessible(access)
                toCrawl push v
              }
            }
          case o ⇒ throw new InternalProcessingError("Unexpected type " + o.getClass)
        }
      }

      objects.toVector
    }
  }

{'fraction_non_alphanumeric': 0.08579612029683635, 'fraction_numerical': 0.0026038276266111184, 'mean_word_length': 2.2578456318914335, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 10, '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': '1121722', 'n_tokens_mistral': 2238, 'n_tokens_neox': 2155, 'n_words': 672}