Skip to content

Commit

Permalink
Don't change glob behavior for accept/reject criteria (#588)
Browse files Browse the repository at this point in the history
  • Loading branch information
lukehutch committed Oct 28, 2021
1 parent 629dd36 commit a50e433
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 11 deletions.
2 changes: 1 addition & 1 deletion src/main/java/io/github/classgraph/ClassGraph.java
Expand Up @@ -1116,7 +1116,7 @@ private void acceptOrRejectLibOrExtJars(final boolean accept, final String... ja
}
if (jarLeafName.contains("*")) {
// Compare wildcarded pattern against all jars in lib and ext dirs
final Pattern pattern = AcceptReject.globToPattern(jarLeafName);
final Pattern pattern = AcceptReject.globToPattern(jarLeafName, /* simpleGlob = */ true);
boolean found = false;
for (final String libOrExtJarPath : SystemJarFinder.getJreLibOrExtJars()) {
final String libOrExtJarLeafName = JarUtils.leafName(libOrExtJarPath);
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/io/github/classgraph/ScanResult.java
Expand Up @@ -690,7 +690,7 @@ public ResourceList getResourcesMatchingWildcard(final String wildcardString) {
if (closed.get()) {
throw new IllegalArgumentException("Cannot use a ScanResult after it has been closed");
}
return getResourcesMatchingPattern(AcceptReject.globToPattern(wildcardString));
return getResourcesMatchingPattern(AcceptReject.globToPattern(wildcardString, /* simpleGlob = */ false));
}

// -------------------------------------------------------------------------------------------------------------
Expand Down
Expand Up @@ -245,7 +245,7 @@ public void addToAccept(final String str) {
this.acceptPatterns = new ArrayList<>();
}
this.acceptGlobs.add(str);
this.acceptPatterns.add(globToPattern(str));
this.acceptPatterns.add(globToPattern(str, /* simpleGlob = */ true));
} else {
if (this.accept == null) {
this.accept = new HashSet<>();
Expand Down Expand Up @@ -301,7 +301,7 @@ public void addToReject(final String str) {
this.rejectPatterns = new ArrayList<>();
}
this.rejectGlobs.add(str);
this.rejectPatterns.add(globToPattern(str));
this.rejectPatterns.add(globToPattern(str, /* simpleGlob = */ true));
} else {
if (this.reject == null) {
this.reject = new HashSet<>();
Expand Down Expand Up @@ -565,19 +565,30 @@ public static String classNameToClassfilePath(final String className) {
}

/**
* Convert a spec with a '*' glob character into a regular expression. Replaces "." with "\.", "**" with ".*",
* "*" with "[^/]*", and "?" with ".", then compiles a regular expression.
* Convert a spec with a '*' glob character into a regular expression.
*
* @param glob
* The glob string.
* @param simpleGlob
* if true, handles simple globs: "*" matches zero or more characters (replaces "." with "\\.", "*"
* with ".*", then compiles a regular expression). If false, handles filesystem-style globs: "**"
* matches zero or more characters, "*" matches zero or more characters other than "/", "?" matches
* one character (replaces "." with "\\.", "**" with ".*", "*" with "[^/]*", and "?" with ".", then
* compiles a regular expression).
* @return The Pattern created from the glob string.
*/
public static Pattern globToPattern(final String glob) {
public static Pattern globToPattern(final String glob, final boolean simpleGlob) {
// TODO: when API is next broken, make all glob behavior consistent between accept/reject criteria
// and resource filtering (i.e. enforce simpleGlob == false)
return Pattern.compile("^" //
+ glob.replace(".", "\\.") //
.replace("*", "[^/]*") //
.replace("[^/]*[^/]*", ".*") //
.replace('?', '.') //
+ (simpleGlob //
? glob.replace(".", "\\.") //
.replace("*", ".*") //
: glob.replace(".", "\\.") //
.replace("*", "[^/]*") //
.replace("[^/]*[^/]*", ".*") //
.replace('?', '.') //
) //
+ "$");
}

Expand Down

0 comments on commit a50e433

Please sign in to comment.