Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ignore packages #12

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ The `<configuration>` stanza can contain several elements:
* `<includeTestClasses>` run Modernizer on test classes. Defaults to true.
* `<violationsFile>` user-specified violation file. Also disables standard violation checks.
* `<exclusionsFile>` disables user-specified violations. This is a text file with one exclusion per line in the javap format: `java/lang/String.getBytes:(Ljava/lang/String;)[B`.
* `<ignorePackages>` package prefixes to ignore, specified using `<ignorePackage>` child elements. Specifying `foo.bar` subsequently ignores `foo.bar.*`, `foo.bar.baz.*` and so on.

To run Modernizer during the compile phase of your build, add the following to
the modernizer `<plugin>` stanza in your pom.xml:
Expand Down
39 changes: 26 additions & 13 deletions src/main/java/org/gaul/modernizer_maven_plugin/ModernizerMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,6 @@

package org.gaul.modernizer_maven_plugin;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Collection;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import javax.xml.parsers.ParserConfigurationException;

import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugins.annotations.Execute;
Expand All @@ -37,6 +25,13 @@
import org.apache.maven.project.MavenProject;
import org.xml.sax.SAXException;

import javax.xml.parsers.ParserConfigurationException;
import java.io.*;
import java.util.Collection;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

@Mojo(name = "modernizer")
@Execute(phase = LifecyclePhase.PROCESS_TEST_CLASSES)
public final class ModernizerMojo extends AbstractMojo {
Expand Down Expand Up @@ -70,6 +65,9 @@ public final class ModernizerMojo extends AbstractMojo {
@Parameter(defaultValue = "${exclusionsFile}")
private String exclusionsFile;

@Parameter
private HashSet<String> ignorePackages;

private Modernizer modernizer;

@Override
Expand Down Expand Up @@ -155,7 +153,7 @@ private long recurseFiles(File file) throws IOException {
count += recurseFiles(new File(file, child));
}
}
} else if (file.getPath().endsWith(".class")) {
} else if (fileMatches(file)) {
InputStream is = new FileInputStream(file);
try {
Collection<ViolationOccurrence> occurrences =
Expand Down Expand Up @@ -184,4 +182,19 @@ private long recurseFiles(File file) throws IOException {
}
return count;
}

private boolean fileMatches(File file) {
String path = file.getPath();
if (path.endsWith(".class")) {
if (ignorePackages != null) {
for (String ignoredPrefix : ignorePackages) {
if (path.replace(File.separatorChar, '.').contains(ignoredPrefix)) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of filtering package names in the Mojo, perhaps Modernizer.check should do this? One approach would be to use ClassReader.getClassName.

return false;
}
}
}
return true;
}
return false;
}
}