Skip to content

Commit

Permalink
feat: working base
Browse files Browse the repository at this point in the history
  • Loading branch information
jenspots committed Apr 26, 2024
1 parent c052b5e commit fae5f6b
Show file tree
Hide file tree
Showing 19 changed files with 414 additions and 310 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package util;
package runner;

import java.util.HashMap;
import java.util.Map;
Expand All @@ -7,7 +7,7 @@

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

public abstract class Template {
public abstract class Processor {
/**
* Processors which wish to log messages should use the logger provided by
* the template class. This logger is created with the name of the class
Expand All @@ -20,13 +20,15 @@ public abstract class Template {
* name. At the time of writing, the user must manually cast the arguments
* to the correct type.
*/
private final Map<String, Object> arguments = new HashMap<>();
private final Map<String, Object> arguments;

public Template(Map<String, Object> arguments) {
this.arguments.putAll(arguments);
public Processor(Map<String, Object> arguments) {
this.arguments = arguments;
}

public Template() {}
public Processor() {
this.arguments = new HashMap<>();
}

protected <T> T getArgument(String name) {
Object result = arguments.get(name);
Expand Down
21 changes: 10 additions & 11 deletions src/main/kotlin/compiler/Compiler.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class Compiler {
)

fun compile(file: File): ByteArray {
logger.info(file.absolutePath)
logger.info("Compiling file://${file.absolutePath}")

// Prepare compilation.
val files = listOf(file)
Expand All @@ -26,15 +26,14 @@ class Compiler {
val diagnosticCollector = DiagnosticCollector<JavaFileObject>()

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

// Execute compilation.
val success = task.call()
Expand All @@ -45,7 +44,7 @@ class Compiler {
}

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

return results.get(file.nameWithoutExtension)
Expand Down
24 changes: 0 additions & 24 deletions src/main/kotlin/compiler/FileClassLoader.kt

This file was deleted.

7 changes: 2 additions & 5 deletions src/main/kotlin/compiler/MemoryClassLoader.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,13 @@ import technology.idlab.logging.fatal
class MemoryClassLoader : ClassLoader() {
private val logger = createLogger()

fun fromBytes(
bytes: ByteArray,
name: String,
): Class<*> {
fun fromBytes(bytes: ByteArray, name: String): Class<*> {
logger.info("Loading class $name")

return try {
defineClass(name, bytes, 0, bytes.size)
} catch (e: ClassFormatError) {
createLogger().fatal("Failed to load class $name")
logger.fatal("Failed to load class $name")
}
}
}
12 changes: 6 additions & 6 deletions src/main/kotlin/compiler/MemoryFileManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,17 @@ class MemoryFileManager(
kind: JavaFileObject.Kind,
sibling: FileObject?,
): JavaFileObject {
val uri =
URI.create(
"string:///" + className.replace('.', '/') + kind.extension,
)
val uriString = "string:///" +
className.replace('.', '/') +
kind.extension
val uri = URI.create(uriString)

return object : SimpleJavaFileObject(uri, kind) {
override fun openOutputStream(): OutputStream {
return object : ByteArrayOutputStream() {
override fun close() {
results[className] = this.toByteArray()
val name = className.substringAfterLast('.')
results[name] = this.toByteArray()
super.close()
}
}
Expand All @@ -45,7 +46,6 @@ class MemoryFileManager(
}

fun get(className: String): ByteArray {
logger.info("Retrieving $className")
return results[className] ?: logger.fatal("Class $className not found")
}
}
22 changes: 12 additions & 10 deletions src/main/kotlin/logging/Logger.kt
Original file line number Diff line number Diff line change
Expand Up @@ -47,19 +47,21 @@ fun Logger.fatal(
message: String,
exception: Exception? = null,
): Nothing {
// Construct message
val completeMessage = if (message != "" && exception != null) {
"$message\n${exception.message}"
} else if (message != "") {
message
} else if (exception != null) {
exception.message
} else {
"An unknown error has occurred."
}

// Log the message.
val caller = Throwable().stackTrace[1]
logp(Level.SEVERE, caller.className, caller.methodName, message)
logp(Level.SEVERE, caller.className, caller.methodName, completeMessage)

// Log the exception if it exists.
if (exception != null) {
logp(
Level.SEVERE,
caller.className,
caller.methodName,
exception.message,
)
}

// Exit the program.
exitProcess(-1)
Expand Down
Loading

0 comments on commit fae5f6b

Please sign in to comment.