diff options
author | 2024-02-18 23:59:01 +0100 | |
---|---|---|
committer | 2024-02-19 00:01:08 +0100 | |
commit | 50138f47e078a89901e4e623239e55d60167abed (patch) | |
tree | 682f92808d2ce16543ce938c9fa641e23d787925 /subprojects/language/src | |
parent | fix(frontend): module name highlighting (diff) | |
download | refinery-50138f47e078a89901e4e623239e55d60167abed.tar.gz refinery-50138f47e078a89901e4e623239e55d60167abed.tar.zst refinery-50138f47e078a89901e4e623239e55d60167abed.zip |
refactor(language): classpath based library context
Diffstat (limited to 'subprojects/language/src')
2 files changed, 14 insertions, 15 deletions
diff --git a/subprojects/language/src/main/java/tools/refinery/language/library/BuiltinLibrary.java b/subprojects/language/src/main/java/tools/refinery/language/library/BuiltinLibrary.java index d88d0299..ce80504e 100644 --- a/subprojects/language/src/main/java/tools/refinery/language/library/BuiltinLibrary.java +++ b/subprojects/language/src/main/java/tools/refinery/language/library/BuiltinLibrary.java | |||
@@ -17,6 +17,11 @@ public class BuiltinLibrary extends ClasspathBasedLibrary { | |||
17 | () -> new IllegalStateException("Builtin library was not found")); | 17 | () -> new IllegalStateException("Builtin library was not found")); |
18 | 18 | ||
19 | public BuiltinLibrary() { | 19 | public BuiltinLibrary() { |
20 | super(BUILTIN_LIBRARY_NAME, List.of(BUILTIN_LIBRARY_NAME)); | 20 | super(BUILTIN_LIBRARY_NAME); |
21 | } | ||
22 | |||
23 | @Override | ||
24 | public List<QualifiedName> getAutomaticImports() { | ||
25 | return List.of(BUILTIN_LIBRARY_NAME); | ||
21 | } | 26 | } |
22 | } | 27 | } |
diff --git a/subprojects/language/src/main/java/tools/refinery/language/library/ClasspathBasedLibrary.java b/subprojects/language/src/main/java/tools/refinery/language/library/ClasspathBasedLibrary.java index 3a9bb920..56bb8b96 100644 --- a/subprojects/language/src/main/java/tools/refinery/language/library/ClasspathBasedLibrary.java +++ b/subprojects/language/src/main/java/tools/refinery/language/library/ClasspathBasedLibrary.java | |||
@@ -12,16 +12,15 @@ import java.nio.file.Path; | |||
12 | import java.util.*; | 12 | import java.util.*; |
13 | 13 | ||
14 | public abstract class ClasspathBasedLibrary implements RefineryLibrary { | 14 | public abstract class ClasspathBasedLibrary implements RefineryLibrary { |
15 | private final Class<?> context; | ||
15 | private final QualifiedName prefix; | 16 | private final QualifiedName prefix; |
16 | private final List<QualifiedName> automaticImports; | ||
17 | private final URI rootUri; | 17 | private final URI rootUri; |
18 | 18 | ||
19 | protected ClasspathBasedLibrary(QualifiedName prefix, List<QualifiedName> automaticImports) { | 19 | protected ClasspathBasedLibrary(Class<?> context, QualifiedName prefix) { |
20 | this.prefix = prefix; | 20 | this.context = context == null ? getClass() : context; |
21 | this.automaticImports = List.copyOf(automaticImports); | 21 | this.prefix = prefix; |
22 | var context = this.getClass(); | 22 | var contextPath = this.context.getCanonicalName().replace('.', '/') + ".class"; |
23 | var contextPath = context.getCanonicalName().replace('.', '/') + ".class"; | 23 | var contextResource = this.context.getClassLoader().getResource(contextPath); |
24 | var contextResource = context.getClassLoader().getResource(contextPath); | ||
25 | if (contextResource == null) { | 24 | if (contextResource == null) { |
26 | throw new IllegalStateException("Failed to find library context"); | 25 | throw new IllegalStateException("Failed to find library context"); |
27 | } | 26 | } |
@@ -32,18 +31,13 @@ public abstract class ClasspathBasedLibrary implements RefineryLibrary { | |||
32 | } | 31 | } |
33 | 32 | ||
34 | protected ClasspathBasedLibrary(QualifiedName prefix) { | 33 | protected ClasspathBasedLibrary(QualifiedName prefix) { |
35 | this(prefix, List.of()); | 34 | this(null, prefix); |
36 | } | ||
37 | |||
38 | @Override | ||
39 | public List<QualifiedName> getAutomaticImports() { | ||
40 | return automaticImports; | ||
41 | } | 35 | } |
42 | 36 | ||
43 | @Override | 37 | @Override |
44 | public Optional<URI> resolveQualifiedName(QualifiedName qualifiedName, List<Path> libraryPaths) { | 38 | public Optional<URI> resolveQualifiedName(QualifiedName qualifiedName, List<Path> libraryPaths) { |
45 | if (qualifiedName.startsWith(prefix)) { | 39 | if (qualifiedName.startsWith(prefix)) { |
46 | return getLibraryUri(this.getClass(), qualifiedName); | 40 | return getLibraryUri(context, qualifiedName); |
47 | } | 41 | } |
48 | return Optional.empty(); | 42 | return Optional.empty(); |
49 | } | 43 | } |