Skip to content

Commit

Permalink
refactor: remove dependency to CompilingClassLoader of Vert.x
Browse files Browse the repository at this point in the history
  • Loading branch information
pk-work committed Dec 4, 2021
1 parent b99e8b8 commit 48e1770
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 8 deletions.
3 changes: 2 additions & 1 deletion src/main/java/io/neonbee/handler/ErrorHandler.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package io.neonbee.handler;

import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import io.neonbee.NeonBee;
import io.vertx.core.Future;

/**
* The class which implements this interface MUST have a default constructor. Otherwise NeonBee can't instantiate the
* class during bootstrap phase.
*/
@SuppressWarnings("NM_SAME_SIMPLE_NAME_AS_INTERFACE")
@SuppressFBWarnings("NM_SAME_SIMPLE_NAME_AS_INTERFACE")
public interface ErrorHandler extends io.vertx.ext.web.handler.ErrorHandler {

/**
Expand Down
46 changes: 39 additions & 7 deletions src/test/java/io/neonbee/internal/ClassTemplate.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,28 @@
package io.neonbee.internal;

import static io.neonbee.test.helper.FileSystemHelper.createTempDirectory;
import static javax.tools.JavaFileObject.Kind.CLASS;
import static javax.tools.JavaFileObject.Kind.SOURCE;
import static javax.tools.StandardLocation.CLASS_OUTPUT;
import static javax.tools.StandardLocation.SOURCE_PATH;

import java.io.IOException;
import java.net.URL;
import java.net.URLClassLoader;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Collections;
import java.util.List;
import java.util.Locale;
import java.util.stream.Collectors;

import javax.tools.DiagnosticCollector;
import javax.tools.FileObject;
import javax.tools.JavaCompiler;
import javax.tools.JavaFileObject;
import javax.tools.StandardJavaFileManager;
import javax.tools.ToolProvider;

import com.google.common.base.Strings;
import io.vertx.core.impl.verticle.CompilingClassLoader;
import com.google.common.io.ByteStreams;

/**
* The purpose of this interface is offering a tool to transform a class template into compiled byte code. A class
Expand Down Expand Up @@ -79,12 +92,31 @@ default byte[] compileToByteCode() throws IOException {
Path tempDir = createTempDirectory();
Path sourceFile = tempDir.resolve(resourcePath());
Files.createDirectories(sourceFile.getParent());

Files.writeString(sourceFile, reifyTemplate());

try (URLClassLoader urlc =
new URLClassLoader(new URL[] { tempDir.toUri().toURL() }, ClassLoader.getSystemClassLoader())) {
CompilingClassLoader compilingLoader = new CompilingClassLoader(urlc, resourcePath());
return compilingLoader.getClassBytes(getClassName());
JavaCompiler javaCompiler = ToolProvider.getSystemJavaCompiler();
if (javaCompiler == null) {
throw new RuntimeException("Unable to detect Java compiler, make sure you're using a JDK not a JRE!");
}

StandardJavaFileManager standardFileManager = javaCompiler.getStandardFileManager(null, null, null);
standardFileManager.setLocation(SOURCE_PATH, Collections.singleton(tempDir.toFile()));
standardFileManager.setLocation(CLASS_OUTPUT, Collections.singleton(tempDir.toFile()));

JavaFileObject javaFileToCompile = standardFileManager.getJavaFileForInput(SOURCE_PATH, getClassName(), SOURCE);

DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<>();
JavaCompiler.CompilationTask task =
javaCompiler.getTask(null, standardFileManager, diagnostics, null, null, List.of(javaFileToCompile));

if (!task.call()) {
String msgs = diagnostics.getDiagnostics().stream().map(d -> d.getMessage(Locale.getDefault()))
.collect(Collectors.joining("\n"));
throw new RuntimeException("Compilation Failed: \n" + msgs);
}

FileObject compiledClassFile = standardFileManager.getJavaFileForInput(CLASS_OUTPUT, getClassName(), CLASS);
return ByteStreams.toByteArray(compiledClassFile.openInputStream());
}
}

0 comments on commit 48e1770

Please sign in to comment.