diff options
author | Ilpo Ruotsalainen <ilpo.ruotsalainen@oath.com> | 2018-11-27 13:29:19 +0100 |
---|---|---|
committer | Ilpo Ruotsalainen <ilpo.ruotsalainen@oath.com> | 2018-11-28 15:54:45 +0100 |
commit | 66815538b85292810f64831eb5bc0864111ce4b5 (patch) | |
tree | 7f752d83ccd26c65811cd1699770883b76a6a49c /abi-check-plugin | |
parent | be26331d4c261bae81adc73b8552237923bc0e75 (diff) |
Simplify data model.
Bake attributes and return type of method into its name string.
Diffstat (limited to 'abi-check-plugin')
4 files changed, 44 insertions, 75 deletions
diff --git a/abi-check-plugin/src/main/java/com/yahoo/abicheck/collector/PublicSignatureCollector.java b/abi-check-plugin/src/main/java/com/yahoo/abicheck/collector/PublicSignatureCollector.java index 4fee85c823a..b7520c58d96 100644 --- a/abi-check-plugin/src/main/java/com/yahoo/abicheck/collector/PublicSignatureCollector.java +++ b/abi-check-plugin/src/main/java/com/yahoo/abicheck/collector/PublicSignatureCollector.java @@ -1,11 +1,12 @@ package com.yahoo.abicheck.collector; import com.yahoo.abicheck.signature.JavaClassSignature; -import com.yahoo.abicheck.signature.JavaMethodSignature; import java.util.Arrays; import java.util.LinkedHashMap; +import java.util.LinkedHashSet; import java.util.List; import java.util.Map; +import java.util.Set; import java.util.stream.Collectors; import org.objectweb.asm.ClassVisitor; import org.objectweb.asm.MethodVisitor; @@ -18,26 +19,29 @@ public class PublicSignatureCollector extends ClassVisitor { private String currentName; private int currentAccess; - private Map<String, JavaMethodSignature> currentMethods; + private Set<String> currentMethods; public PublicSignatureCollector() { super(Opcodes.ASM6); } - private static String methodNameWithArguments(String name, List<String> argumentTypes) { - return String.format("%s(%s)", name, String.join(", ", argumentTypes)); - } - private static boolean testBit(long access, long mask) { return (access & mask) != 0; } + private static String describeMethod(String name, int access, String returnType, + List<String> argumentTypes) { + String attributes = String.join(" ", Util.convertAccess(access, Util.methodFlags)); + return String + .format("%s %s %s(%s)", attributes, returnType, name, String.join(", ", argumentTypes)); + } + @Override public void visit(int version, int access, String name, String signature, String superName, String[] interfaces) { currentName = Type.getObjectType(name).getClassName(); currentAccess = access; - currentMethods = new LinkedHashMap<>(); + currentMethods = new LinkedHashSet<>(); } @Override @@ -47,10 +51,8 @@ public class PublicSignatureCollector extends ClassVisitor { Type method = Type.getMethodType(descriptor); List<String> argumentTypes = Arrays.stream(method.getArgumentTypes()).map(Type::getClassName) .collect(Collectors.toList()); - name = methodNameWithArguments(name, argumentTypes); - currentMethods.put(name, - new JavaMethodSignature(Util.convertAccess(access, Util.methodFlags), - method.getReturnType().getClassName())); + currentMethods + .add(describeMethod(name, access, method.getReturnType().getClassName(), argumentTypes)); } return null; } 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 e1bb97e264b..007d0ff6a51 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 @@ -10,7 +10,6 @@ import com.yahoo.abicheck.classtree.ClassFileTree.Package; import com.yahoo.abicheck.collector.AnnotationCollector; import com.yahoo.abicheck.collector.PublicSignatureCollector; import com.yahoo.abicheck.signature.JavaClassSignature; -import com.yahoo.abicheck.signature.JavaMethodSignature; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; @@ -40,10 +39,9 @@ import org.objectweb.asm.ClassReader; ) public class AbiCheck extends AbstractMojo { + public static final String PACKAGE_INFO_CLASS_FILE_NAME = "package-info.class"; private static final String DEFAULT_SPEC_FILE = "abi-spec.json"; private static final String WRITE_SPEC_PROPERTY = "abicheck.writeSpec"; - public static final String PACKAGE_INFO_CLASS_FILE_NAME = "package-info.class"; - @Parameter(defaultValue = "${project}", readonly = true) private MavenProject project = null; @@ -53,6 +51,32 @@ 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(); @@ -94,10 +118,6 @@ public class AbiCheck extends AbstractMojo { } } - private static String capitalizeFirst(String s) { - return s.substring(0, 1).toUpperCase() + s.substring(1); - } - private void writeSpec(Map<String, JavaClassSignature> publicAbiSignatures) throws IOException { Gson gson = new GsonBuilder().setPrettyPrinting().create(); try (FileWriter writer = new FileWriter(specFileName)) { @@ -105,28 +125,6 @@ public class AbiCheck extends AbstractMojo { } } - 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; - } - private boolean matchingClasses(String className, JavaClassSignature expected, JavaClassSignature actual) { boolean match = true; @@ -135,24 +133,9 @@ public class AbiCheck extends AbstractMojo { .format("Class %s: %s attribute %s", className, capitalizeFirst(error), item)))) { match = false; } - if (!matchingItemSets(expected.methods.keySet(), actual.methods.keySet(), - item -> matchingMethods(className + "." + item, expected.methods.get(item), - actual.methods.get(item)), - (item, error) -> getLog().error( - String.format("Class %s: %s method %s", className, capitalizeFirst(error), item)))) { - match = false; - } - return match; - } - - private boolean matchingMethods(String methodName, JavaMethodSignature expected, - JavaMethodSignature actual) { - boolean match = true; - if (!expected.returnType.equals(actual.returnType)) { + if (!matchingItemSets(expected.methods, actual.methods, item -> true, (item, error) -> getLog() + .error(String.format("Class %s: %s method %s", className, capitalizeFirst(error), item)))) { match = false; - getLog().error(String - .format("Method %s: Expected return type %s, found %s", methodName, expected.returnType, - actual.returnType)); } return match; } diff --git a/abi-check-plugin/src/main/java/com/yahoo/abicheck/signature/JavaClassSignature.java b/abi-check-plugin/src/main/java/com/yahoo/abicheck/signature/JavaClassSignature.java index 774792c0365..c9c2242e245 100644 --- a/abi-check-plugin/src/main/java/com/yahoo/abicheck/signature/JavaClassSignature.java +++ b/abi-check-plugin/src/main/java/com/yahoo/abicheck/signature/JavaClassSignature.java @@ -1,16 +1,14 @@ package com.yahoo.abicheck.signature; -import java.util.LinkedHashSet; import java.util.List; -import java.util.Map; import java.util.Set; public class JavaClassSignature { public final List<String> attributes; - public final Map<String, JavaMethodSignature> methods; + public final Set<String> methods; - public JavaClassSignature(List<String> attributes, Map<String, JavaMethodSignature> methods) { + public JavaClassSignature(List<String> attributes, Set<String> methods) { this.attributes = attributes; this.methods = methods; } diff --git a/abi-check-plugin/src/main/java/com/yahoo/abicheck/signature/JavaMethodSignature.java b/abi-check-plugin/src/main/java/com/yahoo/abicheck/signature/JavaMethodSignature.java deleted file mode 100644 index 0d562d0acb1..00000000000 --- a/abi-check-plugin/src/main/java/com/yahoo/abicheck/signature/JavaMethodSignature.java +++ /dev/null @@ -1,14 +0,0 @@ -package com.yahoo.abicheck.signature; - -import java.util.List; - -public class JavaMethodSignature { - - public final List<String> attributes; - public final String returnType; - - public <T> JavaMethodSignature(List<String> attributes, String returnType) { - this.attributes = attributes; - this.returnType = returnType; - } -} |