Search in sources :

Example 1 with ImplicitThisLiteralNode

use of org.checkerframework.dataflow.cfg.node.ImplicitThisLiteralNode in project checker-framework by typetools.

the class FlowExpressionParseUtil method getReceiverField.

private static Receiver getReceiverField(String s, FlowExpressionContext context, boolean originalReceiver, VariableElement fieldElem) throws FlowExpressionParseException {
    TypeMirror receiverType = context.receiver.getType();
    TypeMirror fieldType = ElementUtils.getType(fieldElem);
    if (ElementUtils.isStatic(fieldElem)) {
        Element classElem = fieldElem.getEnclosingElement();
        Receiver staticClassReceiver = new ClassName(ElementUtils.getType(classElem));
        return new FieldAccess(staticClassReceiver, fieldType, fieldElem);
    }
    Receiver locationOfField;
    if (originalReceiver) {
        locationOfField = context.receiver;
    } else {
        locationOfField = FlowExpressions.internalReprOf(context.checkerContext.getAnnotationProvider(), new ImplicitThisLiteralNode(receiverType));
    }
    if (locationOfField instanceof ClassName) {
        throw constructParserException(s, "a non-static field cannot have a class name as a receiver.");
    }
    return new FieldAccess(locationOfField, fieldType, fieldElem);
}
Also used : ImplicitThisLiteralNode(org.checkerframework.dataflow.cfg.node.ImplicitThisLiteralNode) TypeMirror(javax.lang.model.type.TypeMirror) TypeElement(javax.lang.model.element.TypeElement) Element(javax.lang.model.element.Element) VariableElement(javax.lang.model.element.VariableElement) ExecutableElement(javax.lang.model.element.ExecutableElement) ClassName(org.checkerframework.dataflow.analysis.FlowExpressions.ClassName) Receiver(org.checkerframework.dataflow.analysis.FlowExpressions.Receiver) FieldAccess(org.checkerframework.dataflow.analysis.FlowExpressions.FieldAccess)

Example 2 with ImplicitThisLiteralNode

use of org.checkerframework.dataflow.cfg.node.ImplicitThisLiteralNode in project checker-framework by typetools.

the class WholeProgramInferenceScenes method getEnclosingClassSymbol.

/**
 * Returns the ClassSymbol of the class encapsulating the node n passed as parameter.
 *
 * <p>If the receiver of field is an instance of "this", the implementation obtains the
 * ClassSymbol by using classTree. Otherwise, the ClassSymbol is from the field's receiver.
 */
// TODO: These methods below could be moved somewhere else.
private ClassSymbol getEnclosingClassSymbol(ClassTree classTree, Node field) {
    Node receiverNode = null;
    if (field instanceof FieldAccessNode) {
        receiverNode = ((FieldAccessNode) field).getReceiver();
    } else if (field instanceof LocalVariableNode) {
        receiverNode = ((LocalVariableNode) field).getReceiver();
    } else {
        ErrorReporter.errorAbort("Unexpected type: " + field.getClass());
    }
    if ((receiverNode == null || receiverNode instanceof ImplicitThisLiteralNode) && classTree != null) {
        return (ClassSymbol) TreeUtils.elementFromTree(classTree);
    }
    TypeMirror type = receiverNode.getType();
    if (type instanceof ClassType) {
        TypeSymbol tsym = ((ClassType) type).asElement();
        return tsym.enclClass();
    }
    return getEnclosingClassSymbol(receiverNode.getTree());
}
Also used : ImplicitThisLiteralNode(org.checkerframework.dataflow.cfg.node.ImplicitThisLiteralNode) AnnotatedTypeMirror(org.checkerframework.framework.type.AnnotatedTypeMirror) TypeMirror(javax.lang.model.type.TypeMirror) ClassSymbol(com.sun.tools.javac.code.Symbol.ClassSymbol) FieldAccessNode(org.checkerframework.dataflow.cfg.node.FieldAccessNode) ObjectCreationNode(org.checkerframework.dataflow.cfg.node.ObjectCreationNode) ReturnNode(org.checkerframework.dataflow.cfg.node.ReturnNode) MethodInvocationNode(org.checkerframework.dataflow.cfg.node.MethodInvocationNode) ImplicitThisLiteralNode(org.checkerframework.dataflow.cfg.node.ImplicitThisLiteralNode) LocalVariableNode(org.checkerframework.dataflow.cfg.node.LocalVariableNode) Node(org.checkerframework.dataflow.cfg.node.Node) TypeSymbol(com.sun.tools.javac.code.Symbol.TypeSymbol) ClassType(com.sun.tools.javac.code.Type.ClassType) FieldAccessNode(org.checkerframework.dataflow.cfg.node.FieldAccessNode) LocalVariableNode(org.checkerframework.dataflow.cfg.node.LocalVariableNode)

Aggregations

TypeMirror (javax.lang.model.type.TypeMirror)2 ImplicitThisLiteralNode (org.checkerframework.dataflow.cfg.node.ImplicitThisLiteralNode)2 ClassSymbol (com.sun.tools.javac.code.Symbol.ClassSymbol)1 TypeSymbol (com.sun.tools.javac.code.Symbol.TypeSymbol)1 ClassType (com.sun.tools.javac.code.Type.ClassType)1 Element (javax.lang.model.element.Element)1 ExecutableElement (javax.lang.model.element.ExecutableElement)1 TypeElement (javax.lang.model.element.TypeElement)1 VariableElement (javax.lang.model.element.VariableElement)1 ClassName (org.checkerframework.dataflow.analysis.FlowExpressions.ClassName)1 FieldAccess (org.checkerframework.dataflow.analysis.FlowExpressions.FieldAccess)1 Receiver (org.checkerframework.dataflow.analysis.FlowExpressions.Receiver)1 FieldAccessNode (org.checkerframework.dataflow.cfg.node.FieldAccessNode)1 LocalVariableNode (org.checkerframework.dataflow.cfg.node.LocalVariableNode)1 MethodInvocationNode (org.checkerframework.dataflow.cfg.node.MethodInvocationNode)1 Node (org.checkerframework.dataflow.cfg.node.Node)1 ObjectCreationNode (org.checkerframework.dataflow.cfg.node.ObjectCreationNode)1 ReturnNode (org.checkerframework.dataflow.cfg.node.ReturnNode)1 AnnotatedTypeMirror (org.checkerframework.framework.type.AnnotatedTypeMirror)1