// This is a work around for CDI Uber Jar deployment. When Weld scans the classpath it pick up // RemoteCacheManager // (this is an implementation, not an interface, so it gets instantiated). As a result we get // duplicated classes // in CDI BeanManager. @SuppressWarnings("unused") <T extends RemoteCacheManager> void removeDuplicatedRemoteCacheManager( @Observes ProcessAnnotatedType<T> bean) { if (RemoteCacheManager.class .getCanonicalName() .equals(bean.getAnnotatedType().getJavaClass().getCanonicalName())) { LOGGER.info("removing duplicated RemoteCacheManager" + bean.getAnnotatedType()); bean.veto(); } }
<X> void processType(@Observes ProcessAnnotatedType<X> event) { AnnotatedType<X> original = event.getAnnotatedType(); if (original.isAnnotationPresent(ManagedBean.class) && !original.isAnnotationPresent(Named.class)) { AnnotatedType<X> modified = new AnnotatedTypeBuilder<X>() .readFromType(event.getAnnotatedType(), true) .addToClass(new NamedLiteral(original.getAnnotation(ManagedBean.class).value())) .create(); event.setAnnotatedType(modified); } }
public void vetoSpringBeans(@Observes ProcessAnnotatedType event) { if (!initialized) { init(); } if (!applicationContextFound) { return; } if (beanForClassExists(event.getAnnotatedType().getJavaClass())) { // Do not deploy this class to the CDI context. event.veto(); if (LOG.isDebugEnabled()) { LOG.debug("Vetoing " + event.getAnnotatedType().getJavaClass().getCanonicalName()); } } }
private void camelFactoryProducers( @Observes ProcessAnnotatedType<CdiCamelFactory> pat, BeanManager manager) { pat.setAnnotatedType( new AnnotatedTypeDelegate<>( pat.getAnnotatedType(), pat.getAnnotatedType() .getMethods() .stream() .filter(am -> am.isAnnotationPresent(Produces.class)) .filter(am -> am.getTypeClosure().stream().noneMatch(isEqual(TypeConverter.class))) .peek(am -> producerQualifiers.put(am.getJavaMember(), getQualifiers(am, manager))) .map( am -> new AnnotatedMethodDelegate<>( am, am.getAnnotations() .stream() .filter( annotation -> !manager.isQualifier(annotation.annotationType())) .collect( collectingAndThen( toSet(), annotations -> { annotations.add(EXCLUDED); return annotations; })))) .collect(toSet()))); }
@Before public void setUp() throws Exception { MockitoAnnotations.initMocks(this); subject = new GlobalAlternativeSelector(); subject.loadApplicationAlternatives(bbd); ccl = Thread.currentThread().getContextClassLoader(); when(pat.getAnnotatedType()).thenReturn(at); }
/** * Annotates the superclass of the mocks with @Alternative, to make sure we get no ambiguous * dependencies. * * @param pat * @param <T> */ public <T extends ServiceInterface> void processAnnotatedType( @Observes ProcessAnnotatedType<T> pat) { boolean annotateWithAlternative = false; // test, if the current type is a superclass of a mock Class<?> serviceClass = pat.getAnnotatedType().getJavaClass(); for (Class<? extends ServiceInterface> mock : getMocks()) { if (serviceClass.isAssignableFrom(mock)) { annotateWithAlternative = true; } } if (annotateWithAlternative) { final AnnotatedType<T> type = pat.getAnnotatedType(); AlternativeWrapperAnnotatedType<T> wrapperAnnotatedType = new AlternativeWrapperAnnotatedType<T>(type); pat.setAnnotatedType(wrapperAnnotatedType); } }
public <T> void processAnnotatedType(@Observes ProcessAnnotatedType<T> pat) { Class<?> klass = pat.getAnnotatedType().getJavaClass(); if (klass.isAnnotationPresent(BotCommand.class)) { for (Method m : klass.getMethods()) { if (m.isAnnotationPresent(Trigger.class)) { triggers.add(m); } if (m.isAnnotationPresent(RandomAction.class)) { randoms.add(m); } if (m.isAnnotationPresent(TimedAction.class)) { timers.add(m); } } } }
private void processAnnotatedType(@Observes ProcessAnnotatedType<?> pat) { if (pat.getAnnotatedType().isAnnotationPresent(Vetoed.class)) { pat.veto(); } if (hasAnnotation(pat.getAnnotatedType(), Converter.class)) { converters.add(pat.getAnnotatedType().getJavaClass()); } if (hasAnnotation( pat.getAnnotatedType(), BeanInject.class, Consume.class, EndpointInject.class, Produce.class, PropertyInject.class)) { camelBeans.add(pat.getAnnotatedType()); } if (hasAnnotation(pat.getAnnotatedType(), Consume.class)) { eagerBeans.add(pat.getAnnotatedType()); } if (hasAnnotation(pat.getAnnotatedType(), ImportResource.class)) { resources.add(pat.getAnnotatedType().getAnnotation(ImportResource.class)); } }
/** * Used to register the method validation interceptor bindings. * * @param processAnnotatedTypeEvent event fired for each annotated type * @param <T> the annotated type */ public <T> void processAnnotatedType( @Observes @WithAnnotations({Constraint.class, Valid.class, ValidateOnExecution.class}) ProcessAnnotatedType<T> processAnnotatedTypeEvent) { Contracts.assertNotNull( processAnnotatedTypeEvent, "The ProcessAnnotatedType event cannot be null"); // validation globally disabled if (!isExecutableValidationEnabled) { return; } AnnotatedType<T> type = processAnnotatedTypeEvent.getAnnotatedType(); Set<AnnotatedCallable<? super T>> constrainedCallables = determineConstrainedCallables(type); if (!constrainedCallables.isEmpty()) { ValidationEnabledAnnotatedType<T> wrappedType = new ValidationEnabledAnnotatedType<T>(type, constrainedCallables); processAnnotatedTypeEvent.setAnnotatedType(wrappedType); } }
<T> void processAnnotatedType(@Observes ProcessAnnotatedType<T> pat) { AnnotatedType<T> annotatedType = pat.getAnnotatedType(); Class<T> type = annotatedType.getJavaClass(); // Determine if bean should be processed boolean veto = !context.filter.filter(type); if (!veto) { for (AbstractBean boundBean : context.injector.boundBeans) { Class<?> beanType = boundBean.getBeanClass(); if (beanType.isAssignableFrom(type)) { veto = true; break; } } } // if (veto) { pat.veto(); } }
/** * Register managed beans as Errai services * * @param event - * @param <T> - */ @SuppressWarnings("UnusedDeclaration") public <T> void observeResources(@Observes final ProcessAnnotatedType<T> event) { final AnnotatedType<T> type = event.getAnnotatedType(); for (final Annotation a : type.getJavaClass().getAnnotations()) { if (a.annotationType().isAnnotationPresent(Qualifier.class)) { beanQualifiers.put(a.annotationType().getName(), a); } } // services if (type.isAnnotationPresent(Service.class)) { log.debug("discovered Errai annotation on type: " + type); boolean isRpc = false; final Class<T> javaClass = type.getJavaClass(); for (final Class<?> intf : javaClass.getInterfaces()) { isRpc = intf.isAnnotationPresent(Remote.class); if (isRpc) { if (!managedTypes.getRemoteInterfaces().contains(intf)) { managedTypes.addRemoteInterface(intf); } } } if (!isRpc) { managedTypes.addServiceEndpoint(type); } } else { for (final AnnotatedMethod method : type.getMethods()) { if (method.isAnnotationPresent(Service.class)) { managedTypes.addServiceMethod(type, method); } } } // veto on client side implementations that contain CDI annotations // (i.e. @Observes) Otherwise Weld might try to invoke on them if (vetoClasses.contains(type.getJavaClass().getName()) || (type.getJavaClass().getPackage().getName().contains("client") && !type.getJavaClass().isInterface())) { event.veto(); } /** We must scan for Event consumer injection points to build the tables */ final Class clazz = type.getJavaClass(); for (final Field f : clazz.getDeclaredFields()) { if (f.isAnnotationPresent(Inject.class) && f.isAnnotationPresent(ObserverModel.class)) { processEventInjector(f.getType(), f.getGenericType(), f.getAnnotations()); } } for (final Method m : clazz.getDeclaredMethods()) { if (m.isAnnotationPresent(Inject.class) && m.isAnnotationPresent(ObserverModel.class)) { final Class<?>[] parameterTypes = m.getParameterTypes(); for (int i = 0, parameterTypesLength = parameterTypes.length; i < parameterTypesLength; i++) { final Class<?> parmType = parameterTypes[i]; processEventInjector( parmType, m.getGenericParameterTypes()[i], m.getParameterAnnotations()[i]); } } } for (final Constructor c : clazz.getDeclaredConstructors()) { if (c.isAnnotationPresent(Inject.class) && c.isAnnotationPresent(ObserverModel.class)) { final Class<?>[] parameterTypes = c.getParameterTypes(); for (int i = 0, parameterTypesLength = parameterTypes.length; i < parameterTypesLength; i++) { final Class<?> parmType = parameterTypes[i]; processEventInjector( parmType, c.getGenericParameterTypes()[i], c.getParameterAnnotations()[i]); } } } }
<X> void replaceInjectOnGenericBeans(@Observes ProcessAnnotatedType<X> event) { AnnotatedType<X> type = event.getAnnotatedType(); if (type.isAnnotationPresent(GenericConfiguration.class)) { final Class<? extends Annotation> genericConfigurationType = type.getAnnotation(GenericConfiguration.class).value(); // validate that the configuration type is annotated correctly if (!genericConfigurationType.isAnnotationPresent(GenericType.class)) { errors.add( "Bean " + type.getJavaClass().getName() + " specifies generic annotation " + type.getAnnotation(GenericConfiguration.class) + " however " + genericConfigurationType + " is not annotated @GenericConfiguration."); } else { Class<?> configType = genericConfigurationType.getAnnotation(GenericType.class).value(); if (configType.isAnnotationPresent(GenericConfiguration.class)) { errors.add( "Generic configuration type " + genericConfigurationType + " specifies a value() of " + configType + " however " + configType + " is a generic bean. Generic configuration types may not be generic beans"); } } final AnnotatedTypeBuilder<X> builder = new AnnotatedTypeBuilder<X>().readFromType(type); builder.addToClass(genericBeanQualifier); builder.redefine( Inject.class, new AnnotationRedefiner<Inject>() { public void redefine(RedefinitionContext<Inject> ctx) { if (ctx.getAnnotatedElement() instanceof Field) { if (ctx.getAnnotatedElement().isAnnotationPresent(Generic.class)) { // This is a Generic bean injection point ctx.getAnnotationBuilder() .remove(Inject.class) .add(InjectGenericLiteral.INSTANCE); } } } }); builder.redefine( Produces.class, new AnnotationRedefiner<Produces>() { public void redefine(RedefinitionContext<Produces> ctx) { // Add the marker qualifier ctx.getAnnotationBuilder() .add(GenericMarkerLiteral.INSTANCE) .add(genericBeanQualifier); } }); builder.redefine( Disposes.class, new AnnotationRedefiner<Disposes>() { public void redefine(RedefinitionContext<Disposes> ctx) { // Add the marker qualifier ctx.getAnnotationBuilder() .add(GenericMarkerLiteral.INSTANCE) .add(genericBeanQualifier); } }); builder.redefine( Generic.class, new AnnotationRedefiner<Generic>() { public void redefine(RedefinitionContext<Generic> ctx) { // if it is a parameter annotation if (!(ctx.getAnnotatedElement() instanceof AccessibleObject)) { // stick an InjectGeneric as a marker. ctx.getAnnotationBuilder().remove(Generic.class).add(InjectGenericLiteral.INSTANCE); if (ctx.getRawType().isAnnotationPresent(GenericConfiguration.class)) { ctx.getAnnotationBuilder().add(genericBeanQualifier); } } } }); event.setAnnotatedType(builder.create()); } }
/** * Simply get rid of Solder {@link ELResolverProducer}. Cross-archive specialization is broken so * we cannot use that. */ public void vetoSolderElResolverProducer( @Observes ProcessAnnotatedType<ELResolverProducer> event) { if (event.getAnnotatedType().getJavaClass().equals(ELResolverProducer.class)) { event.veto(); } }
public <T> void detectAnnotation( @Observes final ProcessAnnotatedType<T> event, final BeanManager beanManager) { if (event.getAnnotatedType().isAnnotationPresent(Managed.class)) { types.add(event.getAnnotatedType()); } }
void observeSimpleDecoratorType1(@Observes ProcessAnnotatedType<SimpleDecorator> event) { this.simpleDecoratorType1 = event.getAnnotatedType(); }
void observeSimpleBeanType1(@Observes ProcessAnnotatedType<SimpleBean> event) { this.simpleBeanType1 = event.getAnnotatedType(); }