Refine search
/** * Add an annotation to the class represented by pat. * * @param pat ProcessAnnotatedType for bean. * @param annotation annotation literal to add to the type. * @param <X> any type. */ public static <X> void addClassAnnotation(ProcessAnnotatedType<X> pat, Annotation annotation) { AnnotatedTypeBuilder<X> builder = new AnnotatedTypeBuilder<>(); builder.readFromType(pat.getAnnotatedType()); pat.setAnnotatedType(builder.addToClass(annotation).create()); } }
/** * Add an annotation to the specified method. If the method is not already * present, it will be added. * * @param method the method to add the annotation to * @param annotation the annotation to add * @throws IllegalArgumentException if the annotation is null */ public AnnotatedTypeBuilder<X> addToMethod(AnnotatedMethod<? super X> method, Annotation annotation) { return addToMethod(method.getJavaMember(), annotation); }
public <T> void processAnnotatedType(@Observes ProcessAnnotatedType<T> pat) { boolean modified = false; AnnotatedType<T> annotatedType = pat.getAnnotatedType(); AnnotatedTypeBuilder<T> builder = new AnnotatedTypeBuilder<T>().readFromType(annotatedType); for (AnnotatedField field : annotatedType.getFields()) { Context context = field.getAnnotation(Context.class); if (context != null) { builder.addToField(field, new AnnotationLiteral<Inject>() {}); builder.addToField(field, new AnnotationLiteral<JaxRsQualifier>() {}); modified = true; } } if (modified) { pat.setAnnotatedType(builder.create()); } }
ActivatableAlternativeBuilder(ProcessAnnotatedType<X> pat) { this.pat = pat; type = pat.getAnnotatedType(); javaClass = type.getJavaClass(); builder = new AnnotatedTypeBuilder<>(); builder.readFromType(type); }
AnnotatedTypeBuilder<T> builder = new AnnotatedTypeBuilder<T>().readFromType(annotatedType); Stateless stateless = annotatedType.getAnnotation(Stateless.class); Stateful stateful = annotatedType.getAnnotation(Stateful.class); EJB ejb = method.getAnnotation(EJB.class); if (ejb != null) { builder.addToMethod(method, EJbQualifierLiteral.INSTANCE); builder.removeFromMethod(method, EJB.class); modified = true; if (!ejb.beanName().isEmpty()) { builder.addToMethod(method,new EJbNameLiteral(ejb.beanName())); } else { builder.addToMethod(method,DefaultLiteral.INSTANCE); Produces produces = field.getAnnotation(Produces.class); if (produces == null) { builder.addToField(field, new AnnotationLiteral<Inject>(){private static final long serialVersionUID = 1L;}); builder.removeFromField(field, EJB.class); builder.addToField(field, EJbQualifierLiteral.INSTANCE); if (!ejb.beanName().isEmpty()) { builder.addToField(field,new EJbNameLiteral(ejb.beanName())); } else { builder.addToField(field, DefaultLiteral.INSTANCE); pat.setAnnotatedType(builder.create());
Synthetic declaringBeanSyntheticQualifier = null; Set<Annotation> declaringBeanQualifiers = null; if (event.getAnnotatedType().isAnnotationPresent(DefaultBean.class)) { Set<Annotation> qualifiers = new HashSet<Annotation>(); defaultBean = true; builder = new AnnotatedTypeBuilder<X>().readFromType(tp); for (Annotation a : tp.getAnnotations()) { builder.removeFromClass(a.annotationType()); builder.addToClass(new DefaultBeanInformation.Literal(qualifiers)); declaringBeanQualifiers = new HashSet<Annotation>(qualifiers); declaringBeanSyntheticQualifier = syntheticProvider.get(); beanTypeInformation.put(declaringBeanSyntheticQualifier, new DefaultBeanType(qualifiers, tp.getAnnotation(DefaultBean.class).value())); builder.addToClass(declaringBeanSyntheticQualifier); builder.removeFromMethodParameter(m.getJavaMember(), observerParameter.getPosition(), a.annotationType()); builder.addToMethodParameter(m.getJavaMember(), observerParameter.getPosition(), declaringBeanSyntheticQualifier); ObserverMethodInfo<?> info = ObserverMethodInfo.of(observerQualifiers, declaringBeanQualifiers, m, declaringBeanSyntheticQualifier); defaultObserverMethodsByBean.put(declaringBeanSyntheticQualifier, info); builder = new AnnotatedTypeBuilder<X>().readFromType(tp); builder.removeFromMethod(m, a.annotationType()); builder.addToMethod(m, new DefaultBeanInformation.Literal(qualifiers)); Synthetic syntheticQualifier = producerSyntheticProvider.get();
AnnotatedTypeBuilder<Cat> builder = new AnnotatedTypeBuilder<Cat>(); builder.readFromType(Cat.class); AnnotatedType<Cat> cat = builder.create(); builder.addToClass(new AlternativeLiteral()) .addToClass(new ApplicationScopedLiteral()) .removeFromClass(Named.class) .addToClass(new NamedLiteral("tomcat")); cat = builder.create(); assertNotNull(cat); 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()); 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));
void applyQualifier(Annotation qual, AnnotatedTypeBuilder<?> atb) { AnnotatedType<?> at = atb.create(); //do a loop on all field to replace annotation mark by CDI annotations for (AnnotatedField af : at.getFields()) if (af.isAnnotationPresent(InjectWithQualifier.class)) { atb.addToField(af, InjectLiteral.instance); atb.addToField(af, qual); } //loop on constructors to do the same for (AnnotatedConstructor ac : at.getConstructors()) { Annotation[][] pa = ac.getJavaMember().getParameterAnnotations(); //loop on args to detect marked param for (int i = 0; i < pa.length; i++) for (int j = 0; j < pa[i].length; j++) if (pa[i][j].equals(InjectWithQualifierLiteral.instance)) { atb.addToConstructor(ac, InjectLiteral.instance); atb.addToConstructorParameter(ac.getJavaMember(), i, qual); } } //loop on other methods (setters) for (AnnotatedMethod am : at.getMethods()) if (am.isAnnotationPresent(InjectWithQualifierLiteral.class)) { atb.addToMethod(am, InjectLiteral.instance); atb.addToMethod(am, qual); } }
@Test public void buildValidAnnotationAnnotatedType() { final AnnotatedTypeBuilder<Small> builder = new AnnotatedTypeBuilder<Small>(); builder.readFromType(Small.class); final AnnotatedType<Small> smallAnnotatedType = builder.create(); assertThat(smallAnnotatedType.getMethods().size(), is(1)); assertThat(smallAnnotatedType.getConstructors().size(), is(0)); assertThat(smallAnnotatedType.getFields().size(), is(0)); }
for (final Annotation annotation : type.getAnnotations()) builder = new AnnotatedTypeBuilder<X>().readFromType(type); builder.addToClass(INTERCEPTOR_BINDING); getMetaDataStorage().addSecuredType(type); isSecured = true; for (final AnnotatedMethod<? super X> m : type.getMethods()) builder = new AnnotatedTypeBuilder<X>().readFromType(type); builder.addToMethod(m, INTERCEPTOR_BINDING); getMetaDataStorage().addSecuredMethod(m); break; event.setAnnotatedType(builder.create());
@Test public void modifyAnnotationsOnConstructorParameter() throws NoSuchMethodException { final AnnotatedTypeBuilder<Cat> builder = new AnnotatedTypeBuilder<Cat>(); builder.readFromType(Cat.class, true); builder.removeFromConstructorParameter(Cat.class.getConstructor(String.class, String.class), 1, Default.class); builder.addToConstructorParameter(Cat.class.getConstructor(String.class, String.class), 1, new AnyLiteral()); final AnnotatedType<Cat> catAnnotatedType = builder.create(); Set<AnnotatedConstructor<Cat>> catCtors = catAnnotatedType.getConstructors(); assertThat(catCtors.size(), is(2)); for (AnnotatedConstructor<Cat> ctor : catCtors) { if (ctor.getParameters().size() == 2) { List<AnnotatedParameter<Cat>> ctorParams = ctor.getParameters(); assertThat(ctorParams.get(1).getAnnotations().size(), is(1)); assertThat((AnyLiteral) ctorParams.get(1).getAnnotations().toArray()[0], is(new AnyLiteral())); } } }
void invoke() { ActivatableTestImplementation implementation = type.getAnnotation(ActivatableTestImplementation.class); if (implementation.value().length == 0) { guessReplacableTypes(); } builder.addToClass(new TypedLiteral() { @Override public Class<?>[] value() { return new Class[]{javaClass}; } }); pat.setAnnotatedType(builder.create()); }
protected <T> Bean<T> createBean(Class<T> beanClass, BeanManager beanManager) { Class<? extends InvocationHandler> invocationHandlerClass = Converter.class.isAssignableFrom(beanClass) ? ConverterInvocationHandler.class : ValidatorInvocationHandler.class; AnnotatedType<T> annotatedType = new AnnotatedTypeBuilder<T>().readFromType(beanClass).create(); DeltaSpikeProxyContextualLifecycle lifecycle = new DeltaSpikeProxyContextualLifecycle(beanClass, invocationHandlerClass, ConverterAndValidatorProxyFactory.getInstance(), beanManager); BeanBuilder<T> beanBuilder = new BeanBuilder<T>(beanManager) .readFromType(annotatedType) .passivationCapable(true) .beanLifecycle(lifecycle); return beanBuilder.create(); } }
/** * Reads the annotations from an existing java type. Annotations already * present will be overwritten * * @param type the type to read from * @throws IllegalArgumentException if type is null */ public AnnotatedTypeBuilder<X> readFromType(Class<X> type) { return readFromType(type, true); }
private void guessReplacableTypes() { builder.removeFromClass(ActivatableTestImplementation.class); final ImmutableActivatableTestImplementation.Builder anntationBuilder = ImmutableActivatableTestImplementation.builder(); this.builder.addToClass(anntationBuilder.value(determineUniqueSuperclass()).build()); }
private <T> void processClass(AnnotatedTypeBuilder<T> builder, String name, boolean isSingleton, boolean scopeIsPresent) { logger.trace("processing class: {} singleton: {} scopeIsPresent: {}", name, isSingleton, scopeIsPresent); if (!scopeIsPresent) { if (!isSingleton || builder.getJavaClass().getFields().length > 0) { builder.addToClass(createDependentAnnotation()); } else { builder.addToClass(createApplicationScopedAnnotation()); // For Singleton normally only ApplicationScoped } } builder.addToClass(createDefaultAnnotation()); if (!name.isEmpty()) { builder.addToClass(new EjbName.EjbNameLiteral(name)); } else { builder.addToClass(DefaultLiteral.INSTANCE); } }
private static <T> void processClass(AnnotatedTypeBuilder<T> builder, String name ) { builder.addToClass(new AnnotationLiteral<ApplicationScoped>(){private static final long serialVersionUID = 1L;}); builder.addToClass(EJbQualifierLiteral.INSTANCE); if(!name.isEmpty() ) { builder.addToClass(new EJbNameLiteral(name)); } else { builder.addToClass(DefaultLiteral.INSTANCE); } } }
removeFromClass(annotationType); for (Map.Entry<Field, AnnotationBuilder> field : fields.entrySet())
/** * Add an annotation to the specified field. If the field is not already * present, it will be added. * * @param field the field to add the annotation to * @param annotation the annotation to add * @throws IllegalArgumentException if the annotation is null */ public AnnotatedTypeBuilder<X> addToField(AnnotatedField<? super X> field, Annotation annotation) { return addToField(field.getJavaMember(), annotation); }
/** * Remove an annotation from the specified field. * * @param field the field to remove the annotation from * @param annotationType the annotation type to remove * @throws IllegalArgumentException if the annotationType is null or if the * field is not currently declared on the type */ public AnnotatedTypeBuilder<X> removeFromField(AnnotatedField<? super X> field, Class<? extends Annotation> annotationType) { return removeFromField(field.getJavaMember(), annotationType); }