From 6d325eedc88cb46b545c959d96b4360b5a788aee Mon Sep 17 00:00:00 2001 From: Michael Vorburger Date: Fri, 2 Feb 2018 01:38:30 +0100 Subject: [PATCH] #64 new option to ignore by class FQN regexp Signed-off-by: Michael Vorburger Signed-off-by: Michael Vorburger --- README.md | 3 +- .../modernizer_maven_plugin/Modernizer.java | 31 ++++++++++++-- .../ModernizerMojo.java | 41 ++++++++++++++----- .../ModernizerTest.java | 18 ++++---- 4 files changed, 71 insertions(+), 22 deletions(-) diff --git a/README.md b/README.md index 3fcb7bb..6914465 100644 --- a/README.md +++ b/README.md @@ -37,8 +37,9 @@ The `` stanza can contain several elements: * `` user-specified violations file. The latter files override violations from the former ones, including `violationsFile` and the default violations. Can point to classpath using absolute paths, e.g. `classpath:/your/file.xml`. * `` 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`. * `` violations to disable. Each exclusion should be in the javap format: `java/lang/String.getBytes:(Ljava/lang/String;)[B`. -* `` violation patterns to disable. Each exclusion should be a regular expression that matches the javap format: `java/lang/.*`. +* `` violation patterns to disable, specified using `` child elements. Each exclusion should be a regular expression that matches the javap format: `java/lang/.*` of a violation. * `` package prefixes to ignore, specified using `` child elements. Specifying `foo.bar` subsequently ignores `foo.bar.*`, `foo.bar.baz.*` and so on. +* `` full qualified class names (incl. package) to ignore, specified using `` child elements. Each exclusion should be a regular expression that matches a package and/or class; the package will be / not . separated (ASM's format). To run Modernizer during the verify phase of your build, add the following to the modernizer `` stanza in your pom.xml: diff --git a/src/main/java/org/gaul/modernizer_maven_plugin/Modernizer.java b/src/main/java/org/gaul/modernizer_maven_plugin/Modernizer.java index ae991dd..bb5185e 100644 --- a/src/main/java/org/gaul/modernizer_maven_plugin/Modernizer.java +++ b/src/main/java/org/gaul/modernizer_maven_plugin/Modernizer.java @@ -48,11 +48,13 @@ final class Modernizer { private final Collection exclusions; private final Collection exclusionPatterns; private final Collection ignorePackages; + private final Collection ignoreFullClassNamePatterns; Modernizer(String javaVersion, Map violations, Collection exclusions, Collection exclusionPatterns, - Collection ignorePackages) { + Collection ignorePackages, + Collection ignoreClassNamePatterns) { long version; if (javaVersion.startsWith("1.")) { version = Long.parseLong(javaVersion.substring(2)); @@ -65,13 +67,15 @@ final class Modernizer { this.exclusions = Utils.createImmutableSet(exclusions); this.exclusionPatterns = Utils.createImmutableSet(exclusionPatterns); this.ignorePackages = Utils.createImmutableSet(ignorePackages); + this.ignoreFullClassNamePatterns + = Utils.createImmutableSet(ignoreClassNamePatterns); } Collection check(ClassReader classReader) throws IOException { ModernizerClassVisitor classVisitor = new ModernizerClassVisitor( javaVersion, violations, exclusions, exclusionPatterns, - ignorePackages); + ignorePackages, ignoreFullClassNamePatterns); classReader.accept(classVisitor, 0); return classVisitor.getOccurrences(); } @@ -121,14 +125,17 @@ final class ModernizerClassVisitor extends ClassVisitor { private final Collection exclusions; private final Collection exclusionPatterns; private final Collection ignorePackages; + private final Collection ignoreFullClassNamePatterns; private final Collection occurrences = new ArrayList(); private String packageName; + private String className; ModernizerClassVisitor(long javaVersion, Map violations, Collection exclusions, Collection exclusionPatterns, - Collection ignorePackages) { + Collection ignorePackages, + Collection ignoreFullClassNamePatterns) { super(Opcodes.ASM5); Utils.checkArgument(javaVersion >= 0); this.javaVersion = javaVersion; @@ -136,17 +143,23 @@ final class ModernizerClassVisitor extends ClassVisitor { this.exclusions = Utils.checkNotNull(exclusions); this.exclusionPatterns = Utils.checkNotNull(exclusionPatterns); this.ignorePackages = Utils.checkNotNull(ignorePackages); + this.ignoreFullClassNamePatterns = + Utils.checkNotNull(ignoreFullClassNamePatterns); } @Override public void visit(int version, int access, String name, String signature, String superName, String[] interfaces) { + className = name; if (name.contains("/")) { packageName = name.substring(0, name.lastIndexOf('/')) .replace('/', '.'); } else { packageName = ""; } + if (ignoreClass()) { + return; + } for (String itr : interfaces) { Violation violation = violations.get(itr); checkToken(itr, violation, name, /*lineNumber=*/ -1); @@ -210,6 +223,9 @@ private void checkToken(String token, Violation violation, String name, if (violation != null && !exclusions.contains(token) && javaVersion >= violation.getVersion() && !ignorePackages.contains(packageName)) { + if (ignoreClass()) { + return; + } for (Pattern pattern : exclusionPatterns) { if (pattern.matcher(token).matches()) { return; @@ -225,6 +241,15 @@ private void checkToken(String token, Violation violation, String name, } } + private boolean ignoreClass() { + for (Pattern pattern : ignoreFullClassNamePatterns) { + if (pattern.matcher(className).matches()) { + return true; + } + } + return false; + } + Collection getOccurrences() { return occurrences; } diff --git a/src/main/java/org/gaul/modernizer_maven_plugin/ModernizerMojo.java b/src/main/java/org/gaul/modernizer_maven_plugin/ModernizerMojo.java index a7addee..8816371 100644 --- a/src/main/java/org/gaul/modernizer_maven_plugin/ModernizerMojo.java +++ b/src/main/java/org/gaul/modernizer_maven_plugin/ModernizerMojo.java @@ -76,16 +76,16 @@ public final class ModernizerMojo extends AbstractMojo { * 1.2 but not when targeting Java 1.1. */ @Parameter(required = true, property = "modernizer.javaVersion") - private String javaVersion = null; + private String javaVersion; /** Fail phase if Modernizer detects any violations. */ @Parameter(defaultValue = "true", property = "modernizer.failOnViolations") - private boolean failOnViolations = true; + protected boolean failOnViolations = true; /** Run Modernizer on test classes. */ @Parameter(defaultValue = "true", property = "modernizer.includeTestClasses") - private boolean includeTestClasses = true; + protected boolean includeTestClasses = true; /** * User-specified violation file. Also disables standard violation checks. @@ -96,7 +96,7 @@ public final class ModernizerMojo extends AbstractMojo { * for the default violations file. */ @Parameter(property = "modernizer.violationsFile") - private String violationsFile = "classpath:/modernizer.xml"; + protected String violationsFile = "classpath:/modernizer.xml"; /** * User-specified violation files. The violations loaded from @@ -111,7 +111,7 @@ public final class ModernizerMojo extends AbstractMojo { * for the default violations file. */ @Parameter(property = "modernizer.violationsFiles") - private List violationsFiles = emptyList(); + protected List violationsFiles = emptyList(); /** * Disables user-specified violations. This is a text file with one @@ -120,7 +120,7 @@ public final class ModernizerMojo extends AbstractMojo { * java/lang/String.getBytes:(Ljava/lang/String;)[B. */ @Parameter(property = "modernizer.exclusionsFile") - private String exclusionsFile = null; + private String exclusionsFile; /** * Log level to emit violations at, e.g., error, warn, info, debug. @@ -135,7 +135,7 @@ public final class ModernizerMojo extends AbstractMojo { * java/lang/String.getBytes:(Ljava/lang/String;)[B. */ @Parameter - private Set exclusions = new HashSet(); + protected Set exclusions = new HashSet(); /** * Violation patterns to disable. Each exclusion should be a @@ -144,7 +144,7 @@ public final class ModernizerMojo extends AbstractMojo { * java/lang/.* */ @Parameter - private Set exclusionPatterns = new HashSet(); + protected Set exclusionPatterns = new HashSet(); /** * Package prefixes to ignore, specified using <ignorePackage> child @@ -152,7 +152,17 @@ public final class ModernizerMojo extends AbstractMojo { * foo.bar.baz.* and so on. */ @Parameter - private Set ignorePackages = new HashSet(); + protected Set ignorePackages = new HashSet(); + + /** + * Fully qualified class names (incl. package) to ignore by regular + * expression, specified using <ignoreClassNamePattern> child + * elements. Specifying .*.bar.* ignores foo.bar.*, foo.bar.baz.* but + * also bar.* and so on; or .*Immutable ignores all class with names + * ending in Immutable in all packages. + */ + @Parameter + protected Set ignoreClassNamePatterns = new HashSet(); private Modernizer modernizer; @@ -192,8 +202,19 @@ public void execute() throws MojoExecutionException { } } + Set allIgnoreFullClassNamePatterns = new HashSet(); + for (String pattern : ignoreClassNamePatterns) { + try { + allIgnoreFullClassNamePatterns.add(Pattern.compile(pattern)); + } catch (PatternSyntaxException pse) { + throw new MojoExecutionException( + "Invalid exclusion pattern", pse); + } + } + modernizer = new Modernizer(javaVersion, allViolations, allExclusions, - allExclusionPatterns, ignorePackages); + allExclusionPatterns, ignorePackages, + allIgnoreFullClassNamePatterns); try { long count = recurseFiles(outputDirectory); diff --git a/src/test/java/org/gaul/modernizer_maven_plugin/ModernizerTest.java b/src/test/java/org/gaul/modernizer_maven_plugin/ModernizerTest.java index 2c68362..76e3f64 100644 --- a/src/test/java/org/gaul/modernizer_maven_plugin/ModernizerTest.java +++ b/src/test/java/org/gaul/modernizer_maven_plugin/ModernizerTest.java @@ -205,7 +205,7 @@ public void testMethodLegacyApiCurrentJavaWithExclusion() throws Exception { "java/lang/String.getBytes:(Ljava/lang/String;)[B"); Collection occurrences = new Modernizer( "1.6", violations, exclusions, NO_EXCLUSION_PATTERNS, - NO_IGNORED_PACKAGES).check(cr); + NO_IGNORED_PACKAGES, NO_EXCLUSION_PATTERNS).check(cr); assertThat(occurrences).hasSize(0); } @@ -217,7 +217,7 @@ public void testMethodLegacyApiCurrentJavaWithExclusionPattern() Pattern.compile("java/lang/.*")); Collection occurrences = new Modernizer( "1.6", violations, NO_EXCLUSIONS, exclusionPatterns, - NO_IGNORED_PACKAGES).check(cr); + NO_IGNORED_PACKAGES, NO_EXCLUSION_PATTERNS).check(cr); assertThat(occurrences).hasSize(0); } @@ -229,7 +229,7 @@ public void testMethodLegacyApiCurrentJavaWithIgnorePackages() StringGetBytesString.class.getPackage().getName()); Collection occurrences = new Modernizer( "1.6", violations, NO_EXCLUSIONS, NO_EXCLUSION_PATTERNS, - ignorePackages).check(cr); + ignorePackages, NO_EXCLUSION_PATTERNS).check(cr); assertThat(occurrences).hasSize(0); } @@ -240,7 +240,7 @@ public void testMethodLegacyApiCurrentJavaWithIgnorePackagesPrefix() Collection ignorePackages = Collections.singleton("org.gaul"); Collection occurrences = new Modernizer( "1.6", violations, NO_EXCLUSIONS, NO_EXCLUSION_PATTERNS, - ignorePackages).check(cr); + ignorePackages, NO_EXCLUSION_PATTERNS).check(cr); assertThat(occurrences).hasSize(0); } @@ -251,7 +251,7 @@ public void testMethodLegacyApiCurrentJavaWithIgnorePackagesSubprefix() Collection ignorePackages = Collections.singleton("org"); Collection occurrences = new Modernizer( "1.6", violations, NO_EXCLUSIONS, NO_EXCLUSION_PATTERNS, - ignorePackages).check(cr); + ignorePackages, NO_EXCLUSION_PATTERNS).check(cr); assertThat(occurrences).hasSize(0); } @@ -262,7 +262,7 @@ public void testMethodLegacyApiCurrentJavaWithIgnorePackagesPartialPrefix() Collection ignorePackages = Collections.singleton("org.gau"); Collection occurrences = new Modernizer( "1.6", violations, NO_EXCLUSIONS, NO_EXCLUSION_PATTERNS, - ignorePackages).check(cr); + ignorePackages, NO_EXCLUSION_PATTERNS).check(cr); assertThat(occurrences).hasSize(1); } @@ -293,7 +293,8 @@ public void testAnnotationViolation() throws Exception { testViolations.put(name, new Violation(name, 5, "")); Modernizer modernizer = new Modernizer("1.5", testViolations, - NO_EXCLUSIONS, NO_EXCLUSION_PATTERNS, NO_IGNORED_PACKAGES); + NO_EXCLUSIONS, NO_EXCLUSION_PATTERNS, NO_IGNORED_PACKAGES, + NO_EXCLUSION_PATTERNS); ClassReader cr = new ClassReader(AnnotatedMethod.class.getName()); Collection occurences = modernizer.check(cr); @@ -362,7 +363,8 @@ public void testAllViolations() throws Exception { /** Helper to create Modernizer object with default parameters. */ private Modernizer createModernizer(String javaVersion) { return new Modernizer(javaVersion, violations, NO_EXCLUSIONS, - NO_EXCLUSION_PATTERNS, NO_IGNORED_PACKAGES); + NO_EXCLUSION_PATTERNS, NO_IGNORED_PACKAGES, + NO_EXCLUSION_PATTERNS); } private static class CharsetsTestClass {