@Test public void testProducer() { logger.info("starting producer spi test"); CDI<Object> cdi = current(); BeanManager beanManager = cdi.select(BeanManager.class).get(); AnnotatedField<? super AnotherFactory> field = this .<AnotherFactory, AnnotatedField<AnotherFactory>>getAnnotatedMember(AnotherFactory.class, "jessie", beanManager); Bean<AnotherFactory> declaringBean = cast(beanManager.resolve(beanManager.getBeans(AnotherFactory.class))); ProducerFactory<AnotherFactory> producerFactory = beanManager.getProducerFactory(field, declaringBean); Producer<Toy> producer = cast(producerFactory.createProducer(null)); assertNotNull(producer); assertTrue(producer.getInjectionPoints().isEmpty()); Toy jessie = producer.produce(beanManager.<Toy>createCreationalContext(null)); assertEquals("Jessie", jessie.getName()); }
@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); }
private <X> AnnotatedField<X> decorateContext(AnnotatedField<X> field) { final PersistenceContext persistenceContext = field.getAnnotation(PersistenceContext.class); final UniqueIdentifier identifier = UniqueIdentifierLitteral.random(); Set<Annotation> templateQualifiers = new HashSet<>(); templateQualifiers.add(ServiceLiteral.SERVICE); if (hasUnitName(persistenceContext)) { templateQualifiers.add(new FilterLiteral("(osgi.unit.name=" + persistenceContext.unitName() + ")")); } Bean<JpaTemplate> bean = manager.getExtension(OsgiExtension.class) .globalDependency(JpaTemplate.class, templateQualifiers); Set<Annotation> qualifiers = new HashSet<>(); qualifiers.add(identifier); Bean<EntityManager> b = new SimpleBean<>(EntityManager.class, Dependent.class, Collections.singleton(EntityManager.class), qualifiers, () -> { CreationalContext<JpaTemplate> context = manager.createCreationalContext(bean); JpaTemplate template = (JpaTemplate) manager.getReference(bean, JpaTemplate.class, context); return EntityManagerProducer.create(template); }); beans.add(b); Set<Annotation> fieldAnnotations = new HashSet<>(); fieldAnnotations.add(InjectLiteral.INJECT); fieldAnnotations.add(identifier); return new SyntheticAnnotatedField<>(field, fieldAnnotations); }
private <X> AnnotatedField<X> decorateUnit(AnnotatedField<X> field) { final PersistenceUnit persistenceUnit = field.getAnnotation(PersistenceUnit.class); final UniqueIdentifier identifier = UniqueIdentifierLitteral.random(); Set<Annotation> templateQualifiers = new HashSet<>(); templateQualifiers.add(ServiceLiteral.SERVICE); if (hasUnitName(persistenceUnit)) { templateQualifiers.add(new FilterLiteral("(osgi.unit.name=" + persistenceUnit.unitName() + ")")); } Bean<EntityManagerFactory> bean = manager.getExtension(OsgiExtension.class) .globalDependency(EntityManagerFactory.class, templateQualifiers); Set<Annotation> qualifiers = new HashSet<>(); qualifiers.add(identifier); Bean<EntityManagerFactory> b = new SimpleBean<>(EntityManagerFactory.class, Dependent.class, Collections.singleton(EntityManagerFactory.class), qualifiers, () -> { CreationalContext<EntityManagerFactory> context = manager.createCreationalContext(bean); return (EntityManagerFactory) manager.getReference(bean, EntityManagerFactory.class, context); }); beans.add(b); Set<Annotation> fieldAnnotations = new HashSet<>(); fieldAnnotations.add(InjectLiteral.INJECT); fieldAnnotations.add(identifier); return new SyntheticAnnotatedField<>(field, fieldAnnotations); }
private static CamelContextNameStrategy nameStrategy(Annotated annotated) { if (annotated.isAnnotationPresent(ContextName.class)) { return new ExplicitCamelContextNameStrategy(annotated.getAnnotation(ContextName.class).value()); } else if (annotated.isAnnotationPresent(Named.class)) { // TODO: support stereotype with empty @Named annotation String name = annotated.getAnnotation(Named.class).value(); if (name.isEmpty()) { if (annotated instanceof AnnotatedField) { name = ((AnnotatedField) annotated).getJavaMember().getName(); } else if (annotated instanceof AnnotatedMethod) { name = ((AnnotatedMethod) annotated).getJavaMember().getName(); if (name.startsWith("get")) { name = decapitalize(name.substring(3)); } } else { name = decapitalize(getRawType(annotated.getBaseType()).getSimpleName()); } } return new ExplicitCamelContextNameStrategy(name); } else { // Use a specific naming strategy for Camel CDI as the default one increments the suffix for each CDI proxy created return new CdiCamelContextNameStrategy(); } }
static boolean hasAnnotation(AnnotatedType<?> type, Class<? extends Annotation> annotation) { if (type.isAnnotationPresent(annotation)) { return true; } for (AnnotatedMethod<?> method : type.getMethods()) { if (method.isAnnotationPresent(annotation)) { return true; } } for (AnnotatedConstructor<?> constructor : type.getConstructors()) { if (constructor.isAnnotationPresent(annotation)) { return true; } } for (AnnotatedField<?> field : type.getFields()) { if (field.isAnnotationPresent(annotation)) { return true; } } return false; }
private static CamelContextNameStrategy nameStrategy(Annotated annotated) { if (annotated.isAnnotationPresent(ContextName.class)) { return new ExplicitCamelContextNameStrategy(annotated.getAnnotation(ContextName.class).value()); } else if (annotated.isAnnotationPresent(Named.class)) { // TODO: support stereotype with empty @Named annotation String name = annotated.getAnnotation(Named.class).value(); if (name.isEmpty()) { if (annotated instanceof AnnotatedField) { name = ((AnnotatedField) annotated).getJavaMember().getName(); } else if (annotated instanceof AnnotatedMethod) { name = ((AnnotatedMethod) annotated).getJavaMember().getName(); if (name.startsWith("get")) name = decapitalize(name.substring(3)); } else { name = decapitalize(getRawType(annotated.getBaseType()).getSimpleName()); } } return new ExplicitCamelContextNameStrategy(name); } else { // Use a specific naming strategy for Camel CDI as the default one increments the suffix for each CDI proxy created return new CdiCamelContextNameStrategy(); } }
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); } }
public PersistenceAnnotatedType(BeanManager manager, AnnotatedType<T> delegate) { super(delegate); this.manager = manager; this.fields = new HashSet<>(); for (AnnotatedField<? super T> field : delegate.getFields()) { if (field.isAnnotationPresent(PersistenceContext.class)) { field = decorateContext(field); } else if (field.isAnnotationPresent(PersistenceUnit.class)) { field = decorateUnit(field); } this.fields.add(field); } }
private static <T> boolean isApplicable(AnnotatedType<T> at) { if (isApplicable(at.getJavaClass())) { return true; } Set<AnnotatedField<? super T>> fields = at.getFields(); for (AnnotatedField<? super T> field : fields) { if (field.isAnnotationPresent(Configurable.class)) { return true; } } return false; }
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); }
/** * 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); } } }
private <T> Map<Field, Object> assignPropertiesToFields(Set<AnnotatedField<? super T>> fields, Properties properties) { Map<Field, Object> fieldValues = new HashMap<Field, Object>(); for (AnnotatedField<? super T> field : fields) { if (field.isAnnotationPresent(Property.class)) { Property property = field.getAnnotation(Property.class); Object value = properties.get(property.value()); Field memberField = field.getJavaMember(); fieldValues.put(memberField, value); } } return fieldValues; }
protected boolean isMockSupportEnabled(Annotated annotated) { if ((annotated instanceof AnnotatedMethod || annotated instanceof AnnotatedField) && annotated.getAnnotation(Produces.class) != null) { return TestBaseConfig.MockIntegration.ALLOW_MOCKED_PRODUCERS; } else { return TestBaseConfig.MockIntegration.ALLOW_MOCKED_BEANS; } }
public int compare(AnnotatedField<? super T> arg0, AnnotatedField<? super T> arg1) { if (arg0.getJavaMember().getName().equals(arg1.getJavaMember().getName())) { return arg0.getJavaMember().getDeclaringClass().getName().compareTo(arg1.getJavaMember() .getDeclaringClass().getName()); } return arg0.getJavaMember().getName().compareTo(arg1.getJavaMember().getName()); }
/** * Instantiate a new {@link InjectionPoint} based upon an * {@link AnnotatedField}. * * @param field the field for which to create the injection point * @param qualifiers the qualifiers on the injection point * @param declaringBean the declaringBean declaring the injection point * @param isTransient <code>true</code> if the injection point is transient * @param delegate <code>true</code> if the injection point is a delegate * injection point on a decorator */ public ImmutableInjectionPoint(AnnotatedField<?> field, Set<Annotation> qualifiers, Bean<?> declaringBean, boolean isTransient, boolean delegate) { annotated = field; member = field.getJavaMember(); this.qualifiers = new HashSet<Annotation>(qualifiers); type = field.getJavaMember().getGenericType(); this.isTransient = isTransient; this.delegate = delegate; this.declaringBean = declaringBean; }
/** * Instantiate a new {@link InjectionPoint} based upon an * {@link AnnotatedField}, reading the qualifiers from the annotations * declared on the field. * * @param field the field for which to create the injection point * @param declaringBean the declaringBean declaring the injection point * @param isTransient <code>true</code> if the injection point is transient * @param delegate <code>true</code> if the injection point is a delegate * injection point on a decorator */ public ImmutableInjectionPoint(AnnotatedField<?> field, BeanManager beanManager, Bean<?> declaringBean, boolean isTransient, boolean delegate) { annotated = field; member = field.getJavaMember(); qualifiers = BeanUtils.getQualifiers(beanManager, field.getAnnotations()); type = field.getJavaMember().getGenericType(); this.isTransient = isTransient; this.delegate = delegate; this.declaringBean = declaringBean; }
@Override public Set<AnnotatedField<? super T>> getFields() { return unmodifiableSet(fields); }
public ForwardingAnnotatedField(AnnotatedField<X> delegate) { this.delegate = delegate; }
public SyntheticAnnotatedField(AnnotatedField<X> delegate) { this(delegate, Collections.<Annotation>emptyList()); }
@Override public Set<AnnotatedField<? super X>> getFields() { return delegate.getFields(); }
@Override public Set<AnnotatedField<? super T>> getFields() { return wrapped.getFields(); }
@Override public Set<AnnotatedField<? super X>> getFields() { return original.getFields(); }
@Override public Set<AnnotatedField<? super X>> getFields() { return decoratedType.getFields(); }
public Set<AnnotatedField<? super T>> getFields() { return delegate.getFields(); }
@Override public Set<AnnotatedField<? super T>> getFields() { return delegate.getFields(); }
@Override public <X> ProducerFactory<X> getProducerFactory(AnnotatedField<? super X> field, Bean<X> declaringBean) { // TODO Auto-generated method stub return null; }
@Override public InjectionPoint createInjectionPoint(AnnotatedField<?> field) { // TODO Auto-generated method stub return null; }
@Override public Set<AnnotatedField<? super X>> getFields() { // TODO Auto-generated method stub return null; }
@Override public Set<AnnotatedField<? super T>> getFields() { return type.getFields(); }
@Override public Set<AnnotatedField<? super PersistenceUnitProducer>> getFields() { return annotatedFields; }
@Override public Set<AnnotatedField<? super RequestScoped>> getFields() { return aType.getFields(); }
@Override public Set<AnnotatedField<? super SessionScoped>> getFields() { return aType.getFields(); }
@Override public InjectionPoint createInjectionPoint(final AnnotatedField<?> arg0) { return this.beanManager.createInjectionPoint(arg0); }
@Override public <X> ProducerFactory<X> getProducerFactory( final AnnotatedField<? super X> arg0, final Bean<X> arg1) { return this.beanManager.getProducerFactory(arg0, arg1); }
public static <T> Comparator<AnnotatedField<? super T>> instance() { return new AnnotatedFieldComparator<T>(); }
/** * Generates a unique signature for a concrete class. Annotations are not * read directly from the class, but are read from the * <code>annotations</code>, <code>methods</code>, <code>fields</code> and * <code>constructors</code> arguments * * @param clazz The java class type * @param annotations Annotations present on the java class * @param methods The AnnotatedMethods to include in the signature * @param fields The AnnotatedFields to include in the signature * @param constructors The AnnotatedConstructors to include in the signature * @return A string representation of the type */ public static <X> String createTypeId(Class<X> clazz, Collection<Annotation> annotations, Collection<AnnotatedMethod<? super X>> methods, Collection<AnnotatedField<? super X>> fields, Collection<AnnotatedConstructor<X>> constructors) { StringBuilder builder = new StringBuilder(); builder.append(clazz.getName()); builder.append(createAnnotationCollectionId(annotations)); builder.append("{"); // now deal with the fields List<AnnotatedField<? super X>> sortedFields = new ArrayList<AnnotatedField<? super X>>(); sortedFields.addAll(fields); Collections.sort(sortedFields, AnnotatedFieldComparator.<X>instance()); for (AnnotatedField<? super X> field : sortedFields) { if (!field.getAnnotations().isEmpty()) { builder.append(createFieldId(field)); builder.append(SEPARATOR); } } // methods List<AnnotatedMethod<? super X>> sortedMethods = new ArrayList<AnnotatedMethod<? super X>>(); sortedMethods.addAll(methods); Collections.sort(sortedMethods, AnnotatedMethodComparator.<X>instance()); for (AnnotatedMethod<? super X> method : sortedMethods) { if (!method.getAnnotations().isEmpty() || hasMethodParameters(method)) { builder.append(createCallableId(method)); builder.append(SEPARATOR); } } // constructors List<AnnotatedConstructor<? super X>> sortedConstructors = new ArrayList<AnnotatedConstructor<? super X>>(); sortedConstructors.addAll(constructors); Collections.sort(sortedConstructors, AnnotatedConstructorComparator.<X>instance()); for (AnnotatedConstructor<? super X> constructor : sortedConstructors) { if (!constructor.getAnnotations().isEmpty() || hasMethodParameters(constructor)) { builder.append(createCallableId(constructor)); builder.append(SEPARATOR); } } builder.append("}"); return builder.toString(); }
/** * {@inheritDoc} */ @Override public Set<AnnotatedField<? super X>> getFields() { return Collections.unmodifiableSet(fields); }