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 13c3439
Showing 1 changed file with 42 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,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 +151,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 +177,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 +202,29 @@ 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, null, resolvedTypeParameters, beanDeployment,
resolvedTypeVariablesConsumer);
}

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

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

static Set<Type> getTypeClosure(ClassInfo classInfo, MethodInfo producerMethod, FieldInfo producerField,
Map<TypeVariable, Type> resolvedTypeParameters,
BeanDeployment beanDeployment, BiConsumer<ClassInfo, Map<TypeVariable, Type>> resolvedTypeVariablesConsumer) {
Set<Type> types = new HashSet<>();
List<TypeVariable> typeParameters = classInfo.typeParameters();
Expand All @@ -212,6 +237,14 @@ static Set<Type> getTypeClosure(ClassInfo classInfo, Map<TypeVariable, Type> res
Type[] typeParams = new Type[typeParameters.size()];
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) && (producerField != null || producerMethod != null)) {
LOGGER.info("Producer " + (producerMethod == null ? "field " : "method ") +
(producerMethod == null ? producerField : producerMethod) +
" 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.");
}
}
if (resolvedTypeVariablesConsumer != null) {
Map<TypeVariable, Type> resolved = new HashMap<>();
Expand All @@ -231,7 +264,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, producerMethod, producerField, resolved, beanDeployment,
resolvedTypeVariablesConsumer));
}
}
// Superclass
Expand All @@ -244,7 +278,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, producerMethod, producerField, resolved, beanDeployment,
resolvedTypeVariablesConsumer));
}
}
return types;
Expand Down

0 comments on commit 13c3439

Please sign in to comment.