|
1 | 1 | // Copyright (c) 2020-2024 Tesla (Yinsen) Zhang.
|
2 | 2 | // Use of this source code is governed by the MIT license that can be found in the LICENSE.md file.
|
3 | 3 |
|
4 |
| -import com.javax0.sourcebuddy.Compiler; |
5 |
| -import com.javax0.sourcebuddy.Fluent; |
6 | 4 | import kala.collection.immutable.ImmutableSeq;
|
7 | 5 | import org.aya.compiler.AyaSerializer;
|
8 |
| -import org.aya.compiler.ModuleSerializer; |
9 |
| -import org.aya.generic.NameGenerator; |
10 |
| -import org.aya.primitive.ShapeFactory; |
11 | 6 | import org.aya.resolve.module.DumbModuleLoader;
|
12 | 7 | import org.aya.syntax.compile.JitDef;
|
13 |
| -import org.aya.syntax.core.def.DataDef; |
14 |
| -import org.aya.syntax.core.def.FnDef; |
15 |
| -import org.aya.syntax.core.def.TyckDef; |
16 |
| -import org.intellij.lang.annotations.Language; |
17 | 8 | import org.jetbrains.annotations.NotNull;
|
18 | 9 |
|
| 10 | +import javax.tools.ToolProvider; |
| 11 | +import java.io.IOException; |
19 | 12 | import java.lang.reflect.Field;
|
| 13 | +import java.net.URL; |
| 14 | +import java.net.URLClassLoader; |
| 15 | +import java.nio.file.Files; |
| 16 | +import java.nio.file.Path; |
| 17 | +import java.nio.file.Paths; |
20 | 18 |
|
21 | 19 | public class CompileTester {
|
22 |
| - public final @Language("Java") @NotNull String code; |
23 |
| - private final Fluent.AddSource compiler = Compiler.java(); |
24 |
| - private Class<?> output = null; |
| 20 | + public final @NotNull String code; |
| 21 | + private final Path baka; |
| 22 | + public final ClassLoader cl; |
25 | 23 |
|
26 |
| - public CompileTester(@NotNull String code) { this.code = code; } |
| 24 | + public CompileTester(@NotNull String code) throws IOException { |
| 25 | + this.code = code; |
| 26 | + |
| 27 | + var genDir = Paths.get("src/test/gen"); |
| 28 | + Files.createDirectories(genDir); |
| 29 | + Files.writeString(baka = genDir.resolve("baka.java"), code); |
| 30 | + cl = new URLClassLoader(new URL[]{baka.toUri().toURL()}); |
| 31 | + } |
27 | 32 |
|
28 | 33 | public void compile() {
|
29 | 34 | try {
|
30 |
| - output = compiler.from(STR."\{AyaSerializer.PACKAGE_BASE}.\{DumbModuleLoader.DUMB_MODULE_NAME}", code) |
31 |
| - .compile().load().get(); |
32 |
| - } catch (ClassNotFoundException | Compiler.CompileException e) { |
| 35 | + var compiler = ToolProvider.getSystemJavaCompiler(); |
| 36 | + var fileManager = compiler.getStandardFileManager(null, null, null); |
| 37 | + var compilationUnits = fileManager.getJavaFileObjects(baka); |
| 38 | + var task = compiler.getTask(null, fileManager, null, null, null, compilationUnits); |
| 39 | + task.call(); |
| 40 | + var fqName = STR."\{AyaSerializer.PACKAGE_BASE}.\{DumbModuleLoader.DUMB_MODULE_NAME}"; |
| 41 | + cl.loadClass(fqName); |
| 42 | + } catch (ClassNotFoundException e) { |
33 | 43 | throw new RuntimeException(e);
|
34 | 44 | }
|
35 | 45 | }
|
36 | 46 |
|
37 | 47 | @SuppressWarnings("unchecked")
|
38 | 48 | public <T> @NotNull Class<T> load(String... qualified) {
|
39 | 49 | try {
|
40 |
| - return (Class<T>) output.getClassLoader() |
41 |
| - .loadClass(STR."\{AyaSerializer.PACKAGE_BASE}.\{ImmutableSeq.from(qualified).joinToString("$")}"); |
| 50 | + return (Class<T>) cl.loadClass( |
| 51 | + STR."\{AyaSerializer.PACKAGE_BASE}.\{ImmutableSeq.from(qualified).joinToString("$")}"); |
42 | 52 | } catch (ClassNotFoundException e) {
|
43 | 53 | throw new RuntimeException(e);
|
44 | 54 | }
|
|
0 commit comments