Files
spring-ai/ai-21-migration/tools/ApiDiff.java
T

115 lines
5.5 KiB
Java

import java.io.IOException;
import java.lang.classfile.*;
import java.lang.classfile.attribute.RuntimeVisibleAnnotationsAttribute;
import java.lang.reflect.AccessFlag;
import java.nio.file.*;
import java.util.*;
import java.util.jar.JarFile;
import java.util.stream.Collectors;
/**
* Compares the public API of two sets of Spring AI jars with the JDK class-file API.
* Usage: java tools/ApiDiff.java <oldJarDir> <newJarDir>
* Prints removed public types and members, and what is newly annotated {@code @Deprecated}.
*/
public class ApiDiff {
record Item(String jar, Set<String> flags, String deprecated) { }
public static void main(String[] args) throws IOException {
Map<String, Item> before = scan(Path.of(args[0]));
Map<String, Item> after = scan(Path.of(args[1]));
var removedTypes = before.keySet().stream().filter(k -> !k.contains("#") && !after.containsKey(k)).sorted().toList();
var removedMembers = before.keySet().stream()
.filter(k -> k.contains("#") && !after.containsKey(k) && after.containsKey(owner(k)))
.sorted().toList();
var newlyDeprecated = after.entrySet().stream()
.filter(e -> e.getValue().deprecated() != null)
.filter(e -> before.containsKey(e.getKey()) && before.get(e.getKey()).deprecated() == null)
.map(Map.Entry::getKey).sorted().toList();
System.out.println("REMOVED public types: " + removedTypes.size());
removedTypes.forEach(k -> System.out.println(" " + k + " [" + before.get(k).jar() + "]"));
System.out.println();
System.out.println("REMOVED public/protected members of types that still exist: " + removedMembers.size());
removedMembers.forEach(k -> System.out.println(" " + k));
System.out.println();
System.out.println("NEWLY @Deprecated (types and members that existed before): " + newlyDeprecated.size());
// A deprecated type implies its members; list types first, then members of other types.
var deprecatedTypes = newlyDeprecated.stream().filter(k -> !k.contains("#")).collect(Collectors.toSet());
for (String k : newlyDeprecated) {
if (k.contains("#") && deprecatedTypes.contains(owner(k))) {
continue;
}
System.out.println(" " + k + " " + after.get(k).deprecated());
}
}
static String owner(String key) {
return key.substring(0, key.indexOf('#'));
}
static Map<String, Item> scan(Path dir) throws IOException {
Map<String, Item> result = new TreeMap<>();
try (var jars = Files.list(dir)) {
for (Path jar : jars.filter(p -> p.toString().endsWith(".jar")).sorted().toList()) {
try (var jf = new JarFile(jar.toFile())) {
for (var entry : jf.stream().filter(e -> e.getName().startsWith("org/springframework/ai/")
&& e.getName().endsWith(".class")).toList()) {
ClassModel cm = ClassFile.of().parse(jf.getInputStream(entry).readAllBytes());
if (!visible(cm.flags().flags()) || cm.thisClass().asInternalName().matches(".*\\$\\d.*")) {
continue;
}
String type = cm.thisClass().asInternalName().replace('/', '.');
String name = jar.getFileName().toString().replace(".jar", "");
result.put(type, new Item(name, Set.of(), deprecation(cm)));
for (MethodModel m : cm.methods()) {
var f = m.flags().flags();
if (visible(f) && !f.contains(AccessFlag.SYNTHETIC) && !f.contains(AccessFlag.BRIDGE)) {
String sig = m.methodTypeSymbol().displayDescriptor();
result.put(type + "#" + m.methodName().stringValue() + sig, new Item(name, Set.of(), deprecation(m)));
}
}
for (FieldModel field : cm.fields()) {
var f = field.flags().flags();
if (visible(f) && !f.contains(AccessFlag.SYNTHETIC)) {
result.put(type + "#" + field.fieldName().stringValue(), new Item(name, Set.of(), deprecation(field)));
}
}
}
}
}
}
return result;
}
static boolean visible(Set<AccessFlag> flags) {
return flags.contains(AccessFlag.PUBLIC) || flags.contains(AccessFlag.PROTECTED);
}
static String deprecation(AttributedElement element) {
var attr = element.findAttribute(Attributes.runtimeVisibleAnnotations());
if (attr.isEmpty()) {
return null;
}
for (var a : attr.get().annotations()) {
if (a.classSymbol().descriptorString().equals("Ljava/lang/Deprecated;")) {
String text = a.elements().stream()
.map(e -> e.name().stringValue() + "=" + value(e.value()))
.collect(Collectors.joining(", "));
return "@Deprecated(" + text + ")";
}
}
return null;
}
static String value(AnnotationValue v) {
return switch (v) {
case AnnotationValue.OfString s -> s.stringValue();
case AnnotationValue.OfBoolean b -> String.valueOf(b.booleanValue());
default -> v.toString();
};
}
}