Skip to content

Commit 8a5e4b4

Browse files
laeubiiloveeclipse
authored andcommitted
Fix NPE in JRTUtil
In some cases JRTUtil#getJrtSystem() can return null what results in a NPE a caller most likley do not exspect. This adds required null checks and act accordingly. Fix #1154
1 parent 96cb63e commit 8a5e4b4

1 file changed

Lines changed: 41 additions & 8 deletions

File tree

  • org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/util

org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/util/JRTUtil.java

Lines changed: 41 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
import java.io.ByteArrayInputStream;
1818
import java.io.File;
19+
import java.io.FileNotFoundException;
1920
import java.io.FileReader;
2021
import java.io.IOException;
2122
import java.io.InputStream;
@@ -238,35 +239,67 @@ public static void reset() {
238239
* @throws IOException
239240
*/
240241
public static void walkModuleImage(File image, final JRTUtil.JrtFileVisitor<java.nio.file.Path> visitor, int notify) throws IOException {
241-
getJrtSystem(image, null).walkModuleImage(visitor, notify);
242+
JrtFileSystem system = getJrtSystem(image, null);
243+
if (system == null) {
244+
return;
245+
}
246+
system.walkModuleImage(visitor, notify);
242247
}
243248

244249
public static void walkModuleImage(File image, String release, final JRTUtil.JrtFileVisitor<java.nio.file.Path> visitor, int notify) throws IOException {
245-
getJrtSystem(image, release).walkModuleImage(visitor, notify);
250+
JrtFileSystem system = getJrtSystem(image, release);
251+
if (system == null) {
252+
return;
253+
}
254+
system.walkModuleImage(visitor, notify);
246255
}
247256

248257
public static InputStream getContentFromJrt(File jrt, String fileName, String module) throws IOException {
249-
return getJrtSystem(jrt).getContentFromJrt(fileName, module);
258+
JrtFileSystem system = getJrtSystem(jrt);
259+
if (system == null) {
260+
throw new FileNotFoundException(String.valueOf(jrt));
261+
}
262+
return system.getContentFromJrt(fileName, module);
250263
}
251264

252265
public static byte[] getClassfileContent(File jrt, String fileName, String module) throws IOException {
253-
return getJrtSystem(jrt).getClassfileContent(fileName, module);
266+
JrtFileSystem system = getJrtSystem(jrt);
267+
if (system == null) {
268+
throw new FileNotFoundException(String.valueOf(jrt));
269+
}
270+
return system.getClassfileContent(fileName, module);
254271
}
255272

256273
public static ClassFileReader getClassfile(File jrt, String fileName, String module) throws IOException, ClassFormatException {
257-
return getJrtSystem(jrt).getClassfile(fileName, module);
274+
JrtFileSystem system = getJrtSystem(jrt);
275+
if (system == null) {
276+
throw new FileNotFoundException(String.valueOf(jrt));
277+
}
278+
return system.getClassfile(fileName, module);
258279
}
259280

260281
public static ClassFileReader getClassfile(File jrt, String fileName, String module, Predicate<String> moduleNameFilter) throws IOException, ClassFormatException {
261-
return getJrtSystem(jrt).getClassfile(fileName, module, moduleNameFilter);
282+
JrtFileSystem system = getJrtSystem(jrt);
283+
if (system == null) {
284+
throw new FileNotFoundException(String.valueOf(jrt));
285+
}
286+
return system.getClassfile(fileName, module, moduleNameFilter);
262287
}
263288

264289
public static List<String> getModulesDeclaringPackage(File jrt, String qName, String moduleName) {
265-
return getJrtSystem(jrt).getModulesDeclaringPackage(qName, moduleName);
290+
JrtFileSystem system = getJrtSystem(jrt);
291+
if (system == null) {
292+
return List.of();
293+
}
294+
return system.getModulesDeclaringPackage(qName, moduleName);
266295
}
267296

268297
public static boolean hasCompilationUnit(File jrt, String qualifiedPackageName, String moduleName) {
269-
return getJrtSystem(jrt).hasClassFile(qualifiedPackageName, moduleName);
298+
JrtFileSystem system = getJrtSystem(jrt);
299+
if (system == null) {
300+
return false;
301+
}
302+
return system.hasClassFile(qualifiedPackageName, moduleName);
270303
}
271304

272305
/*

0 commit comments

Comments
 (0)