Java Code Examples for javax.enterprise.inject.spi.AnnotatedMethod#isAnnotationPresent()

The following examples show how to use javax.enterprise.inject.spi.AnnotatedMethod#isAnnotationPresent() . 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
GenericConfig(Class<X> annotationType, AnnotatedMethod<?> annotatedMethod) {
    this(annotatedMethod.getDeclaringType()
            .getJavaClass(), annotatedMethod.getJavaMember(), annotatedMethod, annotationType,
            annotatedMethod.isAnnotationPresent(annotationType) ? annotatedMethod.getAnnotation(annotationType)
                    : annotatedMethod.getDeclaringType()
                            .getAnnotation(annotationType),
            annotatedMethod.isAnnotationPresent(annotationType) ? ElementType.METHOD : ElementType.TYPE);
}
 
Example 2
private static <A extends Annotation> boolean isAnnotated(Class<A> annotationType, AnnotatedMethod<?> annotatedMethod) {
    return annotatedMethod.isAnnotationPresent(annotationType)
            || annotatedMethod.getDeclaringType().isAnnotationPresent(annotationType);
}
 
Example 3
private <T> boolean hasMediatorAnnotations(AnnotatedMethod<? super T> method) {
    return method.isAnnotationPresent(Incomings.class) || method.isAnnotationPresent(Incoming.class)
            || method.isAnnotationPresent(Outgoing.class);
}
 
Example 4
Source Project: tomee   File: BValInterceptor.java    License: Apache License 2.0 4 votes vote down vote up
private <T> boolean computeIsMethodValidated(Class<T> targetClass, Method method) {
    final Signature signature = Signature.of(method);

    AnnotatedMethod<?> declaringMethod = null;

    for (final Class<?> c : Reflection.hierarchy(targetClass, Interfaces.INCLUDE)) {
        final AnnotatedType<?> annotatedType = CDI.current().getBeanManager().createAnnotatedType(c);

        final AnnotatedMethod<?> annotatedMethod = annotatedType.getMethods().stream()
                .filter(am -> Signature.of(am.getJavaMember()).equals(signature)).findFirst().orElse(null);

        if (annotatedMethod != null) {
            declaringMethod = annotatedMethod;
        }
    }
    if (declaringMethod == null) {
        return false;
    }
    final Collection<ExecutableType> declaredExecutableTypes;

    if (declaringMethod.isAnnotationPresent(ValidateOnExecution.class)) {
        final List<ExecutableType> validatedTypesOnMethod =
                Arrays.asList(declaringMethod.getAnnotation(ValidateOnExecution.class).type());

        // implicit directly on method -> early return:
        if (validatedTypesOnMethod.contains(ExecutableType.IMPLICIT)) {
            return true;
        }
        declaredExecutableTypes = validatedTypesOnMethod;
    } else {
        final AnnotatedType<?> declaringType = declaringMethod.getDeclaringType();
        if (declaringType.isAnnotationPresent(ValidateOnExecution.class)) {
            // IMPLICIT is meaningless at class level:
            declaredExecutableTypes =
                    removeFrom(Arrays.asList(declaringType.getAnnotation(ValidateOnExecution.class).type()),
                            ExecutableType.IMPLICIT);
        } else {
            final Package pkg = declaringType.getJavaClass().getPackage();
            if (pkg != null && pkg.isAnnotationPresent(ValidateOnExecution.class)) {
                // presumably IMPLICIT is likewise meaningless at package level:
                declaredExecutableTypes = removeFrom(
                        Arrays.asList(pkg.getAnnotation(ValidateOnExecution.class).type()), ExecutableType.IMPLICIT);
            } else {
                declaredExecutableTypes = null;
            }
        }
    }
    final ExecutableType methodType =
            Methods.isGetter(method) ? ExecutableType.GETTER_METHODS : ExecutableType.NON_GETTER_METHODS;

    return Optional.ofNullable(declaredExecutableTypes).map(ExecutableTypes::interpret)
            .orElse(globalConfiguration.getGlobalExecutableTypes()).contains(methodType);
}
 
Example 5
@Test
public void testTypeLevelAnnotationRedefinition()
{
    AnnotatedTypeBuilder<Cat> builder = new AnnotatedTypeBuilder<Cat>();
    builder.readFromType(Cat.class);

    AnnotatedType<Cat> cat = builder.create();

    assertNotNull(cat);
    assertNotNull(cat.getAnnotation(Named.class));
    assertEquals("cat", cat.getAnnotation(Named.class).value());

    builder.addToClass(new AlternativeLiteral())
            .addToClass(new ApplicationScopedLiteral())
            .removeFromClass(Named.class)
            .addToClass(new NamedLiteral("tomcat"));

    cat = builder.create();
    assertNotNull(cat);

    assertEquals(3, cat.getAnnotations().size());
    assertTrue(cat.isAnnotationPresent(Named.class));
    assertTrue(cat.isAnnotationPresent(Alternative.class));
    assertTrue(cat.isAnnotationPresent(ApplicationScoped.class));
    assertEquals("tomcat", cat.getAnnotation(Named.class).value());
    
    AnnotatedMethod observerMethod = null;
    for (AnnotatedMethod m : cat.getMethods())
    {
        if ("doSomeObservation".equals(m.getJavaMember().getName()))
        {
            observerMethod = m;
            break;
        }
    }
    assertNotNull(observerMethod);
    observerMethod.isAnnotationPresent(Observes.class);
    
    {
        // test reading from an AnnotatedType
        AnnotatedTypeBuilder<Cat> builder2 = new AnnotatedTypeBuilder<Cat>();
        builder2.readFromType(cat);
        builder2.removeFromAll(Named.class);

        final AnnotatedType<Cat> noNameCat = builder2.create();
        assertFalse(noNameCat.isAnnotationPresent(Named.class));
        assertEquals(2, noNameCat.getAnnotations().size());
    }

    {

        // test reading from an AnnotatedType in non-overwrite mode
        AnnotatedTypeBuilder<Cat> builder3 = new AnnotatedTypeBuilder<Cat>();
        builder3.readFromType(cat, true);
        builder3.removeFromAll(Named.class);

        builder3.readFromType(cat, false);

        final AnnotatedType<Cat> namedCat = builder3.create();
        assertTrue(namedCat.isAnnotationPresent(Named.class));
        assertEquals(3, namedCat.getAnnotations().size());
    }
}