dataflow.src.org.checkerframework.dataflow.cfg.node.ClassNameNode Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of checker Show documentation
Show all versions of checker Show documentation
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.
package org.checkerframework.dataflow.cfg.node;
/*>>>
import org.checkerframework.checker.nullness.qual.Nullable;
*/
import org.checkerframework.dataflow.util.HashCodeUtils;
import org.checkerframework.javacutil.InternalUtils;
import org.checkerframework.javacutil.TreeUtils;
import java.util.Collection;
import java.util.Collections;
import javax.lang.model.element.Element;
import com.sun.source.tree.ClassTree;
import com.sun.source.tree.IdentifierTree;
import com.sun.source.tree.MemberSelectTree;
import com.sun.source.tree.Tree;
/**
* A node representing a class name used in an expression
* such as a static method invocation.
*
* parent.class .forName(...)
*
* @author Stefan Heule
* @author Charlie Garrett
*
*/
public class ClassNameNode extends Node {
protected final Tree tree;
/** The class named by this node */
protected final Element element;
/** The parent name, if any. */
protected final /*@Nullable*/ Node parent;
public ClassNameNode(IdentifierTree tree) {
super(InternalUtils.typeOf(tree));
assert tree.getKind() == Tree.Kind.IDENTIFIER;
this.tree = tree;
this.element = TreeUtils.elementFromUse(tree);
this.parent = null;
}
public ClassNameNode(ClassTree tree) {
super(InternalUtils.typeOf(tree));
assert tree.getKind() == Tree.Kind.CLASS || tree.getKind() == Tree.Kind.ENUM || tree.getKind() == Tree.Kind.INTERFACE || tree.getKind() == Tree.Kind.ANNOTATION_TYPE;
this.tree = tree;
this.element = TreeUtils.elementFromDeclaration(tree);
this.parent = null;
}
public ClassNameNode(MemberSelectTree tree, Node parent) {
super(InternalUtils.typeOf(tree));
this.tree = tree;
this.element = TreeUtils.elementFromUse(tree);
this.parent = parent;
}
public Element getElement() {
return element;
}
public Node getParent() {
return parent;
}
@Override
public Tree getTree() {
return tree;
}
@Override
public R accept(NodeVisitor visitor, P p) {
return visitor.visitClassName(this, p);
}
@Override
public String toString() {
return getElement().getSimpleName().toString();
}
@Override
public boolean equals(Object obj) {
if (obj == null || !(obj instanceof ClassNameNode)) {
return false;
}
ClassNameNode other = (ClassNameNode) obj;
if (getParent() == null) {
return other.getParent() == null
&& getElement().equals(other.getElement());
} else {
return getParent().equals(other.getParent())
&& getElement().equals(other.getElement());
}
}
@Override
public int hashCode() {
if (parent == null) {
return HashCodeUtils.hash(getElement());
}
return HashCodeUtils.hash(getElement(), getParent());
}
@Override
public Collection getOperands() {
if (parent == null) {
return Collections.emptyList();
}
return Collections.singleton(parent);
}
}