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; }
public int compare(AnnotatedConstructor<? super T> arg0, AnnotatedConstructor<? super T> arg1) { int result = callableComparator.compare(arg0, arg1); if (result != 0) { return result; } for (int i = 0; i < arg0.getJavaMember().getParameterTypes().length; ++i) { Class<?> p0 = arg0.getJavaMember().getParameterTypes()[i]; Class<?> p1 = arg1.getJavaMember().getParameterTypes()[i]; result = p0.getName().compareTo(p1.getName()); if (result != 0) { return result; } } return 0; }
@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())); } } }
@Override public Set<AnnotatedConstructor<X>> getConstructors() { return delegate.getConstructors(); }
@Override public Set<AnnotatedConstructor<T>> getConstructors() { return wrapped.getConstructors(); }
@Override public Set<AnnotatedConstructor<X>> getConstructors() { return original.getConstructors(); }
@Override public Set<AnnotatedConstructor<X>> getConstructors() { return decoratedType.getConstructors(); }
public Set<AnnotatedConstructor<T>> getConstructors() { return delegate.getConstructors(); }
@Override public Set<AnnotatedConstructor<T>> getConstructors() { return delegate.getConstructors(); }
@Override public Set<AnnotatedConstructor<X>> getConstructors() { // TODO Auto-generated method stub return null; }
@Override public Set<AnnotatedConstructor<T>> getConstructors() { return type.getConstructors(); }
public AnnotatedPersistenceUnitProducerType(Set<Annotation> annotations) throws Exception { this.annotations = annotations; this.annotatedConstructors = new HashSet<AnnotatedConstructor<PersistenceUnitProducer>>(); this.annotatedConstructors.add(new AnnotatedPersistenceUnitProducerConstructor()); }
@Override public Set<AnnotatedConstructor<PersistenceUnitProducer>> getConstructors() { return annotatedConstructors; }
@Override public Set<AnnotatedConstructor<RequestScoped>> getConstructors() { return aType.getConstructors(); }
@Override public Set<AnnotatedConstructor<SessionScoped>> getConstructors() { return aType.getConstructors(); }
public static <T> Comparator<AnnotatedConstructor<? super T>> instance() { return new AnnotatedConstructorComparator<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<AnnotatedConstructor<X>> getConstructors() { return Collections.unmodifiableSet(constructors); }
/** * Add an annotation to the specified constructor. If the constructor is not * already present, it will be added. * * @param constructor the constructor to add the annotation to * @param annotation the annotation to add * @throws IllegalArgumentException if the annotation is null */ public AnnotatedTypeBuilder<X> addToConstructor(AnnotatedConstructor<X> constructor, Annotation annotation) { return addToConstructor(constructor.getJavaMember(), annotation); }
/** * Remove an annotation from the specified constructor. * * @param constructor the constructor to add the annotation to * @param annotationType the annotation to add * @throws IllegalArgumentException if the annotationType is null, if the * annotation does not exist on the type or if the constructor is * not currently declared on the type */ public AnnotatedTypeBuilder<X> removeFromConstructor(AnnotatedConstructor<X> constructor, Class<? extends Annotation> annotationType) { return removeFromConstructor(constructor.getJavaMember(), annotationType); }