@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())); } } }
/** * Remove an annotation from the specified parameter. * * @param parameter the parameter to remove the annotation from * @param annotationType the annotation type to remove * @throws IllegalArgumentException if the annotationType is null, if the * callable which declares the parameter is not currently declared * on the type or if the parameter is not declared on either a * constructor or a method */ public AnnotatedTypeBuilder<X> removeFromParameter(AnnotatedParameter<? super X> parameter, Class<? extends Annotation> annotationType) { if (parameter.getDeclaringCallable().getJavaMember() instanceof Method) { Method method = (Method) parameter.getDeclaringCallable().getJavaMember(); return removeFromMethodParameter(method, parameter.getPosition(), annotationType); } if (parameter.getDeclaringCallable().getJavaMember() instanceof Constructor<?>) { @SuppressWarnings("unchecked") Constructor<X> constructor = (Constructor<X>) parameter.getDeclaringCallable().getJavaMember(); return removeFromConstructorParameter(constructor, parameter.getPosition(), annotationType); } else { throw new IllegalArgumentException("Cannot remove from parameter " + parameter + " - cannot operate on member " + parameter.getDeclaringCallable().getJavaMember()); } }
/** * Remove an annotation from the specified parameter. * * @param parameter the parameter to remove the annotation from * @param annotationType the annotation type to remove * @throws IllegalArgumentException if the annotationType is null, if the * callable which declares the parameter is not currently declared * on the type or if the parameter is not declared on either a * constructor or a method */ public AnnotatedTypeBuilder<X> removeFromParameter(AnnotatedParameter<? super X> parameter, Class<? extends Annotation> annotationType) { if (parameter.getDeclaringCallable().getJavaMember() instanceof Method) { Method method = (Method) parameter.getDeclaringCallable().getJavaMember(); return removeFromMethodParameter(method, parameter.getPosition(), annotationType); } if (parameter.getDeclaringCallable().getJavaMember() instanceof Constructor<?>) { @SuppressWarnings("unchecked") Constructor<X> constructor = (Constructor<X>) parameter.getDeclaringCallable().getJavaMember(); return removeFromConstructorParameter(constructor, parameter.getPosition(), annotationType); } else { throw new IllegalArgumentException("Cannot remove from parameter " + parameter + " - cannot operate on member " + parameter.getDeclaringCallable().getJavaMember()); } }
/** * Remove an annotation from the specified parameter. * * @param parameter the parameter to remove the annotation from * @param annotationType the annotation type to remove * @throws IllegalArgumentException if the annotationType is null, if the * callable which declares the parameter is not currently declared * on the type or if the parameter is not declared on either a * constructor or a method */ public AnnotatedTypeBuilder<X> removeFromParameter(AnnotatedParameter<? super X> parameter, Class<? extends Annotation> annotationType) { if (parameter.getDeclaringCallable().getJavaMember() instanceof Method) { Method method = (Method) parameter.getDeclaringCallable().getJavaMember(); return removeFromMethodParameter(method, parameter.getPosition(), annotationType); } if (parameter.getDeclaringCallable().getJavaMember() instanceof Constructor<?>) { @SuppressWarnings("unchecked") Constructor<X> constructor = (Constructor<X>) parameter.getDeclaringCallable().getJavaMember(); return removeFromConstructorParameter(constructor, parameter.getPosition(), annotationType); } else { throw new IllegalArgumentException("Cannot remove from parameter " + parameter + " - cannot operate on member " + parameter.getDeclaringCallable().getJavaMember()); } }