当前位置: 首页>>代码示例>>Java>>正文


Java Qualifier类代码示例

本文整理汇总了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);
}
 
开发者ID:carml,项目名称:carml,代码行数:17,代码来源:MapperImpl.java

示例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);
}
 
开发者ID:ByteWelder,项目名称:Spork,代码行数:20,代码来源:InjectSignatureMethodCache.java

示例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));
	}
}
 
开发者ID:ByteWelder,项目名称:Spork,代码行数:25,代码来源:ObjectGraphBuilderImpl.java

示例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());
	}
}
 
开发者ID:langtianya,项目名称:spring4-understanding,代码行数:26,代码来源:InjectAnnotationAutowireContextTests.java

示例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;
}
 
开发者ID:baratine,项目名称:baratine,代码行数:17,代码来源:IncludeWebClass.java

示例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());
      }
    });
  }
 
开发者ID:jspare-projects,项目名称:jspare-container,代码行数:19,代码来源:InternalBinder.java

示例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;
}
 
开发者ID:jspare-projects,项目名称:jspare-container,代码行数:17,代码来源:ReflectionUtils.java

示例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);

}
 
开发者ID:ruediste,项目名称:rise,代码行数:20,代码来源:PersistenceUnitManagerHelper.java

示例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;
}
 
开发者ID:kejunxia,项目名称:AndroidMvc,代码行数:33,代码来源:Component.java

示例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);
    }
}
 
开发者ID:kejunxia,项目名称:AndroidMvc,代码行数:27,代码来源:Component.java

示例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;
}
 
开发者ID:inloop,项目名称:Knight,代码行数:23,代码来源:BaseClassBuilder.java

示例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());
}
 
开发者ID:Ladicek,项目名称:ann-docu-gen,代码行数:21,代码来源:DocumentedAnnotations.java

示例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;
}
 
开发者ID:backpaper0,项目名称:radishtainer,代码行数:22,代码来源:Injector.java

示例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;
}
 
开发者ID:aalmiray,项目名称:griffon2,代码行数:23,代码来源:Bindings.java

示例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;
}
 
开发者ID:aalmiray,项目名称:griffon2,代码行数:24,代码来源:TestApplicationBootstrapper.java


注:本文中的javax.inject.Qualifier类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。