<commit_before>package wala

import com.ibm.wala.classLoader.IMethod
import scala.collection.JavaConversions._
import com.ibm.wala.ipa.callgraph.Context
import com.ibm.wala.ipa.callgraph.Context
import com.ibm.wala.classLoader.IClass
import com.ibm.wala.classLoader.ShrikeBTMethod
import com.ibm.wala.util.intset.IntSet
import com.ibm.wala.util.intset.IntSetAction
import com.ibm.wala.types.TypeReference
import com.ibm.wala.types.ClassLoaderReference
import com.ibm.wala.ipa.callgraph.ContextKey
import com.ibm.wala.ipa.callgraph.DelegatingContext
import com.ibm.wala.util.Predicate
import scala.collection._
import com.ibm.wala.util.intset.SparseIntSet
import com.ibm.wala.ipa.slicer.Statement

class WALAConversions extends TypeAliases with WALAConversionsForN with WALAConversionsForP {
  trait Named {
    def name(): String
  }

  implicit def makePredicateFromFunction[T](f: Function1[T, Boolean]) = new Predicate[T] {
    def test(t: T) = f(t)
  }

  implicit def makeIntSetActionFromFunction(f: Function1[Int, Unit]) = new IntSetAction {
    def act(t: Int) = f(t)
  }

  implicit def m2named(m: M): Named = new Named {
    def name = m.getSelector().getName().toString()
  }

  implicit def c2named(c: C): Named = new Named {
    def name = c.getName().getClassName().toString()
  }

  trait PrettyPrintable {
    def prettyPrint(): String
  }

  implicit def m2prettyprintable(m: IMethod): PrettyPrintable = new PrettyPrintable {
    def prettyPrint(): String = {
      val packageName = m.getDeclaringClass().getName().getPackage().toString().replace('/', '.')
      packageName + "." + m.getDeclaringClass().name + "." + m.name
    }
  }

  implicit def type2prettyprintable(t: TypeReference): PrettyPrintable = new PrettyPrintable {
    def prettyPrint(): String = {
      val packageName = t.getName().getPackage().toString().replace('/', '.')
      packageName + "." + t.getName().getClassName()
    }
  }

  implicit def c2prettyprintable(t: C): PrettyPrintable = new PrettyPrintable {
    def prettyPrint(): String =
      try {
        val packageName = t.getName().getPackage().toString().replace('/', '.')
        packageName + "." + t.getName().getClassName()
      } catch {
        case e: NullPointerException => "null"
      }
  }

  // method
  object M {
    def unapply(m: M): Option[(IClass, String)] = {
      Some(m.getDeclaringClass(), m.getSelector().toString())
    }
  }

  // class, iclass
  object C {
    def unapply(c: C): Option[(String, String)] = {
      Some(c.getName().getPackage().toString(), c.getName().getClassName().toString())
    }
  }

  def codeLocation(n: N, bytecodeIndex: Int): String = {
    printCodeLocation(n.getMethod(), bytecodeIndex)
  }

  implicit def mWithLineNo(m: M) = new {
    def lineNoFromBytecodeIndex(bytecodeIndex: Int) = m match {
      case m: ShrikeBTMethod => m.getLineNumber(bytecodeIndex)
      case _ => -1
    }
    def lineNoFromIRNo(irNo: Int) = lineNoFromBytecodeIndex(m.asInstanceOf[ShrikeBTMethod].getBytecodeIndex(irNo))
  }

  def printCodeLocation(m: IMethod, bytecodeIndex: Int): String = {
    val lineNo = m.lineNoFromBytecodeIndex(bytecodeIndex)
    val className = m.getDeclaringClass().getName().getClassName().toString()
    "" + m.prettyPrint + "(" + className + ".java:" + lineNo + ")"
  }

  implicit def iWithEasyUses(i: I) = new {
    def uses: Iterable[V] = Stream.range(0, i.getNumberOfUses()).map(index => { i.getUse(index) })
  }

  implicit def o2prettyprintable(o: O): PrettyPrintable = new PrettyPrintable {
    def prettyPrint(): String = O.prettyPrint(o)
  }

  implicit def f2prettyprintable(f: F): PrettyPrintable = new PrettyPrintable {
    def prettyPrint(): String = f.getName().toString()
  }

  class WrappedIntSet(s: IntSet) {
    def contains(key: Int) = s.contains(key)
    def iterator: Iterator[Int] = {
      val it = s.intIterator()
      new Iterator[Int] {
        def hasNext = it.hasNext()
        def next = it.next()
      }
    }
    def +(elem: Int) = s.union(SparseIntSet.singleton(elem))
    def -(elem: Int) = throw new Exception("unsupported, implement this if you need it")

    def |(other: IntSet) = s.union(other)
    def &(other: IntSet) = s.intersection(other)
    def intersects(other: IntSet) = s.containsAny(other)

    def foreach[U](f: Int => U) = {
      s.foreach(new IntSetAction() {
        override def act(x: Int) = f(x)
      })
    }
  }

  implicit def intsetSet(s: IntSet) = new WrappedIntSet(s)

  def inApplicationScope(n: N): Boolean = inApplicationScope(n.m)
  def inApplicationScope(m: M): Boolean = {
    val classLoader = m.getDeclaringClass().getClassLoader();
    classLoader.getReference() == ClassLoaderReference.Application
  }

  def inPrimordialScope(n: N): Boolean = inPrimordialScope(n.m)
  def inPrimordialScope(m: M) = {
    val classLoader = m.getDeclaringClass().getClassLoader();
    classLoader.getReference() == ClassLoaderReference.Primordial<commit_msg>made WrappedIntSet extend the Set[Int] trait<commit_after>package wala

import com.ibm.wala.classLoader.IMethod
import scala.collection.JavaConversions._
import com.ibm.wala.ipa.callgraph.Context
import com.ibm.wala.ipa.callgraph.Context
import com.ibm.wala.classLoader.IClass
import com.ibm.wala.classLoader.ShrikeBTMethod
import com.ibm.wala.util.intset.IntSet
import com.ibm.wala.util.intset.IntSetAction
import com.ibm.wala.types.TypeReference
import com.ibm.wala.types.ClassLoaderReference
import com.ibm.wala.ipa.callgraph.ContextKey
import com.ibm.wala.ipa.callgraph.DelegatingContext
import com.ibm.wala.util.Predicate
import scala.collection._
import com.ibm.wala.util.intset.SparseIntSet
import com.ibm.wala.ipa.slicer.Statement
import wala.WALAConversions.WrappedIntSet

class WALAConversions extends TypeAliases with WALAConversionsForN with WALAConversionsForP {
  trait Named {
    def name(): String
  }

  implicit def makePredicateFromFunction[T](f: Function1[T, Boolean]) = new Predicate[T] {
    def test(t: T) = f(t)
  }

  implicit def makeIntSetActionFromFunction(f: Function1[Int, Unit]) = new IntSetAction {
    def act(t: Int) = f(t)
  }

  implicit def m2named(m: M): Named = new Named {
    def name = m.getSelector().getName().toString()
  }

  implicit def c2named(c: C): Named = new Named {
    def name = c.getName().getClassName().toString()
  }

  trait PrettyPrintable {
    def prettyPrint(): String
  }

  implicit def m2prettyprintable(m: IMethod): PrettyPrintable = new PrettyPrintable {
    def prettyPrint(): String = {
      val packageName = m.getDeclaringClass().getName().getPackage().toString().replace('/', '.')
      packageName + "." + m.getDeclaringClass().name + "." + m.name
    }
  }

  implicit def type2prettyprintable(t: TypeReference): PrettyPrintable = new PrettyPrintable {
    def prettyPrint(): String = {
      val packageName = t.getName().getPackage().toString().replace('/', '.')
      packageName + "." + t.getName().getClassName()
    }
  }

  implicit def c2prettyprintable(t: C): PrettyPrintable = new PrettyPrintable {
    def prettyPrint(): String =
      try {
        val packageName = t.getName().getPackage().toString().replace('/', '.')
        packageName + "." + t.getName().getClassName()
      } catch {
        case e: NullPointerException => "null"
      }
  }

  // method
  object M {
    def unapply(m: M): Option[(IClass, String)] = {
      Some(m.getDeclaringClass(), m.getSelector().toString())
    }
  }

  // class, iclass
  object C {
    def unapply(c: C): Option[(String, String)] = {
      Some(c.getName().getPackage().toString(), c.getName().getClassName().toString())
    }
  }

  def codeLocation(n: N, bytecodeIndex: Int): String = {
    printCodeLocation(n.getMethod(), bytecodeIndex)
  }

  implicit def mWithLineNo(m: M) = new {
    def lineNoFromBytecodeIndex(bytecodeIndex: Int) = m match {
      case m: ShrikeBTMethod => m.getLineNumber(bytecodeIndex)
      case _ => -1
    }
    def lineNoFromIRNo(irNo: Int) = lineNoFromBytecodeIndex(m.asInstanceOf[ShrikeBTMethod].getBytecodeIndex(irNo))
  }

  def printCodeLocation(m: IMethod, bytecodeIndex: Int): String = {
    val lineNo = m.lineNoFromBytecodeIndex(bytecodeIndex)
    val className = m.getDeclaringClass().getName().getClassName().toString()
    "" + m.prettyPrint + "(" + className + ".java:" + lineNo + ")"
  }

  implicit def iWithEasyUses(i: I) = new {
    def uses: Iterable[V] = Stream.range(0, i.getNumberOfUses()).map(index => { i.getUse(index) })
  }

  implicit def o2prettyprintable(o: O): PrettyPrintable = new PrettyPrintable {
    def prettyPrint(): String = O.prettyPrint(o)
  }

  implicit def f2prettyprintable(f: F): PrettyPrintable = new PrettyPrintable {
    def prettyPrint(): String = f.getName().toString()
  }

  class WrappedIntSet(s: IntSet) extends Set[Int] {
    def contains(key: Int) = s.contains(key)
    def iterator: Iterator[Int] = {
      val it = s.intIterator()
      new Iterator[Int] {
        def hasNext = it.hasNext()
        def next = it.next()
      }
    }
    def +(elem: Int) = new WrappedIntSet(s.union(SparseIntSet.singleton(elem)))
    def -(elem: Int) = throw new Exception("unsupported, implement this if you need it")

    def |(other: IntSet) = s.union(other)
    def &(other: IntSet) = s.intersection(other)
    def intersects(other: IntSet) = s.containsAny(other)

    override def foreach[U](f: Int => U) = {
      s.foreach(new IntSetAction() {
        override def act(x: Int) = f(x)
      })
    }
  }

  implicit def intsetSet(s: IntSet) = new WrappedIntSet(s)

  def inApplicationScope(n: N): Boolean = inApplicationScope(n.m)
  def inApplicationScope(m: M): Boolean = {
    val classLoader = m.getDeclaringClass().getClassLoader();
    classLoader.getReference() == ClassLoaderReference.Application
  }

  def inPrimordialScope(n: N): Boolean = inPrimordialScope(n.m)
  def inPrimordialScope(m: M) = {
    val classLoader = m.getDeclaringClass().getClassLoader();
    classLoader.getReference() == ClassLoaderReference.Primordial
{'fraction_non_alphanumeric': 0.1362545018007203, 'fraction_numerical': 0.0022008803521408565, 'mean_word_length': 3.7066854990583806, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 13, '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': '1874660', 'n_tokens_mistral': 3331, 'n_tokens_neox': 3304, 'n_words': 684}