use of org.revapi.Report in project revapi by revapi.
the class JavaElementDifferenceAnalyzer method endAnalysis.
@Override
public Report endAnalysis(@Nullable Element oldElement, @Nullable Element newElement) {
if (oldElement == nonExistenceOldRoot && newElement == nonExistenceNewRoot) {
nonExistenceMode = false;
nonExistenceOldRoot = null;
nonExistenceNewRoot = null;
}
if (conforms(oldElement, newElement, AnnotationElement.class)) {
// the annotations are always reported at the parent element
return new Report(Collections.emptyList(), oldElement, newElement);
}
List<Difference> differences = new ArrayList<>();
CheckType lastInterest = checkTypeStack.pop();
if (lastInterest.isConcrete()) {
for (Check c : checksStack.pop()) {
List<Difference> p = c.visitEnd();
if (p != null) {
differences.addAll(p);
}
}
}
if (lastAnnotationResults != null && !lastAnnotationResults.isEmpty()) {
differences.addAll(lastAnnotationResults);
lastAnnotationResults.clear();
}
if (!differences.isEmpty()) {
LOG.trace("Detected following problems: {}", differences);
}
Timing.LOG.trace("Ended analysis of {} and {}.", oldElement, newElement);
ListIterator<Difference> it = differences.listIterator();
while (it.hasNext()) {
Difference d = it.next();
if (analysisConfiguration.reportUseForAllDifferences() || analysisConfiguration.getUseReportingCodes().contains(d.code)) {
StringBuilder oldUseChain = null;
StringBuilder newUseChain = null;
if (oldElement != null) {
oldUseChain = new StringBuilder();
appendUses(oldEnvironment, oldElement, oldUseChain);
}
if (newElement != null) {
newUseChain = new StringBuilder();
appendUses(newEnvironment, newElement, newUseChain);
}
Map<String, String> atts = new HashMap<>(d.attachments);
if (oldUseChain != null) {
atts.put("exampleUseChainInOldApi", oldUseChain.toString());
}
if (newUseChain != null) {
atts.put("exampleUseChainInNewApi", newUseChain.toString());
}
d = Difference.builder().addAttachments(atts).addClassifications(d.classification).withCode(d.code).withName(d.name).withDescription(d.description).build();
}
it.set(d);
}
return new Report(differences, oldElement, newElement);
}
use of org.revapi.Report in project revapi by revapi.
the class MissingClassReportingTest method testReportsMissingClasses.
@Test
public void testReportsMissingClasses() throws Exception {
AnalysisContext ctx = AnalysisContext.builder(revapi).withOldAPI(API.of(new ShrinkwrapArchive(apiV1)).build()).withNewAPI(API.of(new ShrinkwrapArchive(apiV2)).build()).withConfigurationFromJSON("{\"revapi\" : { \"java\" : { \"missing-classes\" : {\"behavior\" : \"report\" }}}}").build();
revapi.validateConfiguration(ctx);
List<Report> allReports = revapi.analyze(ctx).getExtensions().getFirstExtension(CollectingReporter.class, null).getReports();
Assert.assertEquals(3, allReports.size());
Assert.assertTrue(containsDifference(allReports, "missing-class B.T$2", "missing-class B.T$2", Code.MISSING_IN_NEW_API.code()));
Assert.assertTrue(containsDifference(allReports, "missing-class B.T$2", "missing-class B.T$2", Code.MISSING_IN_OLD_API.code()));
Assert.assertTrue(containsDifference(allReports, null, "missing-class B.T$3", Code.MISSING_IN_NEW_API.code()));
Assert.assertTrue(containsDifference(allReports, null, "field A.f3", Code.FIELD_ADDED.code()));
boolean containsMissingOld = false;
boolean containsMissingNew = false;
for (Difference d : allReports.get(0).getDifferences()) {
if (d.code.equals(Code.MISSING_IN_NEW_API.code())) {
containsMissingNew = true;
}
if (d.code.equals(Code.MISSING_IN_OLD_API.code())) {
containsMissingOld = true;
}
}
Assert.assertTrue(containsMissingOld);
Assert.assertTrue(containsMissingNew);
}
use of org.revapi.Report in project revapi by revapi.
the class MissingClassReportingTest method testIgnoresMissingClasses.
@Test
public void testIgnoresMissingClasses() throws Exception {
AnalysisResult res = revapi.analyze(AnalysisContext.builder(revapi).withOldAPI(API.of(new ShrinkwrapArchive(apiV1)).build()).withNewAPI(API.of(new ShrinkwrapArchive(apiV2)).build()).withConfigurationFromJSON("{\"revapi\" : { \"java\" : { \"missing-classes\" : {\"behavior\" : \"ignore\" }}}}").build());
List<Report> allReports = res.getExtensions().getFirstExtension(CollectingReporter.class, null).getReports();
Assert.assertEquals(1, allReports.size());
Assert.assertTrue(containsDifference(allReports, null, "field A.f3", Code.FIELD_ADDED.code()));
}
use of org.revapi.Report in project revapi by revapi.
the class SupplementaryJarsTest method testSupplementaryJarsAreTakenIntoAccountWhenComputingAPI.
@Test
public void testSupplementaryJarsAreTakenIntoAccountWhenComputingAPI() throws Exception {
List<Report> allReports;
Revapi revapi = createRevapi(CollectingReporter.class);
AnalysisContext ctx = AnalysisContext.builder(revapi).withOldAPI(API.of(new ShrinkwrapArchive(apiV1)).supportedBy(new ShrinkwrapArchive(supV1)).build()).withNewAPI(API.of(new ShrinkwrapArchive(apiV2)).supportedBy(new ShrinkwrapArchive(supV2)).build()).build();
try (AnalysisResult res = revapi.analyze(ctx)) {
Assert.assertTrue(res.isSuccess());
allReports = res.getExtensions().getFirstExtension(CollectingReporter.class, null).getReports();
}
// 11 removed methods when kind of class changes to interface
Assert.assertEquals(8 + 11, allReports.size());
Assert.assertTrue(containsDifference(allReports, null, "class B.T$1.Private", Code.CLASS_NON_PUBLIC_PART_OF_API.code()));
Assert.assertTrue(containsDifference(allReports, null, "field B.T$2.f2", Code.FIELD_ADDED.code()));
Assert.assertTrue(containsDifference(allReports, null, "field A.f3", Code.FIELD_ADDED.code()));
Assert.assertTrue(containsDifference(allReports, "class B.T$2", "class B.T$2", Code.CLASS_NOW_FINAL.code()));
Assert.assertTrue(containsDifference(allReports, null, "class B.T$3", Code.CLASS_ADDED.code()));
Assert.assertTrue(containsDifference(allReports, null, "class B.PrivateUsedClass", Code.CLASS_NON_PUBLIC_PART_OF_API.code()));
Assert.assertTrue(containsDifference(allReports, "class B.UsedByIgnoredClass", "interface B.UsedByIgnoredClass", Code.CLASS_KIND_CHANGED.code()));
Assert.assertTrue(containsDifference(allReports, "method void B.UsedByIgnoredClass::<init>()", null, Code.METHOD_REMOVED.code()));
// eleven methods removed when kind changed, because interface doesn't have the methods of Object
Assert.assertEquals(11, allReports.stream().filter(r -> {
javax.lang.model.element.TypeElement oldType = null;
if (r.getOldElement() == null || !(r.getOldElement() instanceof JavaModelElement)) {
return false;
}
javax.lang.model.element.Element old = ((JavaModelElement) r.getOldElement()).getDeclaringElement();
do {
if (old instanceof javax.lang.model.element.TypeElement) {
oldType = (javax.lang.model.element.TypeElement) old;
break;
}
old = old.getEnclosingElement();
} while (old != null);
if (oldType == null) {
return false;
}
return oldType.getQualifiedName().contentEquals("java.lang.Object");
}).flatMap(r -> r.getDifferences().stream()).count());
}
use of org.revapi.Report in project revapi by revapi.
the class SupplementaryJarsTest method testExcludedClassesDontDragUsedTypesIntoAPI.
@Test
public void testExcludedClassesDontDragUsedTypesIntoAPI() throws Exception {
List<Report> allReports;
Revapi revapi = createRevapi(CollectingReporter.class);
AnalysisContext ctx = AnalysisContext.builder(revapi).withOldAPI(API.of(new ShrinkwrapArchive(apiV1)).supportedBy(new ShrinkwrapArchive(supV1)).build()).withNewAPI(API.of(new ShrinkwrapArchive(apiV2)).supportedBy(new ShrinkwrapArchive(supV2)).build()).withConfigurationFromJSON("{\"revapi\": {\"java\": {" + "\"filter\": {\"classes\": {\"exclude\": [\"C\"]}}}}}").build();
try (AnalysisResult res = revapi.analyze(ctx)) {
allReports = res.getExtensions().getFirstExtension(CollectingReporter.class, null).getReports();
}
Assert.assertEquals(6, allReports.size());
Assert.assertTrue(containsDifference(allReports, null, "class B.T$1.Private", Code.CLASS_NON_PUBLIC_PART_OF_API.code()));
Assert.assertTrue(containsDifference(allReports, null, "field B.T$2.f2", Code.FIELD_ADDED.code()));
Assert.assertTrue(containsDifference(allReports, null, "field A.f3", Code.FIELD_ADDED.code()));
Assert.assertTrue(containsDifference(allReports, "class B.T$2", "class B.T$2", Code.CLASS_NOW_FINAL.code()));
Assert.assertTrue(containsDifference(allReports, null, "class B.T$3", Code.CLASS_ADDED.code()));
Assert.assertTrue(containsDifference(allReports, null, "class B.PrivateUsedClass", Code.CLASS_NON_PUBLIC_PART_OF_API.code()));
Assert.assertFalse(containsDifference(allReports, "class B.UsedByIgnoredClass", "class B.UsedByIgnoredClass", Code.CLASS_KIND_CHANGED.code()));
Assert.assertFalse(containsDifference(allReports, "method void B.UsedByIgnoredClass::<init>()", null, Code.METHOD_REMOVED.code()));
}
Aggregations