Java Code Examples for org.jboss.jandex.AnnotationTarget.Kind#CLASS

The following examples show how to use org.jboss.jandex.AnnotationTarget.Kind#CLASS . 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: BuildTimeEnabledProcessor.java    From quarkus with Apache License 2.0 5 votes vote down vote up
private void disableBean(AnnotationTarget target, Transformation transform) {
    if (target.kind() == Kind.CLASS) {
        // Veto the class
        transform.add(DotNames.VETOED);
    } else {
        // Add @Alternative to the producer
        transform.add(DotNames.ALTERNATIVE);
    }
}
 
Example 2
Source File: SmallRyeFaultToleranceProcessor.java    From quarkus with Apache License 2.0 5 votes vote down vote up
@BuildStep
AnnotationsTransformerBuildItem transformInterceptorPriority(BeanArchiveIndexBuildItem index) {
    return new AnnotationsTransformerBuildItem(new AnnotationsTransformer() {
        @Override
        public boolean appliesTo(Kind kind) {
            return kind == Kind.CLASS;
        }

        @Override
        public void transform(TransformationContext ctx) {
            if (ctx.isClass()) {
                if (!ctx.getTarget().asClass().name().toString()
                        .equals("io.smallrye.faulttolerance.FaultToleranceInterceptor")) {
                    return;
                }
                final Config config = ConfigProvider.getConfig();

                OptionalInt priority = config.getValue("mp.fault.tolerance.interceptor.priority", OptionalInt.class);
                if (priority.isPresent()) {
                    ctx.transform()
                            .remove(ann -> ann.name().toString().equals(Priority.class.getName()))
                            .add(Priority.class, AnnotationValue.createIntegerValue("value", priority.getAsInt()))
                            .done();
                }
            }
        }
    });
}
 
Example 3
Source File: SmallRyeMetricsProcessor.java    From quarkus with Apache License 2.0 5 votes vote down vote up
@BuildStep
AnnotationsTransformerBuildItem annotationTransformers() {
    // attach @MetricsBinding to each class that contains any metric annotations
    return new AnnotationsTransformerBuildItem(new AnnotationsTransformer() {

        @Override
        public boolean appliesTo(Kind kind) {
            return kind == Kind.CLASS;
        }

        @Override
        public void transform(TransformationContext context) {
            // skip classes in package io.smallrye.metrics.interceptors
            ClassInfo clazz = context.getTarget().asClass();
            if (clazz.name().toString()
                    .startsWith(io.smallrye.metrics.interceptors.MetricsInterceptor.class.getPackage().getName())) {
                return;
            }
            if (clazz.annotations().containsKey(GAUGE)) {
                BuiltinScope beanScope = BuiltinScope.from(clazz);
                if (!isJaxRsEndpoint(clazz) && beanScope != null &&
                        !beanScope.equals(BuiltinScope.APPLICATION) &&
                        !beanScope.equals(BuiltinScope.SINGLETON)) {
                    LOGGER.warnf("Bean %s declares a org.eclipse.microprofile.metrics.annotation.Gauge " +
                            "but is of a scope that typically " +
                            "creates multiple instances. Gauges are forbidden on beans " +
                            "that create multiple instances, this will cause errors " +
                            "when constructing them. Please use annotated gauges only in beans with " +
                            "@ApplicationScoped or @Singleton scopes, or in JAX-RS endpoints.",
                            clazz.name().toString());
                }
                context.transform().add(MetricsBinding.class).done();
            }
        }

    });
}
 
Example 4
Source File: AnnotationsTransformer.java    From quarkus with Apache License 2.0 4 votes vote down vote up
default boolean isClass() {
    return getTarget().kind() == Kind.CLASS;
}
 
Example 5
Source File: AnnotationsTransformerTest.java    From quarkus with Apache License 2.0 4 votes vote down vote up
@Override
public boolean appliesTo(Kind kind) {
    return kind == Kind.CLASS || kind == Kind.FIELD;
}
 
Example 6
Source File: SmallRyeMetricsProcessor.java    From quarkus with Apache License 2.0 4 votes vote down vote up
@BuildStep
AnnotationsTransformerBuildItem transformBeanScope(BeanArchiveIndexBuildItem index,
        CustomScopeAnnotationsBuildItem scopes) {
    return new AnnotationsTransformerBuildItem(new AnnotationsTransformer() {

        @Override
        public int getPriority() {
            // this specifically should run after the JAX-RS AnnotationTransformers
            return BuildExtension.DEFAULT_PRIORITY - 100;
        }

        @Override
        public boolean appliesTo(AnnotationTarget.Kind kind) {
            return kind == Kind.CLASS;
        }

        @Override
        public void transform(TransformationContext ctx) {
            if (scopes.isScopeIn(ctx.getAnnotations())) {
                return;
            }
            ClassInfo clazz = ctx.getTarget().asClass();
            if (!isJaxRsEndpoint(clazz) && !isJaxRsProvider(clazz)) {
                while (clazz != null && clazz.superName() != null) {
                    Map<DotName, List<AnnotationInstance>> annotations = clazz.annotations();
                    if (annotations.containsKey(GAUGE)
                            || annotations.containsKey(SmallRyeMetricsDotNames.CONCURRENT_GAUGE)
                            || annotations.containsKey(SmallRyeMetricsDotNames.COUNTED)
                            || annotations.containsKey(SmallRyeMetricsDotNames.METERED)
                            || annotations.containsKey(SmallRyeMetricsDotNames.SIMPLY_TIMED)
                            || annotations.containsKey(SmallRyeMetricsDotNames.TIMED)
                            || annotations.containsKey(SmallRyeMetricsDotNames.METRIC)) {
                        LOGGER.debugf(
                                "Found metrics business methods on a class %s with no scope defined - adding @Dependent",
                                ctx.getTarget());
                        ctx.transform().add(Dependent.class).done();
                        break;
                    }
                    clazz = index.getIndex().getClassByName(clazz.superName());
                }
            }
        }
    });
}
 
Example 7
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 8
Source File: SmallRyeHealthProcessor.java    From quarkus with Apache License 2.0 4 votes vote down vote up
@BuildStep
AnnotationsTransformerBuildItem annotationTransformer(BeanArchiveIndexBuildItem beanArchiveIndex,
        CustomScopeAnnotationsBuildItem scopes) {
    // Transform health checks that are not annotated with a scope or a stereotype
    Set<DotName> stereotypes = beanArchiveIndex.getIndex().getAnnotations(DotNames.STEREOTYPE).stream()
            .map(AnnotationInstance::name).collect(Collectors.toSet());
    List<DotName> healthAnnotations = new ArrayList<>(5);
    healthAnnotations.add(HEALTH);
    healthAnnotations.add(LIVENESS);
    healthAnnotations.add(READINESS);
    healthAnnotations.add(HEALTH_GROUP);
    healthAnnotations.add(HEALTH_GROUPS);

    return new AnnotationsTransformerBuildItem(new AnnotationsTransformer() {

        @Override
        public boolean appliesTo(Kind kind) {
            return kind == Kind.CLASS || kind == Kind.METHOD;
        }

        @Override
        public void transform(TransformationContext ctx) {
            if (ctx.getAnnotations().isEmpty()) {
                return;
            }
            Collection<AnnotationInstance> annotations;
            if (ctx.isClass()) {
                annotations = ctx.getAnnotations();
                if (containsAny(annotations, stereotypes)) {
                    return;
                }
            } else {
                annotations = getAnnotations(Kind.METHOD, ctx.getAnnotations());
            }
            if (scopes.isScopeIn(annotations)) {
                return;
            }
            if (containsAny(annotations, healthAnnotations)) {
                ctx.transform().add(BuiltinScope.SINGLETON.getName()).done();
            }
        }

    });
}