org.jboss.jandex.Indexer Java Examples

The following examples show how to use org.jboss.jandex.Indexer. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: IndexInitializer.java    From smallrye-graphql with Apache License 2.0 6 votes vote down vote up
private void processFolder(URL url, Indexer indexer) throws IOException {
    try {
        Path folderPath = Paths.get(url.toURI());
        if (Files.isDirectory(folderPath)) {
            try (Stream<Path> walk = Files.walk(folderPath)) {

                List<Path> collected = walk
                        .filter(Files::isRegularFile)
                        .collect(Collectors.toList());

                for (Path c : collected) {
                    String entryName = c.getFileName().toString();
                    processFile(entryName, Files.newInputStream(c), indexer);
                }
            }
        } else {
            SmallRyeGraphQLServletLogging.log.ignoringUrl(url);
        }

    } catch (URISyntaxException ex) {
        SmallRyeGraphQLServletLogging.log.couldNotProcessUrl(url, ex);
    }
}
 
Example #2
Source File: BeanArchives.java    From quarkus with Apache License 2.0 6 votes vote down vote up
private static IndexView buildAdditionalIndex() {
    Indexer indexer = new Indexer();
    // CDI API
    index(indexer, ActivateRequestContext.class.getName());
    index(indexer, Default.class.getName());
    index(indexer, Any.class.getName());
    index(indexer, Named.class.getName());
    index(indexer, Initialized.class.getName());
    index(indexer, BeforeDestroyed.class.getName());
    index(indexer, Destroyed.class.getName());
    index(indexer, Intercepted.class.getName());
    index(indexer, Model.class.getName());
    // Arc built-in beans
    index(indexer, ActivateRequestContextInterceptor.class.getName());
    index(indexer, InjectableRequestContextController.class.getName());
    return indexer.complete();
}
 
Example #3
Source File: NestedSchemaReferenceTests.java    From smallrye-open-api with Apache License 2.0 6 votes vote down vote up
@Test
public void testSimpleNestedSchemaOnParameter() throws IOException, JSONException {
    Indexer indexer = new Indexer();

    // Test samples
    index(indexer, "test/io/smallrye/openapi/runtime/scanner/resources/FooResource.class");
    index(indexer, "test/io/smallrye/openapi/runtime/scanner/resources/FooResource$Foo.class");
    index(indexer, "test/io/smallrye/openapi/runtime/scanner/resources/FooResource$Bar.class");

    OpenApiAnnotationScanner scanner = new OpenApiAnnotationScanner(nestingSupportConfig(), indexer.complete());

    OpenAPI result = scanner.scan();

    printToConsole(result);
    assertJsonEquals("refsEnabled.resource.simple.expected.json", result);
}
 
Example #4
Source File: JaxRsAnnotationScannerTest.java    From smallrye-open-api with Apache License 2.0 6 votes vote down vote up
@Test
public void testRequestBodyComponentGeneration() throws IOException, JSONException {
    Indexer indexer = new Indexer();

    // Test samples
    index(indexer, "test/io/smallrye/openapi/runtime/scanner/resources/RequestBodyTestApplication.class");
    index(indexer, "test/io/smallrye/openapi/runtime/scanner/resources/RequestBodyTestApplication$SomeObject.class");
    index(indexer, "test/io/smallrye/openapi/runtime/scanner/resources/RequestBodyTestApplication$DifferentObject.class");
    index(indexer,
            "test/io/smallrye/openapi/runtime/scanner/resources/RequestBodyTestApplication$RequestBodyResource.class");

    OpenApiAnnotationScanner scanner = new OpenApiAnnotationScanner(customSchemaRegistryConfig(), indexer.complete());

    OpenAPI result = scanner.scan();

    printToConsole(result);
    assertJsonEquals("resource.testRequestBodyComponentGeneration.json", result);
}
 
Example #5
Source File: TestClassIndexer.java    From quarkus with Apache License 2.0 6 votes vote down vote up
public static Index indexTestClasses(Class<?> testClass) {
    final Indexer indexer = new Indexer();
    final Path testClassesLocation = getTestClassesLocation(testClass);
    try {
        if (Files.isDirectory(testClassesLocation)) {
            indexTestClassesDir(indexer, testClassesLocation);
        } else {
            try (FileSystem jarFs = FileSystems.newFileSystem(testClassesLocation, null)) {
                for (Path p : jarFs.getRootDirectories()) {
                    indexTestClassesDir(indexer, p);
                }
            }
        }
    } catch (IOException e) {
        throw new UncheckedIOException("Unable to index the test-classes/ directory.", e);
    }
    return indexer.complete();
}
 
Example #6
Source File: AnnotationProxyProvider.java    From quarkus with Apache License 2.0 6 votes vote down vote up
public <A extends Annotation> AnnotationProxyBuilder<A> builder(AnnotationInstance annotationInstance,
        Class<A> annotationType) {
    if (!annotationInstance.name().toString().equals(annotationType.getName())) {
        throw new IllegalArgumentException("Annotation instance " + annotationInstance + " does not match annotation type "
                + annotationType.getName());
    }
    ClassInfo annotationClass = annotationClasses.computeIfAbsent(annotationInstance.name(), name -> {
        ClassInfo clazz = index.getClassByName(name);
        if (clazz == null) {
            try (InputStream annotationStream = IoUtil.readClass(classLoader, name.toString())) {
                Indexer indexer = new Indexer();
                clazz = indexer.index(annotationStream);
            } catch (Exception e) {
                throw new IllegalStateException("Failed to index: " + name, e);
            }
        }
        return clazz;
    });
    String annotationLiteral = annotationLiterals.computeIfAbsent(annotationInstance.name(), name ->
    // com.foo.MyAnnotation -> com.foo.MyAnnotation_Proxy_AnnotationLiteral
    name.toString().replace('.', '/') + "_Proxy_AnnotationLiteral");

    return new AnnotationProxyBuilder<>(annotationInstance, annotationType, annotationLiteral, annotationClass);
}
 
Example #7
Source File: JaxRsDataObjectScannerTestBase.java    From smallrye-open-api with Apache License 2.0 6 votes vote down vote up
@BeforeClass
public static void createIndex() {
    Indexer indexer = new Indexer();

    // Stand-in stuff
    index(indexer, "io/smallrye/openapi/runtime/scanner/CollectionStandin.class");
    index(indexer, "io/smallrye/openapi/runtime/scanner/IterableStandin.class");
    index(indexer, "io/smallrye/openapi/runtime/scanner/MapStandin.class");

    // Test samples
    indexDirectory(indexer, "test/io/smallrye/openapi/runtime/scanner/entities/");

    // Microprofile TCK classes
    index(indexer, "org/eclipse/microprofile/openapi/apps/airlines/model/Airline.class");
    index(indexer, "org/eclipse/microprofile/openapi/apps/airlines/model/Booking.class");
    index(indexer, "org/eclipse/microprofile/openapi/apps/airlines/model/CreditCard.class");
    index(indexer, "org/eclipse/microprofile/openapi/apps/airlines/model/Flight.class");
    index(indexer, "org/eclipse/microprofile/openapi/apps/airlines/model/Airline.class");

    // Test containers
    //indexDirectory(indexer, "test/io/smallrye/openapi/runtime/scanner/entities/");

    index = indexer.complete();
}
 
Example #8
Source File: CombinedIndexBuildStep.java    From quarkus with Apache License 2.0 6 votes vote down vote up
@BuildStep
CombinedIndexBuildItem build(ApplicationArchivesBuildItem archives,
        List<AdditionalIndexedClassesBuildItem> additionalIndexedClassesItems) {
    List<IndexView> archiveIndexes = new ArrayList<>();

    for (ApplicationArchive i : archives.getAllApplicationArchives()) {
        archiveIndexes.add(i.getIndex());
    }

    CompositeIndex archivesIndex = CompositeIndex.create(archiveIndexes);

    Indexer indexer = new Indexer();
    Set<DotName> additionalIndex = new HashSet<>();

    for (AdditionalIndexedClassesBuildItem additionalIndexedClasses : additionalIndexedClassesItems) {
        for (String classToIndex : additionalIndexedClasses.getClassesToIndex()) {
            IndexingUtil.indexClass(classToIndex, indexer, archivesIndex, additionalIndex,
                    Thread.currentThread().getContextClassLoader());
        }
    }

    return new CombinedIndexBuildItem(CompositeIndex.create(archivesIndex, indexer.complete()));
}
 
Example #9
Source File: CompositeIndexProcessor.java    From wildfly-core with GNU Lesser General Public License v2.1 6 votes vote down vote up
private Index calculateModuleIndex(final Module module) throws ModuleLoadException, IOException {
    final Indexer indexer = new Indexer();
    final PathFilter filter = PathFilters.getDefaultImportFilter();
    final Iterator<Resource> iterator = module.iterateResources(filter);
    while (iterator.hasNext()) {
        Resource resource = iterator.next();
        if(resource.getName().endsWith(".class")) {
            try (InputStream in = resource.openStream()) {
                indexer.index(in);
            } catch (Exception e) {
                ServerLogger.DEPLOYMENT_LOGGER.cannotIndexClass(resource.getName(), resource.getURL().toExternalForm(), e);
            }
        }
    }
    return indexer.complete();
}
 
Example #10
Source File: AddIndexPlugin.java    From hibernate-demos with Apache License 2.0 6 votes vote down vote up
@Override
public ResourcePool transform(ResourcePool in, ResourcePoolBuilder out) {
    Indexer indexer = new Indexer();

    for (String moduleName : modules) {
        ResourcePoolModule module = in.moduleView()
            .findModule( moduleName )
            .orElseThrow( () -> new RuntimeException("Module " + moduleName + "wasn't found" ) );

        module.entries()
            .filter( this::shouldAddToIndex )
            .forEach( e -> addToIndex( indexer, e ) );
    }

    ByteArrayOutputStream index = writeToOutputStream( indexer );
    out.add( ResourcePoolEntry.create( "/" + targetModule + "/META-INF/jandex.idx", index.toByteArray() ) );

    in.transformAndCopy(
        e -> e,
        out
    );

    return out.build();
}
 
Example #11
Source File: IndexInitializer.java    From smallrye-graphql with Apache License 2.0 6 votes vote down vote up
private IndexView createIndexView(Set<URL> urls) {
    Indexer indexer = new Indexer();
    for (URL url : urls) {
        try {
            if (url.toString().endsWith(DOT_JAR) || url.toString().endsWith(DOT_WAR)) {
                SmallRyeGraphQLServletLogging.log.processingFile(url.toString());
                processJar(url.openStream(), indexer);
            } else {
                processFolder(url, indexer);
            }
        } catch (IOException ex) {
            SmallRyeGraphQLServletLogging.log.cannotProcessFile(url.toString(), ex);
        }
    }

    return indexer.complete();
}
 
Example #12
Source File: IndexScannerTestBase.java    From smallrye-open-api with Apache License 2.0 5 votes vote down vote up
protected static void index(Indexer indexer, String resName) {
    try {
        InputStream stream = tcclGetResourceAsStream(resName);
        indexer.index(stream);
    } catch (IOException ioe) {
        throw new UncheckedIOException(ioe);
    }
}
 
Example #13
Source File: JandexUtilTest.java    From quarkus with Apache License 2.0 5 votes vote down vote up
private static Index index(Class<?>... classes) {
    Indexer indexer = new Indexer();
    for (Class<?> clazz : classes) {
        try {
            try (InputStream stream = JandexUtilTest.class.getClassLoader()
                    .getResourceAsStream(clazz.getName().replace('.', '/') + ".class")) {
                indexer.index(stream);
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
    return indexer.complete();
}
 
Example #14
Source File: AddIndexPlugin.java    From hibernate-demos with Apache License 2.0 5 votes vote down vote up
private void addToIndex(Indexer indexer, ResourcePoolEntry entry) {
    try {
        indexer.index( entry.content() );
    }
    catch(Exception ex) {
        throw new RuntimeException( ex );
    }
}
 
Example #15
Source File: BeanArchives.java    From quarkus with Apache License 2.0 5 votes vote down vote up
static boolean index(Indexer indexer, String className, ClassLoader classLoader) {
    boolean result = false;
    try (InputStream stream = classLoader
            .getResourceAsStream(className.replace('.', '/') + ".class")) {
        if (stream != null) {
            indexer.index(stream);
            result = true;
        } else {
            LOGGER.warnf("Failed to index %s: Class does not exist in ClassLoader %s", className, classLoader);
        }
    } catch (IOException e) {
        LOGGER.warnf(e, "Failed to index %s: %s", className, e.getMessage());
    }
    return result;
}
 
Example #16
Source File: Basics.java    From quarkus with Apache License 2.0 5 votes vote down vote up
public static Index index(Class<?>... classes) throws IOException {
    Indexer indexer = new Indexer();
    for (Class<?> clazz : classes) {
        try (InputStream stream = Basics.class.getClassLoader()
                .getResourceAsStream(clazz.getName().replace('.', '/') + ".class")) {
            indexer.index(stream);
        }
    }
    return indexer.complete();
}
 
Example #17
Source File: ArcTestContainer.java    From quarkus with Apache License 2.0 5 votes vote down vote up
private Index index(Iterable<Class<?>> classes) throws IOException {
    Indexer indexer = new Indexer();
    for (Class<?> clazz : classes) {
        try (InputStream stream = ArcTestContainer.class.getClassLoader()
                .getResourceAsStream(clazz.getName().replace('.', '/') + ".class")) {
            indexer.index(stream);
        }
    }
    return indexer.complete();
}
 
Example #18
Source File: SimpleGeneratorTest.java    From quarkus with Apache License 2.0 5 votes vote down vote up
private static Index index(Class<?>... classes) throws IOException {
    Indexer indexer = new Indexer();
    for (Class<?> clazz : classes) {
        try (InputStream stream = SimpleGeneratorTest.class.getClassLoader()
                .getResourceAsStream(clazz.getName().replace('.', '/') + ".class")) {
            indexer.index(stream);
        }
    }
    return indexer.complete();
}
 
Example #19
Source File: HibernateOrmProcessor.java    From quarkus with Apache License 2.0 5 votes vote down vote up
@BuildStep
public JpaModelIndexBuildItem jpaEntitiesIndexer(
        CombinedIndexBuildItem index,
        List<AdditionalJpaModelBuildItem> additionalJpaModelBuildItems) {
    // build a composite index with additional jpa model classes
    Indexer indexer = new Indexer();
    Set<DotName> additionalIndex = new HashSet<>();
    for (AdditionalJpaModelBuildItem jpaModel : additionalJpaModelBuildItems) {
        IndexingUtil.indexClass(jpaModel.getClassName(), indexer, index.getIndex(), additionalIndex,
                HibernateOrmProcessor.class.getClassLoader());
    }
    CompositeIndex compositeIndex = CompositeIndex.create(index.getIndex(), indexer.complete());
    return new JpaModelIndexBuildItem(compositeIndex);
}
 
Example #20
Source File: SpringDIProcessorTest.java    From quarkus with Apache License 2.0 5 votes vote down vote up
private IndexView getIndex(final Class<?>... classes) {
    final Indexer indexer = new Indexer();
    for (final Class<?> clazz : classes) {
        final String className = clazz.getName();
        try (InputStream stream = IoUtil.readClass(getClass().getClassLoader(), className)) {
            final ClassInfo beanInfo = indexer.index(stream);
        } catch (IOException e) {
            throw new IllegalStateException("Failed to index: " + className, e);
        }
    }
    return BeanArchives.buildBeanArchiveIndex(getClass().getClassLoader(), new BeanArchives.PersistentClassIndex(),
            indexer.complete());
}
 
Example #21
Source File: SpringScheduledProcessorTest.java    From quarkus with Apache License 2.0 5 votes vote down vote up
private IndexView getIndex(final Class<?>... classes) {
    final Indexer indexer = new Indexer();
    for (final Class<?> clazz : classes) {
        final String className = clazz.getName();
        try (InputStream stream = IoUtil.readClass(getClass().getClassLoader(), className)) {
            final ClassInfo beanInfo = indexer.index(stream);
        } catch (IOException e) {
            throw new IllegalStateException("Failed to index: " + className, e);
        }
    }
    return BeanArchives.buildBeanArchiveIndex(getClass().getClassLoader(), new BeanArchives.PersistentClassIndex(),
            indexer.complete());
}
 
Example #22
Source File: AdvertisingMetadataProcessorTest.java    From thorntail with Apache License 2.0 5 votes vote down vote up
Index createIndex(Archive<?> archive) throws IOException {
    Indexer indexer = new Indexer();

    Map<ArchivePath, Node> c = archive.getContent();
    for (Map.Entry<ArchivePath, Node> each : c.entrySet()) {
        if (each.getKey().get().endsWith(".class")) {
            indexer.index(each.getValue().getAsset().openStream());
        }
    }


    return indexer.complete();
}
 
Example #23
Source File: MicroOuterDeployer.java    From piranha with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private void addToIndex(String className, ShrinkWrapResource resource, Indexer indexer) {
    try (InputStream classAsStream = resource.getResourceAsStream(className)) {
        indexer.index(classAsStream);
    } catch (IOException ioe) {
        if (LOGGER.isLoggable(WARNING)) {
            LOGGER.log(WARNING, "Unable to add to index", ioe);
        }
    }
}
 
Example #24
Source File: MicroOuterDeployer.java    From piranha with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private byte[] createIndex(ShrinkWrapResource applicationResource, List<ShrinkWrapResource> libResources) {
    Indexer indexer = new Indexer();
    
    // Add all classes from the library resources (the jar files in WEB-INF/lib)
    libResources
        .stream()
        .forEach(libResource -> 
            libResource.getAllLocations()
                       .filter(e -> e.endsWith(".class"))
                       .forEach(className -> addToIndex(className, libResource, indexer)));
    
    
    // Add all classes from the application resource (the class files in WEB-INF/classes to the indexer)
    // Note this must be done last as according to the Servlet spec, WEB-INF/classes overrides WEB-INF/lib)
    applicationResource
        .getAllLocations()
        .filter(e -> e.endsWith(".class"))
        .forEach(className -> addToIndex(className, applicationResource, indexer));
    
    
    Index index = indexer.complete();
    
    // Write the index out to a byte array
    
    ByteArrayOutputStream indexBytes = new ByteArrayOutputStream();
    
    IndexWriter writer = new IndexWriter(indexBytes);
    
    try {
        writer.write(index);
    } catch (IOException ioe) {
        if (LOGGER.isLoggable(WARNING)) {
            LOGGER.log(WARNING, "Unable to write out index", ioe);
        }
    }
    
    return indexBytes.toByteArray();
}
 
Example #25
Source File: ApplicationArchiveBuildStep.java    From quarkus with Apache License 2.0 5 votes vote down vote up
private static Index indexFilePath(Path path) throws IOException {
    Indexer indexer = new Indexer();
    try (Stream<Path> stream = Files.walk(path)) {
        stream.forEach(path1 -> {
            if (path1.toString().endsWith(".class")) {
                try (FileInputStream in = new FileInputStream(path1.toFile())) {
                    indexer.index(in);
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        });
    }
    return indexer.complete();
}
 
Example #26
Source File: ApplicationArchiveBuildStep.java    From quarkus with Apache License 2.0 5 votes vote down vote up
private static Index indexJar(JarFile file) throws IOException {
    Indexer indexer = new Indexer();
    Enumeration<JarEntry> e = file.entries();
    while (e.hasMoreElements()) {
        JarEntry entry = e.nextElement();
        if (entry.getName().endsWith(".class")) {
            try (InputStream inputStream = file.getInputStream(entry)) {
                indexer.index(inputStream);
            }
        }
    }
    return indexer.complete();
}
 
Example #27
Source File: JaxRsAnnotationScannerTest.java    From smallrye-open-api with Apache License 2.0 5 votes vote down vote up
@Test
public void testHiddenOperationPathNotPresent() throws IOException, JSONException {
    Indexer indexer = new Indexer();

    // Test samples
    index(indexer, "test/io/smallrye/openapi/runtime/scanner/resources/HiddenOperationResource.class");

    OpenApiAnnotationScanner scanner = new OpenApiAnnotationScanner(emptyConfig(), indexer.complete());

    OpenAPI result = scanner.scan();

    printToConsole(result);
    assertJsonEquals("resource.testHiddenOperationPathNotPresent.json", result);
}
 
Example #28
Source File: IndexInitializer.java    From smallrye-graphql with Apache License 2.0 5 votes vote down vote up
private void processJar(InputStream inputStream, Indexer indexer) throws IOException {

        ZipInputStream zis = new ZipInputStream(inputStream, StandardCharsets.UTF_8);
        ZipEntry ze;

        while ((ze = zis.getNextEntry()) != null) {
            String entryName = ze.getName();
            processFile(entryName, zis, indexer);
        }
    }
 
Example #29
Source File: IndexInitializer.java    From smallrye-graphql with Apache License 2.0 5 votes vote down vote up
private void processFile(String fileName, InputStream is, Indexer indexer) throws IOException {
    if (fileName.endsWith(DOT_CLASS)) {
        SmallRyeGraphQLServletLogging.log.processingFile(fileName);
        indexer.index(is);
    } else if (fileName.endsWith(DOT_WAR)) {
        // necessary because of the thorntail arquillian adapter
        processJar(is, indexer);
    }
}
 
Example #30
Source File: SchemaBuilderTest.java    From smallrye-graphql with Apache License 2.0 5 votes vote down vote up
static IndexView getTCKIndex() {
    Indexer indexer = new Indexer();
    indexDirectory(indexer, "org/eclipse/microprofile/graphql/tck/apps/basic/api");
    indexDirectory(indexer, "org/eclipse/microprofile/graphql/tck/apps/superhero/api");
    indexDirectory(indexer, "org/eclipse/microprofile/graphql/tck/apps/superhero/db");
    indexDirectory(indexer, "org/eclipse/microprofile/graphql/tck/apps/superhero/model");
    return indexer.complete();
}