@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())); } } }
/** * Add an annotation to the specified parameter. If the callable which * declares the parameter is not already present, it will be added. If the * parameter is not already present on the callable, it will be added. * * @param parameter the parameter to add the annotation to * @param annotation the annotation to add * @throws IllegalArgumentException if the annotation is null or if the * parameter is not declared on either a constructor or a method */ public AnnotatedTypeBuilder<X> addToParameter(AnnotatedParameter<? super X> parameter, Annotation annotation) { if (parameter.getDeclaringCallable().getJavaMember() instanceof Method) { Method method = (Method) parameter.getDeclaringCallable().getJavaMember(); return addToMethodParameter(method, parameter.getPosition(), annotation); } if (parameter.getDeclaringCallable().getJavaMember() instanceof Constructor<?>) { @SuppressWarnings("unchecked") Constructor<X> constructor = (Constructor<X>) parameter.getDeclaringCallable().getJavaMember(); return addToConstructorParameter(constructor, parameter.getPosition(), annotation); } else { throw new IllegalArgumentException("Cannot remove from parameter " + parameter + " - cannot operate on member " + parameter.getDeclaringCallable().getJavaMember()); } }
/** * Add an annotation to the specified parameter. If the callable which * declares the parameter is not already present, it will be added. If the * parameter is not already present on the callable, it will be added. * * @param parameter the parameter to add the annotation to * @param annotation the annotation to add * @throws IllegalArgumentException if the annotation is null or if the * parameter is not declared on either a constructor or a method */ public AnnotatedTypeBuilder<X> addToParameter(AnnotatedParameter<? super X> parameter, Annotation annotation) { if (parameter.getDeclaringCallable().getJavaMember() instanceof Method) { Method method = (Method) parameter.getDeclaringCallable().getJavaMember(); return addToMethodParameter(method, parameter.getPosition(), annotation); } if (parameter.getDeclaringCallable().getJavaMember() instanceof Constructor<?>) { @SuppressWarnings("unchecked") Constructor<X> constructor = (Constructor<X>) parameter.getDeclaringCallable().getJavaMember(); return addToConstructorParameter(constructor, parameter.getPosition(), annotation); } else { throw new IllegalArgumentException("Cannot remove from parameter " + parameter + " - cannot operate on member " + parameter.getDeclaringCallable().getJavaMember()); } }
/** * Add an annotation to the specified parameter. If the callable which * declares the parameter is not already present, it will be added. If the * parameter is not already present on the callable, it will be added. * * @param parameter the parameter to add the annotation to * @param annotation the annotation to add * @throws IllegalArgumentException if the annotation is null or if the * parameter is not declared on either a constructor or a method */ public AnnotatedTypeBuilder<X> addToParameter(AnnotatedParameter<? super X> parameter, Annotation annotation) { if (parameter.getDeclaringCallable().getJavaMember() instanceof Method) { Method method = (Method) parameter.getDeclaringCallable().getJavaMember(); return addToMethodParameter(method, parameter.getPosition(), annotation); } if (parameter.getDeclaringCallable().getJavaMember() instanceof Constructor<?>) { @SuppressWarnings("unchecked") Constructor<X> constructor = (Constructor<X>) parameter.getDeclaringCallable().getJavaMember(); return addToConstructorParameter(constructor, parameter.getPosition(), annotation); } else { throw new IllegalArgumentException("Cannot remove from parameter " + parameter + " - cannot operate on member " + parameter.getDeclaringCallable().getJavaMember()); } }
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); } }