|
1 | 1 | package org.minimallycorrect.javatransformer.api; |
2 | 2 |
|
3 | | -import java.io.File; |
4 | | -import java.io.IOException; |
5 | | -import java.io.InputStream; |
6 | | -import java.nio.file.FileVisitResult; |
7 | | -import java.nio.file.Files; |
8 | 3 | import java.nio.file.Path; |
9 | | -import java.nio.file.SimpleFileVisitor; |
10 | | -import java.nio.file.attribute.BasicFileAttributes; |
11 | | -import java.util.Collection; |
12 | | -import java.util.HashSet; |
13 | | -import java.util.zip.ZipEntry; |
14 | | -import java.util.zip.ZipInputStream; |
| 4 | +import java.util.List; |
15 | 5 |
|
16 | | -import lombok.NonNull; |
17 | | -import lombok.SneakyThrows; |
18 | | -import lombok.val; |
| 6 | +import javax.annotation.Nonnull; |
| 7 | +import javax.annotation.Nullable; |
19 | 8 |
|
20 | 9 | import org.jetbrains.annotations.Contract; |
21 | | -import org.jetbrains.annotations.Nullable; |
22 | 10 |
|
23 | | -import com.github.javaparser.JavaParser; |
24 | | -import com.github.javaparser.ast.CompilationUnit; |
25 | | -import com.github.javaparser.ast.body.TypeDeclaration; |
26 | | - |
27 | | -import org.minimallycorrect.javatransformer.internal.util.JVMUtil; |
28 | | -import org.minimallycorrect.javatransformer.internal.util.Joiner; |
29 | | - |
30 | | -// TODO: make this faster by using dumb regexes instead of JavaParser? |
31 | | -// probably not worth doing |
32 | | -public class ClassPath { |
33 | | - private final HashSet<String> classes = new HashSet<>(); |
34 | | - private final HashSet<Path> inputPaths = new HashSet<>(); |
35 | | - private final ClassPath parent; |
36 | | - private boolean loaded; |
37 | | - |
38 | | - private ClassPath(@Nullable ClassPath parent) { |
39 | | - this.parent = parent; |
40 | | - } |
41 | | - |
42 | | - public ClassPath() { |
43 | | - this((ClassPath) null); |
44 | | - } |
45 | | - |
46 | | - public ClassPath(Collection<Path> paths) { |
47 | | - this(); |
48 | | - addPaths(paths); |
49 | | - } |
50 | | - |
51 | | - public ClassPath createChildWithExtraPaths(Collection<Path> paths) { |
52 | | - val searchPath = new ClassPath(this); |
53 | | - searchPath.addPaths(paths); |
54 | | - return searchPath; |
55 | | - } |
56 | | - |
57 | | - @Override |
58 | | - public String toString() { |
59 | | - initialise(); |
60 | | - return "[" + parent.toString() + ", " + inputPaths + " classes:\n" + Joiner.on("\n").join(classes.stream().sorted()) + "]"; |
61 | | - } |
| 11 | +import org.minimallycorrect.javatransformer.internal.ClassPaths; |
62 | 12 |
|
| 13 | +public interface ClassPath extends Iterable<ClassInfo> { |
63 | 14 | /** |
64 | | - * Returns whether the given class name exists in this class path |
| 15 | + * Returns whether the given class name exists |
65 | 16 | * |
66 | | - * @param className class name in JLS format: package1.package2.ClassName, package1.package2.ClassName$InnerClass |
| 17 | + * @param className class name in JLS format: {@code package1.package2.ClassName}, {@code package1.package2.ClassName$InnerClass} |
67 | 18 | * @return true if the class exists |
68 | 19 | */ |
69 | | - @Contract("null -> fail") |
70 | | - public boolean classExists(@NonNull String className) { |
71 | | - initialise(); |
72 | | - return classes.contains(className) || (parent != null && parent.classExists(className)); |
| 20 | + @Contract(value = "null -> fail", pure = true) |
| 21 | + default boolean classExists(@Nonnull String className) { |
| 22 | + return getClassInfo(className) != null; |
73 | 23 | } |
74 | 24 |
|
75 | 25 | /** |
76 | | - * Adds a {@link Path} to this {@link ClassPath} |
| 26 | + * Returns the class info for the given class |
77 | 27 | * |
78 | | - * @param path {@link Path} to add |
79 | | - * @return true if the path was added, false if the path already existed in this {@link ClassPath} |
| 28 | + * This instance is only guaranteed to provide information required for type resolution. Other information such as method bodies and annotations may be stripped |
| 29 | + * |
| 30 | + * @param className class name in JLS format: {@code package1.package2.ClassName}, {@code package1.package2.ClassName$InnerClass} |
| 31 | + * @return ClassInfo for the given name, or null if not found |
80 | 32 | */ |
81 | | - @Contract("null -> fail") |
82 | | - public boolean addPath(@NonNull Path path) { |
83 | | - path = path.normalize().toAbsolutePath(); |
84 | | - val add = !parentHasPath(path) && inputPaths.add(path); |
85 | | - if (add && loaded) |
86 | | - loadPath(path); |
87 | | - return add; |
88 | | - } |
89 | | - |
90 | | - @Contract("null -> fail") |
91 | | - public void addPaths(@NonNull Collection<Path> paths) { |
92 | | - for (Path path : paths) |
93 | | - addPath(path); |
94 | | - } |
| 33 | + @Contract(value = "null -> fail", pure = true) |
| 34 | + @Nullable |
| 35 | + ClassInfo getClassInfo(@Nonnull String className); |
95 | 36 |
|
96 | 37 | /** |
97 | | - * @param path path must be normalized and absolute |
| 38 | + * Adds the given paths to this {@link ClassPath} |
| 39 | + * |
| 40 | + * @param paths Paths to add |
98 | 41 | */ |
99 | | - private boolean parentHasPath(Path path) { |
100 | | - val parent = this.parent; |
101 | | - return parent != null && (parent.inputPaths.contains(path) || parent.parentHasPath(path)); |
102 | | - } |
103 | | - |
104 | | - private void findPaths(ZipEntry e, ZipInputStream zis) { |
105 | | - val entryName = e.getName(); |
106 | | - if (entryName.endsWith(".java")) |
107 | | - findJavaPaths(zis); |
108 | | - |
109 | | - if (entryName.endsWith(".class")) |
110 | | - classes.add(JVMUtil.fileNameToClassName(entryName)); |
111 | | - } |
112 | | - |
113 | | - private void findJavaPaths(ZipInputStream zis) { |
114 | | - val parsed = JavaParser.parse(new InputStream() { |
115 | | - public int read(@NonNull byte[] b, int off, int len) throws IOException { |
116 | | - return zis.read(b, off, len); |
117 | | - } |
118 | | - |
119 | | - public void close() throws IOException {} |
120 | | - |
121 | | - public int read() throws IOException { |
122 | | - return zis.read(); |
123 | | - } |
124 | | - }); |
125 | | - findJavaPaths(parsed); |
| 42 | + default void addPaths(List<Path> paths) { |
| 43 | + for (Path extraPath : paths) |
| 44 | + addPath(extraPath); |
126 | 45 | } |
127 | 46 |
|
128 | | - private void findJavaPaths(CompilationUnit compilationUnit) { |
129 | | - val typeNames = compilationUnit.getTypes(); |
130 | | - val packageDeclaration = compilationUnit.getPackageDeclaration().orElse(null); |
131 | | - val prefix = packageDeclaration == null ? "" : packageDeclaration.getNameAsString() + '.'; |
132 | | - for (TypeDeclaration<?> typeDeclaration : typeNames) |
133 | | - findJavaPaths(typeDeclaration, prefix); |
134 | | - } |
| 47 | + /** |
| 48 | + * Adds a path to this {@link ClassPath} |
| 49 | + * |
| 50 | + * @param path Path to add |
| 51 | + * @return True if the path was added, false if it was already in this classpath |
| 52 | + * @throws UnsupportedOperationException if this {@link ClassPath} is immutable |
| 53 | + */ |
| 54 | + boolean addPath(Path path); |
135 | 55 |
|
136 | | - private void findJavaPaths(TypeDeclaration<?> typeDeclaration, String packagePrefix) { |
137 | | - val name = packagePrefix + typeDeclaration.getNameAsString(); |
138 | | - classes.add(name); |
139 | | - for (val node : typeDeclaration.getChildNodes()) |
140 | | - if (node instanceof TypeDeclaration) |
141 | | - findJavaPaths((TypeDeclaration<?>) node, name + '.'); |
142 | | - } |
| 56 | + /** |
| 57 | + * Checks if the given path is in this {@link ClassPath} |
| 58 | + * |
| 59 | + * @param path Path to check |
| 60 | + * @return True if this {@link ClassPath} contains the given path |
| 61 | + */ |
| 62 | + boolean hasPath(Path path); |
143 | 63 |
|
144 | | - private void initialise() { |
145 | | - if (loaded) |
146 | | - return; |
147 | | - loaded = true; |
148 | | - for (Path path : inputPaths) |
149 | | - loadPath(path); |
| 64 | + @Contract(pure = true) |
| 65 | + static @Nonnull ClassPath of(@Nonnull Path... paths) { |
| 66 | + return of(ClassPaths.SystemClassPath.SYSTEM_CLASS_PATH, paths); |
150 | 67 | } |
151 | 68 |
|
152 | | - @SneakyThrows |
153 | | - private void loadPath(Path path) { |
154 | | - if (Files.isDirectory(path)) |
155 | | - Files.walkFileTree(path, new SimpleFileVisitor<Path>() { |
156 | | - @Override |
157 | | - public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { |
158 | | - val entryName = path.relativize(file).toString().replace(File.separatorChar, '/'); |
159 | | - if (entryName.endsWith(".java")) { |
160 | | - val parsed = JavaParser.parse(file); |
161 | | - findJavaPaths(parsed); |
162 | | - } |
163 | | - return super.visitFile(file, attrs); |
164 | | - } |
165 | | - }); |
166 | | - else if (Files.isRegularFile(path)) |
167 | | - try (val zis = new ZipInputStream(Files.newInputStream(path))) { |
168 | | - ZipEntry e; |
169 | | - while ((e = zis.getNextEntry()) != null) { |
170 | | - try { |
171 | | - findPaths(e, zis); |
172 | | - } finally { |
173 | | - zis.closeEntry(); |
174 | | - } |
175 | | - } |
176 | | - } |
| 69 | + @Contract(pure = true) |
| 70 | + static @Nonnull ClassPath of(@Nullable ClassPath parent, Path... paths) { |
| 71 | + return ClassPaths.of(parent, paths); |
177 | 72 | } |
178 | 73 | } |
0 commit comments