本文整理汇总了Java中javax.inject.Qualifier类的典型用法代码示例。如果您正苦于以下问题:Java Qualifier类的具体用法?Java Qualifier怎么用?Java Qualifier使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
Qualifier类属于javax.inject包,在下文中一共展示了Qualifier类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: createInvocableSetter
import javax.inject.Qualifier; //导入依赖的package包/类
private Consumer<Object> createInvocableSetter(Method method, DependencyResolver resolver) {
// gather qualifiers on setter
List<Annotation> qualifiers =
Arrays.asList(method.getAnnotations()).stream()
.filter(a -> a.annotationType().getAnnotation(Qualifier.class) != null)
.collect(Collectors.toList());
// determine property/setter type
List<Type> parameterTypes = Arrays.asList(method.getGenericParameterTypes());
if (parameterTypes.isEmpty() || parameterTypes.size() > 1)
throw new RuntimeException("method [" + method.getName() + "], annotated "
+ "with @Inject does NOT take exactly 1 parameter; it takes " + parameterTypes.size());
Type propertyType = parameterTypes.get(0);
return instance -> setterInvocation(method, resolver, instance, propertyType, qualifiers);
}
示例2: createInjectSignature
import javax.inject.Qualifier; //导入依赖的package包/类
/**
* Create the InjectSignature for a specific Method parameter.
*/
private InjectSignature createInjectSignature(Method method, int parameterIndex) {
Class<?> parameterClass = method.getParameterTypes()[parameterIndex];
Annotation[] annotations = method.getParameterAnnotations()[parameterIndex];
Nullability nullability = Nullability.create(annotations);
Class<?> targetType = (parameterClass == Provider.class)
? (Class<?>) ((ParameterizedType) method.getGenericParameterTypes()[parameterIndex]).getActualTypeArguments()[0]
: parameterClass;
Annotation qualifierAnnotation = Annotations.findAnnotationAnnotatedWith(Qualifier.class, annotations);
String qualifier = qualifierAnnotation != null
? qualifierCache.getQualifier(qualifierAnnotation)
: null;
return new InjectSignature(targetType, nullability, qualifier);
}
示例3: collectObjectGraphNodes
import javax.inject.Qualifier; //导入依赖的package包/类
/**
* Collect the ObjectGraphNode instances for a specific module.
*/
private static void collectObjectGraphNodes(ReflectionCache reflectionCache, List<ObjectGraphNode> objectGraphNodes, Object module) {
for (Method method : module.getClass().getDeclaredMethods()) {
// find a matching method
if (!method.isAnnotationPresent(Provides.class)) {
continue;
}
if (!Modifier.isPublic(method.getModifiers())) {
throw new SporkRuntimeException("Module method is not public: " + method.toString());
}
// getQualifier key
Nullability nullability = Nullability.create(method);
Annotation qualifierAnnotation = Annotations.findAnnotationAnnotatedWith(Qualifier.class, method);
String qualifier = qualifierAnnotation != null
? reflectionCache.getQualifier(qualifierAnnotation)
: null;
InjectSignature injectSignature = new InjectSignature(method.getReturnType(), nullability, qualifier);
objectGraphNodes.add(new ObjectGraphNode(injectSignature, module, method));
}
}
示例4: testAutowiredFieldDoesNotResolveWithBaseQualifierAndNonDefaultValueAndMultipleMatchingCandidates
import javax.inject.Qualifier; //导入依赖的package包/类
@Test
public void testAutowiredFieldDoesNotResolveWithBaseQualifierAndNonDefaultValueAndMultipleMatchingCandidates() {
GenericApplicationContext context = new GenericApplicationContext();
ConstructorArgumentValues cavs1 = new ConstructorArgumentValues();
cavs1.addGenericArgumentValue("the real juergen");
RootBeanDefinition person1 = new RootBeanDefinition(Person.class, cavs1, null);
person1.addQualifier(new AutowireCandidateQualifier(Qualifier.class, "juergen"));
ConstructorArgumentValues cavs2 = new ConstructorArgumentValues();
cavs2.addGenericArgumentValue("juergen imposter");
RootBeanDefinition person2 = new RootBeanDefinition(Person.class, cavs2, null);
person2.addQualifier(new AutowireCandidateQualifier(Qualifier.class, "juergen"));
context.registerBeanDefinition("juergen1", person1);
context.registerBeanDefinition("juergen2", person2);
context.registerBeanDefinition("autowired",
new RootBeanDefinition(QualifiedConstructorArgumentWithBaseQualifierNonDefaultValueTestBean.class));
AnnotationConfigUtils.registerAnnotationConfigProcessors(context);
try {
context.refresh();
fail("expected BeanCreationException");
}
catch (BeanCreationException e) {
assertTrue(e instanceof UnsatisfiedDependencyException);
assertEquals("autowired", e.getBeanName());
}
}
示例5: isProduces
import javax.inject.Qualifier; //导入依赖的package包/类
private boolean isProduces(Method m)
{
Annotation []anns = m.getAnnotations();
if (anns == null) {
return false;
}
for (Annotation ann : anns) {
if (ann.annotationType().isAnnotationPresent(Qualifier.class)) {
return true;
}
}
return false;
}
示例6: qualify
import javax.inject.Qualifier; //导入依赖的package包/类
private void qualify(final Bind<?> bind) {
if(Objects.isNull(bind.to())){
return;
}
if (bind.to().isAnnotationPresent(Named.class)) {
bind.name(bind.to().getAnnotation(Named.class).value());
return;
}
Arrays.asList(bind.to().getAnnotations()).stream().forEach(a -> {
// With qualifier qualify with module name
if (a.annotationType().isAnnotationPresent(Qualifier.class)) {
bind.name(a.annotationType().getSimpleName());
}
});
}
示例7: getQualifier
import javax.inject.Qualifier; //导入依赖的package包/类
public String getQualifier(AnnotatedElement field) {
String name = StringUtils.EMPTY;
if (field.isAnnotationPresent(Named.class)) {
name = field.getAnnotation(Named.class).value();
} else {
for (Annotation a : field.getAnnotations()) {
if (a.annotationType().isAnnotationPresent(Qualifier.class)) {
name = a.annotationType().getSimpleName();
}
}
}
return name;
}
示例8: isPartOfPU
import javax.inject.Qualifier; //导入依赖的package包/类
private static boolean isPartOfPU(ClassNode classNode, Class<? extends Annotation> qualifier,
ClassLoader classLoader) {
boolean anyQualifierFound = false;
boolean requiredPUAnnotationFound = false;
for (AnnotationNode annotation : classNode.visibleAnnotations) {
Class<?> annotationClass = AsmUtil.loadClass(Type.getType(annotation.desc), classLoader);
anyQualifierFound |= annotationClass.isAnnotationPresent(Qualifier.class);
requiredPUAnnotationFound |= AnyUnit.class.equals(annotationClass);
if (qualifier == null) {
requiredPUAnnotationFound |= NullUnit.class.equals(annotationClass);
} else {
requiredPUAnnotationFound |= qualifier.equals(annotationClass);
}
}
return requiredPUAnnotationFound || (qualifier == null && !anyQualifierFound);
}
示例9: unregister
import javax.inject.Qualifier; //导入依赖的package包/类
/**
* Unregister component where methods annotated by {@link Provides} will be registered as
* injection providers.
*
* @param providerHolder The object with methods marked by {@link Provides} to provide injectable
* instances
* @return this instance
*
* @throws ProviderMissingException Thrown when the any provider in the provider holder with
* the given type and qualifier cannot be found under this component
*/
public Component unregister(Object providerHolder) throws ProviderMissingException {
Method[] methods = providerHolder.getClass().getDeclaredMethods();
for (Method method : methods) {
if (method.isAnnotationPresent(Provides.class)) {
Class<?> returnType = method.getReturnType();
if (returnType != void.class) {
Annotation qualifier = null;
Annotation[] annotations = method.getAnnotations();
for (Annotation a : annotations) {
if (a.annotationType().isAnnotationPresent(Qualifier.class)) {
qualifier = a;
break;
}
}
unregister(returnType, qualifier);
}
}
}
return this;
}
示例10: registerProvides
import javax.inject.Qualifier; //导入依赖的package包/类
private void registerProvides(final Object providerHolder, final Method method)
throws ProvideException, ProviderConflictException {
Class<?> returnType = method.getReturnType();
if (returnType == void.class) {
throw new ProvideException(String.format("Provides method %s must not return void.",
method.getName()));
} else {
Annotation[] annotations = method.getAnnotations();
Annotation qualifier = null;
for (Annotation a : annotations) {
Class<? extends Annotation> annotationType = a.annotationType();
if (annotationType.isAnnotationPresent(Qualifier.class)) {
if (qualifier != null) {
throw new ProvideException("Only one Qualifier is supported for Provide method. " +
String.format("Found multiple qualifier %s and %s for method %s",
qualifier.getClass().getName(), a.getClass().getName(),
method.getName()));
}
qualifier = a;
}
}
Provider provider = new MethodProvider(returnType, qualifier, scopeCache, providerHolder, method);
register(provider);
}
}
示例11: getQualifiers
import javax.inject.Qualifier; //导入依赖的package包/类
/**
* Returns list of all annotations given <code>element</code> has.
*
* @param element Element.
*/
public List<AnnotationSpec> getQualifiers(Element element) {
List<AnnotationSpec> list = new ArrayList<>();
for (AnnotationMirror a : element.getAnnotationMirrors()) {
if (a.getAnnotationType().asElement().getAnnotation(Qualifier.class) == null) {
continue; // ignore non-Qualifier annotations
}
ClassName annotationClassName = (ClassName) ClassName.get(a.getAnnotationType());
AnnotationSpec.Builder annotation = AnnotationSpec.builder(annotationClassName);
for (Map.Entry<? extends ExecutableElement, ? extends AnnotationValue> entry : a.getElementValues().entrySet()) {
String format = (entry.getValue().getValue() instanceof String) ? "$S" : "$L";
annotation.addMember(entry.getKey().getSimpleName().toString(), format, entry.getValue().getValue());
}
list.add(annotation.build());
}
return list;
}
示例12: DocumentedAnnotations
import javax.inject.Qualifier; //导入依赖的package包/类
public DocumentedAnnotations(Element element) {
List<AnnotationMirror> allAnnotations = new ArrayList<AnnotationMirror>();
allAnnotations.addAll(annotationsOf(element, Qualifier.class));
allAnnotations.addAll(annotationsOf(element, Scope.class));
if (allAnnotations.isEmpty()) {
this.description = Optional.absent();
return;
}
StringBuilder result = new StringBuilder();
for (AnnotationMirror annotation : allAnnotations) {
if (result.length() > 0) {
result.append(" ");
}
result.append(annotation.toString());
}
this.description = Optional.of(result.toString());
}
示例13: getDependency
import javax.inject.Qualifier; //导入依赖的package包/类
protected Object getDependency(Class<?> type, Type genericType, Annotation[] annotations) {
List<Annotation> qualifiers = Arrays.stream(annotations)
.filter(a -> a.annotationType().isAnnotationPresent(Qualifier.class))
.collect(Collectors.toList());
Annotation qualifier = null;
if (qualifiers.isEmpty() == false) {
if (qualifiers.size() > 1) {
throw new IllegalArgumentException();
}
qualifier = qualifiers.get(0);
}
Object dependency;
if (type == Provider.class) {
ParameterizedType pt = (ParameterizedType) genericType;
Class<?> type2 = (Class<?>) pt.getActualTypeArguments()[0];
dependency = container.getProvider(type2, qualifier);
} else {
dependency = container.getInstance(type, qualifier);
}
return dependency;
}
示例14: harvestQualifiers
import javax.inject.Qualifier; //导入依赖的package包/类
protected List<Annotation> harvestQualifiers(Class<?> klass) {
List<Annotation> list = new ArrayList<>();
Annotation[] annotations = klass.getAnnotations();
for (Annotation annotation : annotations) {
if (AnnotationUtils.isAnnotatedWith(annotation, Qualifier.class)) {
// special case @BindTo is only used during tests
if (BindTo.class.isAssignableFrom(annotation.getClass())) {
continue;
}
// special case for @Named
if (Named.class.isAssignableFrom(annotation.getClass())) {
Named named = (Named) annotation;
if (isBlank(named.value())) {
list.add(named(getPropertyName(klass)));
continue;
}
}
list.add(annotation);
}
}
return list;
}
示例15: harvestQualifiers
import javax.inject.Qualifier; //导入依赖的package包/类
@Nonnull
protected List<Annotation> harvestQualifiers(@Nonnull Class<?> clazz) {
List<Annotation> list = new ArrayList<>();
Annotation[] annotations = clazz.getAnnotations();
for (Annotation annotation : annotations) {
if (AnnotationUtils.isAnnotatedWith(annotation, Qualifier.class)) {
if (BindTo.class.isAssignableFrom(annotation.getClass())) {
continue;
}
// special case for @Named
if (Named.class.isAssignableFrom(annotation.getClass())) {
Named named = (Named) annotation;
if (isBlank(named.value())) {
list.add(named(getPropertyName(clazz)));
continue;
}
}
list.add(annotation);
}
}
return list;
}