use of com.sun.source.tree.ClassTree in project checker-framework by typetools.
the class MustCallTransfer method createTemporaryVar.
/**
* Creates a variable declaration for the given expression node, if possible.
*
* @param node an expression node
* @return a variable tree for the node, or null if an appropriate containing element cannot be
* located
*/
@Nullable
protected VariableTree createTemporaryVar(Node node) {
ExpressionTree tree = (ExpressionTree) node.getTree();
TypeMirror treeType = TreeUtils.typeOf(tree);
Element enclosingElement;
TreePath path = atypeFactory.getPath(tree);
if (path == null) {
enclosingElement = TreeUtils.elementFromTree(tree).getEnclosingElement();
} else {
ClassTree classTree = TreePathUtil.enclosingClass(path);
enclosingElement = TreeUtils.elementFromTree(classTree);
}
if (enclosingElement == null) {
return null;
}
// Declare and initialize a new, unique variable
VariableTree tmpVarTree = treeBuilder.buildVariableDecl(treeType, uniqueName("temp-var"), enclosingElement, tree);
return tmpVarTree;
}
use of com.sun.source.tree.ClassTree in project checker-framework by typetools.
the class InitializationAnnotatedTypeFactory method areAllFieldsInitializedOnly.
/**
* Are all fields initialized-only?
*
* @param classTree the class to query
* @return true if all fields are initialized-only
*/
protected boolean areAllFieldsInitializedOnly(ClassTree classTree) {
for (Tree member : classTree.getMembers()) {
if (member.getKind() != Tree.Kind.VARIABLE) {
continue;
}
VariableTree var = (VariableTree) member;
VariableElement varElt = TreeUtils.elementFromDeclaration(var);
// var is not initialized-only
if (getDeclAnnotation(varElt, NotOnlyInitialized.class) != null) {
// not of constructor which is where this is used
if (!varElt.getModifiers().contains(Modifier.STATIC)) {
return false;
}
}
}
return true;
}
use of com.sun.source.tree.ClassTree in project checker-framework by typetools.
the class InitializationAnnotatedTypeFactory method getInitializedInvariantFields.
/**
* Returns the fields that have the invariant annotation and are initialized in a given store.
*
* @param store a store
* @param path the current path; used to compute the current class
* @return the fields that have the invariant annotation and are initialized in a given store
*/
public List<VariableTree> getInitializedInvariantFields(Store store, TreePath path) {
// TODO: Instead of passing the TreePath around, can we use
// getCurrentClassTree?
ClassTree currentClass = TreePathUtil.enclosingClass(path);
List<VariableTree> fields = InitializationChecker.getAllFields(currentClass);
List<VariableTree> initializedFields = new ArrayList<>();
for (VariableTree field : fields) {
VariableElement fieldElem = TreeUtils.elementFromDeclaration(field);
if (!ElementUtils.isStatic(fieldElem)) {
// Does this field need to satisfy the invariant?
if (hasFieldInvariantAnnotation(field)) {
// Has the field been initialized?
if (store.isFieldInitialized(fieldElem)) {
initializedFields.add(field);
}
}
}
}
return initializedFields;
}
use of com.sun.source.tree.ClassTree 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.getKind() == Tree.Kind.BLOCK && !((BlockTree) member).isStatic()) {
BlockTree block = (BlockTree) member;
Store store = atypeFactory.getRegularExitStore(block);
// Add field values for fields with an initializer.
for (FieldInitialValue<Value> fieldInitialValue : store.getAnalysis().getFieldInitialValues()) {
if (fieldInitialValue.initializer != null) {
store.addInitializedField(fieldInitialValue.fieldDecl.getField());
}
}
final List<VariableTree> init = atypeFactory.getInitializedInvariantFields(store, getCurrentPath());
initializedFields.addAll(init);
}
}
super.processClassTree(node);
// Warn about uninitialized static fields.
Tree.Kind nodeKind = node.getKind();
// must be initialized. Java forbids uninitialized variables and static initalizer blocks.
if (nodeKind != Tree.Kind.INTERFACE && nodeKind != Tree.Kind.ANNOTATION_TYPE) {
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 (FieldInitialValue<Value> fieldInitialValue : store.getAnalysis().getFieldInitialValues()) {
if (fieldInitialValue.initializer != null) {
store.addInitializedField(fieldInitialValue.fieldDecl.getField());
}
}
List<AnnotationMirror> receiverAnnotations = Collections.emptyList();
checkFieldsInitialized(node, isStatic, store, receiverAnnotations);
}
}
use of com.sun.source.tree.ClassTree 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() == Tree.Kind.VARIABLE) {
VariableTree vt = (VariableTree) t;
fields.add(vt);
}
}
return fields;
}
Aggregations