use of org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedArrayType in project checker-framework by typetools.
the class BaseTypeVisitor method commonAssignmentCheck.
/**
* Checks the validity of an assignment (or pseudo-assignment) from a value to a variable and
* emits an error message (through the compiler's messaging interface) if it is not valid.
*
* @param varType the annotated type of the lvalue (usually a variable)
* @param valueExp the AST node for the rvalue (the new value)
* @param errorKey the error message to use if the check fails (must be a compiler message key,
* see {@link org.checkerframework.checker.compilermsgs.qual.CompilerMessageKey})
*/
protected void commonAssignmentCheck(AnnotatedTypeMirror varType, ExpressionTree valueExp, @CompilerMessageKey String errorKey) {
if (shouldSkipUses(valueExp)) {
return;
}
if (valueExp.getKind() == Tree.Kind.MEMBER_REFERENCE || valueExp.getKind() == Tree.Kind.LAMBDA_EXPRESSION) {
// and do not need to be checked again as arguments.
return;
}
if (varType.getKind() == TypeKind.ARRAY && valueExp instanceof NewArrayTree && ((NewArrayTree) valueExp).getType() == null) {
AnnotatedTypeMirror compType = ((AnnotatedArrayType) varType).getComponentType();
NewArrayTree arrayTree = (NewArrayTree) valueExp;
assert arrayTree.getInitializers() != null : "array initializers are not expected to be null in: " + valueExp;
checkArrayInitialization(compType, arrayTree.getInitializers());
}
if (!validateTypeOf(valueExp)) {
return;
}
AnnotatedTypeMirror valueType = atypeFactory.getAnnotatedType(valueExp);
assert valueType != null : "null type for expression: " + valueExp;
commonAssignmentCheck(varType, valueType, valueExp, errorKey);
}
use of org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedArrayType in project checker-framework by typetools.
the class BaseTypeVisitor method typeCheckVectorCopyIntoArgument.
/**
* Type checks the method arguments of {@code Vector.copyInto()}.
*
* <p>The Checker Framework special-cases the method invocation, as its type safety cannot be
* expressed by Java's type system.
*
* <p>For a Vector {@code v} of type {@code Vector<E>}, the method invocation {@code
* v.copyInto(arr)} is type-safe iff {@code arr} is an array of type {@code T[]}, where {@code
* T} is a subtype of {@code E}.
*
* <p>In other words, this method checks that the type argument of the receiver method is a
* subtype of the component type of the passed array argument.
*
* @param node a method invocation of {@code Vector.copyInto()}
* @param params the types of the parameters of {@code Vectory.copyInto()}
*/
protected void typeCheckVectorCopyIntoArgument(MethodInvocationTree node, List<? extends AnnotatedTypeMirror> params) {
assert params.size() == 1 : "invalid no. of parameters " + params + " found for method invocation " + node;
assert node.getArguments().size() == 1 : "invalid no. of arguments in method invocation " + node;
AnnotatedTypeMirror passed = atypeFactory.getAnnotatedType(node.getArguments().get(0));
AnnotatedArrayType passedAsArray = (AnnotatedArrayType) passed;
AnnotatedTypeMirror receiver = atypeFactory.getReceiverType(node);
if (vectorType == null) {
vectorType = atypeFactory.fromElement(elements.getTypeElement("java.util.Vector"));
}
AnnotatedDeclaredType receiverAsVector = AnnotatedTypes.asSuper(atypeFactory, receiver, vectorType);
if (receiverAsVector.getTypeArguments().isEmpty()) {
return;
}
AnnotatedTypeMirror argComponent = passedAsArray.getComponentType();
AnnotatedTypeMirror vectorTypeArg = receiverAsVector.getTypeArguments().get(0);
Tree errorLocation = node.getArguments().get(0);
if (TypesUtils.isErasedSubtype(vectorTypeArg.getUnderlyingType(), argComponent.getUnderlyingType(), types)) {
commonAssignmentCheck(argComponent, vectorTypeArg, errorLocation, "vector.copyinto.type.incompatible");
} else {
checker.report(Result.failure("vector.copyinto.type.incompatible", vectorTypeArg, argComponent), errorLocation);
}
}
use of org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedArrayType in project checker-framework by typetools.
the class DefaultReflectionResolver method resolveConstructorCall.
/**
* Resolves a call to {@link Constructor#newInstance(Object...)}.
*
* @param factory the {@link AnnotatedTypeFactory} of the underlying type system
* @param tree the method invocation tree (representing a constructor call) that has to be
* resolved
* @param origResult the original result from {@code factory.methodFromUse}.
*/
private Pair<AnnotatedExecutableType, List<AnnotatedTypeMirror>> resolveConstructorCall(AnnotatedTypeFactory factory, MethodInvocationTree tree, Pair<AnnotatedExecutableType, List<AnnotatedTypeMirror>> origResult) {
debugReflection("Try to resolve reflective constructor call: " + tree);
List<JCNewClass> possibleConstructors = resolveReflectiveConstructor(tree, factory);
// Reflective constructor could not be resolved
if (possibleConstructors.size() == 0) {
return origResult;
}
Set<? extends AnnotationMirror> returnLub = null;
Set<? extends AnnotationMirror> paramsGlb = null;
// parameter types
for (JCNewClass resolvedTree : possibleConstructors) {
debugReflection("Resolved constructor invocation: " + resolvedTree);
if (!checkNewClassArguments(resolvedTree)) {
debugReflection("Spoofed tree's arguments did not match declaration" + resolvedTree.toString());
// in QualifierPolymorphism.PolyCollector.visitArray(...)
continue;
}
Pair<AnnotatedExecutableType, List<AnnotatedTypeMirror>> resolvedResult = factory.constructorFromUse(resolvedTree);
// Lub return types
returnLub = lub(returnLub, resolvedResult.first.getReturnType().getAnnotations(), factory);
// Glb parameter types
for (AnnotatedTypeMirror mirror : resolvedResult.first.getParameterTypes()) {
paramsGlb = glb(paramsGlb, mirror.getAnnotations(), factory);
}
}
if (returnLub == null) {
// None of the spoofed tree's arguments matched the declared method
return origResult;
}
/*
* Clear all original (return, parameter type) annotations and set
* lub/glb annotations from resolved constructors.
*/
// return value
origResult.first.getReturnType().clearAnnotations();
origResult.first.getReturnType().addAnnotations(returnLub);
// parameter types
if (paramsGlb != null) {
AnnotatedArrayType origArrayType = (AnnotatedArrayType) origResult.first.getParameterTypes().get(0);
origArrayType.getComponentType().clearAnnotations();
origArrayType.getComponentType().addAnnotations(paramsGlb);
}
debugReflection("Resolved annotations: " + origResult.first);
return origResult;
}
Aggregations