Skip to content

Commit

Permalink
Define TreeUtils.fieldsFromClassTree() (#6659)
Browse files Browse the repository at this point in the history
  • Loading branch information
mernst authored Jun 6, 2024
1 parent 0ad5fde commit e32e6b0
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -582,7 +582,7 @@ public IPair<List<VariableTree>, List<VariableTree>> getUninitializedFields(
boolean isStatic,
Collection<? extends AnnotationMirror> receiverAnnotations) {
ClassTree currentClass = TreePathUtil.enclosingClass(path);
List<VariableTree> fields = InitializationChecker.getAllFields(currentClass);
List<VariableTree> fields = TreeUtils.fieldsFromClassTree(currentClass);
List<VariableTree> uninitWithInvariantAnno = new ArrayList<>();
List<VariableTree> uninitWithoutInvariantAnno = new ArrayList<>();
for (VariableTree field : fields) {
Expand Down Expand Up @@ -635,7 +635,7 @@ public List<VariableTree> getInitializedInvariantFields(Store store, TreePath pa
// TODO: Instead of passing the TreePath around, can we use
// getCurrentClassTree?
ClassTree currentClass = TreePathUtil.enclosingClass(path);
List<VariableTree> fields = InitializationChecker.getAllFields(currentClass);
List<VariableTree> fields = TreeUtils.fieldsFromClassTree(currentClass);
List<VariableTree> initializedFields = new ArrayList<>();
for (VariableTree field : fields) {
VariableElement fieldElem = TreeUtils.elementFromDeclaration(field);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@
import java.util.List;
import java.util.Set;
import java.util.StringJoiner;
import java.util.stream.Collectors;
import javax.annotation.processing.ProcessingEnvironment;
import javax.lang.model.SourceVersion;
import javax.lang.model.element.AnnotationMirror;
Expand Down Expand Up @@ -334,6 +335,19 @@ public static boolean isSelfAccess(ExpressionTree tree) {
return elementFromDeclaration(tree);
}

/**
* Returns the fields that are declared within the given class declaration.
*
* @param tree the {@link ClassTree} node to get the fields for
* @return the list of fields that are declared within the given class declaration
*/
public static List<VariableTree> fieldsFromClassTree(ClassTree tree) {
return tree.getMembers().stream()
.filter(t -> t.getKind() == Kind.VARIABLE)
.map(t -> (VariableTree) t)
.collect(Collectors.toList());
}

/**
* Returns the element corresponding to the given tree.
*
Expand Down

0 comments on commit e32e6b0

Please sign in to comment.