Search in sources :

Example 6 with TypeSystemError

use of org.checkerframework.javacutil.TypeSystemError in project checker-framework by typetools.

the class ValueTransfer method refineIntRanges.

/**
 * Calculates the result of a binary comparison on a pair of intRange annotations, and refines
 * annotations appropriately.
 */
private List<Boolean> refineIntRanges(Node leftNode, AnnotationMirror leftAnno, Node rightNode, AnnotationMirror rightAnno, ComparisonOperators op, CFStore thenStore, CFStore elseStore) {
    Range leftRange = getIntRangeFromAnnotation(leftNode, leftAnno);
    Range rightRange = getIntRangeFromAnnotation(rightNode, rightAnno);
    final Range thenRightRange;
    final Range thenLeftRange;
    final Range elseRightRange;
    final Range elseLeftRange;
    switch(op) {
        case EQUAL:
            thenRightRange = rightRange.refineEqualTo(leftRange);
            // Only needs to be computed once.
            thenLeftRange = thenRightRange;
            elseRightRange = rightRange.refineNotEqualTo(leftRange);
            elseLeftRange = leftRange.refineNotEqualTo(rightRange);
            break;
        case GREATER_THAN:
            thenLeftRange = leftRange.refineGreaterThan(rightRange);
            thenRightRange = rightRange.refineLessThan(leftRange);
            elseRightRange = rightRange.refineGreaterThanEq(leftRange);
            elseLeftRange = leftRange.refineLessThanEq(rightRange);
            break;
        case GREATER_THAN_EQ:
            thenRightRange = rightRange.refineLessThanEq(leftRange);
            thenLeftRange = leftRange.refineGreaterThanEq(rightRange);
            elseLeftRange = leftRange.refineLessThan(rightRange);
            elseRightRange = rightRange.refineGreaterThan(leftRange);
            break;
        case LESS_THAN:
            thenLeftRange = leftRange.refineLessThan(rightRange);
            thenRightRange = rightRange.refineGreaterThan(leftRange);
            elseRightRange = rightRange.refineLessThanEq(leftRange);
            elseLeftRange = leftRange.refineGreaterThanEq(rightRange);
            break;
        case LESS_THAN_EQ:
            thenRightRange = rightRange.refineGreaterThanEq(leftRange);
            thenLeftRange = leftRange.refineLessThanEq(rightRange);
            elseLeftRange = leftRange.refineGreaterThan(rightRange);
            elseRightRange = rightRange.refineLessThan(leftRange);
            break;
        case NOT_EQUAL:
            thenRightRange = rightRange.refineNotEqualTo(leftRange);
            thenLeftRange = leftRange.refineNotEqualTo(rightRange);
            elseRightRange = rightRange.refineEqualTo(leftRange);
            // Equality only needs to be computed once.
            elseLeftRange = elseRightRange;
            break;
        default:
            throw new TypeSystemError("ValueTransfer: unsupported operation: " + op);
    }
    createAnnotationFromRangeAndAddToStore(thenStore, thenRightRange, rightNode);
    createAnnotationFromRangeAndAddToStore(thenStore, thenLeftRange, leftNode);
    createAnnotationFromRangeAndAddToStore(elseStore, elseRightRange, rightNode);
    createAnnotationFromRangeAndAddToStore(elseStore, elseLeftRange, leftNode);
    // TODO: Refine the type of the comparison.
    return null;
}
Also used : TypeSystemError(org.checkerframework.javacutil.TypeSystemError) ArrayLenRange(org.checkerframework.common.value.qual.ArrayLenRange) Range(org.checkerframework.common.value.util.Range)

Example 7 with TypeSystemError

use of org.checkerframework.javacutil.TypeSystemError in project checker-framework by typetools.

the class UpperBoundAnnotatedTypeFactory method createLTEqLengthOfAnnotation.

/**
 * Creates a new @LTEqLengthOf annotation.
 *
 * @param names the arguments to @LTEqLengthOf
 * @return a new @LTEqLengthOf annotation with the given arguments
 */
AnnotationMirror createLTEqLengthOfAnnotation(String... names) {
    if (names == null || names.length == 0) {
        throw new TypeSystemError("createLTEqLengthOfAnnotation: bad argument %s", Arrays.toString(names));
    }
    AnnotationBuilder builder = new AnnotationBuilder(getProcessingEnv(), LTEqLengthOf.class);
    builder.setValue("value", names);
    return builder.build();
}
Also used : AnnotationBuilder(org.checkerframework.javacutil.AnnotationBuilder) TypeSystemError(org.checkerframework.javacutil.TypeSystemError)

Example 8 with TypeSystemError

use of org.checkerframework.javacutil.TypeSystemError in project checker-framework by typetools.

the class UpperBoundAnnotatedTypeFactory method createLTLengthOfAnnotation.

/**
 * Creates a new @LTLengthOf annotation.
 *
 * @param names the arguments to @LTLengthOf
 * @return a new @LTLengthOf annotation with the given arguments
 */
AnnotationMirror createLTLengthOfAnnotation(String... names) {
    if (names == null || names.length == 0) {
        throw new TypeSystemError("createLTLengthOfAnnotation: bad argument %s", Arrays.toString(names));
    }
    AnnotationBuilder builder = new AnnotationBuilder(getProcessingEnv(), LTLengthOf.class);
    builder.setValue("value", names);
    return builder.build();
}
Also used : AnnotationBuilder(org.checkerframework.javacutil.AnnotationBuilder) TypeSystemError(org.checkerframework.javacutil.TypeSystemError)

Example 9 with TypeSystemError

use of org.checkerframework.javacutil.TypeSystemError in project checker-framework by typetools.

the class UnitsAnnotatedTypeFactory method addUnitsRelations.

/**
 * Look for an @UnitsRelations annotation on the qualifier and add it to the list of
 * UnitsRelations.
 *
 * @param qual the qualifier to investigate
 */
private void addUnitsRelations(Class<? extends Annotation> qual) {
    AnnotationMirror am = AnnotationBuilder.fromClass(elements, qual);
    for (AnnotationMirror ama : am.getAnnotationType().asElement().getAnnotationMirrors()) {
        if (areSameByClass(ama, unitsRelationsAnnoClass)) {
            String theclassname = AnnotationUtils.getElementValueClassName(ama, unitsRelationsValueElement).toString();
            if (!Signatures.isClassGetName(theclassname)) {
                throw new UserError("Malformed class name \"%s\" should be in ClassGetName format in annotation %s", theclassname, ama);
            }
            Class<?> valueElement;
            try {
                ClassLoader classLoader = InternalUtils.getClassLoaderForClass(AnnotationUtils.class);
                valueElement = Class.forName(theclassname, true, classLoader);
            } catch (ClassNotFoundException e) {
                String msg = String.format("Could not load class '%s' for field 'value' in annotation %s", theclassname, ama);
                throw new UserError(msg, e);
            }
            Class<? extends UnitsRelations> unitsRelationsClass;
            try {
                unitsRelationsClass = valueElement.asSubclass(UnitsRelations.class);
            } catch (ClassCastException ex) {
                throw new UserError("Invalid @UnitsRelations meta-annotation found in %s. " + "@UnitsRelations value %s is not a subclass of " + "org.checkerframework.checker.units.UnitsRelations.", qual, ama);
            }
            String classname = unitsRelationsClass.getCanonicalName();
            if (!getUnitsRel().containsKey(classname)) {
                try {
                    unitsRel.put(classname, unitsRelationsClass.getDeclaredConstructor().newInstance().init(processingEnv));
                } catch (Throwable e) {
                    throw new TypeSystemError("Throwable when instantiating UnitsRelations", e);
                }
            }
        }
    }
}
Also used : AnnotationMirror(javax.lang.model.element.AnnotationMirror) UserError(org.checkerframework.javacutil.UserError) AnnotationClassLoader(org.checkerframework.framework.type.AnnotationClassLoader) TypeSystemError(org.checkerframework.javacutil.TypeSystemError)

Example 10 with TypeSystemError

use of org.checkerframework.javacutil.TypeSystemError in project checker-framework by typetools.

the class ElementQualifierHierarchy method getPolymorphicAnnotation.

@Override
@Nullable
public AnnotationMirror getPolymorphicAnnotation(AnnotationMirror start) {
    QualifierKind polyKind = getQualifierKind(start).getPolymorphic();
    if (polyKind == null) {
        return null;
    }
    AnnotationMirror poly = kindToElementlessQualifier.get(polyKind);
    if (poly == null) {
        throw new TypeSystemError("Poly %s has an element. Override ElementQualifierHierarchy#getPolymorphicAnnotation.", polyKind);
    }
    return poly;
}
Also used : AnnotationMirror(javax.lang.model.element.AnnotationMirror) TypeSystemError(org.checkerframework.javacutil.TypeSystemError) QualifierKind(org.checkerframework.framework.util.QualifierKind) Nullable(org.checkerframework.checker.nullness.qual.Nullable)

Aggregations

TypeSystemError (org.checkerframework.javacutil.TypeSystemError)23 AnnotationMirror (javax.lang.model.element.AnnotationMirror)6 ExecutableElement (javax.lang.model.element.ExecutableElement)4 Range (org.checkerframework.common.value.util.Range)4 Element (javax.lang.model.element.Element)3 VariableElement (javax.lang.model.element.VariableElement)3 RequiresNonNull (org.checkerframework.checker.nullness.qual.RequiresNonNull)3 ArrayLenRange (org.checkerframework.common.value.qual.ArrayLenRange)3 MethodTree (com.sun.source.tree.MethodTree)2 ArrayList (java.util.ArrayList)2 TypeElement (javax.lang.model.element.TypeElement)2 MustCallAnnotatedTypeFactory (org.checkerframework.checker.mustcall.MustCallAnnotatedTypeFactory)2 MethodInvocationNode (org.checkerframework.dataflow.cfg.node.MethodInvocationNode)2 ObjectCreationNode (org.checkerframework.dataflow.cfg.node.ObjectCreationNode)2 AnnotatedTypeMirror (org.checkerframework.framework.type.AnnotatedTypeMirror)2 AnnotationBuilder (org.checkerframework.javacutil.AnnotationBuilder)2 ConditionalExpressionTree (com.sun.source.tree.ConditionalExpressionTree)1 ExpressionTree (com.sun.source.tree.ExpressionTree)1 LambdaExpressionTree (com.sun.source.tree.LambdaExpressionTree)1 MemberReferenceTree (com.sun.source.tree.MemberReferenceTree)1