Search in sources :

Example 41 with AnnotatedTypeMirror

use of org.checkerframework.framework.type.AnnotatedTypeMirror in project checker-framework by typetools.

the class BaseTypeVisitor method checkTypecastSafety.

/**
 * Issues a warning if the given explicitly-written typecast is unsafe. Does nothing if the lint
 * option "cast:unsafe" is not set. Only primary qualifiers are checked unless the command line
 * option "checkCastElementType" is supplied.
 *
 * @param typeCastTree an explicitly-written typecast
 */
protected void checkTypecastSafety(TypeCastTree typeCastTree) {
    if (!checker.getLintOption("cast:unsafe", true)) {
        return;
    }
    AnnotatedTypeMirror castType = atypeFactory.getAnnotatedType(typeCastTree);
    AnnotatedTypeMirror exprType = atypeFactory.getAnnotatedType(typeCastTree.getExpression());
    boolean calledOnce = false;
    for (AnnotationMirror top : atypeFactory.getQualifierParameterHierarchies(castType)) {
        if (!isInvariantTypeCastSafe(castType, exprType, top)) {
            checker.reportError(typeCastTree, "invariant.cast.unsafe", exprType.toString(true), castType.toString(true));
        }
        // don't issue cast unsafe warning.
        calledOnce = true;
    }
    // the input types to be subtypes according to Java.
    if (!calledOnce && !isTypeCastSafe(castType, exprType)) {
        checker.reportWarning(typeCastTree, "cast.unsafe", exprType.toString(true), castType.toString(true));
    }
}
Also used : AnnotationMirror(javax.lang.model.element.AnnotationMirror) AnnotatedTypeMirror(org.checkerframework.framework.type.AnnotatedTypeMirror)

Example 42 with AnnotatedTypeMirror

use of org.checkerframework.framework.type.AnnotatedTypeMirror in project checker-framework by typetools.

the class BaseTypeVisitor method checkThisOrSuperConstructorCall.

/**
 * Checks that the following rule is satisfied: The type on a constructor declaration must be a
 * supertype of the return type of "this()" or "super()" invocation within that constructor.
 *
 * @param call the AST node for the constructor call
 * @param errorKey the error message key to use if the check fails
 */
protected void checkThisOrSuperConstructorCall(MethodInvocationTree call, @CompilerMessageKey String errorKey) {
    TreePath path = atypeFactory.getPath(call);
    MethodTree enclosingMethod = TreePathUtil.enclosingMethod(path);
    AnnotatedTypeMirror superType = atypeFactory.getAnnotatedType(call);
    AnnotatedExecutableType constructorType = atypeFactory.getAnnotatedType(enclosingMethod);
    Set<? extends AnnotationMirror> topAnnotations = atypeFactory.getQualifierHierarchy().getTopAnnotations();
    for (AnnotationMirror topAnno : topAnnotations) {
        AnnotationMirror superTypeMirror = superType.getAnnotationInHierarchy(topAnno);
        AnnotationMirror constructorTypeMirror = constructorType.getReturnType().getAnnotationInHierarchy(topAnno);
        if (!atypeFactory.getQualifierHierarchy().isSubtype(superTypeMirror, constructorTypeMirror)) {
            checker.reportError(call, errorKey, constructorTypeMirror, call, superTypeMirror);
        }
    }
}
Also used : AnnotatedExecutableType(org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedExecutableType) AnnotationMirror(javax.lang.model.element.AnnotationMirror) TreePath(com.sun.source.util.TreePath) MethodTree(com.sun.source.tree.MethodTree) AnnotatedTypeMirror(org.checkerframework.framework.type.AnnotatedTypeMirror)

Example 43 with AnnotatedTypeMirror

use of org.checkerframework.framework.type.AnnotatedTypeMirror in project checker-framework by typetools.

the class MethodSignature method getMethodNamesFromStringArg.

/**
 * Returns the string values for the argument passed. The String Values are estimated using the
 * Value Checker.
 *
 * @param arg ExpressionTree whose string values are sought
 * @return string values of arg or the empty list if no values were found
 */
private List<String> getMethodNamesFromStringArg(ExpressionTree arg) {
    ValueAnnotatedTypeFactory valueATF = getTypeFactoryOfSubchecker(ValueChecker.class);
    AnnotatedTypeMirror valueAnno = valueATF.getAnnotatedType(arg);
    AnnotationMirror annotation = valueAnno.getAnnotation(StringVal.class);
    if (annotation != null) {
        return AnnotationUtils.getElementValueArray(annotation, stringValValueElement, String.class);
    } else {
        return EMPTY_STRING_LIST;
    }
}
Also used : AnnotationMirror(javax.lang.model.element.AnnotationMirror) AnnotatedTypeMirror(org.checkerframework.framework.type.AnnotatedTypeMirror) ValueAnnotatedTypeFactory(org.checkerframework.common.value.ValueAnnotatedTypeFactory)

Example 44 with AnnotatedTypeMirror

use of org.checkerframework.framework.type.AnnotatedTypeMirror in project checker-framework by typetools.

the class ValueAnnotatedTypeFactory method getToValueFromIntRange.

/**
 * Finds the appropriate value for the {@code to} value of an annotated type mirror containing an
 * {@code IntRange} annotation.
 *
 * @param atm an annotated type mirror that contains an {@code IntRange} annotation
 * @return either the to value from the passed int range annotation, or the maximum value of the
 *     domain of the underlying type (i.e. Integer.MAX_VALUE if the underlying type is int)
 */
public long getToValueFromIntRange(AnnotatedTypeMirror atm) {
    TypeMirror type = atm.getUnderlyingType();
    long defaultValue = TypeKindUtils.maxValue(toPrimitiveIntegralTypeKind(type));
    AnnotationMirror intRangeAnno = atm.getAnnotation(IntRange.class);
    return getIntRangeToValue(intRangeAnno, defaultValue);
}
Also used : AnnotationMirror(javax.lang.model.element.AnnotationMirror) AnnotatedTypeMirror(org.checkerframework.framework.type.AnnotatedTypeMirror) TypeMirror(javax.lang.model.type.TypeMirror)

Example 45 with AnnotatedTypeMirror

use of org.checkerframework.framework.type.AnnotatedTypeMirror in project checker-framework by typetools.

the class ValueAnnotatedTypeFactory method getDummyAssignedTo.

/**
 * Returns the annotation type mirror for the type of {@code expressionTree} with default
 * annotations applied.
 */
@Override
public AnnotatedTypeMirror getDummyAssignedTo(ExpressionTree expressionTree) {
    TypeMirror type = TreeUtils.typeOf(expressionTree);
    if (type.getKind() != TypeKind.VOID) {
        AnnotatedTypeMirror atm = type(expressionTree);
        addDefaultAnnotations(atm);
        return atm;
    }
    return null;
}
Also used : AnnotatedTypeMirror(org.checkerframework.framework.type.AnnotatedTypeMirror) TypeMirror(javax.lang.model.type.TypeMirror) AnnotatedTypeMirror(org.checkerframework.framework.type.AnnotatedTypeMirror)

Aggregations

AnnotatedTypeMirror (org.checkerframework.framework.type.AnnotatedTypeMirror)299 AnnotationMirror (javax.lang.model.element.AnnotationMirror)84 ExpressionTree (com.sun.source.tree.ExpressionTree)53 AnnotatedDeclaredType (org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedDeclaredType)44 ExecutableElement (javax.lang.model.element.ExecutableElement)41 Tree (com.sun.source.tree.Tree)40 TypeMirror (javax.lang.model.type.TypeMirror)38 AnnotatedExecutableType (org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedExecutableType)38 AnnotatedTypeVariable (org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedTypeVariable)38 MethodTree (com.sun.source.tree.MethodTree)34 ArrayList (java.util.ArrayList)34 BugInCF (org.checkerframework.javacutil.BugInCF)32 MethodInvocationTree (com.sun.source.tree.MethodInvocationTree)30 TypeElement (javax.lang.model.element.TypeElement)29 VariableElement (javax.lang.model.element.VariableElement)29 VariableTree (com.sun.source.tree.VariableTree)28 LambdaExpressionTree (com.sun.source.tree.LambdaExpressionTree)27 AnnotatedArrayType (org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedArrayType)26 ClassTree (com.sun.source.tree.ClassTree)25 Map (java.util.Map)24