diff --git a/README.md b/README.md index 897df76..752c089 100644 --- a/README.md +++ b/README.md @@ -66,6 +66,23 @@ documents all of these. The most commonly used flags: * `-Dmodernizer.failOnViolations` - fail phase if violations detected, defaults to true * `-Dmodernizer.skip` - skip plugin execution, defaults to false +Ignoring elements +----------------- + +You can indicate that a violations with a class or method should be ignored by +the plugin by adding `@SuppressModernizer` to the element you'd like +to ignore and adding the following dependency to your pom: + +```xml + + + org.gaul + modernizer-maven-annotations + 1.8.0 + + +``` + References ---------- * [ASM](http://asm.ow2.org/) provides Java bytecode introspection which enables Modernizer's checks diff --git a/modernizer-maven-annotations/pom.xml b/modernizer-maven-annotations/pom.xml new file mode 100644 index 0000000..3101dfb --- /dev/null +++ b/modernizer-maven-annotations/pom.xml @@ -0,0 +1,30 @@ + + 4.0.0 + + + org.gaul + modernizer-maven-parent + 1.8.0-SNAPSHOT + + + modernizer-maven-annotations + + + + + maven-checkstyle-plugin + + checkstyle/checkstyle.xml + checkstyle/copyright_header.txt + + + + org.gaul + modernizer-maven-policy + ${project.version} + + + + + + diff --git a/modernizer-maven-annotations/src/main/java/org/gaul/modernizer_maven_annotations/SuppressModernizer.java b/modernizer-maven-annotations/src/main/java/org/gaul/modernizer_maven_annotations/SuppressModernizer.java new file mode 100644 index 0000000..02eccee --- /dev/null +++ b/modernizer-maven-annotations/src/main/java/org/gaul/modernizer_maven_annotations/SuppressModernizer.java @@ -0,0 +1,30 @@ +/* + * Copyright 2014-2018 Andrew Gaul + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.gaul.modernizer_maven_annotations; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +@Retention(RetentionPolicy.CLASS) +@Target({ + ElementType.TYPE, + ElementType.METHOD, +}) +public @interface SuppressModernizer { +} diff --git a/modernizer-maven-plugin/pom.xml b/modernizer-maven-plugin/pom.xml new file mode 100644 index 0000000..c227dc7 --- /dev/null +++ b/modernizer-maven-plugin/pom.xml @@ -0,0 +1,119 @@ + + 4.0.0 + + + org.gaul + modernizer-maven-parent + 1.8.0-SNAPSHOT + + + modernizer-maven-plugin + maven-plugin + + Modernizer Maven Plugin + https://github.com/andrewgaul/modernizer-maven-plugin + Detect use of legacy APIs which modern Java versions supersede. + + + 3.0.5 + + + + + com.google.guava + guava + 27.0.1-jre + test + + + com.google.inject + guice + 4.0 + test + + + commons-codec + commons-codec + 1.10 + test + + + commons-io + commons-io + 2.4 + test + + + joda-time + joda-time + 2.8.1 + test + + + junit + junit + 4.12 + test + + + org.apache.maven + maven-plugin-api + 2.0 + + + org.apache.maven.plugin-tools + maven-plugin-annotations + 3.2 + provided + + + org.apache.maven + maven-project + 2.0.9 + + + org.assertj + assertj-core + 1.7.1 + test + + + org.ow2.asm + asm + + + org.ow2.asm + asm-commons + + + org.springframework + spring-beans + 4.1.6.RELEASE + test + + + org.gaul + modernizer-maven-annotations + ${project.version} + + + + + + + maven-checkstyle-plugin + + checkstyle/checkstyle.xml + checkstyle/copyright_header.txt + + + + org.gaul + modernizer-maven-policy + ${project.version} + + + + + + diff --git a/src/main/java/org/gaul/modernizer_maven_plugin/Modernizer.java b/modernizer-maven-plugin/src/main/java/org/gaul/modernizer_maven_plugin/Modernizer.java similarity index 87% rename from src/main/java/org/gaul/modernizer_maven_plugin/Modernizer.java rename to modernizer-maven-plugin/src/main/java/org/gaul/modernizer_maven_plugin/Modernizer.java index 66d27f0..9c9d00d 100644 --- a/src/main/java/org/gaul/modernizer_maven_plugin/Modernizer.java +++ b/modernizer-maven-plugin/src/main/java/org/gaul/modernizer_maven_plugin/Modernizer.java @@ -16,24 +16,27 @@ package org.gaul.modernizer_maven_plugin; +import static org.gaul.modernizer_maven_plugin.Utils.ASM_API; + import java.io.IOException; import java.io.InputStream; import java.util.ArrayList; import java.util.Collection; import java.util.HashMap; import java.util.Map; +import java.util.Set; import java.util.regex.Pattern; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; +import org.gaul.modernizer_maven_annotations.SuppressModernizer; import org.objectweb.asm.AnnotationVisitor; import org.objectweb.asm.ClassReader; import org.objectweb.asm.ClassVisitor; import org.objectweb.asm.Label; import org.objectweb.asm.MethodVisitor; -import org.objectweb.asm.Opcodes; import org.objectweb.asm.Type; import org.objectweb.asm.commons.InstructionAdapter; import org.w3c.dom.Document; @@ -48,12 +51,14 @@ final class Modernizer { private final Collection exclusions; private final Collection exclusionPatterns; private final Collection ignorePackages; + private final Set ignoreClassNames; private final Collection ignoreFullClassNamePatterns; Modernizer(String javaVersion, Map violations, Collection exclusions, Collection exclusionPatterns, Collection ignorePackages, + Set ignoreClassNames, Collection ignoreClassNamePatterns) { long version; if (javaVersion.startsWith("1.")) { @@ -67,6 +72,7 @@ final class Modernizer { this.exclusions = Utils.createImmutableSet(exclusions); this.exclusionPatterns = Utils.createImmutableSet(exclusionPatterns); this.ignorePackages = Utils.createImmutableSet(ignorePackages); + this.ignoreClassNames = Utils.createImmutableSet(ignoreClassNames); this.ignoreFullClassNamePatterns = Utils.createImmutableSet(ignoreClassNamePatterns); } @@ -75,7 +81,7 @@ Collection check(ClassReader classReader) throws IOException { ModernizerClassVisitor classVisitor = new ModernizerClassVisitor( javaVersion, violations, exclusions, exclusionPatterns, - ignorePackages, ignoreFullClassNamePatterns); + ignorePackages, ignoreClassNames, ignoreFullClassNamePatterns); classReader.accept(classVisitor, 0); return classVisitor.getOccurrences(); } @@ -125,6 +131,7 @@ final class ModernizerClassVisitor extends ClassVisitor { private final Collection exclusions; private final Collection exclusionPatterns; private final Collection ignorePackages; + private final Set ignoreClassNames; private final Collection ignoreFullClassNamePatterns; private final Collection occurrences = new ArrayList(); @@ -135,14 +142,16 @@ final class ModernizerClassVisitor extends ClassVisitor { Map violations, Collection exclusions, Collection exclusionPatterns, Collection ignorePackages, + Set ignoreClassNames, Collection ignoreFullClassNamePatterns) { - super(Opcodes.ASM7); + super(ASM_API); Utils.checkArgument(javaVersion >= 0); this.javaVersion = javaVersion; this.violations = Utils.checkNotNull(violations); this.exclusions = Utils.checkNotNull(exclusions); this.exclusionPatterns = Utils.checkNotNull(exclusionPatterns); this.ignorePackages = Utils.checkNotNull(ignorePackages); + this.ignoreClassNames = Utils.checkNotNull(ignoreClassNames); this.ignoreFullClassNamePatterns = Utils.checkNotNull(ignoreFullClassNamePatterns); } @@ -172,11 +181,12 @@ public MethodVisitor visitMethod(int access, final String methodName, String[] exceptions) { MethodVisitor base = super.visitMethod(access, methodName, methodDescriptor, methodSignature, exceptions); - MethodVisitor origVisitor = new MethodVisitor(Opcodes.ASM7, base) { + MethodVisitor origVisitor = new MethodVisitor(ASM_API, base) { }; - InstructionAdapter adapter = new InstructionAdapter(Opcodes.ASM7, + InstructionAdapter adapter = new InstructionAdapter(ASM_API, origVisitor) { private int lineNumber = -1; + private boolean methodSuppressed = false; @Override public void visitFieldInsn(int opcode, String owner, String name, @@ -196,6 +206,9 @@ public void visitMethodInsn(int opcode, String owner, String name, @Override public AnnotationVisitor visitAnnotation(String desc, boolean visible) { + methodSuppressed |= Type.getType(desc).getClassName() + .equals(SuppressModernizer.class.getName()); + String name = Type.getType(desc).getInternalName(); Violation violation = violations.get(name); checkToken(name, violation, name, lineNumber); @@ -214,6 +227,21 @@ private void visitFieldOrMethod(String owner, String name, public void visitLineNumber(int lineNumber, Label start) { this.lineNumber = lineNumber; } + + private void checkToken( + String token, + Violation violation, + String name, + int lineNumber + ) { + if (methodSuppressed) { + return; + } else { + ModernizerClassVisitor.this + .checkToken(token, violation, name, lineNumber); + } + + } }; return adapter; } @@ -242,6 +270,9 @@ private void checkToken(String token, Violation violation, String name, } private boolean ignoreClass() { + if (ignoreClassNames.contains(className)) { + return true; + } for (Pattern pattern : ignoreFullClassNamePatterns) { if (pattern.matcher(className).matches()) { return true; diff --git a/src/main/java/org/gaul/modernizer_maven_plugin/ModernizerMojo.java b/modernizer-maven-plugin/src/main/java/org/gaul/modernizer_maven_plugin/ModernizerMojo.java similarity index 95% rename from src/main/java/org/gaul/modernizer_maven_plugin/ModernizerMojo.java rename to modernizer-maven-plugin/src/main/java/org/gaul/modernizer_maven_plugin/ModernizerMojo.java index 74a0980..dfa55ff 100644 --- a/src/main/java/org/gaul/modernizer_maven_plugin/ModernizerMojo.java +++ b/modernizer-maven-plugin/src/main/java/org/gaul/modernizer_maven_plugin/ModernizerMojo.java @@ -218,9 +218,22 @@ public void execute() throws MojoExecutionException { } } + Set ignoreClassNames = new HashSet(); + try { + ignoreClassNames.addAll( + SuppressModernizerAnnotationDetector.detect(outputDirectory)); + if (includeTestClasses) { + ignoreClassNames.addAll( + SuppressModernizerAnnotationDetector.detect( + testOutputDirectory)); + } + } catch (IOException e) { + throw new MojoExecutionException("Error reading suppressions", e); + } + modernizer = new Modernizer(javaVersion, allViolations, allExclusions, allExclusionPatterns, ignorePackages, - allIgnoreFullClassNamePatterns); + ignoreClassNames, allIgnoreFullClassNamePatterns); try { long count = recurseFiles(outputDirectory); diff --git a/modernizer-maven-plugin/src/main/java/org/gaul/modernizer_maven_plugin/SuppressModernizerAnnotationDetector.java b/modernizer-maven-plugin/src/main/java/org/gaul/modernizer_maven_plugin/SuppressModernizerAnnotationDetector.java new file mode 100644 index 0000000..f3beab7 --- /dev/null +++ b/modernizer-maven-plugin/src/main/java/org/gaul/modernizer_maven_plugin/SuppressModernizerAnnotationDetector.java @@ -0,0 +1,138 @@ +/* + * Copyright 2014-2018 Andrew Gaul + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.gaul.modernizer_maven_plugin; + +import static org.gaul.modernizer_maven_plugin.Utils.ASM_API; + +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.util.HashSet; +import java.util.Set; + +import org.gaul.modernizer_maven_annotations.SuppressModernizer; +import org.objectweb.asm.AnnotationVisitor; +import org.objectweb.asm.ClassReader; +import org.objectweb.asm.ClassVisitor; +import org.objectweb.asm.Type; + +public final class SuppressModernizerAnnotationDetector { + private final Set annotatedClassNames = + new HashSet(); + private final Set allClassNames = + new HashSet(); + + private SuppressModernizerAnnotationDetector() { } + + public static Set detect(File file) throws IOException { + SuppressModernizerAnnotationDetector detector = + new SuppressModernizerAnnotationDetector(); + detector.detectInternal(file); + return detector.computeSuppressedClassNames(); + } + + // For testing + static Set detect(Class... classes) throws IOException { + SuppressModernizerAnnotationDetector detector = + new SuppressModernizerAnnotationDetector(); + for (Class clazz : classes) { + ClassReader classReader = new ClassReader(clazz.getName()); + detector.detectInternal(classReader); + } + return detector.computeSuppressedClassNames(); + } + + private Set computeSuppressedClassNames() { + Set suppressedClassNames = + new HashSet(annotatedClassNames); + for (String className : allClassNames) { + if (suppressedClassNames.contains(className)) { + continue; + } + int fromIndex = 0; + while (true) { + int index = className.indexOf('$', fromIndex); + if (index == -1) { + break; + } + boolean outerSuppressed = + annotatedClassNames.contains(className.substring(0, index)); + if (outerSuppressed) { + suppressedClassNames.add(className); + break; + } + fromIndex = index + 1; + } + } + return suppressedClassNames; + } + + private void detectInternal(File file) throws IOException { + if (!file.exists()) { + return; + } else if (file.isDirectory()) { + String[] children = file.list(); + if (children != null) { + for (String child : children) { + detectInternal(new File(file, child)); + } + } + } else if (file.getPath().endsWith(".class")) { + InputStream inputStream = null; + try { + inputStream = new FileInputStream(file); + detectInternal(new ClassReader(inputStream)); + } finally { + Utils.closeQuietly(inputStream); + } + } + } + + private void detectInternal(ClassReader classReader) { + classReader.accept(new Visitor(), 0); + } + + private final class Visitor extends ClassVisitor { + private String className; + + Visitor() { + super(ASM_API); + } + + @Override + public void visit(int version, int access, String name, + String signature, String superName, + String[] interfaces) { + this.className = name; + allClassNames.add(className); + super.visit(version, access, name, + signature, superName, + interfaces); + } + + @Override + public AnnotationVisitor visitAnnotation(String desc, boolean visible) { + boolean isSuppressModernizer = Type.getType(desc).getClassName() + .equals(SuppressModernizer.class.getName()); + if (isSuppressModernizer) { + annotatedClassNames.add(className); + } + return super.visitAnnotation(desc, visible); + } + } +} diff --git a/src/main/java/org/gaul/modernizer_maven_plugin/Utils.java b/modernizer-maven-plugin/src/main/java/org/gaul/modernizer_maven_plugin/Utils.java similarity index 97% rename from src/main/java/org/gaul/modernizer_maven_plugin/Utils.java rename to modernizer-maven-plugin/src/main/java/org/gaul/modernizer_maven_plugin/Utils.java index b360d44..117cf87 100644 --- a/src/main/java/org/gaul/modernizer_maven_plugin/Utils.java +++ b/modernizer-maven-plugin/src/main/java/org/gaul/modernizer_maven_plugin/Utils.java @@ -30,8 +30,11 @@ import java.util.Map; import java.util.Set; +import org.objectweb.asm.Opcodes; + final class Utils { static final Charset UTF_8 = Charset.forName("UTF-8"); + static final int ASM_API = Opcodes.ASM7; static T checkNotNull(T reference) { if (reference == null) { diff --git a/src/main/java/org/gaul/modernizer_maven_plugin/Violation.java b/modernizer-maven-plugin/src/main/java/org/gaul/modernizer_maven_plugin/Violation.java similarity index 100% rename from src/main/java/org/gaul/modernizer_maven_plugin/Violation.java rename to modernizer-maven-plugin/src/main/java/org/gaul/modernizer_maven_plugin/Violation.java diff --git a/src/main/java/org/gaul/modernizer_maven_plugin/ViolationOccurrence.java b/modernizer-maven-plugin/src/main/java/org/gaul/modernizer_maven_plugin/ViolationOccurrence.java similarity index 100% rename from src/main/java/org/gaul/modernizer_maven_plugin/ViolationOccurrence.java rename to modernizer-maven-plugin/src/main/java/org/gaul/modernizer_maven_plugin/ViolationOccurrence.java diff --git a/src/main/resources/copyright_header.txt b/modernizer-maven-plugin/src/main/resources/copyright_header.txt similarity index 100% rename from src/main/resources/copyright_header.txt rename to modernizer-maven-plugin/src/main/resources/copyright_header.txt diff --git a/src/main/resources/modernizer.xml b/modernizer-maven-plugin/src/main/resources/modernizer.xml similarity index 100% rename from src/main/resources/modernizer.xml rename to modernizer-maven-plugin/src/main/resources/modernizer.xml diff --git a/src/test/java/DefaultPackageClass.java b/modernizer-maven-plugin/src/test/java/DefaultPackageClass.java similarity index 100% rename from src/test/java/DefaultPackageClass.java rename to modernizer-maven-plugin/src/test/java/DefaultPackageClass.java diff --git a/src/test/java/org/gaul/modernizer_maven_plugin/ModernizerTest.java b/modernizer-maven-plugin/src/test/java/org/gaul/modernizer_maven_plugin/ModernizerTest.java similarity index 92% rename from src/test/java/org/gaul/modernizer_maven_plugin/ModernizerTest.java rename to modernizer-maven-plugin/src/test/java/org/gaul/modernizer_maven_plugin/ModernizerTest.java index 7e2c6e0..fb6ba93 100644 --- a/src/test/java/org/gaul/modernizer_maven_plugin/ModernizerTest.java +++ b/modernizer-maven-plugin/src/test/java/org/gaul/modernizer_maven_plugin/ModernizerTest.java @@ -81,6 +81,10 @@ import org.apache.commons.codec.binary.Base64; import org.apache.commons.io.FileUtils; +import org.gaul.modernizer_maven_plugin + .SuppressModernizerTestClasses.SuppressedOnClass; +import org.gaul.modernizer_maven_plugin + .SuppressModernizerTestClasses.SuppressedOnMembers; import org.joda.time.DateTime; import org.joda.time.DateTimeZone; import org.joda.time.Duration; @@ -106,6 +110,8 @@ public final class ModernizerTest { Collections.emptySet(); private static final Collection NO_IGNORED_PACKAGES = Collections.emptySet(); + private static final Set NO_IGNORED_CLASS_NAMES = + Collections.emptySet(); @Before public void setUp() throws Exception { @@ -223,7 +229,9 @@ 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, NO_EXCLUSION_PATTERNS).check(cr); + NO_IGNORED_PACKAGES, NO_IGNORED_CLASS_NAMES, + NO_EXCLUSION_PATTERNS + ).check(cr); assertThat(occurrences).hasSize(0); } @@ -235,7 +243,9 @@ public void testMethodLegacyApiCurrentJavaWithExclusionPattern() Pattern.compile("java/lang/.*")); Collection occurrences = new Modernizer( "1.6", violations, NO_EXCLUSIONS, exclusionPatterns, - NO_IGNORED_PACKAGES, NO_EXCLUSION_PATTERNS).check(cr); + NO_IGNORED_PACKAGES, NO_IGNORED_CLASS_NAMES, + NO_EXCLUSION_PATTERNS + ).check(cr); assertThat(occurrences).hasSize(0); } @@ -247,7 +257,8 @@ public void testMethodLegacyApiCurrentJavaWithIgnorePackages() StringGetBytesString.class.getPackage().getName()); Collection occurrences = new Modernizer( "1.6", violations, NO_EXCLUSIONS, NO_EXCLUSION_PATTERNS, - ignorePackages, NO_EXCLUSION_PATTERNS).check(cr); + ignorePackages, NO_IGNORED_CLASS_NAMES, NO_EXCLUSION_PATTERNS + ).check(cr); assertThat(occurrences).hasSize(0); } @@ -258,7 +269,8 @@ public void testMethodLegacyApiCurrentJavaWithIgnorePackagesPrefix() Collection ignorePackages = Collections.singleton("org.gaul"); Collection occurrences = new Modernizer( "1.6", violations, NO_EXCLUSIONS, NO_EXCLUSION_PATTERNS, - ignorePackages, NO_EXCLUSION_PATTERNS).check(cr); + ignorePackages, NO_IGNORED_CLASS_NAMES, NO_EXCLUSION_PATTERNS + ).check(cr); assertThat(occurrences).hasSize(0); } @@ -269,7 +281,8 @@ public void testMethodLegacyApiCurrentJavaWithIgnorePackagesSubprefix() Collection ignorePackages = Collections.singleton("org"); Collection occurrences = new Modernizer( "1.6", violations, NO_EXCLUSIONS, NO_EXCLUSION_PATTERNS, - ignorePackages, NO_EXCLUSION_PATTERNS).check(cr); + ignorePackages, NO_IGNORED_CLASS_NAMES, NO_EXCLUSION_PATTERNS + ).check(cr); assertThat(occurrences).hasSize(0); } @@ -280,7 +293,8 @@ public void testMethodLegacyApiCurrentJavaWithIgnorePackagesPartialPrefix() Collection ignorePackages = Collections.singleton("org.gau"); Collection occurrences = new Modernizer( "1.6", violations, NO_EXCLUSIONS, NO_EXCLUSION_PATTERNS, - ignorePackages, NO_EXCLUSION_PATTERNS).check(cr); + ignorePackages, NO_IGNORED_CLASS_NAMES, NO_EXCLUSION_PATTERNS + ).check(cr); assertThat(occurrences).hasSize(1); } @@ -292,7 +306,9 @@ public void testMethodLegacyApiCurrentJavaWithIgnoreClassNamePatterns() Pattern.compile(".*StringGetBytesString")); Collection occurrences = new Modernizer( "1.6", violations, NO_EXCLUSIONS, NO_EXCLUSION_PATTERNS, - NO_IGNORED_PACKAGES, ignoreClassNamePatterns).check(cr); + NO_IGNORED_PACKAGES, NO_IGNORED_CLASS_NAMES, + ignoreClassNamePatterns + ).check(cr); assertThat(occurrences).hasSize(0); } @@ -324,7 +340,7 @@ public void testAnnotationViolation() throws Exception { new Violation(name, 5, "")); Modernizer modernizer = new Modernizer("1.5", testViolations, NO_EXCLUSIONS, NO_EXCLUSION_PATTERNS, NO_IGNORED_PACKAGES, - NO_EXCLUSION_PATTERNS); + NO_IGNORED_CLASS_NAMES, NO_EXCLUSION_PATTERNS); ClassReader cr = new ClassReader(AnnotatedMethod.class.getName()); Collection occurences = modernizer.check(cr); @@ -392,11 +408,45 @@ public void testAllViolations() throws Exception { assertThat(actualViolations).containsAll(violations.values()); } + @Test + public void testSuppressModernizer() throws Exception { + Set ignoreClassNames = ImmutableSet.of( + SuppressedOnClass.class.getName().replace('.', '/'), + SuppressedOnClass.InnerClass.class.getName().replace('.', '/'), + SuppressedOnMembers.InnerClass.class.getName().replace('.', '/') + ); + Modernizer modernizer = new Modernizer( + "1.10", + violations, + NO_EXCLUSIONS, + NO_EXCLUSION_PATTERNS, + NO_IGNORED_PACKAGES, + ignoreClassNames, + NO_EXCLUSION_PATTERNS + ); + + Set> classes = ImmutableSet.of( + SuppressedOnClass.class, + SuppressedOnClass.InnerClass.class, + SuppressedOnMembers.class, + SuppressedOnMembers.InnerClass.class + ); + + Collection occurrences = + new ArrayList(); + for (Class clazz : classes) { + occurrences.addAll( + modernizer.check(new ClassReader(clazz.getName()))); + } + + assertThat(occurrences).isEmpty(); + } + /** 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_CLASS_NAMES, NO_EXCLUSION_PATTERNS); } private static class CharsetsTestClass { diff --git a/modernizer-maven-plugin/src/test/java/org/gaul/modernizer_maven_plugin/SuppressModernizerAnnotationDetectorTest.java b/modernizer-maven-plugin/src/test/java/org/gaul/modernizer_maven_plugin/SuppressModernizerAnnotationDetectorTest.java new file mode 100644 index 0000000..2d181b0 --- /dev/null +++ b/modernizer-maven-plugin/src/test/java/org/gaul/modernizer_maven_plugin/SuppressModernizerAnnotationDetectorTest.java @@ -0,0 +1,48 @@ +/* + * Copyright 2014-2018 Andrew Gaul + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.gaul.modernizer_maven_plugin; + +import static org.assertj.core.api.Assertions.assertThat; + +import java.io.IOException; +import java.util.Set; +import java.util.TreeSet; + +import org.gaul.modernizer_maven_plugin + .SuppressModernizerTestClasses.SuppressedOnClass; +import org.gaul.modernizer_maven_plugin + .SuppressModernizerTestClasses.SuppressedOnMembers; +import org.junit.Test; + +public final class SuppressModernizerAnnotationDetectorTest { + + @Test + public void itDetectsSuppressModernizerAnnotation() throws IOException { + Set actual = SuppressModernizerAnnotationDetector.detect( + SuppressedOnClass.class, + SuppressedOnClass.InnerClass.class, + SuppressedOnMembers.class, + SuppressedOnMembers.InnerClass.class + ); + + assertThat(new TreeSet(actual)).containsExactly( + SuppressedOnClass.class.getName().replace('.', '/'), + SuppressedOnClass.InnerClass.class.getName().replace('.', '/'), + SuppressedOnMembers.InnerClass.class.getName().replace('.', '/') + ); + } +} diff --git a/modernizer-maven-plugin/src/test/java/org/gaul/modernizer_maven_plugin/SuppressModernizerTestClasses.java b/modernizer-maven-plugin/src/test/java/org/gaul/modernizer_maven_plugin/SuppressModernizerTestClasses.java new file mode 100644 index 0000000..2b04034 --- /dev/null +++ b/modernizer-maven-plugin/src/test/java/org/gaul/modernizer_maven_plugin/SuppressModernizerTestClasses.java @@ -0,0 +1,53 @@ +/* + * Copyright 2014-2018 Andrew Gaul + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.gaul.modernizer_maven_plugin; + +import java.nio.charset.Charset; + +import com.google.common.base.Charsets; + +import org.gaul.modernizer_maven_annotations.SuppressModernizer; + +public class SuppressModernizerTestClasses { + + @SuppressModernizer + public static final class SuppressedOnClass { + public Charset getCharset() { + return Charsets.UTF_8; + } + + public static final class InnerClass { + public Charset getCharset() { + return Charsets.UTF_8; + } + } + } + + public static final class SuppressedOnMembers { + @SuppressModernizer + public Charset getCharset() { + return Charsets.UTF_8; + } + + @SuppressModernizer + public static final class InnerClass { + public Charset getCharset() { + return Charsets.UTF_8; + } + } + } +} diff --git a/src/test/java/org/gaul/modernizer_maven_plugin/UtilsTest.java b/modernizer-maven-plugin/src/test/java/org/gaul/modernizer_maven_plugin/UtilsTest.java similarity index 100% rename from src/test/java/org/gaul/modernizer_maven_plugin/UtilsTest.java rename to modernizer-maven-plugin/src/test/java/org/gaul/modernizer_maven_plugin/UtilsTest.java diff --git a/modernizer-maven-policy/pom.xml b/modernizer-maven-policy/pom.xml new file mode 100644 index 0000000..b8e2e9c --- /dev/null +++ b/modernizer-maven-policy/pom.xml @@ -0,0 +1,23 @@ + + 4.0.0 + + + org.gaul + modernizer-maven-parent + 1.8.0-SNAPSHOT + + + modernizer-maven-policy + + + + + + org.apache.maven.plugins + maven-deploy-plugin + 2.8.2 + + + + + diff --git a/src/main/resources/checkstyle.xml b/modernizer-maven-policy/src/main/resources/checkstyle/checkstyle.xml similarity index 98% rename from src/main/resources/checkstyle.xml rename to modernizer-maven-policy/src/main/resources/checkstyle/checkstyle.xml index 7390678..f8f5bce 100644 --- a/src/main/resources/checkstyle.xml +++ b/modernizer-maven-policy/src/main/resources/checkstyle/checkstyle.xml @@ -12,7 +12,7 @@ - + diff --git a/modernizer-maven-policy/src/main/resources/checkstyle/copyright_header.txt b/modernizer-maven-policy/src/main/resources/checkstyle/copyright_header.txt new file mode 100644 index 0000000..8b413cd --- /dev/null +++ b/modernizer-maven-policy/src/main/resources/checkstyle/copyright_header.txt @@ -0,0 +1,16 @@ +/* + * Copyright 2014-2018 Andrew Gaul + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + diff --git a/src/main/resources/suppressions.xml b/modernizer-maven-policy/src/main/resources/checkstyle/suppressions.xml similarity index 100% rename from src/main/resources/suppressions.xml rename to modernizer-maven-policy/src/main/resources/checkstyle/suppressions.xml diff --git a/pom.xml b/pom.xml index 79abde2..95de9fe 100644 --- a/pom.xml +++ b/pom.xml @@ -8,13 +8,9 @@ org.gaul - modernizer-maven-plugin + modernizer-maven-parent 1.8.0-SNAPSHOT - maven-plugin - - Modernizer Maven Plugin - https://github.com/andrewgaul/modernizer-maven-plugin - Detect use of legacy APIs which modern Java versions supersede. + pom @@ -38,6 +34,12 @@ + + modernizer-maven-annotations + modernizer-maven-plugin + modernizer-maven-policy + + ossrh @@ -85,8 +87,6 @@ - src/main/resources/checkstyle.xml - src/main/resources/copyright_header.txt