use of org.revapi.Revapi in project revapi by revapi.
the class RevapiTask method initRevapi.
private Revapi initRevapi() throws BuildException {
Revapi.Builder revapiBuilder = Revapi.builder();
if (revapiClasspath != null) {
String[] elements = revapiClasspath.list();
URL[] urls = new URL[elements.length];
for (int i = 0; i < elements.length; ++i) {
try {
urls[i] = new File(elements[i]).toURI().toURL();
} catch (MalformedURLException | IllegalArgumentException | SecurityException e) {
throw new BuildException("Could not compose revapi classpath: " + e.getMessage(), e);
}
}
revapiBuilder.withAllExtensionsFrom(new URLClassLoader(urls, getClass().getClassLoader()));
} else {
revapiBuilder.withAllExtensionsFrom(getClass().getClassLoader());
}
// always add the Ant reporter, so that we get stuff in the Ant log
revapiBuilder.withReporters(AntReporter.class);
return revapiBuilder.build();
}
use of org.revapi.Revapi in project revapi by revapi.
the class AnnotatedElementFilterTest method testWith.
private void testWith(String configJSON, Consumer<List<Element>> test) throws Exception {
ArchiveAndCompilationPath archive = createCompiledJar("test.jar", "annotationfilter/NonPublic.java", "annotationfilter/NonPublicClass.java", "annotationfilter/Public.java", "annotationfilter/PublicClass.java", "annotationfilter/UndecisiveClass.java");
try {
JavaArchiveAnalyzer analyzer = new JavaArchiveAnalyzer(new API(Arrays.asList(new ShrinkwrapArchive(archive.archive)), null), Executors.newSingleThreadExecutor(), null, false, InclusionFilter.acceptAll());
JavaElementForest forest = analyzer.analyze();
AnnotatedElementFilter filter = new AnnotatedElementFilter();
Revapi r = new Revapi(emptySet(), emptySet(), emptySet(), singleton(AnnotatedElementFilter.class));
AnalysisContext ctx = AnalysisContext.builder(r).withConfigurationFromJSON(configJSON).build();
AnalysisContext filterCtx = r.prepareAnalysis(ctx).getFirstConfigurationOrNull(AnnotatedElementFilter.class);
filter.initialize(filterCtx);
List<Element> results = forest.search(Element.class, true, filter, null);
analyzer.getCompilationValve().removeCompiledResults();
test.accept(results);
} finally {
deleteDir(archive.compilationPath);
}
}
use of org.revapi.Revapi in project revapi by revapi.
the class ClassFilterTest method testWith.
static void testWith(ArchiveAndCompilationPath archive, String configJSON, Set<String> expectedResults) throws Exception {
try {
JavaApiAnalyzer apiAnalyzer = new JavaApiAnalyzer(Collections.emptyList());
Revapi r = new Revapi(singleton(JavaApiAnalyzer.class), emptySet(), emptySet(), emptySet());
AnalysisContext ctx = AnalysisContext.builder(r).withConfigurationFromJSON(configJSON).build();
AnalysisContext analyzerCtx = r.prepareAnalysis(ctx).getFirstConfigurationOrNull(JavaApiAnalyzer.class);
apiAnalyzer.initialize(analyzerCtx);
ArchiveAnalyzer archiveAnalyzer = apiAnalyzer.getArchiveAnalyzer(new API(Collections.singletonList(new ShrinkwrapArchive(archive.archive)), null));
ElementForest forest = archiveAnalyzer.analyze();
List<Element> results = forest.search(Element.class, true, new AcceptingFilter(), null);
((JavaArchiveAnalyzer) archiveAnalyzer).getCompilationValve().removeCompiledResults();
List<String> expected = new ArrayList<>(expectedResults);
List<String> actual = results.stream().filter(e -> {
if (e.getArchive() == null) {
return false;
}
if (!(e instanceof JavaModelElement)) {
// exclude annotations
return false;
}
JavaModelElement el = (JavaModelElement) e;
return !el.isInherited();
}).map(Element::getFullHumanReadableString).collect(toList());
Collections.sort(expected);
Collections.sort(actual);
Assert.assertEquals(expected, actual);
} finally {
deleteDir(archive.compilationPath);
}
}
use of org.revapi.Revapi 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.Revapi 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