Skip to content

Commit 385b0ec

Browse files
authored
compiler factory and advanced url handler
1 parent 5dd6bd2 commit 385b0ec

File tree

3 files changed

+56
-19
lines changed

3 files changed

+56
-19
lines changed

src/main/java/mods/Hileb/shotaasm/impl/ShotaCompiler.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ public String name() {
3030

3131
@Override
3232
public Runnable compile(final ScriptFile file) {
33+
CompilerFactory CompilerFactory = new CompilerFactory(Launch.classLoader.getURLs());
34+
3335
if (file.property().containsKey("event")) {
3436
String event = Iterables.getFirst(file.property().get("event"), null);
3537
if (event != null) {
@@ -57,7 +59,7 @@ public Runnable compile(final ScriptFile file) {
5759
.append(file.text())
5860
.append(" }\n}");
5961
try {
60-
Compiler compiler = new Compiler(Launch.classLoader.getURLs());
62+
Compiler compiler = CompilerFactory.compiler();
6163
compiler.addSource(name, builder.toString());
6264
CompileError error = compiler.compile();
6365
if (error != null) throw new RuntimeException(error);

src/main/java/mods/Hileb/shotaasm/impl/compiler/Compiler.java

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
public class Compiler {
1616

17-
private final URL[] extraClasspath;
17+
public List<String> options = null;
1818

1919
private final Map<String, VirtualJavaFileObject> javaFileObjectMap = new HashMap<>();
2020
private Map<String, byte[]> results = new HashMap<>();
@@ -25,8 +25,8 @@ public Compiler(){
2525
this(null);
2626
}
2727

28-
public Compiler(URL[] extraClasspath) {
29-
this.extraClasspath = extraClasspath;
28+
public Compiler(List<String> options) {
29+
this.options = options;
3030
}
3131

3232
public void addSource(String name, String content) {
@@ -63,21 +63,6 @@ public CompileError compile() {
6363
else {
6464
try(StandardJavaFileManager standardJavaFileManager = compiler.getStandardFileManager(null, null, StandardCharsets.UTF_8)) {
6565
VirtualFileManager fileManager = new VirtualFileManager(standardJavaFileManager, this.javaFileObjectMap);
66-
List<String> options = null;
67-
if (extraClasspath != null) {
68-
List<String> pathList = new LinkedList<>();
69-
for (URL url : extraClasspath) {
70-
try {
71-
pathList.add(url.toString());
72-
} catch (URISyntaxException ignored) {} // Ignore the lib.
73-
}
74-
String systemClasspath = System.getProperty("java.class.path");
75-
String combinedClasspath = String.join(File.pathSeparator, pathList) + File.pathSeparator + systemClasspath;
76-
options = new ArrayList<>();
77-
options.add("-classpath");
78-
options.add(combinedClasspath);
79-
options.add("-Xlint:unchecked");
80-
}
8166
try {
8267
StringWriter stringWriter = new StringWriter();
8368
JavaCompiler.CompilationTask task = compiler.getTask(stringWriter, fileManager, (DiagnosticListener<? super JavaFileObject>)(Object) listener, options, null, javaFileObjectMap.values());
@@ -94,6 +79,8 @@ public CompileError compile() {
9479
}
9580
}
9681

82+
83+
9784
public void setListener(DiagnosticListener<VirtualJavaFileObject> listener) {
9885
this.listener = listener;
9986
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package mods.Hileb.shotaasm.impl.compiler;
2+
3+
import com.google.common.collect.Iterables;
4+
import mods.Hileb.shotaasm.impl.compiler.virtual.VirtualFileManager;
5+
import mods.Hileb.shotaasm.impl.compiler.virtual.VirtualJavaFileObject;
6+
7+
import javax.tools.*;
8+
import java.io.*;
9+
import java.net.URISyntaxException;
10+
import java.net.URL;
11+
import java.nio.charset.StandardCharsets;
12+
import java.nio.file.*;
13+
import java.util.*;
14+
15+
public class CompilerFactory{
16+
public List<String> options = null;
17+
18+
public CompilerFactory(URL[] classpath) {
19+
this.options = new ArrayList<>();
20+
this.options.add("-classpath");
21+
this.options.add(buildClasspath(classpath));
22+
this.options.add("-Xlint:unchecked");
23+
}
24+
25+
public Compiler compiler(){
26+
return new Compiler(this.options);
27+
}
28+
29+
public static String buildClasspath(URL[] urls) {
30+
if (urls == null || urls.length == 0) return System.getProperty("java.class.path");
31+
32+
StringBuilder classpath = new StringBuilder(System.getProperty("java.class.path"));
33+
for (URL url : urls) {
34+
try {
35+
//Attempt to handle different URL schemes. This is not exhaustive, but covers common cases.
36+
if("file".equals(url.getProtocol())){
37+
//Handle potential space in file path
38+
classpath.append(File.pathSeparator).append(url.getPath().replace(" ", "%20"));
39+
} else {
40+
classpath.append(File.pathSeparator).append(url.toString());
41+
}
42+
} catch (Exception e) {
43+
ShotaASM.LOGGER.error("Error processing URL {} because of {}", url, e);
44+
}
45+
}
46+
return classpath.toString();
47+
}
48+
}

0 commit comments

Comments
 (0)