Skip to content

Commit

Permalink
refactor: restructured repository
Browse files Browse the repository at this point in the history
  • Loading branch information
jenspots committed Apr 23, 2024
1 parent 099e0ff commit 6f60790
Show file tree
Hide file tree
Showing 21 changed files with 483 additions and 355 deletions.
7 changes: 7 additions & 0 deletions src/main/java/Template.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import java.util.logging.Logger;

import static technology.idlab.logging.LoggerKt.createLogger;

public class Template {
protected Logger logger = createLogger();
}
65 changes: 0 additions & 65 deletions src/main/kotlin/Configuration.kt

This file was deleted.

9 changes: 5 additions & 4 deletions src/main/kotlin/Main.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package technology.idlab

import technology.idlab.runner.Pipeline
import java.io.File
import kotlin.system.exitProcess

Expand All @@ -11,10 +12,10 @@ fun main(args: Array<String>) {
}

// Parse and load the configuration.
val relativePath = args[0]
val absolutePath = File(relativePath).absoluteFile
val config = Configuration(absolutePath.toString())
val configPath = args[0]
val config = File(configPath)
val pipeline = Pipeline(config)

// Execute all functions.
config.executeSync()
pipeline.executeSync()
}
119 changes: 0 additions & 119 deletions src/main/kotlin/Processor.kt

This file was deleted.

51 changes: 0 additions & 51 deletions src/main/kotlin/compiler/CodeHandler.kt

This file was deleted.

61 changes: 61 additions & 0 deletions src/main/kotlin/compiler/Compiler.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package technology.idlab.compiler

import technology.idlab.logging.createLogger
import technology.idlab.logging.fatal
import java.io.File
import java.io.PrintWriter
import javax.tools.DiagnosticCollector
import javax.tools.JavaFileObject
import javax.tools.ToolProvider

class Compiler {
private val logger = createLogger()
private val compiler =
ToolProvider.getSystemJavaCompiler() ?: logger.fatal(
"No Java compiler found.",
)

fun compile(file: File): ByteArray {
logger.info(file.absolutePath)

// Prepare compilation.
val files = listOf(file)
val fileManager = compiler.getStandardFileManager(null, null, null)
val results = MemoryFileManager(fileManager)
val compilationUnits = fileManager.getJavaFileObjectsFromFiles(files)
val diagnosticCollector = DiagnosticCollector<JavaFileObject>()

// Create a compilation task.
val task =
compiler.getTask(
PrintWriter(System.out),
results,
diagnosticCollector,
listOf("-d", ""),
null,
compilationUnits,
)

// Execute compilation.
val success = task.call()

// Write diagnostics to logger.
diagnosticCollector.diagnostics.forEach {
logger.info(it.toString())
}

if (!success) {
logger.fatal("Failure when compiling $file")
}

return results.get(file.nameWithoutExtension)
}

companion object {
private val instance = Compiler()

fun compile(file: File): ByteArray {
return instance.compile(file)
}
}
}
22 changes: 7 additions & 15 deletions src/main/kotlin/compiler/FileClassLoader.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,18 @@ import technology.idlab.logging.createLogger
import technology.idlab.logging.fatal
import java.io.File

class FileClassLoader(private val directory: String) : ClassLoader() {
class FileClassLoader : ClassLoader() {
private val logger = createLogger()

override fun findClass(name: String): Class<*> {
// Define the path to the class file.
var path = "$directory/${name.replace('.', File.separatorChar)}.class"
path = File(path).absolutePath

// Open file pointer.
val file = File(path)
if (!file.exists()) {
logger.fatal("Failed to read file $name")
}

// Read the file into a byte array.
logger.info("Reading $path")
val bytes = file.readBytes()
fun fromFile(
file: File,
name: String,
): Class<*> {
logger.info("Loading ${file.absoluteFile}")

// Define and return the class.
return try {
val bytes = file.readBytes()
defineClass(name, bytes, 0, bytes.size)
} catch (e: ClassFormatError) {
logger.fatal("Failed to load class $name")
Expand Down
Loading

0 comments on commit 6f60790

Please sign in to comment.