Search in sources :

Example 1 with Identifier

use of org.checkerframework.checker.signature.qual.Identifier in project checker-framework by typetools.

the class ValueTreeAnnotator method visitFieldAccess.

/**
 * Visit a tree that might be a field access.
 *
 * @param tree a tree that might be a field access. It is either a MemberSelectTree or an
 *     IdentifierTree (if the programmer omitted the leading `this.`).
 * @param type its type
 */
private void visitFieldAccess(ExpressionTree tree, AnnotatedTypeMirror type) {
    if (!TreeUtils.isFieldAccess(tree) || !handledByValueChecker(type)) {
        return;
    }
    VariableElement fieldElement = (VariableElement) TreeUtils.elementFromTree(tree);
    Object value = fieldElement.getConstantValue();
    if (value != null) {
        // The field is a compile-time constant.
        type.replaceAnnotation(atypeFactory.createResultingAnnotation(type.getUnderlyingType(), value));
        return;
    }
    if (ElementUtils.isStatic(fieldElement) && ElementUtils.isFinal(fieldElement)) {
        // The field is static and final, but its declaration does not initialize it to a
        // compile-time constant.  Obtain its value reflectively.
        Element classElement = fieldElement.getEnclosingElement();
        if (classElement != null) {
            @// TODO: bug in ValueAnnotatedTypeFactory.
            SuppressWarnings(// TODO: bug in ValueAnnotatedTypeFactory.
            "signature") @BinaryName String classname = ElementUtils.getQualifiedClassName(classElement).toString();
            // https://tinyurl.com/cfissue/658 for Name.toString()
            @SuppressWarnings("signature") @Identifier String fieldName = fieldElement.getSimpleName().toString();
            value = atypeFactory.evaluator.evaluateStaticFieldAccess(classname, fieldName, tree);
            if (value != null) {
                type.replaceAnnotation(atypeFactory.createResultingAnnotation(type.getUnderlyingType(), value));
            }
            return;
        }
    }
    return;
}
Also used : Identifier(org.checkerframework.checker.signature.qual.Identifier) VariableElement(javax.lang.model.element.VariableElement) Element(javax.lang.model.element.Element) BinaryName(org.checkerframework.checker.signature.qual.BinaryName) VariableElement(javax.lang.model.element.VariableElement)

Aggregations

Element (javax.lang.model.element.Element)1 VariableElement (javax.lang.model.element.VariableElement)1 BinaryName (org.checkerframework.checker.signature.qual.BinaryName)1 Identifier (org.checkerframework.checker.signature.qual.Identifier)1