All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.checkerframework.dataflow.cfg.node.FieldAccessNode Maven / Gradle / Ivy

Go to download

The Checker Framework enhances Java's type system to make it more powerful and useful. This lets software developers detect and prevent errors in their Java programs. The Checker Framework includes compiler plug-ins ("checkers") that find bugs or verify their absence. It also permits you to write your own compiler plug-ins.

There is a newer version: 3.42.0-eisop4
Show newest version
package org.checkerframework.dataflow.cfg.node;

import com.sun.source.tree.IdentifierTree;
import com.sun.source.tree.MemberSelectTree;
import com.sun.source.tree.Tree;

import org.checkerframework.checker.nullness.qual.Nullable;
import org.checkerframework.dataflow.qual.SideEffectFree;
import org.checkerframework.javacutil.BugInCF;
import org.checkerframework.javacutil.ElementUtils;
import org.checkerframework.javacutil.TreeUtils;

import java.util.Collection;
import java.util.Collections;
import java.util.Objects;

import javax.lang.model.element.VariableElement;

/**
 * A node for a field access, including a method accesses:
 *
 * 
 *   expression . field
 * 
*/ public class FieldAccessNode extends Node { /** The tree of the field access. */ protected final Tree tree; /** The element of the accessed field. */ protected final VariableElement element; /** The name of the accessed field. */ protected final String field; /** The receiver node of the field access. */ protected final Node receiver; /** * Creates a new FieldAccessNode. * * @param tree the tree from which to create a FieldAccessNode * @param receiver the receiver for the resulting FieldAccessNode */ public FieldAccessNode(Tree tree, Node receiver) { super(TreeUtils.typeOf(tree)); assert TreeUtils.isFieldAccess(tree); this.tree = tree; this.receiver = receiver; this.field = TreeUtils.getFieldName(tree); if (tree instanceof MemberSelectTree) { MemberSelectTree mstree = (MemberSelectTree) tree; assert TreeUtils.isUseOfElement(mstree) : "@AssumeAssertion(nullness): tree kind"; this.element = TreeUtils.variableElementFromUse(mstree); } else if (tree instanceof IdentifierTree) { IdentifierTree itree = (IdentifierTree) tree; assert TreeUtils.isUseOfElement(itree) : "@AssumeAssertion(nullness): tree kind"; this.element = TreeUtils.variableElementFromUse(itree); } else { throw new BugInCF("unexpected tree %s [%s]", tree, tree.getClass()); } } public FieldAccessNode(Tree tree, VariableElement element, Node receiver) { super(element.asType()); this.tree = tree; this.element = element; this.receiver = receiver; this.field = element.getSimpleName().toString(); } public VariableElement getElement() { return element; } public Node getReceiver() { return receiver; } public String getFieldName() { return field; } @Override public Tree getTree() { return tree; } @Override public R accept(NodeVisitor visitor, P p) { return visitor.visitFieldAccess(this, p); } @Override public String toString() { return getReceiver() + "." + field; } /** * Determine whether the field is static or not. * * @return whether the field is static or not */ public boolean isStatic() { return ElementUtils.isStatic(getElement()); } @Override public boolean equals(@Nullable Object obj) { if (!(obj instanceof FieldAccessNode)) { return false; } FieldAccessNode other = (FieldAccessNode) obj; return getReceiver().equals(other.getReceiver()) && getFieldName().equals(other.getFieldName()); } @Override public int hashCode() { return Objects.hash(getReceiver(), getFieldName()); } @Override @SideEffectFree public Collection getOperands() { return Collections.singletonList(receiver); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy