Java Code Examples for org.jboss.jandex.IndexView#getAnnotations()

The following examples show how to use org.jboss.jandex.IndexView#getAnnotations() . 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: TestProcessor.java    From quarkus with Apache License 2.0 6 votes vote down vote up
/**
 * Collect the beans with our custom bean defining annotation and configure them with the runtime config
 *
 * @param recorder - runtime recorder
 * @param beanArchiveIndex - index of type information
 * @param testBeanProducer - producer for located Class<IConfigConsumer> bean types
 */
@BuildStep
@Record(STATIC_INIT)
void scanForBeans(TestRecorder recorder, BeanArchiveIndexBuildItem beanArchiveIndex,
        BuildProducer<TestBeanBuildItem> testBeanProducer) {
    IndexView indexView = beanArchiveIndex.getIndex();
    Collection<AnnotationInstance> testBeans = indexView.getAnnotations(TEST_ANNOTATION);
    for (AnnotationInstance ann : testBeans) {
        ClassInfo beanClassInfo = ann.target().asClass();
        try {
            boolean isConfigConsumer = beanClassInfo.interfaceNames()
                    .stream()
                    .anyMatch(dotName -> dotName.equals(DotName.createSimple(IConfigConsumer.class.getName())));
            if (isConfigConsumer) {
                Class<IConfigConsumer> beanClass = (Class<IConfigConsumer>) Class.forName(beanClassInfo.name().toString(),
                        true, Thread.currentThread().getContextClassLoader());
                testBeanProducer.produce(new TestBeanBuildItem(beanClass));
                log.infof("The configured bean: %s", beanClass);
            }
        } catch (ClassNotFoundException e) {
            log.warn("Failed to load bean class", e);
        }
    }
}
 
Example 2
Source File: GoogleDriveProcessor.java    From camel-quarkus with Apache License 2.0 6 votes vote down vote up
@BuildStep
void registerForReflection(BuildProducer<ReflectiveClassBuildItem> reflectiveClass,
        BuildProducer<UnbannedReflectiveBuildItem> unbannedClass, CombinedIndexBuildItem combinedIndex) {
    IndexView index = combinedIndex.getIndex();

    // Google drive component configuration class reflection
    Collection<AnnotationInstance> uriParams = index
            .getAnnotations(DotName.createSimple("org.apache.camel.spi.UriParams"));

    String[] googleDriveConfigClasses = uriParams.stream()
            .map(annotation -> annotation.target())
            .filter(annotationTarget -> annotationTarget.kind().equals(AnnotationTarget.Kind.CLASS))
            .map(annotationTarget -> annotationTarget.asClass().name().toString())
            .filter(className -> className.startsWith("org.apache.camel.component.google.drive"))
            .toArray(String[]::new);

    reflectiveClass.produce(new ReflectiveClassBuildItem(true, true, googleDriveConfigClasses));
    unbannedClass.produce(new UnbannedReflectiveBuildItem(googleDriveConfigClasses));
}
 
Example 3
Source File: GoogleSheetsProcessor.java    From camel-quarkus with Apache License 2.0 6 votes vote down vote up
@BuildStep
void registerForReflection(BuildProducer<ReflectiveClassBuildItem> reflectiveClass,
        BuildProducer<UnbannedReflectiveBuildItem> unbannedClass, CombinedIndexBuildItem combinedIndex) {
    IndexView index = combinedIndex.getIndex();

    // Google sheets component configuration class reflection
    Collection<AnnotationInstance> uriParams = index
            .getAnnotations(DotName.createSimple("org.apache.camel.spi.UriParams"));

    String[] googleMailConfigClasses = uriParams.stream()
            .map(annotation -> annotation.target())
            .filter(annotationTarget -> annotationTarget.kind().equals(AnnotationTarget.Kind.CLASS))
            .map(annotationTarget -> annotationTarget.asClass().name().toString())
            .filter(className -> className.startsWith("org.apache.camel.component.google.sheets"))
            .toArray(String[]::new);

    reflectiveClass.produce(new ReflectiveClassBuildItem(true, true, googleMailConfigClasses));
    unbannedClass.produce(new UnbannedReflectiveBuildItem(googleMailConfigClasses));
}
 
Example 4
Source File: JpaJandexScavenger.java    From quarkus with Apache License 2.0 6 votes vote down vote up
private void enlistJPAModelClasses(IndexView index, JpaEntitiesBuildItem domainObjectCollector,
        Set<String> enumTypeCollector, Set<String> javaTypeCollector, DotName dotName, Set<DotName> unindexedClasses) {
    Collection<AnnotationInstance> jpaAnnotations = index.getAnnotations(dotName);

    if (jpaAnnotations == null) {
        return;
    }

    for (AnnotationInstance annotation : jpaAnnotations) {
        ClassInfo klass = annotation.target().asClass();
        DotName targetDotName = klass.name();
        // ignore non-jpa model classes that we think belong to JPA
        if (nonJpaModelClasses.contains(targetDotName.toString())) {
            continue;
        }
        addClassHierarchyToReflectiveList(index, domainObjectCollector, enumTypeCollector, javaTypeCollector, targetDotName,
                unindexedClasses);
        collectDomainObject(domainObjectCollector, klass);
    }
}
 
Example 5
Source File: HibernateOrmProcessor.java    From quarkus with Apache License 2.0 6 votes vote down vote up
private boolean isUserDefinedProducerMissing(IndexView index, DotName annotationName) {
    for (AnnotationInstance annotationInstance : index.getAnnotations(annotationName)) {
        if (annotationInstance.target().kind() == AnnotationTarget.Kind.METHOD) {
            if (annotationInstance.target().asMethod().hasAnnotation(PRODUCES)) {
                return false;
            }
        } else if (annotationInstance.target().kind() == AnnotationTarget.Kind.FIELD) {
            for (AnnotationInstance i : annotationInstance.target().asField().annotations()) {
                if (i.name().equals(PRODUCES)) {
                    return false;
                }
            }
        }
    }
    return true;
}
 
Example 6
Source File: SpringWebProcessor.java    From quarkus with Apache License 2.0 5 votes vote down vote up
private AnnotationInstance getSingleControllerAdviceInstance(IndexView index) {
    Collection<AnnotationInstance> controllerAdviceInstances = index.getAnnotations(REST_CONTROLLER_ADVICE);

    if (controllerAdviceInstances.isEmpty()) {
        return null;
    }

    if (controllerAdviceInstances.size() > 1) {
        throw new IllegalStateException("You can only have a single class annotated with @ControllerAdvice");
    }

    return controllerAdviceInstances.iterator().next();
}
 
Example 7
Source File: ConstructorPropertiesProcessor.java    From quarkus with Apache License 2.0 5 votes vote down vote up
@BuildStep
void build(BuildProducer<ReflectiveClassBuildItem> reflectiveClass, CombinedIndexBuildItem indexBuildItem) {
    IndexView index = indexBuildItem.getIndex();
    for (AnnotationInstance annotationInstance : index.getAnnotations(CONSTRUCTOR_PROPERTIES)) {
        registerInstance(reflectiveClass, annotationInstance);
    }
}
 
Example 8
Source File: SpringWebProcessor.java    From quarkus with Apache License 2.0 5 votes vote down vote up
@BuildStep
public void process(BeanArchiveIndexBuildItem beanArchiveIndexBuildItem,
        BuildProducer<ReflectiveClassBuildItem> reflectiveClass,
        BuildProducer<ServletInitParamBuildItem> initParamProducer,
        BuildProducer<ResteasyDeploymentCustomizerBuildItem> deploymentCustomizerProducer) {

    validateControllers(beanArchiveIndexBuildItem);

    final IndexView index = beanArchiveIndexBuildItem.getIndex();
    final Collection<AnnotationInstance> annotations = index.getAnnotations(REST_CONTROLLER_ANNOTATION);
    if (annotations.isEmpty()) {
        return;
    }

    final Set<String> classNames = new HashSet<>();
    for (AnnotationInstance annotation : annotations) {
        classNames.add(annotation.target().asClass().toString());
    }

    // initialize the init params that will be used in case of servlet
    initParamProducer.produce(
            new ServletInitParamBuildItem(
                    ResteasyContextParameters.RESTEASY_SCANNED_RESOURCE_CLASSES_WITH_BUILDER,
                    SpringResourceBuilder.class.getName() + ":" + String.join(",", classNames)));
    // customize the deployment that will be used in case of RESTEasy standalone
    deploymentCustomizerProducer.produce(new ResteasyDeploymentCustomizerBuildItem(new Consumer<ResteasyDeployment>() {
        @Override
        public void accept(ResteasyDeployment resteasyDeployment) {
            resteasyDeployment.getScannedResourceClassesWithBuilder().put(SpringResourceBuilder.class.getName(),
                    new ArrayList<>(classNames));
        }
    }));

    reflectiveClass.produce(new ReflectiveClassBuildItem(true, false, false, SpringResourceBuilder.class.getName()));
}
 
Example 9
Source File: RestClientProcessor.java    From quarkus with Apache License 2.0 5 votes vote down vote up
private void findInterfaces(IndexView index, Map<DotName, ClassInfo> interfaces, Set<Type> returnTypes,
        DotName annotationToFind) {
    for (AnnotationInstance annotation : index.getAnnotations(annotationToFind)) {
        AnnotationTarget target = annotation.target();
        ClassInfo theInfo;
        if (target.kind() == AnnotationTarget.Kind.CLASS) {
            theInfo = target.asClass();
        } else if (target.kind() == AnnotationTarget.Kind.METHOD) {
            theInfo = target.asMethod().declaringClass();
        } else {
            continue;
        }

        if (!isRestClientInterface(index, theInfo)) {
            continue;
        }

        interfaces.put(theInfo.name(), theInfo);

        // Find Return types
        processInterfaceReturnTypes(theInfo, returnTypes);
        for (Type interfaceType : theInfo.interfaceTypes()) {
            ClassInfo interfaceClassInfo = index.getClassByName(interfaceType.name());
            if (interfaceClassInfo != null) {
                processInterfaceReturnTypes(interfaceClassInfo, returnTypes);
            }
        }
    }
}
 
Example 10
Source File: HibernateUserTypeProcessor.java    From quarkus with Apache License 2.0 5 votes vote down vote up
@BuildStep
public void build(BuildProducer<ReflectiveClassBuildItem> reflectiveClass, CombinedIndexBuildItem combinedIndexBuildItem) {
    IndexView index = combinedIndexBuildItem.getIndex();

    final Set<String> userTypes = new HashSet<>();

    Collection<AnnotationInstance> typeAnnotationInstances = index.getAnnotations(TYPE);
    Collection<AnnotationInstance> typeDefinitionAnnotationInstances = index.getAnnotations(TYPE_DEFINITION);
    Collection<AnnotationInstance> typeDefinitionsAnnotationInstances = index.getAnnotations(TYPE_DEFINITIONS);

    userTypes.addAll(getUserTypes(typeDefinitionAnnotationInstances));

    for (AnnotationInstance typeDefinitionAnnotationInstance : typeDefinitionsAnnotationInstances) {
        final AnnotationValue typeDefinitionsAnnotationValue = typeDefinitionAnnotationInstance.value();

        if (typeDefinitionsAnnotationValue == null) {
            continue;
        }

        userTypes.addAll(getUserTypes(Arrays.asList(typeDefinitionsAnnotationValue.asNestedArray())));
    }

    for (AnnotationInstance typeAnnotationInstance : typeAnnotationInstances) {
        final AnnotationValue typeValue = typeAnnotationInstance.value(TYPE_VALUE);
        if (typeValue == null) {
            continue;
        }

        final String type = typeValue.asString();
        final DotName className = DotName.createSimple(type);
        if (index.getClassByName(className) == null) {
            continue; // Either already registered through TypeDef annotation scanning or not present in the index
        }
        userTypes.add(type);
    }

    if (!userTypes.isEmpty()) {
        reflectiveClass.produce(new ReflectiveClassBuildItem(true, true, userTypes.toArray(new String[] {})));
    }
}
 
Example 11
Source File: ResteasyServerCommonProcessor.java    From quarkus with Apache License 2.0 5 votes vote down vote up
private static void checkParameterNames(IndexView index,
        List<AdditionalJaxRsResourceMethodParamAnnotations> additionalJaxRsResourceMethodParamAnnotations) {

    final List<DotName> methodParameterAnnotations = new ArrayList<>(RESTEASY_PARAM_ANNOTATIONS.length);
    methodParameterAnnotations.addAll(Arrays.asList(RESTEASY_PARAM_ANNOTATIONS));
    for (AdditionalJaxRsResourceMethodParamAnnotations annotations : additionalJaxRsResourceMethodParamAnnotations) {
        methodParameterAnnotations.addAll(annotations.getAnnotationClasses());
    }

    OUTER: for (DotName annotationType : methodParameterAnnotations) {
        Collection<AnnotationInstance> instances = index.getAnnotations(annotationType);
        for (AnnotationInstance instance : instances) {
            // we only care about method parameters, because properties or fields always work
            if (instance.target().kind() != Kind.METHOD_PARAMETER) {
                continue;
            }
            MethodParameterInfo param = instance.target().asMethodParameter();
            if (param.name() == null) {
                log.warnv(
                        "Detected RESTEasy annotation {0} on method parameter {1}.{2} with no name. Either specify its name,"
                                + " or tell your compiler to enable debug info (-g) or parameter names (-parameters). This message is only"
                                + " logged for the first such parameter.",
                        instance.name(),
                        param.method().declaringClass(), param.method().name());
                break OUTER;
            }
        }
    }
}
 
Example 12
Source File: JsonbProcessor.java    From quarkus with Apache License 2.0 5 votes vote down vote up
@BuildStep
void build(BuildProducer<ReflectiveClassBuildItem> reflectiveClass,
        BuildProducer<NativeImageResourceBundleBuildItem> resourceBundle,
        BuildProducer<ServiceProviderBuildItem> serviceProvider,
        BuildProducer<AdditionalBeanBuildItem> additionalBeans,
        CombinedIndexBuildItem combinedIndexBuildItem) {
    reflectiveClass.produce(new ReflectiveClassBuildItem(false, false,
            JsonBindingProvider.class.getName()));

    resourceBundle.produce(new NativeImageResourceBundleBuildItem("yasson-messages"));

    serviceProvider.produce(new ServiceProviderBuildItem(JsonbComponentInstanceCreator.class.getName(),
            QuarkusJsonbComponentInstanceCreator.class.getName()));

    // this needs to be registered manually since the runtime module is not indexed by Jandex
    additionalBeans.produce(new AdditionalBeanBuildItem(JsonbProducer.class));

    IndexView index = combinedIndexBuildItem.getIndex();

    // handle the various @JsonSerialize cases
    for (AnnotationInstance serializeInstance : index.getAnnotations(JSONB_TYPE_SERIALIZER)) {
        registerInstance(reflectiveClass, serializeInstance);
    }

    // handle the various @JsonDeserialize cases
    for (AnnotationInstance deserializeInstance : index.getAnnotations(JSONB_TYPE_DESERIALIZER)) {
        registerInstance(reflectiveClass, deserializeInstance);
    }
}
 
Example 13
Source File: BeanDeployment.java    From quarkus with Apache License 2.0 5 votes vote down vote up
private static Map<DotName, ClassInfo> findInterceptorBindings(IndexView index) {
    Map<DotName, ClassInfo> bindings = new HashMap<>();
    // Note: doesn't use AnnotationStore, this will operate on classes without applying annotation transformers
    for (AnnotationInstance binding : index.getAnnotations(DotNames.INTERCEPTOR_BINDING)) {
        bindings.put(binding.target().asClass().name(), binding.target().asClass());
    }
    return bindings;
}
 
Example 14
Source File: BeanDeployment.java    From quarkus with Apache License 2.0 5 votes vote down vote up
private static Map<DotName, ClassInfo> findQualifiers(IndexView index) {
    Map<DotName, ClassInfo> qualifiers = new HashMap<>();
    for (AnnotationInstance qualifier : index.getAnnotations(DotNames.QUALIFIER)) {
        qualifiers.put(qualifier.target().asClass().name(), qualifier.target().asClass());
    }
    return qualifiers;
}
 
Example 15
Source File: ResteasyServerCommonProcessor.java    From quarkus with Apache License 2.0 5 votes vote down vote up
private static void registerReflectionForSerialization(BuildProducer<ReflectiveClassBuildItem> reflectiveClass,
        BuildProducer<ReflectiveHierarchyBuildItem> reflectiveHierarchy,
        CombinedIndexBuildItem combinedIndexBuildItem,
        BeanArchiveIndexBuildItem beanArchiveIndexBuildItem,
        List<AdditionalJaxRsResourceMethodAnnotationsBuildItem> additionalJaxRsResourceMethodAnnotations) {
    IndexView index = combinedIndexBuildItem.getIndex();
    IndexView beanArchiveIndex = beanArchiveIndexBuildItem.getIndex();

    // This is probably redundant with the automatic resolution we do just below but better be safe
    for (AnnotationInstance annotation : index.getAnnotations(JSONB_ANNOTATION)) {
        if (annotation.target().kind() == AnnotationTarget.Kind.CLASS) {
            reflectiveClass
                    .produce(new ReflectiveClassBuildItem(true, true, annotation.target().asClass().name().toString()));
        }
    }

    final List<DotName> annotations = new ArrayList<>(METHOD_ANNOTATIONS.length);
    annotations.addAll(Arrays.asList(METHOD_ANNOTATIONS));
    for (AdditionalJaxRsResourceMethodAnnotationsBuildItem additionalJaxRsResourceMethodAnnotation : additionalJaxRsResourceMethodAnnotations) {
        annotations.addAll(additionalJaxRsResourceMethodAnnotation.getAnnotationClasses());
    }

    // Declare reflection for all the types implicated in the Rest end points (return types and parameters).
    // It might be needed for serialization.
    for (DotName annotationType : annotations) {
        scanMethodParameters(annotationType, reflectiveHierarchy, index);
        scanMethodParameters(annotationType, reflectiveHierarchy, beanArchiveIndex);
    }

    // In the case of a constraint violation, these elements might be returned as entities and will be serialized
    reflectiveClass.produce(new ReflectiveClassBuildItem(true, true, ViolationReport.class.getName()));
    reflectiveClass.produce(new ReflectiveClassBuildItem(true, true, ResteasyConstraintViolation.class.getName()));
}
 
Example 16
Source File: TestResourceManager.java    From quarkus with Apache License 2.0 5 votes vote down vote up
private Collection<AnnotationInstance> findQuarkusTestResourceInstances(IndexView index) {
    Set<AnnotationInstance> testResourceAnnotations = new HashSet<>(index
            .getAnnotations(DotName.createSimple(QuarkusTestResource.class.getName())));
    for (AnnotationInstance annotation : index
            .getAnnotations(DotName.createSimple(QuarkusTestResource.List.class.getName()))) {
        Collections.addAll(testResourceAnnotations, annotation.value().asNestedArray());
    }
    return testResourceAnnotations;
}
 
Example 17
Source File: HibernateSearchElasticsearchProcessor.java    From quarkus with Apache License 2.0 4 votes vote down vote up
private void registerReflection(IndexView index, BuildProducer<ReflectiveClassBuildItem> reflectiveClass,
        BuildProducer<ReflectiveHierarchyBuildItem> reflectiveHierarchy) {
    Set<DotName> reflectiveClassCollector = new HashSet<>();

    if (buildTimeConfig.defaultBackend.analysis.configurer.isPresent()) {
        reflectiveClass.produce(
                new ReflectiveClassBuildItem(true, false, buildTimeConfig.defaultBackend.analysis.configurer.get()));
    }

    if (buildTimeConfig.defaultBackend.layout.strategy.isPresent()) {
        reflectiveClass.produce(
                new ReflectiveClassBuildItem(true, false, buildTimeConfig.defaultBackend.layout.strategy.get()));
    }

    if (buildTimeConfig.backgroundFailureHandler.isPresent()) {
        reflectiveClass.produce(
                new ReflectiveClassBuildItem(true, false, buildTimeConfig.backgroundFailureHandler.get()));
    }

    Set<Type> reflectiveHierarchyCollector = new HashSet<>();

    for (AnnotationInstance propertyMappingMetaAnnotationInstance : index
            .getAnnotations(PROPERTY_MAPPING_META_ANNOTATION)) {
        for (AnnotationInstance propertyMappingAnnotationInstance : index
                .getAnnotations(propertyMappingMetaAnnotationInstance.name())) {
            AnnotationTarget annotationTarget = propertyMappingAnnotationInstance.target();
            if (annotationTarget.kind() == Kind.FIELD) {
                FieldInfo fieldInfo = annotationTarget.asField();
                addReflectiveClass(index, reflectiveClassCollector, reflectiveHierarchyCollector,
                        fieldInfo.declaringClass());
                addReflectiveType(index, reflectiveClassCollector, reflectiveHierarchyCollector,
                        fieldInfo.type());
            } else if (annotationTarget.kind() == Kind.METHOD) {
                MethodInfo methodInfo = annotationTarget.asMethod();
                addReflectiveClass(index, reflectiveClassCollector, reflectiveHierarchyCollector,
                        methodInfo.declaringClass());
                addReflectiveType(index, reflectiveClassCollector, reflectiveHierarchyCollector,
                        methodInfo.returnType());
            }
        }
    }

    for (AnnotationInstance typeBridgeMappingInstance : index.getAnnotations(TYPE_MAPPING_META_ANNOTATION)) {
        for (AnnotationInstance typeBridgeInstance : index.getAnnotations(typeBridgeMappingInstance.name())) {
            addReflectiveClass(index, reflectiveClassCollector, reflectiveHierarchyCollector,
                    typeBridgeInstance.target().asClass());
        }
    }

    reflectiveClassCollector.addAll(GSON_CLASSES);

    String[] reflectiveClasses = reflectiveClassCollector.stream().map(DotName::toString).toArray(String[]::new);
    reflectiveClass.produce(new ReflectiveClassBuildItem(true, true, reflectiveClasses));

    for (Type reflectiveHierarchyType : reflectiveHierarchyCollector) {
        reflectiveHierarchy.produce(new ReflectiveHierarchyBuildItem(reflectiveHierarchyType));
    }
}
 
Example 18
Source File: EventBusCodecProcessor.java    From quarkus with Apache License 2.0 4 votes vote down vote up
@BuildStep
public void registerCodecs(
        BeanArchiveIndexBuildItem beanArchiveIndexBuildItem,
        BuildProducer<MessageCodecBuildItem> messageCodecs) {

    final IndexView index = beanArchiveIndexBuildItem.getIndex();
    Collection<AnnotationInstance> consumeEventAnnotationInstances = index.getAnnotations(CONSUME_EVENT);
    Map<Type, DotName> codecByTypes = new HashMap<>();
    for (AnnotationInstance consumeEventAnnotationInstance : consumeEventAnnotationInstances) {
        AnnotationTarget typeTarget = consumeEventAnnotationInstance.target();
        if (typeTarget.kind() != AnnotationTarget.Kind.METHOD) {
            throw new UnsupportedOperationException("@ConsumeEvent annotation must target a method");
        }

        MethodInfo method = typeTarget.asMethod();
        Type codecTargetFromReturnType = extractPayloadTypeFromReturn(method);
        Type codecTargetFromParameter = extractPayloadTypeFromParameter(method);

        // If the @ConsumeEvent set the codec, use this codec. It applies to the parameter
        AnnotationValue codec = consumeEventAnnotationInstance.value("codec");
        if (codec != null && codec.asClass().kind() == Type.Kind.CLASS) {
            if (codecTargetFromParameter == null) {
                throw new IllegalStateException("Invalid `codec` argument in @ConsumeEvent - no parameter");
            }
            codecByTypes.put(codecTargetFromParameter, codec.asClass().asClassType().name());
        } else if (codecTargetFromParameter != null) {
            // Codec is not set, check if we have a built-in codec
            if (!hasBuiltInCodec(codecTargetFromParameter)) {
                // Ensure local delivery.
                AnnotationValue local = consumeEventAnnotationInstance.value("local");
                if (local != null && !local.asBoolean()) {
                    throw new UnsupportedOperationException(
                            "The generic message codec can only be used for local delivery,"
                                    + ", implement your own event bus codec for " + codecTargetFromParameter.name()
                                            .toString());
                } else if (!codecByTypes.containsKey(codecTargetFromParameter)) {
                    LOGGER.infof("Local Message Codec registered for type %s",
                            codecTargetFromParameter.toString());
                    codecByTypes.put(codecTargetFromParameter, LOCAL_EVENT_BUS_CODEC);
                }
            }
        }

        if (codecTargetFromReturnType != null && !hasBuiltInCodec(codecTargetFromReturnType)
                && !codecByTypes.containsKey(codecTargetFromReturnType)) {

            LOGGER.infof("Local Message Codec registered for type %s", codecTargetFromReturnType.toString());
            codecByTypes.put(codecTargetFromReturnType, LOCAL_EVENT_BUS_CODEC);
        }
    }

    // Produce the build items
    for (Map.Entry<Type, DotName> entry : codecByTypes.entrySet()) {
        messageCodecs.produce(new MessageCodecBuildItem(entry.getKey().toString(), entry.getValue().toString()));
    }

    // Register codec classes for reflection.
    codecByTypes.values().stream().map(DotName::toString).distinct()
            .forEach(name -> reflectiveClass.produce(new ReflectiveClassBuildItem(true, false, name)));
}
 
Example 19
Source File: JaxbProcessor.java    From quarkus with Apache License 2.0 4 votes vote down vote up
@BuildStep
void processAnnotationsAndIndexFiles(
        BuildProducer<NativeImageSystemPropertyBuildItem> nativeImageProps,
        BuildProducer<ServiceProviderBuildItem> providerItem,
        BuildProducer<NativeImageProxyDefinitionBuildItem> proxyDefinitions,
        CombinedIndexBuildItem combinedIndexBuildItem,
        List<JaxbFileRootBuildItem> fileRoots) {

    IndexView index = combinedIndexBuildItem.getIndex();

    // Register classes for reflection based on JAXB annotations
    boolean jaxbRootAnnotationsDetected = false;

    for (DotName jaxbRootAnnotation : JAXB_ROOT_ANNOTATIONS) {
        for (AnnotationInstance jaxbRootAnnotationInstance : index
                .getAnnotations(jaxbRootAnnotation)) {
            if (jaxbRootAnnotationInstance.target().kind() == Kind.CLASS) {
                addReflectiveClass(true, true,
                        jaxbRootAnnotationInstance.target().asClass().name().toString());
                jaxbRootAnnotationsDetected = true;
            }
        }
    }

    if (!jaxbRootAnnotationsDetected && fileRoots.isEmpty()) {
        return;
    }

    // Register package-infos for reflection
    for (AnnotationInstance xmlSchemaInstance : index.getAnnotations(XML_SCHEMA)) {
        if (xmlSchemaInstance.target().kind() == Kind.CLASS) {
            reflectiveClass.produce(
                    new ReflectiveClassBuildItem(false, false, xmlSchemaInstance.target().asClass().name().toString()));
        }
    }

    // Register XML Java type adapters for reflection
    for (AnnotationInstance xmlJavaTypeAdapterInstance : index.getAnnotations(XML_JAVA_TYPE_ADAPTER)) {
        reflectiveClass.produce(
                new ReflectiveClassBuildItem(true, true, xmlJavaTypeAdapterInstance.value().asClass().name().toString()));
    }

    if (!index.getAnnotations(XML_ANY_ELEMENT).isEmpty()) {
        addReflectiveClass(false, false, "javax.xml.bind.annotation.W3CDomHandler");
    }

    JAXB_ANNOTATIONS.stream()
            .map(Class::getName)
            .forEach(className -> {
                proxyDefinitions.produce(new NativeImageProxyDefinitionBuildItem(className, Locatable.class.getName()));
                addReflectiveClass(true, false, className);
            });

    for (JaxbFileRootBuildItem i : fileRoots) {
        try (Stream<Path> stream = iterateResources(i.getFileRoot())) {
            stream.filter(p -> p.getFileName().toString().equals("jaxb.index"))
                    .forEach(this::handleJaxbFile);
        }
    }
}
 
Example 20
Source File: ResteasyServerCommonProcessor.java    From quarkus with Apache License 2.0 4 votes vote down vote up
private Set<DotName> findSubresources(IndexView index, Map<DotName, ClassInfo> scannedResources) {
    // First identify sub-resource candidates
    Set<DotName> subresources = new HashSet<>();
    for (DotName annotation : METHOD_ANNOTATIONS) {
        Collection<AnnotationInstance> annotationInstances = index.getAnnotations(annotation);
        for (AnnotationInstance annotationInstance : annotationInstances) {
            DotName declaringClassName = annotationInstance.target().asMethod().declaringClass().name();
            if (scannedResources.containsKey(declaringClassName)) {
                // Skip resource classes
                continue;
            }
            subresources.add(declaringClassName);
        }
    }
    if (!subresources.isEmpty()) {
        // Collect sub-resource locator return types
        Set<DotName> subresourceLocatorTypes = new HashSet<>();
        for (ClassInfo resourceClass : scannedResources.values()) {
            ClassInfo clazz = resourceClass;
            while (clazz != null) {
                for (MethodInfo method : clazz.methods()) {
                    if (method.hasAnnotation(ResteasyDotNames.PATH)) {
                        subresourceLocatorTypes.add(method.returnType().name());
                    }
                }
                if (clazz.superName().equals(DotNames.OBJECT)) {
                    clazz = null;
                } else {
                    clazz = index.getClassByName(clazz.superName());
                }
            }
        }
        // Remove false positives
        for (Iterator<DotName> iterator = subresources.iterator(); iterator.hasNext();) {
            DotName subresource = iterator.next();
            for (DotName type : subresourceLocatorTypes) {
                // Sub-resource may be a subclass of a locator return type
                if (!subresource.equals(type)
                        && index.getAllKnownSubclasses(type).stream().noneMatch(c -> c.name().equals(subresource))) {
                    iterator.remove();
                    break;
                }
            }
        }
    }
    log.trace("Sub-resources found: " + subresources);
    return subresources;
}