use of com.sun.source.tree.VariableTree in project checker-framework by typetools.
the class TreePathUtil method isTreeInStaticScope.
/**
* Returns true if the leaf of the tree path is in a static scope.
*
* @param path TreePath whose leaf may or may not be in static scope
* @return true if the leaf of the tree path is in a static scope
*/
public static boolean isTreeInStaticScope(TreePath path) {
MethodTree enclosingMethod = enclosingMethod(path);
if (enclosingMethod != null) {
return enclosingMethod.getModifiers().getFlags().contains(Modifier.STATIC);
}
// no enclosing method, check for static or initializer block
BlockTree block = enclosingTopLevelBlock(path);
if (block != null) {
return block.isStatic();
}
// check if its in a variable initializer
Tree t = enclosingVariable(path);
if (t != null) {
return ((VariableTree) t).getModifiers().getFlags().contains(Modifier.STATIC);
}
ClassTree classTree = enclosingClass(path);
if (classTree != null) {
return classTree.getModifiers().getFlags().contains(Modifier.STATIC);
}
return false;
}
use of com.sun.source.tree.VariableTree in project checker-framework by typetools.
the class TreePathUtil method getAssignmentContext.
/**
* Returns the "assignment context" for the leaf of {@code treePath}, which is often the leaf of
* the parent of {@code treePath}. (Does not handle pseudo-assignment of an argument to a
* parameter or a receiver expression to a receiver.) This is not the same as {@code
* org.checkerframework.dataflow.cfg.node.AssignmentContext}, which represents the left-hand side
* rather than the assignment itself.
*
* <p>The assignment context for {@code treePath} is the leaf of its parent, if that leaf is one
* of the following trees:
*
* <ul>
* <li>AssignmentTree
* <li>CompoundAssignmentTree
* <li>MethodInvocationTree
* <li>NewArrayTree
* <li>NewClassTree
* <li>ReturnTree
* <li>VariableTree
* </ul>
*
* If the parent is a ConditionalExpressionTree we need to distinguish two cases: If the leaf is
* either the then or else branch of the ConditionalExpressionTree, then recurse on the parent. If
* the leaf is the condition of the ConditionalExpressionTree, then return null to not consider
* this assignment context.
*
* <p>If the leaf is a ParenthesizedTree, then recurse on the parent.
*
* <p>Otherwise, null is returned.
*
* @param treePath a path
* @return the assignment context as described, {@code null} otherwise
*/
@Nullable
public static Tree getAssignmentContext(final TreePath treePath) {
TreePath parentPath = treePath.getParentPath();
if (parentPath == null) {
return null;
}
Tree parent = parentPath.getLeaf();
switch(parent.getKind()) {
// See below for CompoundAssignmentTree.
case ASSIGNMENT:
case METHOD_INVOCATION:
case NEW_ARRAY:
case NEW_CLASS:
case RETURN:
case VARIABLE:
return parent;
case CONDITIONAL_EXPRESSION:
ConditionalExpressionTree cet = (ConditionalExpressionTree) parent;
// AST node comparison
@SuppressWarnings("interning:not.interned") boolean conditionIsLeaf = (cet.getCondition() == treePath.getLeaf());
if (conditionIsLeaf) {
// No point in going on.
return null;
}
// Otherwise use the context of the ConditionalExpressionTree.
return getAssignmentContext(parentPath);
case PARENTHESIZED:
return getAssignmentContext(parentPath);
default:
// so use instanceof rather than listing all 11.
if (parent instanceof CompoundAssignmentTree) {
return parent;
}
return null;
}
}
use of com.sun.source.tree.VariableTree in project checker-framework by typetools.
the class SameLenTransfer method addInformationFromPreconditions.
/**
* Overridden to ensure that SameLen annotations on method parameters are symmetric.
*/
@Override
protected void addInformationFromPreconditions(CFStore info, AnnotatedTypeFactory factory, UnderlyingAST.CFGMethod method, MethodTree methodTree, ExecutableElement methodElement) {
super.addInformationFromPreconditions(info, factory, method, methodTree, methodElement);
List<? extends VariableTree> paramTrees = methodTree.getParameters();
int numParams = paramTrees.size();
List<String> paramNames = new ArrayList<>(numParams);
List<AnnotatedTypeMirror> params = new ArrayList<>(numParams);
for (VariableTree tree : paramTrees) {
paramNames.add(tree.getName().toString());
params.add(aTypeFactory.getAnnotatedType(tree));
}
for (int index = 0; index < numParams; index++) {
// If the parameter has a samelen annotation, then look for other parameters in that
// annotation and propagate default the other annotation so that it is symmetric.
AnnotatedTypeMirror atm = params.get(index);
AnnotationMirror sameLenAnno = atm.getAnnotation(SameLen.class);
if (sameLenAnno == null) {
continue;
}
List<String> values = AnnotationUtils.getElementValueArray(sameLenAnno, aTypeFactory.sameLenValueElement, String.class);
for (String value : values) {
int otherParamIndex = paramNames.indexOf(value);
if (otherParamIndex == -1) {
continue;
}
// the SameLen value is in the list of params, so modify the type of
// that param in the store
AnnotationMirror newSameLen = aTypeFactory.createSameLen(Collections.singletonList(paramNames.get(index)));
JavaExpression otherParamRec = JavaExpression.fromVariableTree(paramTrees.get(otherParamIndex));
info.insertValuePermitNondeterministic(otherParamRec, newSameLen);
}
}
}
use of com.sun.source.tree.VariableTree in project checker-framework by typetools.
the class InitializationAnnotatedTypeFactory method getUninitializedFields.
/**
* Returns the fields that are not yet initialized in a given store. The result is a pair of
* lists:
*
* <ul>
* <li>fields that are not yet initialized and have the invariant annotation
* <li>fields that are not yet initialized and do not have the invariant annotation
* </ul>
*
* @param store a store
* @param path the current path, used to determine the current class
* @param isStatic whether to report static fields or instance fields
* @param receiverAnnotations the annotations on the receiver
* @return the fields that are not yet initialized in a given store (a pair of lists)
*/
public Pair<List<VariableTree>, List<VariableTree>> getUninitializedFields(Store store, TreePath path, boolean isStatic, Collection<? extends AnnotationMirror> receiverAnnotations) {
ClassTree currentClass = TreePathUtil.enclosingClass(path);
List<VariableTree> fields = InitializationChecker.getAllFields(currentClass);
List<VariableTree> uninitWithInvariantAnno = new ArrayList<>();
List<VariableTree> uninitWithoutInvariantAnno = new ArrayList<>();
for (VariableTree field : fields) {
if (isUnused(field, receiverAnnotations)) {
// don't consider unused fields
continue;
}
VariableElement fieldElem = TreeUtils.elementFromDeclaration(field);
if (ElementUtils.isStatic(fieldElem) == isStatic) {
// Has the field been initialized?
if (!store.isFieldInitialized(fieldElem)) {
// Does this field need to satisfy the invariant?
if (hasFieldInvariantAnnotation(field)) {
uninitWithInvariantAnno.add(field);
} else {
uninitWithoutInvariantAnno.add(field);
}
}
}
}
return Pair.of(uninitWithInvariantAnno, uninitWithoutInvariantAnno);
}
use of com.sun.source.tree.VariableTree in project checker-framework by typetools.
the class LowerBoundTransfer method addInformationFromPreconditions.
/**
* Adds a default NonNegative annotation to every character. Implements case 33.
*/
@Override
protected void addInformationFromPreconditions(CFStore info, AnnotatedTypeFactory factory, UnderlyingAST.CFGMethod method, MethodTree methodTree, ExecutableElement methodElement) {
super.addInformationFromPreconditions(info, factory, method, methodTree, methodElement);
List<? extends VariableTree> paramTrees = methodTree.getParameters();
for (VariableTree variableTree : paramTrees) {
if (TreeUtils.typeOf(variableTree).getKind() == TypeKind.CHAR) {
JavaExpression je = JavaExpression.fromVariableTree(variableTree);
info.insertValuePermitNondeterministic(je, aTypeFactory.NN);
}
}
}
Aggregations