use of com.sun.source.tree.VariableTree in project checker-framework by typetools.
the class InitializationChecker method getAllFields.
/**
* Returns a list of all fields of the given class
*/
public static List<VariableTree> getAllFields(ClassTree clazz) {
List<VariableTree> fields = new ArrayList<>();
for (Tree t : clazz.getMembers()) {
if (t.getKind().equals(Tree.Kind.VARIABLE)) {
VariableTree vt = (VariableTree) t;
fields.add(vt);
}
}
return fields;
}
use of com.sun.source.tree.VariableTree in project checker-framework by typetools.
the class InitializationVisitor method checkFieldsInitialized.
/**
* Checks that all fields (all static fields if {@code staticFields} is true) are initialized in
* the given store.
*/
// TODO: the code for checking if fields are initialized should be re-written,
// as the current version contains quite a few ugly parts, is hard to understand,
// and it is likely that it does not take full advantage of the information
// about initialization we compute in
// GenericAnnotatedTypeFactory.initializationStaticStore and
// GenericAnnotatedTypeFactory.initializationStore.
protected void checkFieldsInitialized(Tree blockNode, boolean staticFields, Store store, List<? extends AnnotationMirror> receiverAnnotations) {
// successfully
if (store != null) {
List<VariableTree> violatingFields = atypeFactory.getUninitializedInvariantFields(store, getCurrentPath(), staticFields, receiverAnnotations);
if (staticFields) {
// TODO: Why is nothing done for static fields?
// Do we need the following?
// violatingFields.removeAll(store.initializedFields);
} else {
// remove fields that have already been initialized by an
// initializer block
violatingFields.removeAll(initializedFields);
}
// Remove fields with a relevant @SuppressWarnings annotation.
Iterator<VariableTree> itor = violatingFields.iterator();
while (itor.hasNext()) {
VariableTree f = itor.next();
Element e = TreeUtils.elementFromTree(f);
if (checker.shouldSuppressWarnings(e, COMMITMENT_FIELDS_UNINITIALIZED)) {
itor.remove();
}
}
if (!violatingFields.isEmpty()) {
StringBuilder fieldsString = new StringBuilder();
boolean first = true;
for (VariableTree f : violatingFields) {
if (!first) {
fieldsString.append(", ");
}
first = false;
fieldsString.append(f.getName());
}
checker.report(Result.failure(COMMITMENT_FIELDS_UNINITIALIZED, fieldsString), blockNode);
}
}
}
use of com.sun.source.tree.VariableTree in project checker-framework by typetools.
the class InitializationVisitor method processClassTree.
@Override
public void processClassTree(ClassTree node) {
// them later when checking constructors.
for (Tree member : node.getMembers()) {
if (member instanceof BlockTree && !((BlockTree) member).isStatic()) {
BlockTree block = (BlockTree) member;
Store store = atypeFactory.getRegularExitStore(block);
if (store != null) {
// Add field values for fields with an initializer.
for (Pair<VariableElement, Value> t : store.getAnalysis().getFieldValues()) {
store.addInitializedField(t.first);
}
final List<VariableTree> init = atypeFactory.getInitializedInvariantFields(store, getCurrentPath());
initializedFields.addAll(init);
}
}
}
super.processClassTree(node);
// Is there a static initializer block?
boolean hasStaticInitializer = false;
for (Tree t : node.getMembers()) {
switch(t.getKind()) {
case BLOCK:
if (((BlockTree) t).isStatic()) {
hasStaticInitializer = true;
}
break;
default:
break;
}
}
// initializer (otherwise, errors are reported there).
if (!hasStaticInitializer && node.getKind() == Kind.CLASS) {
boolean isStatic = true;
// See GenericAnnotatedTypeFactory.performFlowAnalysis for why we use
// the regular exit store of the class here.
Store store = atypeFactory.getRegularExitStore(node);
// Add field values for fields with an initializer.
for (Pair<VariableElement, Value> t : store.getAnalysis().getFieldValues()) {
store.addInitializedField(t.first);
}
List<AnnotationMirror> receiverAnnotations = Collections.emptyList();
checkFieldsInitialized(node, isStatic, store, receiverAnnotations);
}
}
use of com.sun.source.tree.VariableTree in project checker-framework by typetools.
the class LockVisitor method issueErrorIfGuardSatisfiedAnnotationInUnsupportedLocation.
/**
* Issues an error if a GuardSatisfied annotation is found in a location other than a method
* return type or parameter (including the receiver).
*
* @param annotationTree AnnotationTree used for error reporting and to help determine that an
* array parameter has no GuardSatisfied annotations except on the array type
*/
// TODO: Remove this method once @TargetLocations are enforced (i.e. once
// issue https://github.com/typetools/checker-framework/issues/515 is closed).
private void issueErrorIfGuardSatisfiedAnnotationInUnsupportedLocation(AnnotationTree annotationTree) {
TreePath currentPath = getCurrentPath();
TreePath path = getPathForLocalVariableRetrieval(currentPath);
if (path != null) {
Tree tree = path.getLeaf();
Tree.Kind kind = tree.getKind();
if (kind == Tree.Kind.METHOD) {
// The @GuardSatisfied annotation is on the return type.
return;
} else if (kind == Tree.Kind.VARIABLE) {
VariableTree varTree = (VariableTree) tree;
Tree varTypeTree = varTree.getType();
if (varTypeTree != null) {
TreePath parentPath = path.getParentPath();
if (parentPath != null && parentPath.getLeaf().getKind() == Tree.Kind.METHOD) {
Tree.Kind varTypeTreeKind = varTypeTree.getKind();
if (varTypeTreeKind == Tree.Kind.ANNOTATED_TYPE) {
AnnotatedTypeTree annotatedTypeTree = (AnnotatedTypeTree) varTypeTree;
if (annotatedTypeTree.getUnderlyingType().getKind() != Tree.Kind.ARRAY_TYPE || annotatedTypeTree.getAnnotations().contains(annotationTree)) {
// Method parameter
return;
}
} else if (varTypeTreeKind != Tree.Kind.ARRAY_TYPE) {
// Method parameter or receiver
return;
}
}
}
}
}
checker.report(Result.failure("guardsatisfied.location.disallowed"), annotationTree);
}
use of com.sun.source.tree.VariableTree in project st-js by st-js.
the class TreeUtils method getAssignmentContext.
/**
* Returns the tree with the assignment context for the treePath leaf node.
*
* The assignment context for the treepath is the most enclosing tree of type:
* <ul>
* <li>AssignmentTree</li>
* <li>CompoundAssignmentTree</li>
* <li>MethodInvocationTree</li>
* <li>NewArrayTree</li>
* <li>NewClassTree</li>
* <li>ReturnTree</li>
* <li>VariableTree</li>
* </ul>
*
* @param treePath
* a {@link com.sun.source.util.TreePath} object.
* @return the assignment context as described.
*/
public static Tree getAssignmentContext(final TreePath treePath) {
TreePath path = treePath.getParentPath();
if (path == null) {
return null;
}
Tree node = path.getLeaf();
if ((node instanceof AssignmentTree) || (node instanceof CompoundAssignmentTree) || (node instanceof MethodInvocationTree) || (node instanceof NewArrayTree) || (node instanceof NewClassTree) || (node instanceof ReturnTree) || (node instanceof VariableTree)) {
return node;
}
return null;
}
Aggregations