Skip to content
This repository has been archived by the owner on Nov 10, 2023. It is now read-only.

Commit

Permalink
java_binary blocklist pattern refactoring
Browse files Browse the repository at this point in the history
Summary: ^

Reviewed By: IanChilds

fbshipit-source-id: 938ece57420ddf14e94ff278b82c07543fe3bac6
  • Loading branch information
mykola-semko authored and facebook-github-bot committed Sep 28, 2021
1 parent 92c6901 commit ae5b505
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 43 deletions.
12 changes: 6 additions & 6 deletions src/com/facebook/buck/jvm/java/JavaBinary.java
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,9 @@ public class JavaBinary extends AbstractBuildRuleWithDeclaredAndExtraDeps

@SuppressWarnings("PMD.UnusedPrivateField")
@AddToRuleKey
private final ImmutableSet<Pattern> blacklist;
private final ImmutableSet<Pattern> blocklist;

private final PatternsMatcher blacklistPatternsMatcher;
private final PatternsMatcher blocklistPatternsMatcher;

private final ImmutableSet<JavaLibrary> transitiveClasspathDeps;
private final ImmutableSet<SourcePath> transitiveClasspaths;
Expand All @@ -99,7 +99,7 @@ public JavaBinary(
boolean mergeManifests,
boolean disallowAllDuplicates,
@Nullable Path metaInfDirectory,
ImmutableSet<Pattern> blacklist,
ImmutableSet<Pattern> blocklist,
ImmutableSet<JavaLibrary> transitiveClasspathDeps,
ImmutableSet<SourcePath> transitiveClasspaths,
boolean cache,
Expand All @@ -114,8 +114,8 @@ public JavaBinary(
metaInfDirectory != null
? PathSourcePath.of(getProjectFilesystem(), metaInfDirectory)
: null;
this.blacklist = blacklist;
blacklistPatternsMatcher = new PatternsMatcher(blacklist);
this.blocklist = blocklist;
this.blocklistPatternsMatcher = new PatternsMatcher(blocklist);
this.transitiveClasspathDeps = transitiveClasspathDeps;
this.transitiveClasspaths = transitiveClasspaths;
this.cache = cache;
Expand Down Expand Up @@ -181,7 +181,7 @@ public ImmutableList<Step> getBuildSteps(
.setDuplicatesLogLevel(duplicatesLogLevel)
.setRemoveEntryPredicate(
entry ->
blacklistPatternsMatcher.substringMatches(((ZipEntry) entry).getName()))
blocklistPatternsMatcher.substringMatches(((ZipEntry) entry).getName()))
.build());
commands.add(jar);

Expand Down
33 changes: 14 additions & 19 deletions src/com/facebook/buck/util/PatternsMatcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@

package com.facebook.buck.util;

import com.google.common.collect.ImmutableCollection;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Maps;
import java.util.Collection;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

/**
* Helper class that keeps a list of compiled patterns and provides a method to check whether a
Expand All @@ -34,48 +34,43 @@ public class PatternsMatcher {
/** A pattern which matches no string */
public static final PatternsMatcher NONE = new PatternsMatcher(ImmutableSet.of(), false);

private final Collection<Pattern> patterns;
private final ImmutableSet<Pattern> patterns;
/** True is like having a pattern {@code .*} in the pattern set */
private final boolean matchesAny;

private PatternsMatcher(Collection<Pattern> patterns, boolean matchesAny) {
private PatternsMatcher(ImmutableSet<Pattern> patterns, boolean matchesAny) {
this.patterns = patterns;
this.matchesAny = matchesAny;
}

public PatternsMatcher(Collection<String> rawPatterns) {
patterns = rawPatterns.stream().map(Pattern::compile).collect(Collectors.toList());
matchesAny = false;
public PatternsMatcher(ImmutableCollection<String> rawPatterns) {
this(rawPatterns.stream().map(Pattern::compile).collect(ImmutableSet.toImmutableSet()), false);
}

public PatternsMatcher(ImmutableSet<Pattern> compiledPatterns) {
patterns = compiledPatterns;
matchesAny = false;
this(compiledPatterns, false);
}

/** @return true if the given string matches some of the patterns */
public boolean matches(String string) {
if (matchesAny) {
return true;
}
for (Pattern pattern : patterns) {
if (pattern.matcher(string).matches()) {
return true;
}
}
return false;
return match(string, true);
}

/**
* @return true if a substring of the given string matches some of the patterns or there are no
* patterns
*/
public boolean substringMatches(String string) {
return match(string, false);
}

private boolean match(String string, boolean fullMatch) {
if (matchesAny) {
return true;
}
for (Pattern pattern : patterns) {
if (pattern.matcher(string).find()) {
Matcher matcher = pattern.matcher(string);
if (fullMatch ? matcher.matches() : matcher.find()) {
return true;
}
}
Expand Down
2 changes: 1 addition & 1 deletion test/com/facebook/buck/jvm/java/JavaBinaryTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public void testGetExecutableCommand() {
/* merge manifests */ true,
false,
null,
/* blacklist */ ImmutableSet.of(),
/* blocklist */ ImmutableSet.of(),
ImmutableSet.of(),
ImmutableSet.of(),
/* cache */ true,
Expand Down
33 changes: 16 additions & 17 deletions test/com/facebook/buck/util/PatternsMatcherTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

import java.util.Arrays;
import java.util.Collections;
import com.google.common.collect.ImmutableList;
import java.util.Map;
import java.util.TreeMap;
import org.junit.Test;
Expand All @@ -31,7 +30,7 @@ public class PatternsMatcherTest {
@Test
public void testMatchesPattern() {
PatternsMatcher patternsMatcher =
new PatternsMatcher(Arrays.asList("pattern.*", "test_pattern"));
new PatternsMatcher(ImmutableList.of("pattern.*", "test_pattern"));

assertTrue(patternsMatcher.matches("pattern"));
assertTrue(patternsMatcher.matches("test_pattern"));
Expand All @@ -42,72 +41,72 @@ public void testMatchesPattern() {
@Test
public void testMatchesAnyWithExactMatch() {
PatternsMatcher patternsMatcher =
new PatternsMatcher(Arrays.asList("pattern.*", "test_pattern"));
new PatternsMatcher(ImmutableList.of("pattern.*", "test_pattern"));

assertTrue(patternsMatcher.matches("test_pattern"));
}

@Test
public void testMatchesAnyWithWildcard() {
PatternsMatcher patternsMatcher =
new PatternsMatcher(Arrays.asList("pattern.*", "test_pattern"));
new PatternsMatcher(ImmutableList.of("pattern.*", "test_pattern"));

assertTrue(patternsMatcher.matches("pattern"));
}

@Test
public void testDoesNotMatchPrefix() {
PatternsMatcher patternsMatcher = new PatternsMatcher(Collections.singletonList("test"));
PatternsMatcher patternsMatcher = new PatternsMatcher(ImmutableList.of("test"));

assertFalse(patternsMatcher.matches("test_pattern"));
}

@Test
public void testMatchAnyWithNonMatchingPrefixReturnsFalse() {
PatternsMatcher patternsMatcher = new PatternsMatcher(Collections.singletonList("test"));
PatternsMatcher patternsMatcher = new PatternsMatcher(ImmutableList.of("test"));

assertFalse(patternsMatcher.matches("test_pattern"));
}

@Test
public void testSubstringMatchesPrefix() {
PatternsMatcher patternsMatcher = new PatternsMatcher(Collections.singletonList("test"));
PatternsMatcher patternsMatcher = new PatternsMatcher(ImmutableList.of("test"));

assertTrue(patternsMatcher.substringMatches("test_pattern"));
}

@Test
public void testDoesNotMatchSuffix() {
PatternsMatcher patternsMatcher = new PatternsMatcher(Collections.singletonList("pattern"));
PatternsMatcher patternsMatcher = new PatternsMatcher(ImmutableList.of("pattern"));

assertFalse(patternsMatcher.matches("test_pattern"));
}

@Test
public void testSubstringMatchesSuffix() {
PatternsMatcher patternsMatcher = new PatternsMatcher(Collections.singletonList("pattern"));
PatternsMatcher patternsMatcher = new PatternsMatcher(ImmutableList.of("pattern"));

assertTrue(patternsMatcher.substringMatches("test_pattern"));
}

@Test
public void testDoesNotMatchInfix() {
PatternsMatcher patternsMatcher = new PatternsMatcher(Collections.singletonList("_"));
PatternsMatcher patternsMatcher = new PatternsMatcher(ImmutableList.of("_"));

assertFalse(patternsMatcher.matches("test_pattern"));
}

@Test
public void testSubstringMatchesInfix() {
PatternsMatcher patternsMatcher = new PatternsMatcher(Collections.singletonList("_"));
PatternsMatcher patternsMatcher = new PatternsMatcher(ImmutableList.of("_"));

assertTrue(patternsMatcher.substringMatches("test_pattern"));
}

@Test
public void testDoesNotMatchPattern() {
PatternsMatcher patternsMatcher =
new PatternsMatcher(Arrays.asList("pattern.*", "test_pattern"));
new PatternsMatcher(ImmutableList.of("pattern.*", "test_pattern"));

assertFalse(patternsMatcher.matches("wrong_pattern"));
assertFalse(patternsMatcher.substringMatches("wrong_pat"));
Expand All @@ -116,7 +115,7 @@ public void testDoesNotMatchPattern() {
@Test
public void testMatchesAnyDoesNotMatchPattern() {
PatternsMatcher patternsMatcher =
new PatternsMatcher(Arrays.asList("pattern.*", "test_pattern"));
new PatternsMatcher(ImmutableList.of("pattern.*", "test_pattern"));

assertFalse(patternsMatcher.matches("wrong_pattern"));
}
Expand All @@ -134,15 +133,15 @@ public void testMatchesMatchesEmptyPatterns() {
@Test
public void testHasPatterns() {
PatternsMatcher patternsMatcher =
new PatternsMatcher(Arrays.asList("pattern.*", "test_pattern"));
new PatternsMatcher(ImmutableList.of("pattern.*", "test_pattern"));

assertFalse(patternsMatcher.isMatchesAny());
assertFalse(patternsMatcher.isMatchesNone());
}

@Test
public void testHasNoPatterns() {
PatternsMatcher patternsMatcher = new PatternsMatcher(Collections.emptyList());
PatternsMatcher patternsMatcher = new PatternsMatcher(ImmutableList.of());

assertTrue(patternsMatcher.isMatchesNone());
assertFalse(patternsMatcher.isMatchesAny());
Expand All @@ -166,7 +165,7 @@ public void testFilterMatchingMapEntriesWithEmptyPatterns() {

@Test
public void testFilterMatchingMapEntries() {
PatternsMatcher patternsMatcher = new PatternsMatcher(Arrays.asList("e1", "e2"));
PatternsMatcher patternsMatcher = new PatternsMatcher(ImmutableList.of("e1", "e2"));

Map<String, String> entries =
new TreeMap<String, String>() {
Expand Down

0 comments on commit ae5b505

Please sign in to comment.