Java Code Examples for org.jboss.jandex.Indexer

The following examples show how to use org.jboss.jandex.Indexer. These examples are extracted from open source projects. 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 Project: smallrye-graphql   Source File: IndexInitializer.java    License: 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 2
Source Project: smallrye-graphql   Source File: IndexInitializer.java    License: 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 3
@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 4
@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
@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 6
Source Project: quarkus   Source File: BeanArchives.java    License: 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 7
Source Project: quarkus   Source File: TestClassIndexer.java    License: 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 8
Source Project: quarkus   Source File: AnnotationProxyProvider.java    License: 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 9
Source Project: quarkus   Source File: CombinedIndexBuildStep.java    License: 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 10
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 11
Source Project: hibernate-demos   Source File: AddIndexPlugin.java    License: 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 12
Source Project: smallrye-graphql   Source File: IndexInitializer.java    License: 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 13
Source Project: smallrye-graphql   Source File: IndexInitializer.java    License: 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 14
Source Project: smallrye-graphql   Source File: SchemaBuilderTest.java    License: 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();
}
 
Example 15
Source Project: smallrye-graphql   Source File: SchemaBuilderTest.java    License: Apache License 2.0 5 votes vote down vote up
public static void indexDirectory(Indexer indexer, String baseDir) {
    InputStream directoryStream = getResourceAsStream(baseDir);
    BufferedReader reader = new BufferedReader(new InputStreamReader(directoryStream));
    reader.lines()
            .filter(resName -> resName.endsWith(".class"))
            .map(resName -> Paths.get(baseDir, resName))
            .forEach(path -> index(indexer, path.toString()));
}
 
Example 16
Source Project: smallrye-graphql   Source File: SchemaBuilderTest.java    License: Apache License 2.0 5 votes vote down vote up
static void index(Indexer indexer, String resName) {
    try {
        InputStream stream = getResourceAsStream(resName);
        indexer.index(stream);
    } catch (IOException ioe) {
        throw new RuntimeException(ioe);
    }
}
 
Example 17
Source Project: smallrye-graphql   Source File: FieldNameTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testFieldNamePriority() {
    Indexer indexer = new Indexer();
    indexDirectory(indexer, "io/smallrye/graphql/schema/creator/fieldnameapp");
    IndexView index = indexer.complete();

    ClassInfo classInfo = index.getClassByName(DotName.createSimple(SomeObjectAnnotatedGetters.class.getName()));
    // @Name
    MethodInfo methodInfo = classInfo.method("getName");
    Annotations annotations = Annotations.getAnnotationsForMethod(methodInfo);
    assertEquals("a", FieldCreator.getFieldName(Direction.OUT, annotations, "name"));

    // @Query
    methodInfo = classInfo.method("getQuery");
    annotations = Annotations.getAnnotationsForMethod(methodInfo);
    assertEquals("d", FieldCreator.getFieldName(Direction.OUT, annotations, "query"));

    // @JsonbProperty
    methodInfo = classInfo.method("getJsonbProperty");
    annotations = Annotations.getAnnotationsForMethod(methodInfo);
    assertEquals("f", FieldCreator.getFieldName(Direction.OUT, annotations, "jsonbProperty"));

    // no annotation
    methodInfo = classInfo.method("getFieldName");
    annotations = Annotations.getAnnotationsForMethod(methodInfo);
    assertEquals("fieldName", FieldCreator.getFieldName(Direction.OUT, annotations, "fieldName"));
}
 
Example 18
Source Project: smallrye-graphql   Source File: IndexCreator.java    License: Apache License 2.0 5 votes vote down vote up
public static Index index(Class<?> clazz) throws IOException {
    final Indexer indexer = new Indexer();
    InputStream stream = IndexCreator.class.getClassLoader()
            .getResourceAsStream(clazz.getName().replace('.', '/') + ".class");
    indexer.index(stream);
    return indexer.complete();
}
 
Example 19
Source Project: helidon-build-tools   Source File: Jar.java    License: Apache License 2.0 5 votes vote down vote up
private Index buildIndex() {
    Log.info("  creating missing index for CDI beans archive %s", this);
    final Indexer indexer = new Indexer();
    classEntries().forEach(entry -> {
        try {
            indexer.index(entry.data());
        } catch (IOException e) {
            Log.warn("  could not index class %s in %s: %s", entry.path(), this, e.getMessage());
        }
    });
    return indexer.complete();
}
 
Example 20
@Test
public void testHiddenOperationNotPresent() throws IOException, JSONException {
    Indexer indexer = new Indexer();

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

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

    OpenAPI result = scanner.scan();

    printToConsole(result);
    assertJsonEquals("resource.testHiddenOperationNotPresent.json", result);
}
 
Example 21
@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 22
@Test
public void testPackageInfoDefinitionScanning() throws IOException, JSONException {
    Indexer indexer = new Indexer();
    index(indexer, "test/io/smallrye/openapi/runtime/scanner/package-info.class");
    index(indexer, "test/io/smallrye/openapi/runtime/scanner/resources/PackageInfoTestApplication.class");
    index(indexer,
            "test/io/smallrye/openapi/runtime/scanner/resources/PackageInfoTestApplication$PackageInfoTestResource.class");
    OpenApiAnnotationScanner scanner = new OpenApiAnnotationScanner(emptyConfig(), indexer.complete());

    OpenAPI result = scanner.scan();

    printToConsole(result);
    assertJsonEquals("resource.testPackageInfoDefinitionScanning.json", result);
}
 
Example 23
@BeforeClass
public static void createIndex() {
    Indexer indexer = new Indexer();

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

    index = indexer.complete();
}
 
Example 24
Source Project: smallrye-open-api   Source File: ArchiveUtil.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Index the ShrinkWrap archive to produce a jandex index.
 * 
 * @param config OpenApiConfig
 * @param archive Shrinkwrap Archive
 * @return indexed classes in Archive
 */
public static IndexView archiveToIndex(OpenApiConfig config, Archive<?> archive) {
    if (archive == null) {
        throw TckMessages.msg.nullArchive();
    }

    Indexer indexer = new Indexer();
    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");
    indexArchive(config, indexer, archive);
    return indexer.complete();
}
 
Example 25
Source Project: smallrye-open-api   Source File: ArchiveUtil.java    License: Apache License 2.0 5 votes vote down vote up
private static void index(Indexer indexer, String resName) {
    ClassLoader cl = OpenApiAnnotationScanner.class.getClassLoader();
    try (InputStream klazzStream = cl.getResourceAsStream(resName)) {
        indexer.index(klazzStream);
    } catch (IOException ioe) {
        throw new UncheckedIOException(ioe);
    }
}
 
Example 26
Source Project: smallrye-open-api   Source File: ArchiveUtil.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Indexes the given archive.
 * 
 * @param config
 * @param indexer
 * @param archive
 */
private static void indexArchive(OpenApiConfig config, Indexer indexer, Archive<?> archive) {
    FilteredIndexView filter = new FilteredIndexView(null, config);
    Map<ArchivePath, Node> c = archive.getContent();
    try {
        for (Map.Entry<ArchivePath, Node> each : c.entrySet()) {
            ArchivePath archivePath = each.getKey();
            if (archivePath.get().endsWith(OpenApiConstants.CLASS_SUFFIX)
                    && acceptClassForScanning(filter, archivePath.get())) {
                try (InputStream contentStream = each.getValue().getAsset().openStream()) {
                    TckLogging.log.indexing(archivePath.get(), archive.getName());
                    indexer.index(contentStream);
                }
                continue;
            }
            if (archivePath.get().endsWith(OpenApiConstants.JAR_SUFFIX)
                    && acceptJarForScanning(config, archivePath.get())) {
                try (InputStream contentStream = each.getValue().getAsset().openStream()) {
                    JavaArchive jarArchive = ShrinkWrap.create(JavaArchive.class, archivePath.get())
                            .as(ZipImporter.class).importFrom(contentStream).as(JavaArchive.class);
                    indexArchive(config, indexer, jarArchive);
                }
                continue;
            }
        }
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
 
Example 27
Source Project: smallrye-open-api   Source File: TypeUtil.java    License: Apache License 2.0 5 votes vote down vote up
private static void index(Indexer indexer, Class<?> klazz) {
    try (InputStream stream = klazz.getResourceAsStream(klazz.getSimpleName() + ".class")) {
        indexer.index(stream);
    } catch (IOException ioe) {
        throw new UncheckedIOException(ioe);
    }
}
 
Example 28
private static void index(Indexer indexer, String resourceName) {
    try (InputStream stream = OpenApiDataObjectScanner.class.getResourceAsStream(resourceName)) {
        indexer.index(stream);
    } catch (IOException ioe) {
        throw new UncheckedIOException(ioe);
    }
}
 
Example 29
Source Project: smallrye-open-api   Source File: IndexScannerTestBase.java    License: Apache License 2.0 5 votes vote down vote up
protected static void indexDirectory(Indexer indexer, String baseDir) {
    InputStream directoryStream = tcclGetResourceAsStream(baseDir);
    BufferedReader reader = new BufferedReader(new InputStreamReader(directoryStream));
    reader.lines()
            .filter(resName -> resName.endsWith(".class"))
            .map(resName -> Paths.get(baseDir, resName)) // e.g. test/io/smallrye/openapi/runtime/scanner/entities/ + Bar.class
            .forEach(path -> index(indexer, path.toString()));
}
 
Example 30
Source Project: smallrye-open-api   Source File: IndexScannerTestBase.java    License: Apache License 2.0 5 votes vote down vote up
public static Index indexOf(Class<?>... classes) {
    Indexer indexer = new Indexer();

    for (Class<?> klazz : classes) {
        index(indexer, pathOf(klazz));
    }

    return indexer.complete();
}