Search in sources :

Example 1 with Revapi

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();
}
Also used : MalformedURLException(java.net.MalformedURLException) Revapi(org.revapi.Revapi) URL(java.net.URL) URLClassLoader(java.net.URLClassLoader) BuildException(org.apache.tools.ant.BuildException) File(java.io.File)

Example 2 with Revapi

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);
    }
}
Also used : JavaElementForest(org.revapi.java.model.JavaElementForest) Revapi(org.revapi.Revapi) Element(org.revapi.Element) MethodParameterElement(org.revapi.java.model.MethodParameterElement) MethodElement(org.revapi.java.model.MethodElement) AnnotatedElement(java.lang.reflect.AnnotatedElement) API(org.revapi.API) AnalysisContext(org.revapi.AnalysisContext) AnnotatedElementFilter(org.revapi.java.filters.AnnotatedElementFilter)

Example 3 with Revapi

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);
    }
}
Also used : ElementForest(org.revapi.ElementForest) Revapi(org.revapi.Revapi) JavaModelElement(org.revapi.java.spi.JavaModelElement) JavaModelElement(org.revapi.java.spi.JavaModelElement) Element(org.revapi.Element) ArrayList(java.util.ArrayList) ArchiveAnalyzer(org.revapi.ArchiveAnalyzer) AnalysisContext(org.revapi.AnalysisContext) API(org.revapi.API)

Example 4 with Revapi

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());
}
Also used : API(org.revapi.API) ShrinkWrap(org.jboss.shrinkwrap.api.ShrinkWrap) Code(org.revapi.java.spi.Code) AnalysisContext(org.revapi.AnalysisContext) Report(org.revapi.Report) JavaModelElement(org.revapi.java.spi.JavaModelElement) AnalysisResult(org.revapi.AnalysisResult) Test(org.junit.Test) Revapi(org.revapi.Revapi) List(java.util.List) JavaArchive(org.jboss.shrinkwrap.api.spec.JavaArchive) After(org.junit.After) Assert(org.junit.Assert) Before(org.junit.Before) Revapi(org.revapi.Revapi) Report(org.revapi.Report) JavaModelElement(org.revapi.java.spi.JavaModelElement) JavaModelElement(org.revapi.java.spi.JavaModelElement) AnalysisContext(org.revapi.AnalysisContext) AnalysisResult(org.revapi.AnalysisResult) Test(org.junit.Test)

Example 5 with Revapi

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()));
}
Also used : Revapi(org.revapi.Revapi) Report(org.revapi.Report) AnalysisContext(org.revapi.AnalysisContext) AnalysisResult(org.revapi.AnalysisResult) Test(org.junit.Test)

Aggregations

Revapi (org.revapi.Revapi)17 AnalysisContext (org.revapi.AnalysisContext)15 AnalysisResult (org.revapi.AnalysisResult)9 Test (org.junit.Test)8 File (java.io.File)5 API (org.revapi.API)4 StringWriter (java.io.StringWriter)3 List (java.util.List)3 ModelNode (org.jboss.dmr.ModelNode)3 Report (org.revapi.Report)3 BufferedReader (java.io.BufferedReader)2 FileInputStream (java.io.FileInputStream)2 FileReader (java.io.FileReader)2 IOException (java.io.IOException)2 PrintWriter (java.io.PrintWriter)2 MalformedURLException (java.net.MalformedURLException)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 MavenProject (org.apache.maven.project.MavenProject)2