use of org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedDeclaredType in project checker-framework by typetools.
the class BaseTypeVisitor method visitNewClass.
/**
* Performs a new class invocation check.
*
* <p>An invocation of a constructor, c, is valid only if:
*
* <ul>
* <li>passed arguments are subtypes of corresponding c parameters
* <li>if c is generic, passed type arguments are subtypes of c type variables
* </ul>
*/
@Override
public Void visitNewClass(NewClassTree node, Void p) {
if (checker.shouldSkipUses(TreeUtils.constructor(node))) {
return super.visitNewClass(node, p);
}
Pair<AnnotatedExecutableType, List<AnnotatedTypeMirror>> fromUse = atypeFactory.constructorFromUse(node);
AnnotatedExecutableType constructor = fromUse.first;
List<AnnotatedTypeMirror> typeargs = fromUse.second;
List<? extends ExpressionTree> passedArguments = node.getArguments();
List<AnnotatedTypeMirror> params = AnnotatedTypes.expandVarArgs(atypeFactory, constructor, passedArguments);
checkArguments(params, passedArguments);
checkVarargs(constructor, node);
List<AnnotatedTypeParameterBounds> paramBounds = new ArrayList<>();
for (AnnotatedTypeVariable param : constructor.getTypeVariables()) {
paramBounds.add(param.getBounds());
}
checkTypeArguments(node, paramBounds, typeargs, node.getTypeArguments());
boolean valid = validateTypeOf(node);
if (valid) {
AnnotatedDeclaredType dt = atypeFactory.getAnnotatedType(node);
if (atypeFactory.getDependentTypesHelper() != null) {
atypeFactory.getDependentTypesHelper().checkType(dt, node);
}
checkConstructorInvocation(dt, constructor, node);
}
// Do not call super, as that would observe the arguments without
// a set assignment context.
scan(node.getEnclosingExpression(), p);
scan(node.getIdentifier(), p);
scan(node.getClassBody(), p);
return null;
}
use of org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedDeclaredType in project checker-framework by typetools.
the class BaseTypeVisitor method visitLambdaExpression.
@Override
public Void visitLambdaExpression(LambdaExpressionTree node, Void p) {
Pair<AnnotatedDeclaredType, AnnotatedExecutableType> result = atypeFactory.getFnInterfaceFromTree(node);
AnnotatedExecutableType functionType = result.second;
if (node.getBody().getKind() != Tree.Kind.BLOCK) {
// Check return type for single statement returns here.
AnnotatedTypeMirror ret = functionType.getReturnType();
if (ret.getKind() != TypeKind.VOID) {
visitorState.setAssignmentContext(Pair.of((Tree) node, ret));
commonAssignmentCheck(ret, (ExpressionTree) node.getBody(), "return.type.incompatible");
}
}
// Check parameters
for (int i = 0; i < functionType.getParameterTypes().size(); ++i) {
AnnotatedTypeMirror lambdaParameter = atypeFactory.getAnnotatedType(node.getParameters().get(i));
commonAssignmentCheck(lambdaParameter, functionType.getParameterTypes().get(i), node.getParameters().get(i), "lambda.param.type.incompatible");
}
return super.visitLambdaExpression(node, p);
}
use of org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedDeclaredType in project checker-framework by typetools.
the class NullnessAnnotatedTypeFactory method adaptGetClassReturnTypeToReceiver.
@Override
public void adaptGetClassReturnTypeToReceiver(final AnnotatedExecutableType getClassType, final AnnotatedTypeMirror receiverType) {
super.adaptGetClassReturnTypeToReceiver(getClassType, receiverType);
// Make the wildcard always @NonNull, regardless of the declared type.
final AnnotatedDeclaredType returnAdt = (AnnotatedDeclaredType) getClassType.getReturnType();
final List<AnnotatedTypeMirror> typeArgs = returnAdt.getTypeArguments();
final AnnotatedWildcardType classWildcardArg = (AnnotatedWildcardType) typeArgs.get(0);
classWildcardArg.getExtendsBoundField().replaceAnnotation(NONNULL);
}
use of org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedDeclaredType in project checker-framework by typetools.
the class NullnessVisitor method visitNewClass.
@Override
public Void visitNewClass(NewClassTree node, Void p) {
AnnotatedDeclaredType type = atypeFactory.getAnnotatedType(node);
ExpressionTree identifier = node.getIdentifier();
if (identifier instanceof AnnotatedTypeTree) {
AnnotatedTypeTree t = (AnnotatedTypeTree) identifier;
for (AnnotationMirror a : atypeFactory.getAnnotatedType(t).getAnnotations()) {
// is this an annotation of the nullness checker?
boolean nullnessCheckerAnno = containsSameIgnoringValues(atypeFactory.getNullnessAnnotations(), a);
if (nullnessCheckerAnno && !AnnotationUtils.areSame(NONNULL, a)) {
// The type is not non-null => warning
checker.report(Result.warning("new.class.type.invalid", type.getAnnotations()), node);
// Note that other consistency checks are made by isValid.
}
}
if (t.toString().contains("@PolyNull")) {
// TODO: this is a hack, but PolyNull gets substituted
// afterwards
checker.report(Result.warning("new.class.type.invalid", type.getAnnotations()), node);
}
}
// isValidNewClassType or some such.
return super.visitNewClass(node, p);
}
use of org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedDeclaredType in project checker-framework by typetools.
the class StubParser method processType.
/**
* @return list of AnnotatedTypeVariable of the type's type parameter declarations
*/
private List<AnnotatedTypeVariable> processType(ClassOrInterfaceDeclaration decl, TypeElement elt) {
annotateDecl(declAnnos, elt, decl.getAnnotations());
AnnotatedDeclaredType type = atypeFactory.fromElement(elt);
annotate(type, decl.getAnnotations());
final List<? extends AnnotatedTypeMirror> typeArguments = type.getTypeArguments();
final List<TypeParameter> typeParameters = decl.getTypeParameters();
if (debugStubParser) {
int numParams = (typeParameters == null ? 0 : typeParameters.size());
int numArgs = (typeArguments == null ? 0 : typeArguments.size());
if (numParams != numArgs) {
stubDebug(String.format("parseType: mismatched sizes for typeParameters=%s (size %d) and typeArguments=%s (size %d); decl=%s; elt=%s (%s); type=%s (%s); parseState=%s", typeParameters, numParams, typeArguments, numArgs, decl.toString().replace(LINE_SEPARATOR, " "), elt.toString().replace(LINE_SEPARATOR, " "), elt.getClass(), type, type.getClass(), parseState));
stubDebug("Proceeding despite mismatched sizes");
}
}
annotateTypeParameters(decl, elt, atypes, typeArguments, typeParameters);
annotateSupertypes(decl, type);
putNew(atypes, elt, type);
List<AnnotatedTypeVariable> typeVariables = new ArrayList<>();
for (AnnotatedTypeMirror typeV : type.getTypeArguments()) {
if (typeV.getKind() != TypeKind.TYPEVAR) {
stubWarn("expected an AnnotatedTypeVariable but found type kind " + typeV.getKind() + ": " + typeV);
} else {
typeVariables.add((AnnotatedTypeVariable) typeV);
}
}
return typeVariables;
}
Aggregations