1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
|
/*******************************************************************************
* Copyright (c) 2015, 2017 IBM Corporation.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* IBM Corporation - initial API and implementation
*******************************************************************************/
package org.eclipse.jdt.internal.compiler.apt.util;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Reader;
import java.io.Writer;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.net.URLClassLoader;
import java.nio.charset.Charset;
import java.nio.file.FileSystems;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import java.util.zip.ZipException;
import javax.tools.JavaFileObject;
import org.eclipse.jdt.internal.compiler.apt.util.ModuleLocationHandler.ModuleLocationWrapper;
import org.eclipse.jdt.internal.compiler.classfmt.ClassFileReader;
import org.eclipse.jdt.internal.compiler.classfmt.ClassFormatException;
import org.eclipse.jdt.internal.compiler.util.JRTUtil;
public class JrtFileSystem extends Archive {
private static URI JRT_URI = URI.create("jrt:/"); //$NON-NLS-1$
static final String BOOT_MODULE = "jrt-fs.jar"; //$NON-NLS-1$
public HashMap<String, Path> modulePathMap;
Path modules;
private java.nio.file.FileSystem jrtfs;
public JrtFileSystem(File file) throws ZipException, IOException {
this.file = file;
initialize();
}
public void initialize() throws IOException {
// initialize packages
this.modulePathMap = new HashMap<>();
URL jrtPath = null;
if (this.file.exists()) {
jrtPath = Paths.get(this.file.toPath().toString(), "lib", JRTUtil.JRT_FS_JAR).toUri().toURL(); //$NON-NLS-1$
try (URLClassLoader loader = new URLClassLoader(new URL[] { jrtPath })) {
HashMap<String, ?> env = new HashMap<>();
this.jrtfs = FileSystems.newFileSystem(JRT_URI, env, loader);
this.modules = this.jrtfs.getPath("/modules"); //$NON-NLS-1$
}
} else {
return;
}
org.eclipse.jdt.internal.compiler.util.JRTUtil.walkModuleImage(this.file,
new org.eclipse.jdt.internal.compiler.util.JRTUtil.JrtFileVisitor<Path>() {
@Override
public FileVisitResult visitPackage(Path dir, Path mod, BasicFileAttributes attrs)
throws IOException {
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult visitFile(Path f, Path mod, BasicFileAttributes attrs)
throws IOException {
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult visitModule(Path mod) throws IOException {
JrtFileSystem.this.modulePathMap.put(mod.getFileName().toString(), mod);
return FileVisitResult.CONTINUE;
}
}, JRTUtil.NOTIFY_MODULES);
}
public List<JrtFileObject> list(ModuleLocationWrapper location, String packageName,
Set<JavaFileObject.Kind> kinds, boolean recurse, Charset charset) {
String module = location.modName;
Path mPath = this.modules.resolve(module);
Path resolve = mPath.resolve(packageName);
java.util.List<Path> files = null;
try (Stream<Path> p = Files.list(resolve)) {
files = p.filter((path) -> {
if (Files.isDirectory(path))
return false;
else
return true;
}).collect(Collectors.toList());
} catch (IOException e) {
// ignore
}
List<JrtFileObject> result = new ArrayList<>();
for (Path p: files) {
result.add(new JrtFileObject(this.file, p, module, charset));
}
return result;
}
@Override
public ArchiveFileObject getArchiveFileObject(String fileName, String module, Charset charset) {
return new JrtFileObject(this.file, this.modules.resolve(module).resolve(fileName), module, charset);
}
@Override
public boolean contains(String entryName) {
// FIXME
return false;
}
@Override
public String toString() {
return "JRT: " + (this.file == null ? "UNKNOWN_ARCHIVE" : this.file.getAbsolutePath()); //$NON-NLS-1$ //$NON-NLS-2$
}
class JrtFileObject extends ArchiveFileObject {
String module;
Path path;
private JrtFileObject(File file, Path path, String module, Charset charset) {
super(file, path.toString(), charset);
this.path = path;
}
@Override
protected void finalize() throws Throwable {
// Nothing to do here
}
@Override
protected ClassFileReader getClassReader() {
ClassFileReader reader = null;
try {
byte[] content = JRTUtil.getClassfileContent(this.file, this.entryName, this.module);
if (content == null) return null;
return new ClassFileReader(content, this.entryName.toCharArray());
} catch (ClassFormatException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return reader;
}
/* (non-Javadoc)
* @see javax.tools.FileObject#getCharContent(boolean)
*/
@Override
public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOException {
try {
return Util.getCharContents(this, ignoreEncodingErrors,
org.eclipse.jdt.internal.compiler.util.JRTUtil.getClassfileContent(this.file, this.entryName, this.module),
this.charset.name());
} catch (ClassFormatException e) {
e.printStackTrace();
return null;
}
}
/* (non-Javadoc)
* @see javax.tools.FileObject#getLastModified()
*/
@Override
public long getLastModified() {
return 0;
}
/* (non-Javadoc)
* @see javax.tools.FileObject#getName()
*/
@Override
public String getName() {
return this.path.toString();
}
/* (non-Javadoc)
* @see javax.tools.FileObject#openInputStream()
*/
@Override
public InputStream openInputStream() throws IOException {
return Files.newInputStream(this.path);
}
/* (non-Javadoc)
* @see javax.tools.FileObject#openOutputStream()
*/
@Override
public OutputStream openOutputStream() throws IOException {
throw new UnsupportedOperationException();
}
/* (non-Javadoc)
* @see javax.tools.FileObject#openReader(boolean)
*/
@Override
public Reader openReader(boolean ignoreEncodingErrors) throws IOException {
throw new UnsupportedOperationException();
}
/* (non-Javadoc)
* @see javax.tools.FileObject#openWriter()
*/
@Override
public Writer openWriter() throws IOException {
throw new UnsupportedOperationException();
}
/* (non-Javadoc)
* @see javax.tools.FileObject#toUri()
*/
@Override
public URI toUri() {
try {
return new URI("JRT:" + this.file.toURI().getPath() + "!" + this.entryName); //$NON-NLS-1$//$NON-NLS-2$
} catch (URISyntaxException e) {
return null;
}
}
@Override
public String toString() {
return this.file.getAbsolutePath() + "[" + this.entryName + "]";//$NON-NLS-1$//$NON-NLS-2$
}
}
}
|