@SuppressWarnings("unused") // will be called automatically by the CDI container for all annotated types private void skipUnusedStepsFromBeanDiscovery(@Observes ProcessAnnotatedType<?> event, BeanManager beanManager) throws MojoExecutionException, MojoFailureException { // https://github.com/shillner/maven-cdi-plugin-utils/issues/14 Class<?> type = event.getAnnotatedType().getJavaClass(); ProcessingStep annotation = type.getAnnotation(ProcessingStep.class); if (annotation != null) { // adding the step to the list of all available processing steps String id = annotation.id(); Preconditions.checkState(!this.allAvailableProcessingSteps.containsKey(id), "The processing step id '" + id + "' is not unique!"); this.allAvailableProcessingSteps.put(id, annotation); // vetoing the bean discovery of a step that is not part of the current workflow // this prevents the issue that data shall be injected that isn't produced anywhere! ProcessingWorkflow workflow = getWorkflow(); if (!workflow.containsStep(annotation.id())) { event.veto(); } } }
<T> void findJobs( @Observes @WithAnnotations({Cron.class}) ProcessAnnotatedType<T> pat, BeanManager beanManager ) { // Ensure we are named otherwise job won't fire as we can't locate it AnnotatedType<?> type = pat.getAnnotatedType(); Class<?> clazz = type.getJavaClass(); CDIUtils.addTypeAnnotation( pat, Named.class, () -> new NamedImpl( "Schedule_" + (id++) ) ); if( type.isAnnotationPresent( Cron.class ) ) { if( Job.class.isAssignableFrom( clazz ) ) { jobClasses.add( clazz ); } else { throw new UnsupportedOperationException( "@Cron on type must implement Job" ); } } else { for( AnnotatedMethod<?> meth: type.getMethods() ) { if( meth.isAnnotationPresent( Cron.class ) ) { jobClasses.add( clazz ); } } } }
/** * Ensure we have an annotation present * <p> * @param <T> * @param pat * @param clazz * @param s * <p> * @return */ public static <T> AnnotatedType<T> addTypeAnnotation( ProcessAnnotatedType<T> pat, Class<? extends Annotation> clazz, Supplier<Annotation> s ) { AnnotatedType<T> t = pat.getAnnotatedType(); if( !t.isAnnotationPresent( clazz ) ) { MutableAnnotatedType<T> mat; if( t instanceof MutableAnnotatedType ) { mat = (MutableAnnotatedType<T>) t; } else { mat = new MutableAnnotatedType<>( t ); pat.setAnnotatedType( mat ); } mat.add( s.get() ); return mat; } return t; }
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)); } }
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()))); }
/** * Activates the alternatives declared with {@code @Beans} globally for the * application. * <p/> * For every types and every methods of every types declared with * {@link Beans#alternatives()}, the {@code Priority} annotation is added * so that the corresponding alternatives are selected globally for the * entire application. * * @see Beans */ private <T> void alternatives(@Observes @WithAnnotations(Alternative.class) ProcessAnnotatedType<T> pat) { AnnotatedType<T> type = pat.getAnnotatedType(); if (!Arrays.asList(beans.alternatives()).contains(type.getJavaClass())) { // Only select globally the alternatives that are declared with @Beans return; } Set<AnnotatedMethod<? super T>> methods = new HashSet<>(); for (AnnotatedMethod<? super T> method : type.getMethods()) { if (method.isAnnotationPresent(Alternative.class) && !method.isAnnotationPresent(Priority.class)) { methods.add(new AnnotatedMethodDecorator<>(method, PriorityLiteral.of(APPLICATION))); } } if (type.isAnnotationPresent(Alternative.class) && !type.isAnnotationPresent(Priority.class)) { pat.setAnnotatedType(new AnnotatedTypeDecorator<>(type, PriorityLiteral.of(APPLICATION), methods)); } else if (!methods.isEmpty()) { pat.setAnnotatedType(new AnnotatedTypeDecorator<>(type, methods)); } }
public <T> void processPackageAnnotatedType(@Observes final ProcessAnnotatedType<T> event) { final Package pkg = event.getAnnotatedType().getJavaClass().getPackage(); if (pkg == null) { return; } final String packageName = pkg.getName(); if (processedPackages.contains(packageName)) { return; // already processed a class from this package } processedPackages.add(packageName); if (pkg.isAnnotationPresent(FilterParameters.class)) { addFilterParameters(pkg.getAnnotation(FilterParameters.class)); } if (pkg.isAnnotationPresent(FilterParameter.class)) { // TODO addFilterParameter(pkg.getAnnotation(FilterParameter.class)); } }
protected void convertJsf2Scopes(@Observes ProcessAnnotatedType processAnnotatedType) { if (!isActivated) { return; } //TODO //CodiStartupBroadcaster.broadcastStartup(); Class<? extends Annotation> jsf2ScopeAnnotation = getJsf2ScopeAnnotation(processAnnotatedType); if (jsf2ScopeAnnotation != null && !isBeanWithManagedBeanAnnotation(processAnnotatedType)) { processAnnotatedType.setAnnotatedType( convertBean(processAnnotatedType.getAnnotatedType(), jsf2ScopeAnnotation)); } }
@SuppressWarnings("UnusedDeclaration") protected void buildViewConfigMetaDataTree(@Observes final ProcessAnnotatedType pat) { if (!isActivated) { return; } buildViewConfigMetaDataTreeFor( pat.getAnnotatedType().getJavaClass(), pat.getAnnotatedType().getAnnotations(), new VetoCallback() { @Override public void veto() { pat.veto(); } }); }
public void ensureNamingConvention(@Observes ProcessAnnotatedType processAnnotatedType) { Class<?> beanClass = processAnnotatedType.getAnnotatedType().getJavaClass(); Named namedAnnotation = beanClass.getAnnotation(Named.class); if (namedAnnotation != null && namedAnnotation.value().length() > 0 && Character.isUpperCase(namedAnnotation.value().charAt(0))) { AnnotatedTypeBuilder builder = new AnnotatedTypeBuilder(); builder.readFromType(beanClass); String beanName = namedAnnotation.value(); String newBeanName = beanName.substring(0, 1).toLowerCase() + beanName.substring(1); builder.removeFromClass(Named.class) .addToClass(new NamedLiteral(newBeanName)); processAnnotatedType.setAnnotatedType(builder.create()); } }
@SuppressWarnings("UnusedDeclaration") protected void detectInterfaces(@Observes ProcessAnnotatedType processAnnotatedType) { if (!isActivated) { return; } AnnotatedType<?> type = processAnnotatedType.getAnnotatedType(); if (type.isAnnotationPresent(MessageBundle.class)) { if (validateMessageBundle(type.getJavaClass())) { messageBundleTypes.add(type); } } }
private boolean evalExcludeInProjectStage(ProcessAnnotatedType processAnnotatedType, Exclude exclude, ProjectStage currentlyConfiguredProjectStage) { Class<? extends ProjectStage>[] activatedIn = exclude.ifProjectStage(); if (activatedIn.length == 0) { return true; } if (isInProjectStage(activatedIn, currentlyConfiguredProjectStage)) { veto(processAnnotatedType, "IfProjectState"); return false; } return true; }
private boolean evalExcludeNotInProjectStage(ProcessAnnotatedType processAnnotatedType, Exclude exclude, ProjectStage currentlyConfiguredProjectStage) { Class<? extends ProjectStage>[] notIn = exclude.exceptIfProjectStage(); if (notIn.length == 0) { return true; } if (!isInProjectStage(notIn, currentlyConfiguredProjectStage)) { veto(processAnnotatedType, "ExceptIfProjectState"); return false; } return true; }
public <X> void beans( final @Observes ProcessAnnotatedType<X> processBean ) { final MessageDriven annotation = processBean.getAnnotatedType().getAnnotation(MessageDriven.class); if (annotation != null) { final Class<X> javaClass = processBean.getAnnotatedType().getJavaClass(); if (!MessageListener.class.isAssignableFrom(javaClass)) { throw new TestEEfiException("The @MessageDriven bean " + javaClass.getName() + " does not implement MessageListener" ); } registry.register(annotation, (Class<? extends MessageListener>) javaClass); } }
public <X> void beans( final @Observes ProcessAnnotatedType<X> processBean ) { if (!processBean.getAnnotatedType().isAnnotationPresent(Interceptor.class)) { return; } final FilteringAnnotatedTypeWrapper<X> filtered = new FilteringAnnotatedTypeWrapper<>( processBean.getAnnotatedType(), it -> it != Priority.class ); processBean.setAnnotatedType(filtered); }
public <T> void processAnnotatedType(@Observes ProcessAnnotatedType<T> event, BeanManager manager) { boolean hasPersistenceField = false; for (AnnotatedField<? super T> field : event.getAnnotatedType().getFields()) { if (field.isAnnotationPresent(PersistenceContext.class) || field.isAnnotationPresent(PersistenceUnit.class)) { hasPersistenceField = true; break; } } if (hasPersistenceField) { PersistenceAnnotatedType<T> pat = new PersistenceAnnotatedType<T>(manager, event.getAnnotatedType()); beans.addAll(pat.getProducers()); event.setAnnotatedType(pat); } }
/** * Discover all classes that implements HealthCheckProcedure */ public void observeResources(@Observes @WithAnnotations({Health.class}) ProcessAnnotatedType<? extends HealthCheck> event) { AnnotatedType<? extends HealthCheck> annotatedType = event.getAnnotatedType(); Class<? extends HealthCheck> javaClass = annotatedType.getJavaClass(); MicroProfileHealthLogger.ROOT_LOGGER.debugf("Discovered health check procedure %s", javaClass); delegates.add(annotatedType); }
<T> void processAnnotatedType(@Observes @WithAnnotations(EnableSnoopEEClient.class) ProcessAnnotatedType<T> pat) { // workaround for WELD bug revealed by JDK8u60 final ProcessAnnotatedType<T> snoopAnnotated = pat; LOGGER.config(() -> "Found @EnableSnoopEEClient annotated class: " + snoopAnnotated.getAnnotatedType().getJavaClass().getName()); snoopEnabled = true; serviceName = snoopAnnotated.getAnnotatedType().getAnnotation(EnableSnoopEEClient.class).serviceName(); LOGGER.config(() -> "SnoopEE Service name is: " + serviceName); }
void findServiceInterfaces(@Observes @WithAnnotations(ProxyGen.class) ProcessAnnotatedType<?> event, BeanManager beanManager) { AnnotatedType<?> annotatedType = event.getAnnotatedType(); if (annotatedType.isAnnotationPresent(ProxyGen.class) && annotatedType.getJavaClass().isInterface()) { LOGGER.debug("Service interface {0} discovered", annotatedType.getJavaClass()); serviceInterfaces.add(annotatedType.getJavaClass()); } }
/** * Pass an AnnotatedType to a consumer if an annotation is present * * @param <T> * @param pat * @param annotationType * @param c */ public static <T> void forEachType( ProcessAnnotatedType<T> pat, Class<? extends Annotation> annotationType, Consumer<? super AnnotatedType<? extends Object>> c ) { AnnotatedType<?> type = pat.getAnnotatedType(); if( type.isAnnotationPresent( annotationType ) ) { c.accept( type ); } }
/** * For each method in a type that has a specific annotation pass the class and method to a consumer * * @param <T> * @param pat * @param annotationType * @param c */ public static <T> void forEachMethod( ProcessAnnotatedType<T> pat, Class<? extends Annotation> annotationType, BiConsumer<Class<?>, ? super AnnotatedMethod<? extends Object>> c ) { AnnotatedType<?> type = pat.getAnnotatedType(); Class<?> clazz = type.getJavaClass(); type.getMethods() .stream(). filter( meth -> meth.isAnnotationPresent( annotationType ) ). forEach( m -> c.accept( clazz, m ) ); }
public <T> void observeResources(@Observes @WithAnnotations({Health.class}) ProcessAnnotatedType<T> event) { AnnotatedType<T> annotatedType = event.getAnnotatedType(); Class<T> javaClass = annotatedType.getJavaClass(); for (Class<?> intf : javaClass.getInterfaces()) { if (intf.getName().equals(HealthCheck.class.getName())) { log.info(">> Discovered health check procedure " + javaClass); delegates.add(annotatedType); } } }
<T> void processAnnotatedType(@Observes @WithAnnotations(EnableSnoopClient.class) ProcessAnnotatedType<T> pat) { // workaround for WELD bug revealed by JDK8u60 final ProcessAnnotatedType<T> snoopAnnotated = pat; LOGGER.config(() -> "Found @EnableSnoopClient annotated class: " + snoopAnnotated.getAnnotatedType().getJavaClass().getName()); snoopEnabled = true; serviceName = snoopAnnotated.getAnnotatedType().getAnnotation(EnableSnoopClient.class).serviceName(); LOGGER.config(() -> "Snoop Service name is: " + serviceName); }
<T> void processAnnotatedType(@Observes @WithAnnotations({EnableEurekaClient.class}) ProcessAnnotatedType<T> pat) { LOGGER.config(() -> "Found @EnableEurekaClient annotated class: " + pat.getAnnotatedType().getJavaClass().getName()); eurekaEnabled = true; applicationName = pat.getAnnotatedType().getAnnotation(EnableEurekaClient.class).name(); LOGGER.config(() -> "Eureka application name is: " + applicationName); }
<T> void processAnnotatedType(@Observes ProcessAnnotatedType<T> pat) { if (pat.getAnnotatedType().isAnnotationPresent(Configuration.class)) { final AnnotatedType<T> type = pat.getAnnotatedType(); AnnotatedType<T> wrapped = new SpringConfigurationWrapper<>(type); pat.setAnnotatedType(wrapped); logger.debug("Configuration is added as CDIBean with Annotation: {} : {}", OcelotSpringConfiguration.class, wrapped); } }
<X> void processAggregateRootAnnotatedType( @Observes final ProcessAnnotatedType<X> pat, final BeanManager beanManager) { AnnotatedType<X> at = pat.getAnnotatedType(); boolean isAggregateRoot = AxonUtils.isAnnotatedAggregateRoot(at.getJavaClass()); if (isAggregateRoot) { configuration.add(AggregateRootInfo.of(beanManager, at)); pat.veto(); } }
<X> void processSagaAnnotatedType( @Observes final ProcessAnnotatedType<X> pat, final BeanManager beanManager) { AnnotatedType<X> at = pat.getAnnotatedType(); if (AxonUtils.isAnnotatedSaga(at.getJavaClass())) { configuration.add(SagaInfo.of(beanManager, at)); // pat.veto(); // don't veto this bean. Because we need it to discover EventScheduler injected beans } }
<X> void processCommandsAndEventsHandlerTypes( @Observes final ProcessAnnotatedType<X> pat, final BeanManager beanManager) { AnnotatedType<X> at = pat.getAnnotatedType(); boolean isCommandHandler = AxonUtils.isCommandHandler(at.getJavaClass()); boolean isEventHandler = AxonUtils.isEventHandler(at.getJavaClass()); Preconditions.checkArgument(!isEventHandler || !isCommandHandler, "Provided type cannot be both event and command handler: %s", at); if (isCommandHandler) { configuration.add(HandlerInfo.commandHandler(beanManager, at)); } else if (isEventHandler) { configuration.add(HandlerInfo.eventHandler(beanManager, at)); } }
public <T> void processAnnotatedType( @Observes @WithAnnotations({ FilterParameters.class, FilterParameter.class }) final ProcessAnnotatedType<T> event) { final AnnotatedType<T> annotatedType = event.getAnnotatedType(); if (annotatedType.isAnnotationPresent(FilterParameters.class)) { final FilterParameters annotation = annotatedType.getAnnotation(FilterParameters.class); addFilterParameters(annotation); } if (annotatedType.isAnnotationPresent(FilterParameter.class)) { // TODO deal with single parameters } }
public <X> void processInjectedPersistenceResources(@Observes ProcessAnnotatedType<X> event) { for (AnnotatedMethod<? super X> method : event.getAnnotatedType().getMethods()) handleAnnotatedMember(method); for (AnnotatedField<? super X> field : event.getAnnotatedType().getFields()) handleAnnotatedMember(field); }
/** * Looks for {@link MongoClientDefinition} annotation to capture it. * Also Checks if the application contains more than one of these definition */ void detectMongoClientDefinition( @Observes @WithAnnotations(MongoClientDefinition.class) ProcessAnnotatedType<?> pat) { AnnotatedType at = pat.getAnnotatedType(); MongoClientDefinition md = at.getAnnotation(MongoClientDefinition.class); String name = md.name(); if (mongoDef != null) { moreThanOne = true; } else { mongoDef = md; } }
<T> void processAnnotatedType(@Observes ProcessAnnotatedType<T> pat) { Logger.getAnonymousLogger().log(Level.INFO, "CDI Extension Processing Annotation -> {0}", pat. getAnnotatedType(). getJavaClass(). getName()); }
@SuppressWarnings({ "unchecked", "rawtypes" }) public void annotates(@Observes ProcessAnnotatedType pat) { AnnotatedType annotatedType = pat.getAnnotatedType(); Class type = annotatedType.getJavaClass(); // verify if it has Controller in its name if(type.getSimpleName().endsWith("Controller")) { pat.setAnnotatedType(new AnnotatedTypeControllerWrapper(annotatedType)); System.out.println("[CDI Extension] Controller found: " + type.getSimpleName()); } }
/** * Adds {@link Transactional} and {@link RequestScoped} to the given annotated type and converts * its EJB injection points into CDI injection points (i.e. it adds the {@link Inject}) * @param <X> the type of the annotated type * @param pat the process annotated type. */ private <X> void modifiyAnnotatedTypeMetadata(ProcessAnnotatedType<X> pat) { AnnotatedType at = pat.getAnnotatedType(); AnnotatedTypeBuilder<X> builder = new AnnotatedTypeBuilder<X>().readFromType(at); builder.addToClass(AnnotationInstances.TRANSACTIONAL).addToClass(AnnotationInstances.REQUEST_SCOPED); InjectionHelper.addInjectAnnotation(at, builder); //Set the wrapper instead the actual annotated type pat.setAnnotatedType(builder.create()); }
public <X> void processAnnotatedType(@Observes ProcessAnnotatedType<X> pat) { // Wrap this to override the annotations of the class final AnnotatedType<X> at = pat.getAnnotatedType(); final AnnotatedTypeConfigurator<X> cat = pat.configureAnnotatedType(); // Collect Secure annotation from the type List<Secure> typeSecureAnnotations = new ArrayList<Secure>(); for (Annotation annotation : pat.getAnnotatedType().getAnnotations()) { collectAnnotations(Secure.class, annotation, typeSecureAnnotations); } // Collect the Secure annotations from the methods for (AnnotatedMethodConfigurator<? super X> methodConfiguration : cat.methods()) { AnnotatedMethod<?> method = methodConfiguration.getAnnotated(); final List<Secure> methodAnnotations = new ArrayList<Secure>(typeSecureAnnotations); collectAnnotations(Secure.class, method, methodAnnotations); // Store in the map if we find Secure annotations if (methodAnnotations.size() > 0) { InterceptSecure is = new InterceptSecureImpl(methodAnnotations.toArray(new Secure[methodAnnotations.size()])); // Add InterceptSecure annotation methodConfiguration.add(is); interceptSecureForMethods.put(method.getJavaMember(), is); } } }
public <T> void observeResources(@WithAnnotations({ Path.class }) @Observes ProcessAnnotatedType<T> event) { AnnotatedType<T> annotatedType = event.getAnnotatedType(); if (!annotatedType.getJavaClass().isInterface()) { this.resources.add(annotatedType.getJavaClass()); } }
public <T> void observeProviders(@WithAnnotations({ Provider.class }) @Observes ProcessAnnotatedType<T> event) { AnnotatedType<T> annotatedType = event.getAnnotatedType(); if (!annotatedType.getJavaClass().isInterface()) { this.providers.add(annotatedType.getJavaClass()); } }
public ActivatableAlternativeBuilder(ProcessAnnotatedType<X> pat) { this.pat = pat; type = pat.getAnnotatedType(); javaClass = type.getJavaClass(); builder = new AnnotatedTypeBuilder<>(); builder.readFromType(type); }
public <X> void processAnnotatedTypes(@Observes ProcessAnnotatedType<X> pat) { AnnotatedType<X> type = pat.getAnnotatedType(); final Class<X> javaClass = type.getJavaClass(); if (javaClass.isAnnotationPresent(ActivatableTestImplementation.class)) { new ActivatableAlternativeBuilder<X>(pat).invoke(); } else if (ReflectionsUtils.isTestClass(javaClass)) { AnnotationUtils.addClassAnnotation(pat, new AnnotationLiteral<TestSuiteScoped>() { }); } else if (ReflectionsUtils.shouldProxyCdiType(javaClass)) { AnnotationUtils.addClassAnnotation(pat, new AnnotationLiteral<Replaceable>() { }); } updateDecoratedTypes(pat); }