Search in sources :

Example 1 with JavaElementForest

use of org.revapi.java.model.JavaElementForest 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 2 with JavaElementForest

use of org.revapi.java.model.JavaElementForest in project revapi by revapi.

the class JavaArchiveAnalyzerTest method testSimple.

@Test
public void testSimple() throws Exception {
    ArchiveAndCompilationPath archive = createCompiledJar("test.jar", "misc/A.java", "misc/B.java", "misc/C.java", "misc/D.java", "misc/I.java");
    JavaArchiveAnalyzer analyzer = new JavaArchiveAnalyzer(new API(Arrays.asList(new ShrinkwrapArchive(archive.archive)), null), Executors.newSingleThreadExecutor(), null, false, InclusionFilter.acceptAll());
    try {
        JavaElementForest forest = analyzer.analyze();
        Assert.assertEquals(6, forest.getRoots().size());
    } finally {
        deleteDir(archive.compilationPath);
        analyzer.getCompilationValve().removeCompiledResults();
    }
}
Also used : JavaElementForest(org.revapi.java.model.JavaElementForest) API(org.revapi.API) Test(org.junit.Test)

Example 3 with JavaElementForest

use of org.revapi.java.model.JavaElementForest in project revapi by revapi.

the class JavaArchiveAnalyzerTest method testTypeParametersDragIntoAPI.

@Test
public void testTypeParametersDragIntoAPI() throws Exception {
    ArchiveAndCompilationPath compRes = createCompiledJar("a.jar", "misc/Generics.java", "misc/GenericsParams.java");
    JavaArchive api = ShrinkWrap.create(JavaArchive.class, "api.jar").addAsResource(compRes.compilationPath.resolve("Generics.class").toFile(), "Generics.class");
    JavaArchive sup = ShrinkWrap.create(JavaArchive.class, "sup.jar").addAsResource(compRes.compilationPath.resolve("GenericsParams.class").toFile(), "GenericsParams.class").addAsResource(compRes.compilationPath.resolve("GenericsParams$TypeParam.class").toFile(), "GenericsParams$TypeParam.class").addAsResource(compRes.compilationPath.resolve("GenericsParams$ExtendsBound.class").toFile(), "GenericsParams$ExtendsBound.class").addAsResource(compRes.compilationPath.resolve("GenericsParams$SuperBound.class").toFile(), "GenericsParams$SuperBound.class").addAsResource(compRes.compilationPath.resolve("GenericsParams$TypeVar.class").toFile(), "GenericsParams$TypeVar.class").addAsResource(compRes.compilationPath.resolve("GenericsParams$TypeVarIface.class").toFile(), "GenericsParams$TypeVarIface.class").addAsResource(compRes.compilationPath.resolve("GenericsParams$TypeVarImpl.class").toFile(), "GenericsParams$TypeVarImpl.class").addAsResource(compRes.compilationPath.resolve("GenericsParams$Unused.class").toFile(), "GenericsParams$Unused.class");
    JavaArchiveAnalyzer analyzer = new JavaArchiveAnalyzer(new API(Arrays.asList(new ShrinkwrapArchive(api)), Arrays.asList(new ShrinkwrapArchive(sup))), Executors.newSingleThreadExecutor(), null, false, InclusionFilter.acceptAll());
    try {
        JavaElementForest forest = analyzer.analyze();
        Set<TypeElement> roots = forest.getRoots();
        Assert.assertEquals(7, roots.size());
        Assert.assertTrue(roots.stream().anyMatch(hasName("class Generics<T extends GenericsParams.TypeVar, GenericsParams.TypeVarIface, U extends Generics<GenericsParams.TypeVarImpl, ?>>")));
        Assert.assertTrue(roots.stream().anyMatch(hasName("class GenericsParams.ExtendsBound")));
        Assert.assertTrue(roots.stream().anyMatch(hasName("class GenericsParams.SuperBound")));
        Assert.assertTrue(roots.stream().anyMatch(hasName("class GenericsParams.TypeParam")));
        Assert.assertTrue(roots.stream().anyMatch(hasName("class GenericsParams.TypeVar")));
        Assert.assertTrue(roots.stream().anyMatch(hasName("interface GenericsParams.TypeVarIface")));
        Assert.assertTrue(roots.stream().anyMatch(hasName("class GenericsParams.TypeVarImpl")));
        Assert.assertFalse(roots.stream().anyMatch(hasName("class GenericsParams.Unused")));
    } finally {
        deleteDir(compRes.compilationPath);
        analyzer.getCompilationValve().removeCompiledResults();
    }
}
Also used : JavaElementForest(org.revapi.java.model.JavaElementForest) TypeElement(org.revapi.java.model.TypeElement) API(org.revapi.API) JavaArchive(org.jboss.shrinkwrap.api.spec.JavaArchive) Test(org.junit.Test)

Example 4 with JavaElementForest

use of org.revapi.java.model.JavaElementForest in project revapi by revapi.

the class JavaArchiveAnalyzerTest method testPreventRecursionWhenConstructingInheritedMembers.

@Test
public void testPreventRecursionWhenConstructingInheritedMembers() throws Exception {
    ArchiveAndCompilationPath archive = createCompiledJar("a.jar", "misc/MemberInheritsOwner.java");
    JavaArchiveAnalyzer analyzer = new JavaArchiveAnalyzer(new API(Arrays.asList(new ShrinkwrapArchive(archive.archive)), null), Executors.newSingleThreadExecutor(), null, false, InclusionFilter.acceptAll());
    try {
        JavaElementForest forest = analyzer.analyze();
        forest.getRoots();
        Assert.assertEquals(1, forest.getRoots().size());
        Predicate<Element> findMethod = c -> "method void MemberInheritsOwner::method()".equals(c.getFullHumanReadableString());
        Predicate<Element> findMember1 = c -> "interface MemberInheritsOwner.Member1".equals(c.getFullHumanReadableString());
        Predicate<Element> findMember2 = c -> "interface MemberInheritsOwner.Member2".equals(c.getFullHumanReadableString());
        Element root = forest.getRoots().first();
        Assert.assertEquals(3, root.getChildren().size());
        Assert.assertTrue(root.getChildren().stream().anyMatch(findMethod));
        Assert.assertTrue(root.getChildren().stream().anyMatch(findMember1));
        Assert.assertTrue(root.getChildren().stream().anyMatch(findMember2));
        Assert.assertEquals(1, root.getChildren().stream().filter(findMember1).findFirst().get().getChildren().size());
        Assert.assertEquals(1, root.getChildren().stream().filter(findMember2).findFirst().get().getChildren().size());
    } finally {
        deleteDir(archive.compilationPath);
        analyzer.getCompilationValve().removeCompiledResults();
    }
}
Also used : API(org.revapi.API) Arrays(java.util.Arrays) ShrinkWrap(org.jboss.shrinkwrap.api.ShrinkWrap) Iterator(java.util.Iterator) Predicate(java.util.function.Predicate) TypeElement(org.revapi.java.model.TypeElement) Element(org.revapi.Element) Set(java.util.Set) JavaElementForest(org.revapi.java.model.JavaElementForest) IOException(java.io.IOException) Test(org.junit.Test) ZipExporter(org.jboss.shrinkwrap.api.exporter.ZipExporter) Executors(java.util.concurrent.Executors) JavaArchive(org.jboss.shrinkwrap.api.spec.JavaArchive) Archive(org.revapi.Archive) Assert(org.junit.Assert) Nonnull(javax.annotation.Nonnull) InclusionFilter(org.revapi.java.compilation.InclusionFilter) InputStream(java.io.InputStream) JavaElementForest(org.revapi.java.model.JavaElementForest) TypeElement(org.revapi.java.model.TypeElement) Element(org.revapi.Element) API(org.revapi.API) Test(org.junit.Test)

Example 5 with JavaElementForest

use of org.revapi.java.model.JavaElementForest in project revapi by revapi.

the class JavaArchiveAnalyzerTest method testWithSupplementary.

@Test
public void testWithSupplementary() throws Exception {
    ArchiveAndCompilationPath compRes = createCompiledJar("a.jar", "v1/supplementary/a/A.java", "v1/supplementary/b/B.java", "v1/supplementary/a/C.java");
    JavaArchive api = ShrinkWrap.create(JavaArchive.class, "api.jar").addAsResource(compRes.compilationPath.resolve("A.class").toFile(), "A.class");
    JavaArchive sup = ShrinkWrap.create(JavaArchive.class, "sup.jar").addAsResource(compRes.compilationPath.resolve("B.class").toFile(), "B.class").addAsResource(compRes.compilationPath.resolve("B$T$1.class").toFile(), "B$T$1.class").addAsResource(compRes.compilationPath.resolve("B$T$1$TT$1.class").toFile(), "B$T$1$TT$1.class").addAsResource(compRes.compilationPath.resolve("B$T$2.class").toFile(), "B$T$2.class").addAsResource(compRes.compilationPath.resolve("C.class").toFile(), "C.class").addAsResource(compRes.compilationPath.resolve("B$UsedByIgnoredClass.class").toFile(), "B$UsedByIgnoredClass.class").addAsResource(compRes.compilationPath.resolve("A$PrivateEnum.class").toFile(), "A$PrivateEnum.class");
    JavaArchiveAnalyzer analyzer = new JavaArchiveAnalyzer(new API(Arrays.asList(new ShrinkwrapArchive(api)), Arrays.asList(new ShrinkwrapArchive(sup))), Executors.newSingleThreadExecutor(), null, false, InclusionFilter.acceptAll());
    try {
        JavaElementForest forest = analyzer.analyze();
        Assert.assertEquals(3, forest.getRoots().size());
        Iterator<TypeElement> roots = forest.getRoots().iterator();
        TypeElement A = roots.next();
        TypeElement B_T$1 = roots.next();
        TypeElement B_T$2 = roots.next();
        Assert.assertEquals("A", A.getCanonicalName());
        Assert.assertEquals("A", A.getDeclaringElement().getQualifiedName().toString());
        Assert.assertEquals("B.T$1", B_T$1.getCanonicalName());
        Assert.assertEquals("B.T$1", B_T$1.getDeclaringElement().getQualifiedName().toString());
        Assert.assertEquals("B.T$2", B_T$2.getCanonicalName());
        Assert.assertEquals("B.T$2", B_T$2.getDeclaringElement().getQualifiedName().toString());
    } finally {
        deleteDir(compRes.compilationPath);
        analyzer.getCompilationValve().removeCompiledResults();
    }
}
Also used : JavaElementForest(org.revapi.java.model.JavaElementForest) TypeElement(org.revapi.java.model.TypeElement) API(org.revapi.API) JavaArchive(org.jboss.shrinkwrap.api.spec.JavaArchive) Test(org.junit.Test)

Aggregations

API (org.revapi.API)5 JavaElementForest (org.revapi.java.model.JavaElementForest)5 Test (org.junit.Test)4 JavaArchive (org.jboss.shrinkwrap.api.spec.JavaArchive)3 TypeElement (org.revapi.java.model.TypeElement)3 Element (org.revapi.Element)2 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 AnnotatedElement (java.lang.reflect.AnnotatedElement)1 Arrays (java.util.Arrays)1 Iterator (java.util.Iterator)1 Set (java.util.Set)1 Executors (java.util.concurrent.Executors)1 Predicate (java.util.function.Predicate)1 Nonnull (javax.annotation.Nonnull)1 ShrinkWrap (org.jboss.shrinkwrap.api.ShrinkWrap)1 ZipExporter (org.jboss.shrinkwrap.api.exporter.ZipExporter)1 Assert (org.junit.Assert)1 AnalysisContext (org.revapi.AnalysisContext)1 Archive (org.revapi.Archive)1