Skip to content

Commit

Permalink
Arc - log a warning when we detect wildcard as a bean type.
Browse files Browse the repository at this point in the history
  • Loading branch information
manovotn committed Oct 22, 2019
1 parent 994427c commit f75169f
Showing 1 changed file with 35 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.util.Set;
import java.util.function.BiConsumer;
import org.jboss.jandex.AnnotationInstance;
import org.jboss.jandex.AnnotationTarget;
import org.jboss.jandex.AnnotationValue;
import org.jboss.jandex.ClassInfo;
import org.jboss.jandex.DotName;
Expand All @@ -31,13 +32,16 @@
import org.jboss.jandex.Type.Kind;
import org.jboss.jandex.TypeVariable;
import org.jboss.jandex.WildcardType;
import org.jboss.logging.Logger;

/**
*
* @author Martin Kouba
*/
final class Types {

static final Logger LOGGER = Logger.getLogger(Types.class);

private static final Type OBJECT_TYPE = Type.create(DotNames.OBJECT, Kind.CLASS);

private Types() {
Expand Down Expand Up @@ -148,9 +152,9 @@ static Set<Type> getProducerMethodTypeClosure(MethodInfo producerMethod, BeanDep
"Producer method return type not found in index: " + producerMethod.returnType().name());
}
if (Kind.CLASS.equals(returnType.kind())) {
types = getTypeClosure(returnTypeClassInfo, Collections.emptyMap(), beanDeployment, null);
types = getTypeClosure(returnTypeClassInfo, producerMethod, Collections.emptyMap(), beanDeployment, null);
} else if (Kind.PARAMETERIZED_TYPE.equals(returnType.kind())) {
types = getTypeClosure(returnTypeClassInfo,
types = getTypeClosure(returnTypeClassInfo, producerMethod,
buildResolvedMap(returnType.asParameterizedType().arguments(), returnTypeClassInfo.typeParameters(),
Collections.emptyMap(), beanDeployment.getIndex()),
beanDeployment, null);
Expand All @@ -174,9 +178,9 @@ static Set<Type> getProducerFieldTypeClosure(FieldInfo producerField, BeanDeploy
throw new IllegalArgumentException("Producer field type not found in index: " + producerField.type().name());
}
if (Kind.CLASS.equals(fieldType.kind())) {
types = getTypeClosure(fieldClassInfo, Collections.emptyMap(), beanDeployment, null);
types = getTypeClosure(fieldClassInfo, producerField, Collections.emptyMap(), beanDeployment, null);
} else if (Kind.PARAMETERIZED_TYPE.equals(fieldType.kind())) {
types = getTypeClosure(fieldClassInfo,
types = getTypeClosure(fieldClassInfo, producerField,
buildResolvedMap(fieldType.asParameterizedType().arguments(), fieldClassInfo.typeParameters(),
Collections.emptyMap(), beanDeployment.getIndex()),
beanDeployment, null);
Expand All @@ -199,7 +203,15 @@ static Set<Type> getClassBeanTypeClosure(ClassInfo classInfo, BeanDeployment bea
return restrictBeanTypes(types, beanDeployment.getAnnotations(classInfo));
}

static Set<Type> getTypeClosure(ClassInfo classInfo, Map<TypeVariable, Type> resolvedTypeParameters,
static Set<Type> getTypeClosure(ClassInfo classInfo,
Map<TypeVariable, Type> resolvedTypeParameters,
BeanDeployment beanDeployment, BiConsumer<ClassInfo, Map<TypeVariable, Type>> resolvedTypeVariablesConsumer) {
return getTypeClosure(classInfo, null, resolvedTypeParameters, beanDeployment,
resolvedTypeVariablesConsumer);
}

static Set<Type> getTypeClosure(ClassInfo classInfo, AnnotationTarget producerFieldOrMethod,
Map<TypeVariable, Type> resolvedTypeParameters,
BeanDeployment beanDeployment, BiConsumer<ClassInfo, Map<TypeVariable, Type>> resolvedTypeVariablesConsumer) {
Set<Type> types = new HashSet<>();
List<TypeVariable> typeParameters = classInfo.typeParameters();
Expand All @@ -210,8 +222,19 @@ static Set<Type> getTypeClosure(ClassInfo classInfo, Map<TypeVariable, Type> res
} else {
// Canonical ParameterizedType with unresolved type variables
Type[] typeParams = new Type[typeParameters.size()];
boolean skipThisType = false;
for (int i = 0; i < typeParameters.size(); i++) {
typeParams[i] = resolvedTypeParameters.get(typeParameters.get(i));
// this should only be the case for producers; wildcard is not a legal bean type
// see https://docs.jboss.org/cdi/spec/2.0/cdi-spec.html#legal_bean_types
if (typeParams[i].kind().equals(Kind.WILDCARD_TYPE) && producerFieldOrMethod != null) {
LOGGER.info("Producer " +
(producerFieldOrMethod.kind().equals(AnnotationTarget.Kind.FIELD) ? "field " : "method ") +
producerFieldOrMethod +
" contains a parameterized typed with a wildcard. This type is not a legal bean type" +
" according to CDI specification and will be ignored during bean resolution.");
skipThisType = true;
}
}
if (resolvedTypeVariablesConsumer != null) {
Map<TypeVariable, Type> resolved = new HashMap<>();
Expand All @@ -220,7 +243,9 @@ static Set<Type> getTypeClosure(ClassInfo classInfo, Map<TypeVariable, Type> res
}
resolvedTypeVariablesConsumer.accept(classInfo, resolved);
}
types.add(ParameterizedType.create(classInfo.name(), typeParams, null));
if (!skipThisType) {
types.add(ParameterizedType.create(classInfo.name(), typeParams, null));
}
}
// Interfaces
for (Type interfaceType : classInfo.interfaceTypes()) {
Expand All @@ -231,7 +256,8 @@ static Set<Type> getTypeClosure(ClassInfo classInfo, Map<TypeVariable, Type> res
resolved = buildResolvedMap(interfaceType.asParameterizedType().arguments(),
interfaceClassInfo.typeParameters(), resolvedTypeParameters, beanDeployment.getIndex());
}
types.addAll(getTypeClosure(interfaceClassInfo, resolved, beanDeployment, resolvedTypeVariablesConsumer));
types.addAll(getTypeClosure(interfaceClassInfo, producerFieldOrMethod, resolved, beanDeployment,
resolvedTypeVariablesConsumer));
}
}
// Superclass
Expand All @@ -244,7 +270,8 @@ static Set<Type> getTypeClosure(ClassInfo classInfo, Map<TypeVariable, Type> res
superClassInfo.typeParameters(),
resolvedTypeParameters, beanDeployment.getIndex());
}
types.addAll(getTypeClosure(superClassInfo, resolved, beanDeployment, resolvedTypeVariablesConsumer));
types.addAll(getTypeClosure(superClassInfo, producerFieldOrMethod, resolved, beanDeployment,
resolvedTypeVariablesConsumer));
}
}
return types;
Expand Down

0 comments on commit f75169f

Please sign in to comment.