diff options
4 files changed, 168 insertions, 7 deletions
diff --git a/api/src/main/java/org/osjava/jardiff/PublicDiffCriteria.java b/api/src/main/java/org/osjava/jardiff/PublicDiffCriteria.java new file mode 100644 index 0000000..102d9f6 --- /dev/null +++ b/api/src/main/java/org/osjava/jardiff/PublicDiffCriteria.java @@ -0,0 +1,139 @@ +/** + * Copyright 2012-2014 Julien Eluard and contributors + * This project includes software developed by Julien Eluard: https://github.com/jeluard/ + * + * 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.osjava.jardiff; + +import java.util.Arrays; +import java.util.HashSet; +import java.util.Set; + +/** + * A specific type of DiffCriteria which is only true for classes, methods + * and fields which are not synthetic and public. + * + * @author <a href="mailto:[email protected]">Antony Riley</a> + */ +public class PublicDiffCriteria implements DiffCriteria +{ + /** + * Check if a class is valid. + * If the class is not synthetic and public, return true. + * + * @param info Info describing the class. + * @return True if the class meets the criteria, false otherwise. + */ + public boolean validClass(ClassInfo info) { + return !info.isSynthetic() && info.isPublic(); + } + + /** + * Check if a method is valid. + * If the method is not synthetic and public, return true. + * + * @param info Info describing the method. + * @return True if the method meets the criteria, false otherwise. + */ + public boolean validMethod(MethodInfo info) { + return !info.isSynthetic() && info.isPublic(); + } + + /** + * Check if a field is valid. + * If the method is not synthetic and public, return true. + * + * @param info Info describing the field. + * @return True if the field meets the criteria, false otherwise. + */ + public boolean validField(FieldInfo info) { + return !info.isSynthetic() && info.isPublic(); + } + + /** + * Check if there is a change between two versions of a class. + * Returns true if the access flags differ, or if the superclass differs + * or if the implemented interfaces differ. + * + * @param oldInfo Info about the old version of the class. + * @param newInfo Info about the new version of the class. + * @return True if the classes differ, false otherwise. + */ + public boolean differs(ClassInfo oldInfo, ClassInfo newInfo) { + if (oldInfo.getAccess() != newInfo.getAccess()) + return true; + // Yes classes can have a null supername, e.g. java.lang.Object ! + if(oldInfo.getSupername() == null) { + if(newInfo.getSupername() != null) { + return true; + } + } else if (!oldInfo.getSupername().equals(newInfo.getSupername())) { + return true; + } + Set<String> oldInterfaces + = new HashSet(Arrays.asList(oldInfo.getInterfaces())); + Set<String> newInterfaces + = new HashSet(Arrays.asList(newInfo.getInterfaces())); + if (!oldInterfaces.equals(newInterfaces)) + return true; + return false; + } + + /** + * Check if there is a change between two versions of a method. + * Returns true if the access flags differ, or if the thrown + * exceptions differ. + * + * @param oldInfo Info about the old version of the method. + * @param newInfo Info about the new version of the method. + * @return True if the methods differ, false otherwise. + */ + public boolean differs(MethodInfo oldInfo, MethodInfo newInfo) { + if (oldInfo.getAccess() != newInfo.getAccess()) + return true; + if (oldInfo.getExceptions() == null + || newInfo.getExceptions() == null) { + if (oldInfo.getExceptions() != newInfo.getExceptions()) + return true; + } else { + Set<String> oldExceptions + = new HashSet(Arrays.asList(oldInfo.getExceptions())); + Set<String> newExceptions + = new HashSet(Arrays.asList(newInfo.getExceptions())); + if (!oldExceptions.equals(newExceptions)) + return true; + } + return false; + } + + /** + * Check if there is a change between two versions of a field. + * Returns true if the access flags differ, or if the inital value + * of the field differs. + * + * @param oldInfo Info about the old version of the field. + * @param newInfo Info about the new version of the field. + * @return True if the fields differ, false otherwise. + */ + public boolean differs(FieldInfo oldInfo, FieldInfo newInfo) { + if (oldInfo.getAccess() != newInfo.getAccess()) + return true; + if (oldInfo.getValue() == null || newInfo.getValue() == null) { + if (oldInfo.getValue() != newInfo.getValue()) + return true; + } else if (!oldInfo.getValue().equals(newInfo.getValue())) + return true; + return false; + } +} diff --git a/api/src/main/java/org/semver/Comparer.java b/api/src/main/java/org/semver/Comparer.java index a9ff2fc..d682994 100755 --- a/api/src/main/java/org/semver/Comparer.java +++ b/api/src/main/java/org/semver/Comparer.java @@ -16,11 +16,12 @@ */ package org.semver; +import javax.annotation.concurrent.NotThreadSafe; import java.io.File; import java.io.IOException; import java.util.Set; -import javax.annotation.concurrent.NotThreadSafe; +import org.osjava.jardiff.DiffCriteria; import org.osjava.jardiff.DiffException; import org.osjava.jardiff.JarDiff; import org.osjava.jardiff.SimpleDiffCriteria; @@ -34,19 +35,22 @@ import org.semver.jardiff.DifferenceAccumulatingHandler; @NotThreadSafe public class Comparer { + private final DiffCriteria diffCriteria; private final File previousJAR; private final File currentJAR; private final Set<String> includes; private final Set<String> excludes; - public Comparer(final File previousJAR, final File currentJAR, final Set<String> includes, final Set<String> excludes) { + public Comparer(final DiffCriteria diffCriteria, final File previousJAR, final File currentJAR, + final Set<String> includes, final Set<String> excludes) { if (!previousJAR.isFile()) { throw new IllegalArgumentException("<"+previousJAR+"> is not a valid file"); } if (!currentJAR.isFile()) { throw new IllegalArgumentException("<"+currentJAR+"> is not a valid file"); } - + + this.diffCriteria = diffCriteria; this.previousJAR = previousJAR; this.currentJAR = currentJAR; this.includes = includes; @@ -63,7 +67,7 @@ public class Comparer { jarDiff.loadOldClasses(this.previousJAR); jarDiff.loadNewClasses(this.currentJAR); final DifferenceAccumulatingHandler handler = new DifferenceAccumulatingHandler(this.includes, this.excludes); - jarDiff.diff(handler, new SimpleDiffCriteria()); + jarDiff.diff(handler, diffCriteria); return handler.getDelta(); } catch (DiffException e) { throw new RuntimeException(e); diff --git a/api/src/main/java/org/semver/Main.java b/api/src/main/java/org/semver/Main.java index 97a5316..54e621b 100755 --- a/api/src/main/java/org/semver/Main.java +++ b/api/src/main/java/org/semver/Main.java @@ -25,6 +25,9 @@ import java.util.Set; import de.tototec.cmdoption.CmdOption; import de.tototec.cmdoption.CmdlineParser; import de.tototec.cmdoption.CmdlineParserException; +import org.osjava.jardiff.DiffCriteria; +import org.osjava.jardiff.PublicDiffCriteria; +import org.osjava.jardiff.SimpleDiffCriteria; /** * @@ -43,6 +46,9 @@ public class Main { @CmdOption(names = { "--check", "-c" }, conflictsWith = { "--diff", "--infer", "--validate" }, description = "Check the compatibility of two jars.") public boolean check; + @CmdOption(names = {"--publicOnly", "-p"}, description = "Checks public members only") + public boolean publicOnly; + @CmdOption(names = { "--infer", "-i" }, requires = { "--base-version" }, conflictsWith = { "--diff", "--check", "--validate" }, description = "Infer the version of the new jar based on the previous jar.") public boolean infer; @@ -101,8 +107,9 @@ public class Main { System.exit(0); } - final Comparer comparer = new Comparer(new File(config.baseJar), new File(config.newJar), config.includes, - config.excludes); + final DiffCriteria diffCriteria = config.publicOnly ? new PublicDiffCriteria() : new SimpleDiffCriteria(); + final Comparer comparer = new Comparer(diffCriteria, new File(config.baseJar), new File(config.newJar), + config.includes, config.excludes); final Delta delta = comparer.diff(); if (config.diff) { diff --git a/enforcer-rule/src/main/java/org/semver/enforcer/AbstractEnforcerRule.java b/enforcer-rule/src/main/java/org/semver/enforcer/AbstractEnforcerRule.java index 8525be6..38c2517 100755 --- a/enforcer-rule/src/main/java/org/semver/enforcer/AbstractEnforcerRule.java +++ b/enforcer-rule/src/main/java/org/semver/enforcer/AbstractEnforcerRule.java @@ -39,6 +39,9 @@ import org.apache.maven.enforcer.rule.api.EnforcerRuleException; import org.apache.maven.enforcer.rule.api.EnforcerRuleHelper; import org.apache.maven.project.MavenProject; import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluationException; +import org.osjava.jardiff.DiffCriteria; +import org.osjava.jardiff.PublicDiffCriteria; +import org.osjava.jardiff.SimpleDiffCriteria; import org.semver.Comparer; import org.semver.Delta; import org.semver.Dumper; @@ -79,6 +82,13 @@ public abstract class AbstractEnforcerRule implements EnforcerRule { * @parameter */ private boolean dumpDetails = false; + + /** + * Check public members only + * + * @parameter + */ + private boolean publicOnly = false; private Set<String> extractFilters(final String[] filtersAsStringArray) { if (filtersAsStringArray == null) { @@ -149,7 +159,8 @@ public abstract class AbstractEnforcerRule implements EnforcerRule { helper.getLog().info("Using <"+currentJar+"> as current JAR"); try { - final Comparer comparer = new Comparer(previousJar, currentJar, extractFilters(this.includes), extractFilters(this.excludes)); + final DiffCriteria diffCriteria = publicOnly ? new PublicDiffCriteria() : new SimpleDiffCriteria(); + final Comparer comparer = new Comparer(diffCriteria, previousJar, currentJar, extractFilters(this.includes), extractFilters(this.excludes)); final Delta delta = comparer.diff(); enforce(helper, delta, previous, current); |