Skip to content

Commit

Permalink
Simplify data-structures in BuildErrorReporters
Browse files Browse the repository at this point in the history
  • Loading branch information
HannesWell committed Apr 26, 2024
1 parent 1049786 commit 692af00
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 206 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,17 @@
package org.eclipse.pde.internal.core.builders;

import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.function.Predicate;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.ParserConfigurationException;
Expand Down Expand Up @@ -151,22 +151,6 @@ public void addAttributes(Map<String, String> attributes) {
}
}

static class WildcardFilenameFilter implements FilenameFilter {

private final Pattern pattern;

public WildcardFilenameFilter(String file) {
pattern = PatternConstructor.createPattern(file, false);
}

@Override
public boolean accept(File dir, String name) {
Matcher matcher = pattern.matcher(name);
return matcher.matches();
}

}

protected List<BuildProblem> fProblemList = new ArrayList<>();
protected int fBuildSeverity;
protected int fClasspathSeverity;
Expand Down Expand Up @@ -736,12 +720,15 @@ private void validateBinIncludes(IBuildEntry binIncludes, String key) {
// check for wildcards
IPath project = fFile.getProject().getLocation();
if (project != null && token != null) {
File projectFile = project.toFile();
File[] files = projectFile.listFiles(new WildcardFilenameFilter(token));
for (File file : files) {
if (file.toString().endsWith(key)) {
return true;
Predicate<String> isMatch = toMatcher(token);
try (var files = Files.newDirectoryStream(project.toPath(),
path -> isMatch.test(path.getFileName().toString()));) {
for (Path file : files) {
if (file.toString().endsWith(key)) {
return true;
}
}
} catch (IOException e) { // ignore
}
}
return false;
Expand Down Expand Up @@ -820,29 +807,14 @@ private void validateMissingSourceInBinIncludes(IBuildEntry binIncludes, List<St
continue;
}
}
key = key.substring(PROPERTY_SOURCE_PREFIX.length());
boolean found = false;
String[] binIncludesTokens = binIncludes.getTokens();
for (String token : binIncludesTokens) {
Pattern pattern = PatternConstructor.createPattern(token, false);
if (pattern.matcher(key).matches()) {
found = true;
}
}
String libName = key.substring(PROPERTY_SOURCE_PREFIX.length());
List<Predicate<String>> matchers = Arrays.stream(binIncludes.getTokens()).map(BuildErrorReporter::toMatcher)
.toList();
boolean found = matchers.stream().anyMatch(m -> m.test(libName));
// account for trailing slash on class file folders
if (!found) {
IPath path = IPath.fromOSString(key);
if (path.getFileExtension() == null) {
if (!key.endsWith("/")) { //$NON-NLS-1$
key = key + "/"; //$NON-NLS-1$
for (String token : binIncludesTokens) {
Pattern pattern = PatternConstructor.createPattern(token, false);
if (pattern.matcher(key).matches()) {
found = true;
}
}
}
}
if (!found && IPath.fromOSString(libName).getFileExtension() == null && !libName.endsWith("/")) { //$NON-NLS-1$
String folderName = libName + "/"; //$NON-NLS-1$
found = matchers.stream().anyMatch(m -> m.test(folderName));
}
if (!found) {
String msg = NLS.bind(PDECoreMessages.BuildErrorReporter_binIncludesMissing, key);
Expand Down Expand Up @@ -1333,6 +1305,10 @@ private VirtualMarker report(String message, int line, int problemID, String bui
return marker;
}

private static Predicate<String> toMatcher(String token) {
return PatternConstructor.createPattern(token, false).asMatchPredicate();
}

public boolean isCustomBuild() {
WorkspaceBuildModel wbm = new WorkspaceBuildModel(fFile);
IBuild build = wbm.getBuild();
Expand Down
Loading

0 comments on commit 692af00

Please sign in to comment.