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

The following examples show how to use org.jboss.jandex.AnnotationTarget.Kind#METHOD . 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: ParameterProcessor.java    From smallrye-open-api with Apache License 2.0 6 votes vote down vote up
boolean haveSameAnnotatedTarget(ParameterContext context, AnnotationTarget target, String name) {
    /*
     * Consider names to match if one is unspecified or they are equal.
     */
    boolean nameMatches = (context.name == null || name == null || Objects.equals(context.name, name));

    if (target.equals(context.target)) {
        /*
         * The name must match for annotations on a method because it is
         * ambiguous which parameters is being referenced.
         */
        return nameMatches || target.kind() != Kind.METHOD;
    }

    if (nameMatches && target.kind() == Kind.METHOD && context.target.kind() == Kind.METHOD_PARAMETER) {
        return context.target.asMethodParameter().method().equals(target);
    }

    return false;
}
 
Example 2
Source File: ResteasyServerCommonProcessor.java    From quarkus with Apache License 2.0 6 votes vote down vote up
private static void scanMethodParameters(DotName annotationType,
        BuildProducer<ReflectiveHierarchyBuildItem> reflectiveHierarchy, IndexView index) {
    Collection<AnnotationInstance> instances = index.getAnnotations(annotationType);
    for (AnnotationInstance instance : instances) {
        if (instance.target().kind() != Kind.METHOD) {
            continue;
        }

        MethodInfo method = instance.target().asMethod();
        String source = method.declaringClass() + "[" + method + "]";

        reflectiveHierarchy.produce(new ReflectiveHierarchyBuildItem(method.returnType(), index,
                ResteasyDotNames.IGNORE_FOR_REFLECTION_PREDICATE, source));

        for (short i = 0; i < method.parameters().size(); i++) {
            Type parameterType = method.parameters().get(i);
            if (!hasAnnotation(method, i, ResteasyDotNames.CONTEXT)) {
                reflectiveHierarchy.produce(new ReflectiveHierarchyBuildItem(parameterType, index,
                        ResteasyDotNames.IGNORE_FOR_REFLECTION_PREDICATE, source));
            }
        }
    }
}
 
Example 3
Source File: ParameterProcessor.java    From smallrye-open-api with Apache License 2.0 5 votes vote down vote up
boolean haveSameAnnotatedTarget(ParameterContext context, AnnotationTarget target, String name) {
    boolean nameMatches = Objects.equals(context.name, name);

    if (target.equals(context.target)) {
        return true;
    }

    if (nameMatches && target.kind() == Kind.METHOD && context.target.kind() == Kind.METHOD_PARAMETER) {
        return context.target.asMethodParameter().method().equals(target);
    }

    return false;
}
 
Example 4
Source File: QuteProcessor.java    From quarkus with Apache License 2.0 5 votes vote down vote up
private Type resolveType(AnnotationTarget member, Match match, IndexView index) {
    Type matchType;
    if (member.kind() == Kind.FIELD) {
        matchType = member.asField().type();
    } else if (member.kind() == Kind.METHOD) {
        matchType = member.asMethod().returnType();
    } else {
        throw new IllegalStateException("Unsupported member type: " + member);
    }
    // If needed attempt to resolve the type variables using the declaring type
    if (Types.containsTypeVariable(matchType)) {
        // First get the type closure of the current match type
        Set<Type> closure = Types.getTypeClosure(match.clazz, Types.buildResolvedMap(
                match.getParameterizedTypeArguments(), match.getTypeParameters(),
                new HashMap<>(), index), index);
        DotName declaringClassName = member.kind() == Kind.METHOD ? member.asMethod().declaringClass().name()
                : member.asField().declaringClass().name();
        // Then find the declaring type with resolved type variables
        Type declaringType = closure.stream()
                .filter(t -> t.name().equals(declaringClassName)).findAny()
                .orElse(null);
        if (declaringType != null
                && declaringType.kind() == org.jboss.jandex.Type.Kind.PARAMETERIZED_TYPE) {
            matchType = Types.resolveTypeParam(matchType,
                    Types.buildResolvedMap(declaringType.asParameterizedType().arguments(),
                            index.getClassByName(declaringType.name()).typeParameters(),
                            Collections.emptyMap(),
                            index),
                    index);
        }
    }
    return matchType;
}
 
Example 5
Source File: ParameterProcessor.java    From smallrye-open-api with Apache License 2.0 4 votes vote down vote up
/**
 * Merges MP-OAI {@link Parameter}s and Spring parameters for the same {@link In} and name,
 * and {@link Style}. When overriddenParametersOnly is true, new parameters not already known
 * in {@link #params} will be ignored.
 * 
 * The given {@link ParameterContextKey key} contains:
 * 
 * <ul>
 * <li>the name of the parameter specified by application
 * <li>location, given by {@link org.eclipse.microprofile.openapi.annotations.parameters.Parameter#in @Parameter.in}
 * or implied by the type of Spring annotation used on the target
 * <li>style, the parameter's style, either specified by the application or implied by the parameter's location
 * </ul>
 *
 * @param key the key for the parameter being processed
 * @param oaiParam scanned {@link org.eclipse.microprofile.openapi.annotations.parameters.Parameter @Parameter}
 * @param springParam Meta detail about the Spring Mapping being processed, if found.
 * @param springDefaultValue value read from the mapping defaultValue property
 *        annotation.
 * @param target target of the annotation
 * @param overriddenParametersOnly
 */
void readParameter(ParameterContextKey key,
        Parameter oaiParam,
        SpringParameter springParam,
        Object springDefaultValue,
        AnnotationTarget target,
        boolean overriddenParametersOnly) {

    //TODO: Test to ensure @Parameter attributes override Spring for the same parameter
    //      (unless @Parameter was already specified at a "lower" level)

    ParameterContext context = getParameterContext(key, target);
    boolean addParam = false;

    if (context == null) {
        if (overriddenParametersOnly) {
            return;
        }

        context = new ParameterContext();
        addParam = true;
    }

    boolean oaiNameOverride = oaiParam != null && key.name != null && !key.name.equals(context.name)
            && context.location != In.PATH;

    if (context.name == null || oaiNameOverride) {
        if (context.name != null) {
            // Name is being overridden by the OAI @Parameter name
            params.remove(new ParameterContextKey(context));
            addParam = true;
        }
        context.name = key.name;
    }

    if (context.location == null) {
        context.location = key.location;
    }

    if (context.style == null) {
        context.style = key.style;
    }

    context.oaiParam = MergeUtil.mergeObjects(context.oaiParam, oaiParam);

    if (context.springParam == null) {
        context.springParam = springParam;
        context.springDefaultValue = springDefaultValue;
    }

    if (context.target == null || context.target.kind() == Kind.METHOD) {
        context.target = target;
        context.targetType = getType(target);
    }

    if (addParam) {
        params.put(new ParameterContextKey(context), context);
    }
}
 
Example 6
Source File: Injection.java    From quarkus with Apache License 2.0 4 votes vote down vote up
boolean isMethod() {
    return Kind.METHOD == target.kind();
}
 
Example 7
Source File: AnnotationsTransformer.java    From quarkus with Apache License 2.0 4 votes vote down vote up
default boolean isMethod() {
    return getTarget().kind() == Kind.METHOD;
}
 
Example 8
Source File: InjectionPointInfo.java    From quarkus with Apache License 2.0 4 votes vote down vote up
public boolean isParam() {
    return target.kind() == Kind.METHOD;
}
 
Example 9
Source File: AnnotationsTransformerInterceptorBindingTest.java    From quarkus with Apache License 2.0 4 votes vote down vote up
@Override
public boolean appliesTo(Kind kind) {
    return kind == Kind.METHOD;
}
 
Example 10
Source File: AddObservesTest.java    From quarkus with Apache License 2.0 4 votes vote down vote up
@Override
public boolean appliesTo(Kind kind) {
    return Kind.METHOD == kind;
}
 
Example 11
Source File: ValueResolverGenerator.java    From quarkus with Apache License 2.0 4 votes vote down vote up
static boolean propertiesFilter(AnnotationTarget target) {
    if (target.kind() == Kind.METHOD) {
        return target.asMethod().parameters().size() == 0;
    }
    return true;
}
 
Example 12
Source File: MethodValidatedAnnotationsTransformer.java    From quarkus with Apache License 2.0 4 votes vote down vote up
@Override
public boolean appliesTo(Kind kind) {
    return Kind.METHOD == kind;
}
 
Example 13
Source File: InterceptedStaticMethodsProcessor.java    From quarkus with Apache License 2.0 4 votes vote down vote up
@BuildStep
void collectInterceptedStaticMethods(BeanArchiveIndexBuildItem beanArchiveIndex,
        BuildProducer<InterceptedStaticMethodBuildItem> interceptedStaticMethods,
        InterceptorResolverBuildItem interceptorResolver, TransformedAnnotationsBuildItem transformedAnnotations) {

    // In this step we collect all intercepted static methods, ie. static methods annotated with interceptor bindings  
    Set<DotName> interceptorBindings = interceptorResolver.getInterceptorBindings();

    for (ClassInfo clazz : beanArchiveIndex.getIndex().getKnownClasses()) {
        for (MethodInfo method : clazz.methods()) {
            // Find all static methods (except for static initializers)
            if (!Modifier.isStatic(method.flags()) || "<clinit>".equals(method.name())) {
                continue;
            }
            // Get the (possibly transformed) set of annotations
            Collection<AnnotationInstance> annotations = transformedAnnotations.getAnnotations(method);
            if (annotations.isEmpty()) {
                continue;
            }
            // Only method-level bindings are considered due to backwards compatibility
            Set<AnnotationInstance> methodLevelBindings = null;
            for (AnnotationInstance annotationInstance : annotations) {
                if (annotationInstance.target().kind() == Kind.METHOD
                        && interceptorBindings.contains(annotationInstance.name())) {
                    if (methodLevelBindings == null) {
                        methodLevelBindings = new HashSet<>();
                    }
                    methodLevelBindings.add(annotationInstance);
                }
            }
            if (methodLevelBindings == null || methodLevelBindings.isEmpty()) {
                continue;
            }
            if (Modifier.isPrivate(method.flags())) {
                LOGGER.warnf(
                        "Interception of private static methods is not supported; bindings found on %s: %s",
                        method.declaringClass().name(),
                        method);
            } else {
                List<InterceptorInfo> interceptors = interceptorResolver.get().resolve(
                        InterceptionType.AROUND_INVOKE,
                        methodLevelBindings);
                if (!interceptors.isEmpty()) {
                    LOGGER.debugf("Intercepted static method found on %s: %s", method.declaringClass().name(),
                            method);
                    interceptedStaticMethods.produce(
                            new InterceptedStaticMethodBuildItem(method, methodLevelBindings, interceptors));
                }
            }
        }
    }
}
 
Example 14
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();
            }
        }

    });
}
 
Example 15
Source File: CacheAnnotationsTransformer.java    From quarkus with Apache License 2.0 4 votes vote down vote up
@Override
public boolean appliesTo(Kind kind) {
    return Kind.METHOD == kind;
}
 
Example 16
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 17
Source File: ParameterProcessor.java    From smallrye-open-api with Apache License 2.0 4 votes vote down vote up
/**
 * Merges MP-OAI {@link Parameter}s and JAX-RS parameters for the same {@link In} and name,
 * and {@link Style}. When overriddenParametersOnly is true, new parameters not already known
 * in {@link #params} will be ignored.
 * 
 * The given {@link ParameterContextKey key} contains:
 * 
 * <ul>
 * <li>the name of the parameter specified by application
 * <li>location, given by {@link org.eclipse.microprofile.openapi.annotations.parameters.Parameter#in @Parameter.in}
 * or implied by the type of JAX-RS annotation used on the target
 * <li>style, the parameter's style, either specified by the application or implied by the parameter's location
 * </ul>
 *
 * @param key the key for the parameter being processed
 * @param oaiParam scanned {@link org.eclipse.microprofile.openapi.annotations.parameters.Parameter @Parameter}
 * @param jaxRsParam Meta detail about the JAX-RS *Param being processed, if found.
 * @param jaxRsDefaultValue value read from the {@link javax.ws.rs.DefaultValue @DefaultValue}
 *        annotation.
 * @param target target of the annotation
 * @param overriddenParametersOnly true if only parameters already known to the scanner are considered, false otherwise
 */
void readParameter(ParameterContextKey key,
        Parameter oaiParam,
        JaxRsParameter jaxRsParam,
        Object jaxRsDefaultValue,
        AnnotationTarget target,
        boolean overriddenParametersOnly) {

    //TODO: Test to ensure @Parameter attributes override JAX-RS for the same parameter
    //      (unless @Parameter was already specified at a "lower" level)

    ParameterContext context = getParameterContext(key, target);
    boolean addParam = false;

    if (context == null) {
        if (overriddenParametersOnly) {
            return;
        }

        context = new ParameterContext();
        addParam = true;
    }

    boolean oaiNameOverride = oaiParam != null && key.name != null && !key.name.equals(context.name)
            && context.location != In.PATH;

    if (context.name == null || oaiNameOverride) {
        if (context.name != null) {
            // Name is being overridden by the OAI @Parameter name
            params.remove(new ParameterContextKey(context));
            addParam = true;
        }
        context.name = key.name;
    }

    if (context.location == null) {
        context.location = key.location;
    }

    if (context.style == null) {
        context.style = key.style;
    }

    context.oaiParam = MergeUtil.mergeObjects(context.oaiParam, oaiParam);

    if (context.jaxRsParam == null) {
        context.jaxRsParam = jaxRsParam;
        context.jaxRsDefaultValue = jaxRsDefaultValue;
    }

    if (context.target == null || context.target.kind() == Kind.METHOD) {
        context.target = target;
        context.targetType = getType(target);
    }

    if (addParam) {
        params.put(new ParameterContextKey(context), context);
    }
}