use of com.sun.source.tree.ClassTree in project checker-framework by typetools.
the class CFGTranslationPhaseOne method visitVariable.
@Override
public Node visitVariable(VariableTree tree, Void p) {
// see JLS 14.4
boolean isField = false;
if (getCurrentPath().getParentPath() != null) {
Tree.Kind kind = TreeUtils.getKindRecordAsClass(getCurrentPath().getParentPath().getLeaf());
// CLASS includes records.
if (kind == Tree.Kind.CLASS || kind == Tree.Kind.INTERFACE || kind == Tree.Kind.ENUM) {
isField = true;
}
}
Node node = null;
ClassTree enclosingClass = TreePathUtil.enclosingClass(getCurrentPath());
TypeElement classElem = TreeUtils.elementFromDeclaration(enclosingClass);
Node receiver = new ImplicitThisNode(classElem.asType());
if (isField) {
ExpressionTree initializer = tree.getInitializer();
assert initializer != null;
node = translateAssignment(tree, new FieldAccessNode(tree, TreeUtils.elementFromDeclaration(tree), receiver), initializer);
} else {
// local variable definition
VariableDeclarationNode decl = new VariableDeclarationNode(tree);
extendWithNode(decl);
// initializer
ExpressionTree initializer = tree.getInitializer();
if (initializer != null) {
node = translateAssignment(tree, new LocalVariableNode(tree, receiver), initializer);
}
}
return node;
}
use of com.sun.source.tree.ClassTree 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.ClassTree in project checker-framework by typetools.
the class InitializationAnnotatedTypeFactory method setSelfTypeInInitializationCode.
/**
* Side-effects argument {@code selfType} to make it @Initialized or @UnderInitialization,
* depending on whether all fields have been set.
*
* @param tree a tree
* @param selfType the type to side-effect
* @param path a path
*/
protected void setSelfTypeInInitializationCode(Tree tree, AnnotatedDeclaredType selfType, TreePath path) {
ClassTree enclosingClass = TreePathUtil.enclosingClass(path);
Type classType = ((JCTree) enclosingClass).type;
AnnotationMirror annotation = null;
// there might still be subclasses that need initialization.
if (areAllFieldsInitializedOnly(enclosingClass)) {
Store store = getStoreBefore(tree);
if (store != null && getUninitializedInvariantFields(store, path, false, Collections.emptyList()).isEmpty()) {
if (classType.isFinal()) {
annotation = INITIALIZED;
} else {
annotation = createUnderInitializationAnnotation(classType);
}
}
}
if (annotation == null) {
annotation = getUnderInitializationAnnotationOfSuperType(classType);
}
selfType.replaceAnnotation(annotation);
}
use of com.sun.source.tree.ClassTree 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.ClassTree in project checker-framework by typetools.
the class InitializationAnnotatedTypeFactory method findTopLevelClassMemberForTree.
/**
* In the first enclosing class, find the path to the top-level member that contains {@code path}.
*
* @param path the path whose leaf is the target
* @return path to a top-level member containing the leaf of {@code path}
*/
// AST node comparison
@SuppressWarnings("interning:not.interned")
private TreePath findTopLevelClassMemberForTree(TreePath path) {
if (TreeUtils.isClassTree(path.getLeaf())) {
path = path.getParentPath();
if (path == null) {
return null;
}
}
ClassTree enclosingClass = TreePathUtil.enclosingClass(path);
if (enclosingClass != null) {
List<? extends Tree> classMembers = enclosingClass.getMembers();
TreePath searchPath = path;
while (searchPath.getParentPath() != null && searchPath.getParentPath().getLeaf() != enclosingClass) {
searchPath = searchPath.getParentPath();
if (classMembers.contains(searchPath.getLeaf())) {
return searchPath;
}
}
}
return null;
}
Aggregations