Search in sources :

Example 31 with Index

use of org.jboss.jandex.Index in project wildfly by wildfly.

the class AnnotationsTestCase method testProcessAuthenticationMechanism.

/**
     * Process: AuthenticationMechanism -- verification of the
     * processAuthenticationMechanism method
     *
     * @throws Throwable throwable exception
     */
@Test
public void testProcessAuthenticationMechanism() throws Throwable {
    try {
        URI uri = getURI("/ra16annoauthmech.rar");
        final VirtualFile virtualFile = VFS.getChild(uri);
        final Indexer indexer = new Indexer();
        final List<VirtualFile> classChildren = virtualFile.getChildren(new SuffixMatchFilter(".class", VisitorAttributes.RECURSE_LEAVES_ONLY));
        for (VirtualFile classFile : classChildren) {
            InputStream inputStream = null;
            try {
                inputStream = classFile.openStream();
                indexer.index(inputStream);
            } finally {
                VFSUtils.safeClose(inputStream);
            }
        }
        final Index index = indexer.complete();
        AnnotationRepository ar = new JandexAnnotationRepositoryImpl(index, Thread.currentThread().getContextClassLoader());
        annotations.process(ar, null, Thread.currentThread().getContextClassLoader());
    } catch (Throwable t) {
        fail(t.getMessage());
    }
}
Also used : VirtualFile(org.jboss.vfs.VirtualFile) Indexer(org.jboss.jandex.Indexer) InputStream(java.io.InputStream) SuffixMatchFilter(org.jboss.vfs.util.SuffixMatchFilter) AnnotationRepository(org.jboss.jca.common.spi.annotations.repository.AnnotationRepository) Index(org.jboss.jandex.Index) URI(java.net.URI) Test(org.junit.Test)

Example 32 with Index

use of org.jboss.jandex.Index in project wildfly by wildfly.

the class AnnotationsTestCase method testProcessAdministeredObject.

/**
     * Process: AdministeredObject -- verification of the
     * processAdministeredObject method
     *
     * @throws Throwable throwable exception
     */
@Test
public void testProcessAdministeredObject() throws Throwable {
    try {
        URI uri = getURI("/ra16annoadminobj.rar");
        final VirtualFile virtualFile = VFS.getChild(uri);
        final Indexer indexer = new Indexer();
        final List<VirtualFile> classChildren = virtualFile.getChildren(new SuffixMatchFilter(".class", VisitorAttributes.RECURSE_LEAVES_ONLY));
        for (VirtualFile classFile : classChildren) {
            InputStream inputStream = null;
            try {
                inputStream = classFile.openStream();
                indexer.index(inputStream);
            } finally {
                VFSUtils.safeClose(inputStream);
            }
        }
        final Index index = indexer.complete();
        AnnotationRepository ar = new JandexAnnotationRepositoryImpl(index, Thread.currentThread().getContextClassLoader());
        annotations.process(ar, null, Thread.currentThread().getContextClassLoader());
    } catch (Throwable t) {
        fail(t.getMessage());
    }
}
Also used : VirtualFile(org.jboss.vfs.VirtualFile) Indexer(org.jboss.jandex.Indexer) InputStream(java.io.InputStream) SuffixMatchFilter(org.jboss.vfs.util.SuffixMatchFilter) AnnotationRepository(org.jboss.jca.common.spi.annotations.repository.AnnotationRepository) Index(org.jboss.jandex.Index) URI(java.net.URI) Test(org.junit.Test)

Example 33 with Index

use of org.jboss.jandex.Index in project drools by kiegroup.

the class KieDMNModelJandexTest method testReflectConfigJSON.

@Test
public void testReflectConfigJSON() throws Exception {
    Indexer indexer = new Indexer();
    InputStream stream = getClass().getClassLoader().getResourceAsStream("org/kie/dmn/model/api/DMNModelInstrumentedBase.class");
    indexer.index(stream);
    stream.close();
    scanFile(new File("./target/classes"), indexer);
    Index index = indexer.complete();
    Set<ClassInfo> founds = index.getAllKnownImplementors(DotName.createSimple(DMNModelInstrumentedBase.class.getCanonicalName()));
    LOG.debug("founds: \n{}", founds);
    Jsonb jsonb = JsonbBuilder.create(new JsonbConfig().withFormatting(true));
    List<Object> results = new ArrayList<>();
    for (ClassInfo found : founds.stream().sorted(Comparator.comparing(ClassInfo::name)).collect(Collectors.toList())) {
        results.add(toReflectConfigMap(found));
    }
    LOG.debug("results: \n{}", results);
    String json = jsonb.toJson(results);
    LOG.debug("Expected reflect-config.json: \n{}", json);
    @SuppressWarnings("unchecked") List<Map<String, Object>> fromJson = jsonb.fromJson(new FileReader("src/main/resources/META-INF/native-image/org.kie/" + moduleName + "/reflect-config.json"), List.class);
    List<DotName> dotNamesInJSON = fromJson.stream().map(m -> DotName.createSimple((String) m.get("name"))).collect(Collectors.toList());
    Set<DotName> foundsViaJandex = founds.stream().map(ClassInfo::name).collect(Collectors.toSet());
    Set<DotName> foundsViaJSON = dotNamesInJSON.stream().collect(Collectors.toSet());
    Assertions.assertThat(foundsViaJandex).as("List of classes found via Jandex during test and listed in JSON file must be same.").isEqualTo(foundsViaJSON);
}
Also used : JsonbBuilder(javax.json.bind.JsonbBuilder) DMNModelInstrumentedBase(org.kie.dmn.model.api.DMNModelInstrumentedBase) Logger(org.slf4j.Logger) DotName(org.jboss.jandex.DotName) LoggerFactory(org.slf4j.LoggerFactory) Set(java.util.Set) Test(org.junit.Test) FileInputStream(java.io.FileInputStream) ClassInfo(org.jboss.jandex.ClassInfo) Collectors(java.util.stream.Collectors) JsonbConfig(javax.json.bind.JsonbConfig) File(java.io.File) FileNotFoundException(java.io.FileNotFoundException) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) Indexer(org.jboss.jandex.Indexer) List(java.util.List) Map(java.util.Map) Assertions(org.assertj.core.api.Assertions) Jsonb(javax.json.bind.Jsonb) FileReader(java.io.FileReader) Comparator(java.util.Comparator) Index(org.jboss.jandex.Index) InputStream(java.io.InputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) Index(org.jboss.jandex.Index) DotName(org.jboss.jandex.DotName) Jsonb(javax.json.bind.Jsonb) JsonbConfig(javax.json.bind.JsonbConfig) Indexer(org.jboss.jandex.Indexer) FileReader(java.io.FileReader) File(java.io.File) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) ClassInfo(org.jboss.jandex.ClassInfo) Test(org.junit.Test)

Example 34 with Index

use of org.jboss.jandex.Index in project wildfly by wildfly.

the class WeldModuleDeploymentTestCase method createTestModule.

private static void createTestModule(File testModuleRoot) throws IOException {
    if (testModuleRoot.exists()) {
        throw new IllegalArgumentException(testModuleRoot + " already exists");
    }
    File file = new File(testModuleRoot, "main");
    if (!file.mkdirs()) {
        throw new IllegalArgumentException("Could not create " + file);
    }
    URL url = WeldModuleDeploymentTestCase.class.getResource("module.xml");
    if (url == null) {
        throw new IllegalStateException("Could not find module.xml");
    }
    copyFile(new File(file, "module.xml"), url.openStream());
    JavaArchive jar = ShrinkWrap.create(JavaArchive.class, "weldTest.jar");
    jar.addClasses(SimpleBean.class, ModuleEjb.class);
    jar.addAsManifestResource(new StringAsset("<beans bean-discovery-mode=\"all\"></beans>"), "beans.xml");
    Indexer indexer = new Indexer();
    try (InputStream resource = ModuleEjb.class.getResourceAsStream(ModuleEjb.class.getSimpleName() + ".class")) {
        indexer.index(resource);
    }
    Index index = indexer.complete();
    ByteArrayOutputStream data = new ByteArrayOutputStream();
    IndexWriter writer = new IndexWriter(data);
    writer.write(index);
    jar.addAsManifestResource(new ByteArrayAsset(data.toByteArray()), "jandex.idx");
    FileOutputStream jarFile = new FileOutputStream(new File(file, "weldTest.jar"));
    try {
        jar.as(ZipExporter.class).exportTo(jarFile);
    } finally {
        jarFile.flush();
        jarFile.close();
    }
}
Also used : StringAsset(org.jboss.shrinkwrap.api.asset.StringAsset) ByteArrayAsset(org.jboss.shrinkwrap.api.asset.ByteArrayAsset) InputStream(java.io.InputStream) ZipExporter(org.jboss.shrinkwrap.api.exporter.ZipExporter) Index(org.jboss.jandex.Index) ByteArrayOutputStream(java.io.ByteArrayOutputStream) URL(java.net.URL) JavaArchive(org.jboss.shrinkwrap.api.spec.JavaArchive) Indexer(org.jboss.jandex.Indexer) IndexWriter(org.jboss.jandex.IndexWriter) FileOutputStream(java.io.FileOutputStream) File(java.io.File)

Example 35 with Index

use of org.jboss.jandex.Index in project wildfly by wildfly.

the class WebModuleDeploymentTestCase method createTestModule.

private static void createTestModule(File testModuleRoot) throws IOException {
    if (testModuleRoot.exists()) {
        throw new IllegalArgumentException(testModuleRoot + " already exists");
    }
    File file = new File(testModuleRoot, "main");
    if (!file.mkdirs()) {
        throw new IllegalArgumentException("Could not create " + file);
    }
    URL url = WebModuleDeploymentTestCase.class.getResource("module.xml");
    if (url == null) {
        throw new IllegalStateException("Could not find module.xml");
    }
    copyFile(new File(file, "module.xml"), url.openStream());
    JavaArchive jar = ShrinkWrap.create(JavaArchive.class, "webTest.jar");
    jar.addClasses(ModuleServlet.class);
    Indexer indexer = new Indexer();
    try (InputStream resource = ModuleServlet.class.getResourceAsStream(ModuleServlet.class.getSimpleName() + ".class")) {
        indexer.index(resource);
    }
    Index index = indexer.complete();
    ByteArrayOutputStream data = new ByteArrayOutputStream();
    IndexWriter writer = new IndexWriter(data);
    writer.write(index);
    jar.addAsManifestResource(new ByteArrayAsset(data.toByteArray()), "jandex.idx");
    FileOutputStream jarFile = new FileOutputStream(new File(file, "webTest.jar"));
    try {
        jar.as(ZipExporter.class).exportTo(jarFile);
    } finally {
        jarFile.flush();
        jarFile.close();
    }
}
Also used : ByteArrayAsset(org.jboss.shrinkwrap.api.asset.ByteArrayAsset) InputStream(java.io.InputStream) ZipExporter(org.jboss.shrinkwrap.api.exporter.ZipExporter) Index(org.jboss.jandex.Index) ByteArrayOutputStream(java.io.ByteArrayOutputStream) URL(java.net.URL) JavaArchive(org.jboss.shrinkwrap.api.spec.JavaArchive) Indexer(org.jboss.jandex.Indexer) IndexWriter(org.jboss.jandex.IndexWriter) FileOutputStream(java.io.FileOutputStream) File(java.io.File)

Aggregations

Index (org.jboss.jandex.Index)50 Indexer (org.jboss.jandex.Indexer)24 InputStream (java.io.InputStream)21 Test (org.junit.Test)18 File (java.io.File)16 URI (java.net.URI)15 AnnotationRepository (org.jboss.jca.common.spi.annotations.repository.AnnotationRepository)13 VirtualFile (org.jboss.vfs.VirtualFile)13 URL (java.net.URL)11 SuffixMatchFilter (org.jboss.vfs.util.SuffixMatchFilter)11 ClassInfo (org.jboss.jandex.ClassInfo)10 ResourceRoot (org.jboss.as.server.deployment.module.ResourceRoot)8 IOException (java.io.IOException)7 HashMap (java.util.HashMap)6 DeploymentUnit (org.jboss.as.server.deployment.DeploymentUnit)6 ArrayList (java.util.ArrayList)5 HashSet (java.util.HashSet)5 Map (java.util.Map)5 ByteArrayOutputStream (java.io.ByteArrayOutputStream)4 CompositeIndex (org.jboss.jandex.CompositeIndex)4