use of javax.inject.Named in project core by weld.
the class ResolvableBuilder method addQualifier.
private ResolvableBuilder addQualifier(Annotation qualifier, InjectionPoint injectionPoint) {
QualifierInstance qualifierInstance = QualifierInstance.of(qualifier, store);
final Class<? extends Annotation> annotationType = qualifierInstance.getAnnotationClass();
// Handle the @New qualifier special case
if (annotationType.equals(New.class)) {
New newQualifier = New.class.cast(qualifier);
if (newQualifier.value().equals(New.class) && rawType == null) {
throw new IllegalStateException("Cannot transform @New when there is no known raw type");
} else if (newQualifier.value().equals(New.class)) {
qualifier = new NewLiteral(rawType);
qualifierInstance = QualifierInstance.of(qualifier, store);
}
} else if (injectionPoint != null && annotationType.equals(Named.class)) {
Named named = (Named) qualifier;
if (named.value().equals("")) {
// WELD-1739
// This is an injection point with an @Named qualifier, with no value specified, we need to assume the name of the field or parameter is the
// value
Member member = injectionPoint.getMember();
if (member instanceof Executable) {
// Method or constructor injection
Executable executable = (Executable) member;
AnnotatedParameter<?> annotatedParameter = (AnnotatedParameter<?>) injectionPoint.getAnnotated();
Parameter parameter = executable.getParameters()[annotatedParameter.getPosition()];
named = new NamedLiteral(parameter.getName());
} else {
named = new NamedLiteral(injectionPoint.getMember().getName());
}
qualifier = named;
qualifierInstance = QualifierInstance.of(named, store);
}
}
checkQualifier(qualifier, qualifierInstance, annotationType);
this.qualifierInstances.add(qualifierInstance);
return this;
}
use of javax.inject.Named in project vertx-zero by silentbalanceyh.
the class AffluxThread method scanQualifier.
private void scanQualifier(final Field field, final List<Class<?>> instanceCls) {
// Field must annotated with @Qualifier
final Annotation annotation = field.getAnnotation(Qualifier.class);
Fn.flingUp(null == annotation, LOGGER, QualifierMissedException.class, this.getClass(), field.getName(), field.getDeclaringClass().getName());
// All implementation class must be annotated with @Named
final boolean match = instanceCls.stream().allMatch(item -> item.isAnnotationPresent(Named.class));
final Set<String> names = instanceCls.stream().map(Class::getName).collect(Collectors.toSet());
Fn.flingUp(!match, LOGGER, NamedImplementionException.class, this.getClass(), names, field.getType().getName());
// Named value must be reflect with @Qualifier
final String value = Instance.invoke(annotation, "value");
final Optional<Class<?>> verified = instanceCls.stream().filter(item -> {
final Annotation target = item.getAnnotation(Named.class);
final String targetValue = Instance.invoke(target, "value");
return value.equals(targetValue) && !Ut.isNil(targetValue);
}).findAny();
Fn.flingUp(!verified.isPresent(), LOGGER, NamedNotFoundException.class, this.getClass(), names, value);
// Passed all specification
this.fieldMap.put(field.getName(), verified.get());
}
use of javax.inject.Named in project wicket by apache.
the class AnnotProxyFieldValueFactory method getFieldValue.
@Override
public Object getFieldValue(final Field field, final Object fieldOwner) {
if (supportsField(field)) {
SpringBean annot = field.getAnnotation(SpringBean.class);
String name;
boolean required;
if (annot != null) {
name = annot.name();
required = annot.required();
} else {
Named named = field.getAnnotation(Named.class);
name = named != null ? named.value() : "";
required = true;
}
Class<?> generic = ResolvableType.forField(field).resolveGeneric(0);
String beanName = getBeanName(field, name, required, generic);
SpringBeanLocator locator = new SpringBeanLocator(beanName, field.getType(), field, contextLocator);
// only check the cache if the bean is a singleton
Object cachedValue = cache.get(locator);
if (cachedValue != null) {
return cachedValue;
}
Object target;
try {
// check whether there is a bean with the provided properties
target = locator.locateProxyTarget();
} catch (IllegalStateException isx) {
if (required) {
throw isx;
} else {
return null;
}
}
if (wrapInProxies) {
target = LazyInitProxyFactory.createProxy(field.getType(), locator);
}
// only put the proxy into the cache if the bean is a singleton
if (locator.isSingletonBean()) {
Object tmpTarget = cache.putIfAbsent(locator, target);
if (tmpTarget != null) {
target = tmpTarget;
}
}
return target;
}
return null;
}
use of javax.inject.Named in project endpoints-java by cloudendpoints.
the class AnnotationApiConfigGeneratorTest method testMultipleCollectionRequests.
@Test
public void testMultipleCollectionRequests() throws Exception {
@Api
class MultipleCollections {
@SuppressWarnings("unused")
public void foo(@Named("ids") Long[] ids, @Named("authors") List<String> authors) {
}
}
String apiConfigSource = g.generateConfig(MultipleCollections.class).get("myapi-v1.api");
ObjectNode root = objectMapper.readValue(apiConfigSource, ObjectNode.class);
JsonNode methodNode = root.path("methods").path("myapi.multipleCollections.foo");
assertFalse(methodNode.isMissingNode());
verifyMethodRequestParameter(methodNode.get("request"), "ids", "int64", true, true);
verifyMethodRequestParameter(methodNode.get("request"), "authors", "string", true, true);
}
use of javax.inject.Named in project endpoints-java by cloudendpoints.
the class AnnotationApiConfigGeneratorTest method testGenericCollectionRequest.
@Test
public void testGenericCollectionRequest() throws Exception {
@Api
abstract class GenericCollection<T> {
@SuppressWarnings("unused")
public void foo(@Named("collection") Collection<T> t) {
}
}
// TODO: remove with JDK8, dummy to force inclusion of GenericArray to InnerClass attribute
// http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=2210448
GenericCollection<Long> dummy = new GenericCollection<Long>() {
};
class Int64Collection extends GenericCollection<Long> {
}
String apiConfigSource = g.generateConfig(Int64Collection.class).get("myapi-v1.api");
ObjectNode root = objectMapper.readValue(apiConfigSource, ObjectNode.class);
JsonNode methodNode = root.path("methods").path("myapi.int64Collection.foo");
assertFalse(methodNode.isMissingNode());
verifyMethodRequestParameter(methodNode.get("request"), "collection", "int64", true, true);
}
Aggregations