Java Code Examples for javax.enterprise.inject.spi.AnnotatedType#getMethods()

The following examples show how to use javax.enterprise.inject.spi.AnnotatedType#getMethods() . 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: AnnotatedTypeProcessor.java    From krazo with Apache License 2.0 6 votes vote down vote up
public <T> AnnotatedType<T> getReplacement(AnnotatedType<T> originalType) {

        boolean modified = false;
        Set<AnnotatedMethod<? super T>> methods = new LinkedHashSet<>();

        for (AnnotatedMethod<? super T> originalMethod : originalType.getMethods()) {
            AnnotatedMethod<? super T> replacement = getReplacement(originalType, originalMethod);
            if (replacement != null) {
                methods.add(replacement);
                modified = true;
            } else {
                methods.add(originalMethod);
            }
        }

        if (modified) {
            return new AnnotatedTypeWrapper<T>(originalType, methods);
        }
        return null;

    }
 
Example 2
Source File: AnnotatedTypeProcessor.java    From ozark with Apache License 2.0 6 votes vote down vote up
public <T> AnnotatedType<T> getReplacement(AnnotatedType<T> originalType) {

        boolean modified = false;
        Set<AnnotatedMethod<? super T>> methods = new LinkedHashSet<>();

        for (AnnotatedMethod<? super T> originalMethod : originalType.getMethods()) {
            AnnotatedMethod<? super T> replacement = getReplacement(originalType, originalMethod);
            if (replacement != null) {
                methods.add(replacement);
                modified = true;
            } else {
                methods.add(originalMethod);
            }
        }

        if (modified) {
            return new AnnotatedTypeWrapper<T>(originalType, methods);
        }
        return null;

    }
 
Example 3
Source File: AnnotatedMethods.java    From deltaspike with Apache License 2.0 6 votes vote down vote up
public static AnnotatedMethod<?> findMethod(final AnnotatedType<?> type, final Method method)
{
    AnnotatedMethod<?> annotatedMethod = null;
    for (final AnnotatedMethod<?> am : type.getMethods())
    {
        if (am.getJavaMember().equals(method))
        {
            annotatedMethod = am;
            break;
        }
    }
    if (annotatedMethod == null)
    {
        throw new IllegalStateException("No annotated method for " + method);
    }
    return annotatedMethod;
}
 
Example 4
Source File: FaultToleranceExtension.java    From smallrye-fault-tolerance with Apache License 2.0 5 votes vote down vote up
/**
 * Observe all enabled managed beans and identify/validate FT operations. This allows us to:
 * <ul>
 * <li>Skip validation of types which are not recognized as beans (e.g. are vetoed)</li>
 * <li>Take the final values of AnnotatedTypes</li>
 * <li>Support annotations added via portable extensions</li>
 * </ul>
 *
 * @param event
 */
void collectFaultToleranceOperations(@Observes ProcessManagedBean<?> event) {
    AnnotatedType<?> annotatedType = event.getAnnotatedBeanClass();
    for (AnnotatedMethod<?> annotatedMethod : annotatedType.getMethods()) {
        FaultToleranceOperation operation = FaultToleranceOperation.of(annotatedMethod);
        if (operation.isLegitimate()) {
            operation.validate();
            LOGGER.debugf("Found %s", operation);
            faultToleranceOperations.put(getCacheKey(annotatedType.getJavaClass(), annotatedMethod.getJavaMember()),
                    operation);
        }
    }
}
 
Example 5
Source File: MediatorManager.java    From smallrye-reactive-messaging with Apache License 2.0 5 votes vote down vote up
public <T> void analyze(AnnotatedType<T> annotatedType, Bean<T> bean) {
    log.scanningType(annotatedType.getJavaClass());
    Set<AnnotatedMethod<? super T>> methods = annotatedType.getMethods();

    methods.stream()
            .filter(this::hasMediatorAnnotations)
            .forEach(method -> collected.add(method.getJavaMember(), bean));
}
 
Example 6
Source File: WorkerPoolRegistry.java    From smallrye-reactive-messaging with Apache License 2.0 5 votes vote down vote up
public <T> void analyzeWorker(AnnotatedType<T> annotatedType) {
    Objects.requireNonNull(annotatedType, msg.annotatedTypeWasEmpty());

    Set<AnnotatedMethod<? super T>> methods = annotatedType.getMethods();

    methods.stream()
            .filter(m -> m.isAnnotationPresent(Blocking.class))
            .forEach(m -> defineWorker(m.getJavaMember()));
}
 
Example 7
Source File: InjectionHelper.java    From BeanTest with Apache License 2.0 5 votes vote down vote up
/**
 * Adds the {@link Inject} annotation to the fields and setters of the annotated type if required.
 *
 * @param <X>
 *            the type of the annotated type
 * @param annotatedType
 *            the annotated type whose fields and setters the inject annotation should be added to
 * @param builder
 *            the builder that should be used to add the annotation.
 * @see #shouldInjectionAnnotationBeAddedToMember(AnnotatedMember)
 */
public static <X> void addInjectAnnotation(final AnnotatedType<X> annotatedType, AnnotatedTypeBuilder<X> builder) {
    for (AnnotatedField<? super X> field : annotatedType.getFields()) {
        if (shouldInjectionAnnotationBeAddedToMember(field)) {
            builder.addToField(field, AnnotationInstances.INJECT);
        }
    }
    for (AnnotatedMethod<? super X> method : annotatedType.getMethods()) {
        if (shouldInjectionAnnotationBeAddedToMember(method)) {
            builder.addToMethod(method,  AnnotationInstances.INJECT);
        }
    }
}
 
Example 8
Source File: CdiHelper.java    From metrics-cdi with Apache License 2.0 5 votes vote down vote up
static <T extends Annotation> void declareAsInterceptorBinding(Class<T> annotation, BeanManager manager, BeforeBeanDiscovery bbd) {
    AnnotatedType<T> annotated = manager.createAnnotatedType(annotation);
    Set<AnnotatedMethod<? super T>> methods = new HashSet<>();
    for (AnnotatedMethod<? super T> method : annotated.getMethods())
        methods.add(new AnnotatedMethodDecorator<>(method, NON_BINDING));

    bbd.addInterceptorBinding(new AnnotatedTypeDecorator<>(annotated, INTERCEPTOR_BINDING, methods));
}
 
Example 9
Source File: SecurityMetaDataStorage.java    From deltaspike with Apache License 2.0 5 votes vote down vote up
void addSecuredType(AnnotatedType<?> annotatedType)
{
    for (AnnotatedMethod<?> securedMethod : annotatedType.getMethods())
    {
        addSecuredMethod(securedMethod);
    }
}
 
Example 10
Source File: ExceptionControlExtension.java    From deltaspike with Apache License 2.0 5 votes vote down vote up
/**
 * Listener to ProcessBean event to locate handlers.
 *
 * @param processBean current {@link AnnotatedType}
 * @param beanManager  Activated Bean Manager
 * @throws TypeNotPresentException if any of the actual type arguments refers to a non-existent type declaration
 *                                 when trying to obtain the actual type arguments from a
 *                                 {@link java.lang.reflect.ParameterizedType}
 * @throws java.lang.reflect.MalformedParameterizedTypeException
 *                                 if any of the actual type parameters refer to a parameterized type that cannot
 *                                 be instantiated for any reason when trying to obtain the actual type arguments
 *                                 from a {@link java.lang.reflect.ParameterizedType}
 */
@SuppressWarnings("UnusedDeclaration")
public <T> void findHandlers(@Observes final ProcessBean<?> processBean, final BeanManager beanManager)
{
    if (!isActivated)
    {
        return;
    }

    if (processBean.getBean() instanceof Interceptor || processBean.getBean() instanceof Decorator ||
            !(processBean.getAnnotated() instanceof AnnotatedType))
    {
        return;
    }

    AnnotatedType annotatedType = (AnnotatedType)processBean.getAnnotated();

    if (annotatedType.getJavaClass().isAnnotationPresent(ExceptionHandler.class))
    {
        final Set<AnnotatedMethod<? super T>> methods = annotatedType.getMethods();

        for (AnnotatedMethod<? super T> method : methods)
        {
            if (HandlerMethodImpl.isHandler(method))
            {
                if (method.getJavaMember().getExceptionTypes().length != 0)
                {
                    processBean.addDefinitionError(new IllegalArgumentException(
                        String.format("Handler method %s must not throw exceptions", method.getJavaMember())));
                }

                //beanManager won't be stored in the instance -> no issue with wls12c
                registerHandlerMethod(new HandlerMethodImpl(processBean.getBean(), method, beanManager));
            }
        }
    }
}
 
Example 11
Source File: CDIInterceptorWrapperImpl.java    From cxf with Apache License 2.0 4 votes vote down vote up
private static Map<Method, List<InterceptorInvoker>> initInterceptorInvokers(BeanManager beanManager,
                                                                             CreationalContext<?> creationalContext,
                                                                             Class<?> restClient) {
    Map<Method, List<InterceptorInvoker>> invokers = new HashMap<>();
    // Interceptor as a key in a map is not entirely correct (custom interceptors) but should work in most cases
    Map<Interceptor<?>, Object> interceptorInstances = new HashMap<>();
    
    AnnotatedType<?> restClientType = beanManager.createAnnotatedType(restClient);

    List<Annotation> classBindings = getBindings(restClientType.getAnnotations(), beanManager);

    for (AnnotatedMethod<?> method : restClientType.getMethods()) {
        Method javaMethod = method.getJavaMember();
        if (javaMethod.isDefault() || method.isStatic()) {
            continue;
        }
        List<Annotation> methodBindings = getBindings(method.getAnnotations(), beanManager);

        if (!classBindings.isEmpty() || !methodBindings.isEmpty()) {
            Annotation[] interceptorBindings = merge(methodBindings, classBindings);

            List<Interceptor<?>> interceptors =
                new ArrayList<>(beanManager.resolveInterceptors(InterceptionType.AROUND_INVOKE, 
                                                                interceptorBindings));
            if (LOG.isLoggable(Level.FINEST)) {
                LOG.finest("Resolved interceptors from beanManager, " + beanManager + ":" + interceptors);
            }

            if (!interceptors.isEmpty()) {
                List<InterceptorInvoker> chain = new ArrayList<>();
                for (Interceptor<?> interceptor : interceptors) {
                    chain.add(
                        new InterceptorInvoker(interceptor, 
                                               interceptorInstances.computeIfAbsent(interceptor, 
                                                   i -> beanManager.getReference(i, 
                                                                                 i.getBeanClass(), 
                                                                                 creationalContext))));
                }
                invokers.put(javaMethod, chain);
            }
        }
    }
    return invokers.isEmpty() ? Collections.emptyMap() : invokers;
}
 
Example 12
Source File: AnnotatedTypeBuilderTest.java    From deltaspike with Apache License 2.0 4 votes vote down vote up
@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());
    }
}