use of org.checkerframework.framework.type.AnnotatedTypeMirror 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 varTree the AST node for 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(Tree varTree, ExpressionTree valueExp, @CompilerMessageKey String errorKey) {
AnnotatedTypeMirror var = atypeFactory.getAnnotatedTypeLhs(varTree);
assert var != null : "no variable found for tree: " + varTree;
if (!validateType(varTree, var)) {
return;
}
checkAssignability(var, varTree);
commonAssignmentCheck(var, valueExp, errorKey);
}
use of org.checkerframework.framework.type.AnnotatedTypeMirror in project checker-framework by typetools.
the class BaseTypeVisitor method checkTypecastSafety.
protected void checkTypecastSafety(TypeCastTree node, Void p) {
if (!checker.getLintOption("cast:unsafe", true)) {
return;
}
AnnotatedTypeMirror castType = atypeFactory.getAnnotatedType(node);
AnnotatedTypeMirror exprType = atypeFactory.getAnnotatedType(node.getExpression());
// the input types to be subtypes according to Java
if (!isTypeCastSafe(castType, exprType)) {
checker.report(Result.warning("cast.unsafe", exprType.toString(true), castType.toString(true)), node);
}
}
use of org.checkerframework.framework.type.AnnotatedTypeMirror in project checker-framework by typetools.
the class DefaultReflectionResolver method resolveMethodCall.
/**
* Resolves a call to {@link Method#invoke(Object, Object...)}.
*
* @param factory the {@link AnnotatedTypeFactory} of the underlying type system
* @param tree the method invocation tree that has to be resolved
* @param origResult the original result from {@code factory.methodFromUse}.
*/
private Pair<AnnotatedExecutableType, List<AnnotatedTypeMirror>> resolveMethodCall(AnnotatedTypeFactory factory, MethodInvocationTree tree, Pair<AnnotatedExecutableType, List<AnnotatedTypeMirror>> origResult) {
debugReflection("Try to resolve reflective method call: " + tree);
List<MethodInvocationTree> possibleMethods = resolveReflectiveMethod(tree, factory);
// Reflective method could not be resolved
if (possibleMethods.size() == 0) {
return origResult;
}
Set<? extends AnnotationMirror> returnLub = null;
Set<? extends AnnotationMirror> receiverGlb = null;
Set<? extends AnnotationMirror> paramsGlb = null;
// and parameter types
for (MethodInvocationTree resolvedTree : possibleMethods) {
debugReflection("Resolved method invocation: " + resolvedTree);
if (!checkMethodAgruments(resolvedTree)) {
debugReflection("Spoofed tree's arguments did not match declaration" + resolvedTree.toString());
// in QualifierPolymorphism.PolyCollector.visitArray(...)
continue;
}
Pair<AnnotatedExecutableType, List<AnnotatedTypeMirror>> resolvedResult = factory.methodFromUse(resolvedTree);
// Lub return types
returnLub = lub(returnLub, resolvedResult.first.getReturnType().getAnnotations(), factory);
// Check for static methods whose receiver is null
if (resolvedResult.first.getReceiverType() == null) {
// If the method is static the first argument to Method.invoke isn't used,
// so assume top.
receiverGlb = glb(receiverGlb, factory.getQualifierHierarchy().getTopAnnotations(), factory);
} else {
receiverGlb = glb(receiverGlb, resolvedResult.first.getReceiverType().getAnnotations(), factory);
}
// the types of different formal parameters.
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, receiver, parameter type) annotations and
* set lub/glb annotations from resolved method(s)
*/
// return value
origResult.first.getReturnType().clearAnnotations();
origResult.first.getReturnType().addAnnotations(returnLub);
// receiver type
origResult.first.getParameterTypes().get(0).clearAnnotations();
origResult.first.getParameterTypes().get(0).addAnnotations(receiverGlb);
// parameter types
if (paramsGlb != null) {
AnnotatedArrayType origArrayType = (AnnotatedArrayType) origResult.first.getParameterTypes().get(1);
origArrayType.getComponentType().clearAnnotations();
origArrayType.getComponentType().addAnnotations(paramsGlb);
}
debugReflection("Resolved annotations: " + origResult.first);
return origResult;
}
use of org.checkerframework.framework.type.AnnotatedTypeMirror in project checker-framework by typetools.
the class PropagationTreeAnnotator method visitBinary.
@Override
public Void visitBinary(BinaryTree node, AnnotatedTypeMirror type) {
if (hasPrimaryAnnotationInAllHierarchies(type)) {
// expensive.
return null;
}
AnnotatedTypeMirror a = atypeFactory.getAnnotatedType(node.getLeftOperand());
AnnotatedTypeMirror b = atypeFactory.getAnnotatedType(node.getRightOperand());
Set<? extends AnnotationMirror> lubs = qualHierarchy.leastUpperBounds(a.getEffectiveAnnotations(), b.getEffectiveAnnotations());
type.addMissingAnnotations(lubs);
return null;
}
use of org.checkerframework.framework.type.AnnotatedTypeMirror in project checker-framework by typetools.
the class PropagationTypeAnnotator method getTypeParamFromEnclosingClass.
/**
* Search parent's type arguments for wildcard. Using the index of wildcard, find the
* corresponding type parameter element and return it. Returns null if the wildcard is the
* result of substitution and therefore not in the list of type arguments.
*/
private Element getTypeParamFromEnclosingClass(final AnnotatedWildcardType wildcard, final AnnotatedDeclaredType parent) {
Integer wildcardIndex = null;
int currentIndex = 0;
for (AnnotatedTypeMirror typeArg : parent.getTypeArguments()) {
// which they should have been replaced by capture
if (typeArg == wildcard) {
wildcardIndex = currentIndex;
break;
}
currentIndex += 1;
}
if (wildcardIndex != null) {
final TypeElement typeElement = (TypeElement) typeFactory.getProcessingEnv().getTypeUtils().asElement(parent.getUnderlyingType());
return typeElement.getTypeParameters().get(wildcardIndex);
}
return null;
}
Aggregations