diff options
author | Ilpo Ruotsalainen <ilpo.ruotsalainen@oath.com> | 2018-11-30 12:26:42 +0100 |
---|---|---|
committer | Ilpo Ruotsalainen <ilpo.ruotsalainen@oath.com> | 2018-11-30 12:26:42 +0100 |
commit | 233f44b0585d02d1f4fd100eead9d274f4bdb9ef (patch) | |
tree | b3415becc024bed6bceb33282bf5c35db29d7012 /abi-check-plugin | |
parent | 4ab17a2abf972fe0892469f3e0205acc21e24975 (diff) |
Generalize set comparison method.
Diffstat (limited to 'abi-check-plugin')
-rw-r--r-- | abi-check-plugin/src/main/java/com/yahoo/abicheck/mojo/AbiCheck.java | 73 | ||||
-rw-r--r-- | abi-check-plugin/src/main/java/com/yahoo/abicheck/setmatcher/SetMatcher.java | 31 |
2 files changed, 58 insertions, 46 deletions
diff --git a/abi-check-plugin/src/main/java/com/yahoo/abicheck/mojo/AbiCheck.java b/abi-check-plugin/src/main/java/com/yahoo/abicheck/mojo/AbiCheck.java index 516c1bb015f..87adbd414cf 100644 --- a/abi-check-plugin/src/main/java/com/yahoo/abicheck/mojo/AbiCheck.java +++ b/abi-check-plugin/src/main/java/com/yahoo/abicheck/mojo/AbiCheck.java @@ -1,6 +1,5 @@ package com.yahoo.abicheck.mojo; -import com.google.common.collect.Sets; import com.google.gson.Gson; import com.google.gson.GsonBuilder; import com.google.gson.reflect.TypeToken; @@ -9,6 +8,7 @@ import com.yahoo.abicheck.classtree.ClassFileTree.ClassFile; import com.yahoo.abicheck.classtree.ClassFileTree.Package; import com.yahoo.abicheck.collector.AnnotationCollector; import com.yahoo.abicheck.collector.PublicSignatureCollector; +import com.yahoo.abicheck.setmatcher.SetMatcher; import com.yahoo.abicheck.signature.JavaClassSignature; import java.io.FileReader; import java.io.FileWriter; @@ -18,14 +18,12 @@ import java.util.HashSet; import java.util.LinkedHashMap; import java.util.Map; import java.util.Optional; -import java.util.Set; -import java.util.function.BiConsumer; -import java.util.function.Predicate; import java.util.jar.JarFile; import org.apache.maven.artifact.Artifact; import org.apache.maven.plugin.AbstractMojo; import org.apache.maven.plugin.MojoExecutionException; import org.apache.maven.plugin.MojoFailureException; +import org.apache.maven.plugin.logging.Log; import org.apache.maven.plugins.annotations.LifecyclePhase; import org.apache.maven.plugins.annotations.Mojo; import org.apache.maven.plugins.annotations.Parameter; @@ -52,32 +50,6 @@ public class AbiCheck extends AbstractMojo { @Parameter private String specFileName = DEFAULT_SPEC_FILE; - private static String capitalizeFirst(String s) { - return s.substring(0, 1).toUpperCase() + s.substring(1); - } - - private static <T> boolean matchingItemSets(Set<T> expected, Set<T> actual, - Predicate<T> itemsMatch, BiConsumer<T, String> onError) { - boolean mismatch = false; - Set<T> missing = Sets.difference(expected, actual); - for (T name : missing) { - mismatch = true; - onError.accept(name, "missing"); - } - Set<T> extra = Sets.difference(actual, expected); - for (T name : extra) { - mismatch = true; - onError.accept(name, "extra"); - } - Set<T> both = Sets.intersection(actual, expected); - for (T name : both) { - if (!itemsMatch.test(name)) { - mismatch = true; - } - } - return !mismatch; - } - @Override public void execute() throws MojoExecutionException, MojoFailureException { Artifact mainArtifact = project.getArtifact(); @@ -100,10 +72,10 @@ public class AbiCheck extends AbstractMojo { writeSpec(signatures, specFileName); } else { Map<String, JavaClassSignature> abiSpec = readSpec(specFileName); - if (!matchingItemSets(abiSpec.keySet(), signatures.keySet(), + if (!SetMatcher.compare(abiSpec.keySet(), signatures.keySet(), item -> matchingClasses(item, abiSpec.get(item), signatures.get(item)), - (item, error) -> getLog() - .error(String.format("%s class: %s", capitalizeFirst(error), item)))) { + item -> getLog().error(String.format("Missing class: %s", item)), + item -> getLog().error(String.format("Extra class: %s", item)))) { throw new MojoFailureException("ABI spec mismatch"); } } @@ -132,28 +104,37 @@ public class AbiCheck extends AbstractMojo { private boolean matchingClasses(String className, JavaClassSignature expected, JavaClassSignature actual) { + Log log = getLog(); boolean match = true; if (!expected.superClass.equals(actual.superClass)) { match = false; - getLog().error(String + log.error(String .format("Class %s: Expected superclass %s, found %s", className, expected.superClass, actual.superClass)); } - if (!matchingItemSets(expected.interfaces, actual.interfaces, item -> true, - (item, error) -> getLog().error( - String.format("Class %s: %s interface %s", className, capitalizeFirst(error), item)))) { - if (!matchingItemSets(new HashSet<>(expected.attributes), new HashSet<>(actual.attributes), - item -> true, (item, error) -> getLog().error(String - .format("Class %s: %s attribute %s", className, capitalizeFirst(error), item)))) { - match = false; - } + if (!SetMatcher.compare(expected.interfaces, actual.interfaces, + item -> true, + item -> log.error(String.format("Class %s: Missing interface %s", className, item)), + item -> log.error(String.format("Class %s: Extra interface %s", className, item)))) { + match = false; + } + if (!SetMatcher + .compare(new HashSet<>(expected.attributes), new HashSet<>(actual.attributes), + item -> true, + item -> log.error(String.format("Class %s: Missing attribute %s", className, item)), + item -> log.error(String.format("Class %s: Extra attribute %s", className, item)))) { + match = false; } - if (!matchingItemSets(expected.methods, actual.methods, item -> true, (item, error) -> getLog() - .error(String.format("Class %s: %s method %s", className, capitalizeFirst(error), item)))) { + if (!SetMatcher.compare(expected.methods, actual.methods, + item -> true, + item -> log.error(String.format("Class %s: Missing method %s", className, item)), + item -> log.error(String.format("Class %s: Extra method %s", className, item)))) { match = false; } - if (!matchingItemSets(expected.fields, actual.fields, item -> true, (item, error) -> getLog() - .error(String.format("Class %s: %s field %s", className, capitalizeFirst(error), item)))) { + if (!SetMatcher.compare(expected.fields, actual.fields, + item -> true, + item -> log.error(String.format("Class %s: Missing field %s", className, item)), + item -> log.error(String.format("Class %s: Extra field %s", className, item)))) { match = false; } return match; diff --git a/abi-check-plugin/src/main/java/com/yahoo/abicheck/setmatcher/SetMatcher.java b/abi-check-plugin/src/main/java/com/yahoo/abicheck/setmatcher/SetMatcher.java new file mode 100644 index 00000000000..56b08bef74f --- /dev/null +++ b/abi-check-plugin/src/main/java/com/yahoo/abicheck/setmatcher/SetMatcher.java @@ -0,0 +1,31 @@ +package com.yahoo.abicheck.setmatcher; + +import com.google.common.collect.Sets; +import java.util.Set; +import java.util.function.Consumer; +import java.util.function.Predicate; + +public class SetMatcher { + + public static <T> boolean compare(Set<T> expected, Set<T> actual, Predicate<T> itemsMatch, + Consumer<T> onMissing, Consumer<T> onExtra) { + boolean mismatch = false; + Set<T> missing = Sets.difference(expected, actual); + for (T item : missing) { + mismatch = true; + onMissing.accept(item); + } + Set<T> extra = Sets.difference(actual, expected); + for (T item : extra) { + mismatch = true; + onExtra.accept(item); + } + Set<T> both = Sets.intersection(actual, expected); + for (T item : both) { + if (!itemsMatch.test(item)) { + mismatch = true; + } + } + return !mismatch; + } +} |