下面列出了javax.inject.Qualifier#com.google.inject.BindingAnnotation 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
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;
}
public static boolean isBindingAnnotation(Annotation annotation) {
LinkedList<Class<?>> queue = new LinkedList<>();
queue.add(annotation.getClass());
queue.addAll(Arrays.asList(annotation.getClass().getInterfaces()));
while (!queue.isEmpty()) {
Class<?> clazz = queue.removeFirst();
if (clazz.getAnnotation(BindingAnnotation.class) != null) {
return true;
} else {
if (clazz.getSuperclass() != null) {
queue.addFirst(clazz.getSuperclass());
}
}
}
return false;
}
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;
}
private Annotation findBindingAnnotation(Iterable<Annotation> annotations)
{
for (Annotation annotation : annotations) {
// Check on guice (BindingAnnotation) & javax (Qualifier) based injections
if (annotation.annotationType().isAnnotationPresent(BindingAnnotation.class) ||
annotation.annotationType().isAnnotationPresent(Qualifier.class))
{
return annotation;
}
}
return null;
}
@SuppressWarnings({ "unchecked", "rawtypes" })
@Override
protected void configure() {
/**
* Configure injected constants.
*/
PropertyModule module = new PropertyModule(this);
for (Property property : module.getProperties()) {
for (Annotation annotation : property.getAnnotations()) {
if (annotation.annotationType().getAnnotation(BindingAnnotation.class) != null) {
Class<?> type = property.getType();
Object value = property.getValue();
ConstantBindingBuilder builder = bindConstant(annotation);
if (type.equals(Integer.TYPE)) {
builder.to((Integer) value);
} else if (type.equals(Long.TYPE)) {
builder.to((Long) value);
} else if (type.equals(Double.TYPE)) {
builder.to((Double) value);
} else if (type.equals(Float.TYPE)) {
builder.to((Float) value);
} else if (type.equals(Byte.TYPE)) {
builder.to((Byte) value);
} else if (type.equals(Short.TYPE)) {
builder.to((Short) value);
} else if (type.equals(Boolean.TYPE)) {
builder.to((Boolean) value);
} else if (type.equals(Character.TYPE)) {
builder.to((Character) value);
} else if (type.equals(String.class)) {
builder.to((String) value);
} else if (type.equals(Class.class)) {
builder.to((Class<?>) value);
} else if (value instanceof Enum<?>) {
builder.to((Enum) value);
} else {
String message = "Constant type not bindable: " + type + " of field " + property.getName() + " in module "
+ this.getClass().getName();
throw new ConfigurationException(Arrays.asList(new Message(message)));
}
}
}
}
multi(OptimizerStateListener.class);
multi(OptimizerIterationListener.class);
multi(IndividualStateListener.class);
config();
}
private static boolean isBindingAnnotation(Annotation annotation) {
Class<? extends Annotation> annotationType = annotation.annotationType();
return annotationType.isAnnotationPresent(Qualifier.class)
|| annotationType.isAnnotationPresent(BindingAnnotation.class);
}
private boolean isQualifierAnnotation(final Annotation... annotations) {
final Annotation ann = annotations[0];
return annotations.length == 1
&& (AnnotationSupport.isAnnotated(ann.annotationType(), Qualifier.class)
|| AnnotationSupport.isAnnotated(ann.annotationType(), 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;
}