All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.fburato.highwheelmodules.bytecodeparser.classpath.DirectoryClassPathRoot.scala Maven / Gradle / Ivy

package com.github.fburato.highwheelmodules.bytecodeparser.classpath

import com.github.fburato.highwheelmodules.model.bytecode.ElementName
import com.github.fburato.highwheelmodules.model.classpath.ClasspathRoot

import java.io.{File, FileInputStream, InputStream}
import scala.annotation.tailrec
import scala.collection.mutable
import scala.util.Try

class DirectoryClassPathRoot(root: File) extends ClasspathRoot {
  override def getData(elementName: ElementName): Try[Option[InputStream]] =
    getResource(elementName.asJavaName.replace('.', File.separatorChar).concat(".class"))

  override def classNames: Try[Seq[ElementName]] = Try {
    def fileToClassName(f: File): ElementName = ElementName.fromString(
      f.getAbsolutePath
        .substring(root.getAbsolutePath.length + 1, f.getAbsolutePath.length - ".class".length)
        .replace(File.separatorChar, '.')
    )

    @tailrec
    def classNames(
      accumulated: mutable.ArrayBuffer[ElementName],
      toProcess: mutable.ArrayBuffer[File]
    ): mutable.ArrayBuffer[ElementName] = toProcess.headOption match {
      case None => accumulated
      case Some(f) =>
        if (!f.exists) {
          classNames(accumulated, toProcess.tail)
        } else if (!f.isDirectory) {
          accumulated.append(fileToClassName(f))
          classNames(accumulated, toProcess.tail)
        } else {
          classNames(accumulated, toProcess.tail ++ f.listFiles())
        }
    }

    classNames(mutable.ArrayBuffer(), mutable.ArrayBuffer(root)).toSeq
  }

  override def getResource(name: String): Try[Option[InputStream]] = Try {
    val file = new File(root, name)
    Some(file)
      .filter(_.canRead)
      .map(new FileInputStream(_))
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy