@SuppressWarnings("unchecked") private <X, A extends AnnotatedMember<? super X>> A getAnnotatedMember(Class<X> javaClass, String memberName, BeanManager beanManager) { AnnotatedType<X> type = beanManager.createAnnotatedType(javaClass); for (AnnotatedField<? super X> field : type.getFields()) { if (field.getJavaMember().getName().equals(memberName)) { return (A) field; } } for (AnnotatedMethod<? super X> method : type.getMethods()) { if (method.getJavaMember().getName().equals(memberName)) { return (A) method; } } throw new IllegalArgumentException("Member " + memberName + " not found on " + javaClass); }
static String getConfigKey(InjectionPoint ip, ConfigProperty configProperty) { String key = configProperty.name(); if (!key.trim().isEmpty()) { return key; } if (ip.getAnnotated() instanceof AnnotatedMember) { AnnotatedMember member = (AnnotatedMember) ip.getAnnotated(); AnnotatedType declaringType = member.getDeclaringType(); if (declaringType != null) { String[] parts = declaringType.getJavaClass().getCanonicalName().split("\\."); StringBuilder sb = new StringBuilder(parts[0]); for (int i = 1; i < parts.length; i++) { sb.append(".").append(parts[i]); } sb.append(".").append(member.getJavaMember().getName()); return sb.toString(); } } throw new IllegalStateException("Could not find default name for @ConfigProperty InjectionPoint " + ip); }
void registerMetrics(@Observes AfterDeploymentValidation adv, BeanManager manager) { // Produce and register custom metrics MetricRegistry registry = getReference(manager, MetricRegistry.class); MetricName name = getReference(manager, MetricName.class); for (Map.Entry<Bean<?>, AnnotatedMember<?>> bean : metrics.entrySet()) { if (// skip non @Default beans !bean.getKey().getQualifiers().contains(DEFAULT) // skip producer methods with injection point metadata || hasInjectionPointMetadata(bean.getValue())) { continue; } String metricName = name.of(bean.getValue()); registry.register(metricName, getReference(manager, bean.getValue().getBaseType(), bean.getKey())); } // Let's clear the collected metric producers metrics.clear(); }
/** * Get the property key to use. * In case the {@link ConfigProperty#name()} is empty we will try to determine the key name from the InjectionPoint. */ public static String getConfigKey(InjectionPoint ip, ConfigProperty configProperty) { String key = configProperty.name(); if (key.length() > 0) { return key; } if (ip.getAnnotated() instanceof AnnotatedMember) { AnnotatedMember member = (AnnotatedMember) ip.getAnnotated(); AnnotatedType declaringType = member.getDeclaringType(); if (declaringType != null) { String[] parts = declaringType.getJavaClass().getName().split("."); String cn = parts[parts.length-1]; parts[parts.length-1] = Character.toLowerCase(cn.charAt(0)) + (cn.length() > 1 ? cn.substring(1) : ""); StringBuilder sb = new StringBuilder(parts[0]); for (int i = 1; i < parts.length; i++) { sb.append(".").append(parts[i]); } // now add the field name sb.append(".").append(member.getJavaMember().getName()); return sb.toString(); } } throw new IllegalStateException("Could not find default name for @ConfigProperty InjectionPoint " + ip); }
/** * Collect all SqlSessionFactory producers annotated with SessionFactoryProvider. * * @param <T> the generic type * @param <X> the generic type * @param pp the pp */ @SuppressWarnings("UnusedDeclaration") protected <T, X> void processProducer(@Observes final ProcessProducer<T, X> pp) { final AnnotatedMember<T> am = pp.getAnnotatedMember(); final boolean isAnnotated = am.isAnnotationPresent(SessionFactoryProvider.class); final boolean isSqlSessionFactory = am.getBaseType().equals(SqlSessionFactory.class); final Object[] logData = { am.getJavaMember().getDeclaringClass().getSimpleName(), am.getJavaMember().getName() }; if (isAnnotated && isSqlSessionFactory) { LOGGER.log(Level.INFO, "MyBatis CDI Module - SqlSessionFactory producer {0}.{1}", logData); this.sessionProducers.add(new BeanKey((Class<Type>) (Type) SqlSession.class, am.getAnnotations())); } else if (isAnnotated && !isSqlSessionFactory) { LOGGER.log(Level.SEVERE, "MyBatis CDI Module - Invalid return type (Must be SqlSessionFactory): {0}.{1}", logData); pp.addDefinitionError(new MybatisCdiConfigurationException(String .format("SessionFactoryProvider producers must return SqlSessionFactory (%s.%s)", logData[0], logData[1]))); } else if (!isAnnotated && isSqlSessionFactory) { LOGGER.log(Level.WARNING, "MyBatis CDI Module - Ignored SqlSessionFactory producer because it is not annotated with @SessionFactoryProvider: {0}.{1}", logData); } }
private void configuration(@Observes AfterDeploymentValidation adv, BeanManager manager) { // Fire configuration event manager.fireEvent(configuration); configuration.unmodifiable(); // Produce and register custom metrics MetricRegistry registry = getReference(manager, MetricRegistry.class); MetricName name = getReference(manager, MetricName.class); for (Map.Entry<Bean<?>, AnnotatedMember<?>> bean : metrics.entrySet()) { // TODO: add MetricSet metrics into the metric registry if (bean.getKey().getTypes().contains(MetricSet.class) // skip non @Default beans || !bean.getKey().getQualifiers().contains(DEFAULT) // skip producer methods with injection point || hasInjectionPoints(bean.getValue())) continue; registry.register(name.of(bean.getValue()), (Metric) getReference(manager, bean.getValue().getBaseType(), bean.getKey())); } // Let's clear the collected metric producers metrics.clear(); }
private static boolean hasInjectionPointMetadata(AnnotatedMember<?> member) { if (!(member instanceof AnnotatedMethod)) { return false; } AnnotatedMethod<?> method = (AnnotatedMethod<?>) member; for (AnnotatedParameter<?> parameter : method.getParameters()) { if (parameter.getBaseType().equals(InjectionPoint.class)) { return true; } } return false; }
@Override public String of(InjectionPoint ip) { Annotated annotated = ip.getAnnotated(); if (annotated instanceof AnnotatedMember) { return of((AnnotatedMember<?>) annotated); } else if (annotated instanceof AnnotatedParameter) { return of((AnnotatedParameter<?>) annotated); } else { throw new IllegalArgumentException("Unable to retrieve metric name for injection point [" + ip + "], only members and parameters are supported"); } }
@Override public String of(AnnotatedMember<?> member) { if (member.isAnnotationPresent(Metric.class)) { Metric metric = member.getAnnotation(Metric.class); String name = (metric.name().isEmpty()) ? member.getJavaMember().getName() : of(metric.name()); return metric.absolute() | parameters.contains(MetricsParameter.useAbsoluteName) ? name : MetricRegistry.name(member.getJavaMember().getDeclaringClass(), name); } else { return parameters.contains(MetricsParameter.useAbsoluteName) ? member.getJavaMember().getName() : MetricRegistry.name(member.getJavaMember().getDeclaringClass(), member.getJavaMember().getName()); } }
<T, X extends CommandBus> void processCommandBusProducer( @Observes final ProcessProducer<T, X> processProducer, final BeanManager beanManager) { AnnotatedMember<T> annotatedMember = processProducer.getAnnotatedMember(); if (annotatedMember.isAnnotationPresent(AutoConfigure.class)) { Producer<X> originalProducer = processProducer.getProducer(); Producer<X> producer = new AutoConfiguringCommandBusProducer<>(originalProducer, annotatedMember, getAggregateRoots(), getCommandHandlers(), beanManager); processProducer.setProducer(producer); } }
<T, X extends EventStore> void processEventStoreProducer( @Observes final ProcessProducer<T, X> processProducer, final BeanManager beanManager) { AnnotatedMember<T> annotatedMember = processProducer.getAnnotatedMember(); if (annotatedMember.isAnnotationPresent(AutoConfigure.class)) { Producer<X> originalProducer = processProducer.getProducer(); Producer<X> producer = new AutoConfiguringEventStoreProducer<>(originalProducer, annotatedMember, getEventHandlers(), getSagas(), beanManager); processProducer.setProducer(producer); } }
<T, X extends Snapshotter> void processSnapshotterProducer( @Observes final ProcessProducer<T, X> processProducer, final BeanManager beanManager) { Producer<X> originalProducer = processProducer.getProducer(); AnnotatedMember<T> annotatedMember = processProducer.getAnnotatedMember(); if (annotatedMember.isAnnotationPresent(AutoConfigure.class)) { Producer<X> producer = new AutoConfiguringAggregateSnapshotterProducer<>(originalProducer, annotatedMember, getAggregateRoots(), beanManager); processProducer.setProducer(producer); } }
<T, X extends SagaRepository<?>> void processSagaRepositoryProducer( @Observes final ProcessProducer<T, X> processProducer, final BeanManager beanManager) { Producer<X> originalProducer = processProducer.getProducer(); AnnotatedMember<T> annotatedMember = processProducer.getAnnotatedMember(); if (annotatedMember.isAnnotationPresent(AutoConfigure.class)) { Producer<X> producer = new AutoConfiguringSagaRepositoryProducer<>(originalProducer, annotatedMember, beanManager); processProducer.setProducer(producer); } }
public AbstractAutoConfiguringProducer(final Producer<X> wrappedProducer, final AnnotatedMember<?> annotatedMember, final BeanManager beanManager) { this.wrappedProducer = wrappedProducer; this.beanManager = beanManager; this.annotatedMember = annotatedMember; }
public AutoConfiguringEventStoreProducer(final Producer<X> wrappedProducer, final AnnotatedMember<?> annotatedMember, final Set<HandlerInfo> eventHandlersInfo, final Set<SagaInfo> sagasInfo, final BeanManager beanManager) { super(wrappedProducer, annotatedMember, beanManager); this.eventHandlersInfo = eventHandlersInfo; this.sagaManagersInfo = SagaManagerInfo.from(sagasInfo); }
public AutoConfiguringCommandBusProducer(Producer<X> wrappedProducer, AnnotatedMember<?> annotatedMember, Set<AggregateRootInfo> aggregateRootsInfo, Set<HandlerInfo> commandHandlersInfo, BeanManager beanManager) { super(wrappedProducer, annotatedMember, beanManager); this.commandHandlersInfo = commandHandlersInfo; this.aggregateRootsInfo = aggregateRootsInfo; }
private void handleAnnotatedMember(AnnotatedMember<?> member) { if (member.isAnnotationPresent(PersistenceContext.class)) { PersistenceContext persistenceContext = member.getAnnotation(PersistenceContext.class); injectedPersistenceContexts.put(persistenceContext.unitName(), member); } if (member.isAnnotationPresent(PersistenceUnit.class)) { PersistenceUnit persistenceUnit = member.getAnnotation(PersistenceUnit.class); injectedPersistenceUnits.add(persistenceUnit.unitName()); } }
public void produceMissingPersistenceUnits(@Observes AfterTypeDiscovery event, BeanManager beanManager) { for (String unitName : injectedPersistenceUnits) injectedPersistenceContexts.remove(unitName); for (AnnotatedMember<?> member : injectedPersistenceContexts.values()) { if (!member.isAnnotationPresent(SpearalEnabled.class)) continue; PersistenceContext persistenceContext = member.getAnnotation(PersistenceContext.class); try { final Set<Annotation> annotations = new HashSet<Annotation>(member.getAnnotations()); Iterator<Annotation> ia = annotations.iterator(); while (ia.hasNext()) { Annotation a = ia.next(); if (a.annotationType().equals(PersistenceContext.class)) ia.remove(); } PersistenceUnit persistenceUnit = new PersistenceUnitAnnotation(persistenceContext.name(), persistenceContext.unitName()); annotations.add(persistenceUnit); final AnnotatedType<PersistenceUnitProducer> annotatedPU = new AnnotatedPersistenceUnitProducerType(annotations); event.addAnnotatedType(annotatedPU, "org.spearal.jpa2.PersistenceUnit." + persistenceContext.unitName()); } catch (Exception e) { log.logp(Level.WARNING, SpearalExtension.class.getName(), "afterTypeDiscovery", "Could not setup PersistenceUnit integration {0}", new Object[] { persistenceContext.unitName() }); } } }
private static boolean hasInjectionPoints(AnnotatedMember<?> member) { if (!(member instanceof AnnotatedMethod)) return false; AnnotatedMethod<?> method = (AnnotatedMethod<?>) member; for (AnnotatedParameter<?> parameter : method.getParameters()) { if (parameter.getBaseType().equals(InjectionPoint.class)) return true; } return false; }
@Override public String of(InjectionPoint ip) { Annotated annotated = ip.getAnnotated(); if (annotated instanceof AnnotatedMember) return of((AnnotatedMember<?>) annotated); else if (annotated instanceof AnnotatedParameter) return of((AnnotatedParameter<?>) annotated); else throw new IllegalArgumentException("Unable to retrieve metric name for injection point [" + ip + "], only members and parameters are supported"); }
@Override public String of(AnnotatedMember<?> member) { if (member.isAnnotationPresent(Metric.class)) { Metric metric = member.getAnnotation(Metric.class); String name = (metric.name().isEmpty()) ? member.getJavaMember().getName() : of(metric.name()); return metric.absolute() | parameters.contains(useAbsoluteName) ? name : MetricRegistry.name(member.getJavaMember().getDeclaringClass(), name); } else { return parameters.contains(useAbsoluteName) ? member.getJavaMember().getName() : MetricRegistry.name(member.getJavaMember().getDeclaringClass(), member.getJavaMember().getName()); } }
AnnotatedMemberDelegate(AnnotatedMember<T> delegate, Set<Annotation> annotations) { super(delegate, annotations); this.delegate = delegate; }
@Override public BeanAttributes<?> createBeanAttributes(AnnotatedMember<?> type) { // TODO Auto-generated method stub return null; }
protected final AnnotatedMember<?> getAnnotatedMember() { return annotatedMember; }
public AutoConfiguringAggregateSnapshotterProducer(final Producer<X> wrappedProducer, final AnnotatedMember<?> annotatedMember, final Set<AggregateRootInfo> aggregateRoots, final BeanManager beanManager) { super(wrappedProducer, annotatedMember, beanManager); this.aggregateRootsInfo = aggregateRoots; }
public AutoConfiguringSagaRepositoryProducer(final Producer<X> wrappedProducer, final AnnotatedMember<?> annotatedMember, final BeanManager beanManager) { super(wrappedProducer, annotatedMember, beanManager); }
@Override public BeanAttributes<?> createBeanAttributes(final AnnotatedMember<?> arg0) { return this.beanManager.createBeanAttributes(arg0); }
@Override public boolean isMockedImplementationSupported(BeanManager beanManager, Annotated annotated) { if (!isMockSupportEnabled(annotated)) { return false; } Class origin = null; if (annotated instanceof AnnotatedType) { origin = ((AnnotatedType)annotated).getJavaClass(); Set<Annotation> annotations = new HashSet<Annotation>(); annotations.addAll(annotated.getAnnotations()); for (AnnotatedMethod annotatedMethod : (Set<javax.enterprise.inject.spi.AnnotatedMethod>)((AnnotatedType) annotated).getMethods()) { annotations.addAll(annotatedMethod.getAnnotations()); } if (isEjbOrAnnotatedTypeWithInterceptorAnnotation( beanManager, annotations, origin.getName())) { return false; } } else if (annotated instanceof AnnotatedMember) { Member member = ((AnnotatedMember)annotated).getJavaMember(); origin = member.getDeclaringClass(); if (isEjbOrAnnotatedTypeWithInterceptorAnnotation( beanManager, annotated.getAnnotations(), member.toString())) { return false; } } if (origin != null && origin.getPackage() == null) { LOG.warning("Please don't use the default-package for " + origin.getName()); return true; } return origin != null && !isInternalPackage(origin.getPackage().getName()); }
public <X, T> void onProcessProducer(@Observes ProcessProducer<X, T> processProducer, BeanManager beanManager) { if (!isActivated) { return; } for (MockFilter mockFilter : mockFilters) { if (!mockFilter.isMockedImplementationSupported(beanManager, processProducer.getAnnotatedMember())) { return; } } final Producer<T> originalProducer = processProducer.getProducer(); AnnotatedMember<X> annotatedMember = processProducer.getAnnotatedMember(); List<Annotation> qualifiers = new ArrayList<Annotation>(); for (Annotation annotation : annotatedMember.getAnnotations()) { if (beanManager.isQualifier(annotation.annotationType())) { qualifiers.add(annotation); } } Typed typed = annotatedMember.getAnnotation(Typed.class); List<Type> foundTypes = new ArrayList<Type>(); if (typed != null) { Collections.addAll(foundTypes, typed.value()); } else if (annotatedMember.getBaseType() instanceof Class) { foundTypes.addAll(extractTypes((Class)annotatedMember.getBaseType())); } if (foundTypes.isEmpty()) { return; } processProducer.setProducer(new MockAwareProducerWrapper<T>( beanManager, originalProducer, foundTypes, qualifiers)); }
/** * Returns <code>true</code> if at least one of the following Java EE annotations is present in the given member: * <ul> * <li> {@link EJB} * <li> {@link PersistenceContext} * <li> {@link Resource} * </ul> * Otherwise, it returns <code>false</code>. * @param <X> the type of the annotated member. * @param member the member whose annotations should be verified. * @return <code>true</code> if the member is at least annotated with one of the following annotations: {@link EJB}, {@link PersistenceContext} or {@link Resource}. */ private static <X> boolean hasJavaEEAnnotations(AnnotatedMember<? super X> member) { for(Class<? extends Annotation> javaEEannotation : JAVA_EE_ANNOTATIONS) { if (member.isAnnotationPresent(javaEEannotation)) { return true; } } return false; }
/** * Returns <code>true</code> if the member is NOT annotated with {@link Inject} and is annotated with one of the following annotations: * <ul> * <li> {@link EJB} * <li> {@link PersistenceContext} * <li> {@link Resource} * </ul> * Otherwise, it returns <code>false</code>. * * @param <X> * the type of the annotated member * @param member * the annotated member whose annotations should be verified. * @return <code>true</code> if the member is NOT annotated with {@link Inject} and is annotated with {@link EJB}, * {@link PersistenceContext} or {@link Resource} */ public static <X> boolean shouldInjectionAnnotationBeAddedToMember(AnnotatedMember<? super X> member) { return !member.isAnnotationPresent(Inject.class) && hasJavaEEAnnotations(member); }
String of(AnnotatedMember<?> member);