private ObjectBuilder.FieldBuilder createInjectedField(ObjectBuilder target, GambitTypes gambitScope, java.lang.reflect.Type genericType, Annotation[] annotation) { TypeLiteral<?> genericParam = TypeLiteral.get(genericType); ObjectBuilder.FieldBuilder injectedField = target.field(gensym("inject$"), gambitScope.adapt(genericParam.getRawType(), false)); injectedField.annotate(Inject.class); Annotation bindingAnnotation = null; for (Annotation ann : annotation) { if (ann.getClass().isAnnotationPresent(BindingAnnotation.class)) { Preconditions.checkArgument(bindingAnnotation == null, "Already found a binding annotation %s and now found another %s: that's too many", bindingAnnotation, ann); bindingAnnotation = ann; } } if (bindingAnnotation != null) { injectedField.annotate(bindingAnnotation); } return injectedField; }
private void addQualifierExtractors() { // qualifiers config.requiredQualifierExtractors.add(annotatedElement -> { return Arrays.stream(annotatedElement.getAnnotations()) .filter(a -> a.annotationType() .isAnnotationPresent(BindingAnnotation.class) || a.annotationType().isAnnotationPresent( javax.inject.Qualifier.class)); }); config.availableQualifierExtractors .add(new Function<AnnotatedElement, Stream<Annotation>>() { @Override public Stream<Annotation> apply( AnnotatedElement annotated) { return Arrays.stream(annotated.getAnnotations()) .filter(a -> a.annotationType() .isAnnotationPresent( BindingAnnotation.class) || a.annotationType() .isAnnotationPresent( javax.inject.Qualifier.class)); } }); }
protected Annotation getBindingAnnotation(Annotation[] annotations) { Annotation bindingAnnotation = null; for (Annotation annotation : annotations) { if (annotation.annotationType().getAnnotation(BindingAnnotation.class) != null || annotation.annotationType() == Named.class) { if (bindingAnnotation != null) { throw new ProvisionException( String.format("More than one binding annotation found on %s: %s, %s.", this, annotation, bindingAnnotation)); } bindingAnnotation = annotation; // Keep going so we can find any rogue additional binding annotations } } return bindingAnnotation; }
@Override protected void doStart() throws Exception { // Please do not remove // This kludge is about ensuring these annotations are parsed to prevent // a deadlock Annotations.isRetainedAtRuntime(Assisted.class); Annotations.isRetainedAtRuntime(AssistedInject.class); Annotations.isRetainedAtRuntime(BindingAnnotation.class); // End kludge privatePluginService = (PrivatePluginService) AbstractPluginService.get(); getManager().getRegistry().registerListener(this); setupBeanLocators(); }
private Collection<Annotation> getBindingAnnotations(Field field) { List<Annotation> result = new ArrayList<>(); for (Annotation annotation : field.getAnnotations()) { if (annotation.annotationType().isAnnotationPresent(BindingAnnotation.class)) { result.add(annotation); } } return result; }
/** * Returns the first {@link Annotation} from the given array that * is a {@link BindingAnnotation}. * * @see BindingAnnotation */ private static Annotation getBindingAnnotation(Annotation[] annotations) { for (Annotation annotation : annotations) { Class<? extends Annotation> type = annotation.annotationType(); if (type.isAnnotationPresent(BindingAnnotation.class)) { return annotation; } } return null; }
private <T> Annotation findQualifierAnnotation(Class<? extends T> impl) { for (Annotation a : impl.getAnnotations()) { Class<? extends Annotation> at = a.annotationType(); if (at.isAnnotationPresent(Qualifier.class) || at.isAnnotationPresent(BindingAnnotation.class)) return a; } return null; }
private static Key<?> getFieldKey(Field field, TypeLiteral<?> typeLiteral) { List<Annotation> annotations = getElementAnnotations(field, BindingAnnotation.class); if (annotations.size() == 0) { return Key.get(typeLiteral); } if (annotations.size() == 1) { return Key.get(typeLiteral, annotations.get(0)); } throw new MoreThanOneBindingAnnotationException(field, annotations); }
@Test public void get_element_annotations_returns_annotations_of_given_type() throws SecurityException, NoSuchFieldException { Class<ClassWithFieldWithManyAnnotations> klass = ClassWithFieldWithManyAnnotations.class; Field field = klass.getField("field"); List<Annotation> annotations = getElementAnnotations(field, BindingAnnotation.class); assertThat(annotations).hasSize(2); assertThat(annotations.get(0).annotationType()).isSameAs(Named.class); assertThat(annotations.get(1).annotationType()).isSameAs(MyAnnotation.class); }
@Test public void get_element_annotation_returns_empty_set_for_not_annotated_field() throws SecurityException, NoSuchFieldException { Class<ClassWithFieldWithoutAnnotations> klass = ClassWithFieldWithoutAnnotations.class; Field field = klass.getField("field"); List<Annotation> annotations = getElementAnnotations(field, BindingAnnotation.class); assertThat(annotations).isEmpty(); }
@Test public void test() throws Exception { Field field = this.getClass().getDeclaredField("field"); List<Annotation> annotations = getElementAnnotations(field, BindingAnnotation.class); Exception exception = new MoreThanOneBindingAnnotationException(field, annotations); assertThat(exception.getMessage()).isEqualTo( "Field field has more than one binding annotation: @Named, @MyBindingAnnotation"); }
/** * Returns true if {@code annotation} is a Guice binding annotation; false otherwise. */ public static boolean isBindingAnnotation(Class<? extends Annotation> annotation) { return annotation.isAnnotationPresent(BindingAnnotation.class) || annotation.isAnnotationPresent(Qualifier.class); }
private static boolean isBindingAnnotation(Annotation annotation) { Class<? extends Annotation> annotationType = annotation.annotationType(); return annotationType.isAnnotationPresent(Qualifier.class) || annotationType.isAnnotationPresent(BindingAnnotation.class); }
private static boolean isBindingAnnotation(final Annotation annotation) { Class<? extends Annotation> annotationType = annotation.annotationType(); return annotationType.isAnnotationPresent(Qualifier.class) || annotationType.isAnnotationPresent(BindingAnnotation.class); }
/** * Checks that the given annotation type is a {@link BindingAnnotation @BindingAnnotation}. * * @param annotationType The annotation type to check. * @param <T> The type of the binding annotation. * @return The checked binding annotation type. * @throws NullPointerException If the given {@code annotationType} is null. * @throws IllegalArgumentException If the given {@code annotationType} is not a * {@literal @BindingAnnotation}. */ public static <T extends Annotation> Class<T> checkBindingAnnotation(Class<T> annotationType) { Preconditions.checkNotNull(annotationType); boolean bindingAnnotation = annotationType.isAnnotationPresent(BindingAnnotation.class); boolean qualifier = annotationType.isAnnotationPresent(Qualifier.class); Preconditions.checkArgument(bindingAnnotation || qualifier, "%s is not a @BindingAnnotation or @Qualifier", annotationType); return annotationType; }