Skip to content

Commit

Permalink
Mitigate chance to get NPE on accidental dependency
Browse files Browse the repository at this point in the history
With matching "classpath*.jar" file name.

Fixes #822.
  • Loading branch information
szpak authored and hcoles committed Nov 1, 2020
1 parent 89b4f5c commit b3d7529
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
8 changes: 5 additions & 3 deletions pitest/src/main/java/org/pitest/classpath/ClassPath.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@
import org.pitest.util.PitError;
import org.pitest.util.StreamUtil;

import static org.pitest.util.ManifestUtils.CLASSPATH_JAR_FILE_PREFIX;

public class ClassPath {

private static final Logger LOG = Log.getLogger();
Expand Down Expand Up @@ -115,7 +117,7 @@ public URL findResource(final String name) {

public static Collection<String> getClassPathElementsAsPaths() {
final Set<String> filesAsString = new LinkedHashSet<>();
FCollection.mapTo(getClassPathElementsAsFiles(), file -> file.getPath(),
FCollection.mapTo(getClassPathElementsAsFiles(), File::getPath,
filesAsString);
return filesAsString;
}
Expand All @@ -138,8 +140,8 @@ public static Collection<File> getClassPathElementsAsFiles() {
* @param elements existing elements
*/
private static void addEntriesFromClasspathManifest(final Set<File> elements) {
Optional<File> maybeJar = elements.stream().filter( f -> f.getName().startsWith("classpath") && f.getName().endsWith(".jar"))
.findFirst();
Optional<File> maybeJar = elements.stream().filter(f -> f.getName().startsWith(CLASSPATH_JAR_FILE_PREFIX) && f.getName().endsWith(".jar"))
.findFirst();
maybeJar.ifPresent(file -> elements.addAll(ManifestUtils.readClasspathManifest(file)));
}

Expand Down
4 changes: 3 additions & 1 deletion pitest/src/main/java/org/pitest/util/ManifestUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
*/
public class ManifestUtils {

public static final String CLASSPATH_JAR_FILE_PREFIX = "pitest-classpath-jar-file-";

// Method based on
// https://github.com/JetBrains/intellij-community/blob/master/java/java-runtime/src/com/intellij/rt/execution/testFrameworks/ForkedByModuleSplitter.java
// JetBrains copyright notice and licence retained above.
Expand All @@ -59,7 +61,7 @@ public static File createClasspathJarFile(String classpath)
}
attributes.put(Attributes.Name.CLASS_PATH, classpathForManifest.toString());

File jarFile = File.createTempFile("classpath", ".jar");
File jarFile = File.createTempFile(CLASSPATH_JAR_FILE_PREFIX, ".jar");
try (BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(jarFile));
ZipOutputStream jarPlugin = new JarOutputStream(out, manifest);
) {
Expand Down

0 comments on commit b3d7529

Please sign in to comment.