@Override public boolean apply(Tree tree) { switch (tree.getKind()) { case CLASS: return ((ClassTree) tree).getModifiers().getFlags().contains(STATIC); case METHOD: return ((MethodTree) tree).getModifiers().getFlags().contains(STATIC); case VARIABLE: return ((VariableTree) tree).getModifiers().getFlags().contains(STATIC); case BLOCK: return ((BlockTree) tree).isStatic(); case ENUM: case ANNOTATION_TYPE: case INTERFACE: return true; default: throw new AssertionError("unknown member type:" + tree.getKind()); } } });
@Override public Void visitBlock(BlockTree node, InitializationContext init) { if (getCurrentPath().getParentPath().getLeaf().getKind() == Kind.CLASS) { init = node.isStatic() ? InitializationContext.STATIC : InitializationContext.INSTANCE; } return super.visitBlock(node, init); }
if (blockTree.isStatic()) { initThusFar.addAll( nullnessAnalysis.getNonnullStaticFieldsAtExit(memberPath, state.context));
if (blockTree.isStatic()) { staticInitializerBlocks.add(blockTree); } else {
/** * For each class, visits constructors, instance variables, and instance initializers. Delegates * further scanning of these "constructor-scope" constructs to {@link #traverse}. */ @Override public Description matchClass(ClassTree tree, VisitorState state) { // TODO(b/36395371): filter here to exclude some classes (e.g. not immutable) if (TEST_CLASS.matches(tree, state)) { return NO_MATCH; } for (Tree member : tree.getMembers()) { if (isSuppressed(member)) { continue; } if ((member instanceof MethodTree && Matchers.methodIsConstructor().matches((MethodTree) member, state)) || (member instanceof BlockTree && !((BlockTree) member).isStatic()) || (member instanceof VariableTree && !Matchers.isStatic().matches(member, state))) { traverse(member, state); } } return NO_MATCH; }
return ASTHelpers.getSymbol(tree).isStatic(); case BLOCK: // static initializer if (((BlockTree) tree).isStatic()) { return true;
&& !ASTHelpers.getSymbol((MethodTree) enclosing).isStatic()) { return Description.NO_MATCH; } else if (enclosing instanceof BlockTree && !((BlockTree) enclosing).isStatic()) { return Description.NO_MATCH;
@Override public String visitBlock(BlockTree reference, Void v) { return (reference != null) ? detailedKindString(reference, reference.isStatic() ? "static" : "non-static") : ""; }
@Override public String visitBlock(BlockTree reference, Void v) { return (reference != null) ? detailedKindString(reference, reference.isStatic() ? "static" : "non-static") : ""; }
@Override public String visitBlock(BlockTree reference, Void v) { return (reference != null) ? detailedKindString(reference, reference.isStatic() ? "static" : "non-static") : ""; }
@Override public Void visitBlock(BlockTree expected, Tree actual) { Optional<BlockTree> other = checkTypeAndCast(expected, actual); if (!other.isPresent()) { addTypeMismatch(expected, actual); return null; } checkForDiff(expected.isStatic() == other.get().isStatic(), "Expected block to be <%s> but was <%s>.", expected.isStatic() ? "static" : "non-static", other.get().isStatic() ? "static" : "non-static"); parallelScan(expected.getStatements(), other.get().getStatements()); return null; }
@Override public Void visitBlock(BlockTree expected, Tree actual) { Optional<BlockTree> other = checkTypeAndCast(expected, actual); if (!other.isPresent()) { addTypeMismatch(expected, actual); return null; } checkForDiff(expected.isStatic() == other.get().isStatic(), "Expected block to be <%s> but was <%s>.", expected.isStatic() ? "static" : "non-static", other.get().isStatic() ? "static" : "non-static"); parallelScan(expected.getStatements(), other.get().getStatements()); return null; }
@Override public Void visitBlock(BlockTree expected, Tree actual) { Optional<BlockTree> other = checkTypeAndCast(expected, actual); if (!other.isPresent()) { addTypeMismatch(expected, actual); return null; } checkForDiff(expected.isStatic() == other.get().isStatic(), "Expected block to be <%s> but was <%s>.", expected.isStatic() ? "static" : "non-static", other.get().isStatic() ? "static" : "non-static"); parallelScan(expected.getStatements(), other.get().getStatements()); return null; }
private static boolean isSingleStatemenBlockAndStatement(Tree t1, Tree t2) { Kind k1 = t1.getKind(); Kind k2 = t2.getKind(); if (k1 == Kind.BLOCK && ((BlockTree) t1).getStatements().size() == 1 && !((BlockTree) t1).isStatic()) { return StatementTree.class.isAssignableFrom(k2.asInterface()); } return false; }
private BlockTree modifyBlockStatement(BlockTree block, int index, StatementTree statement, Operation op) { BlockTree copy = Block( c(block.getStatements(), index, statement, op), block.isStatic() ); return copy; }
@Override public boolean apply(Tree tree) { switch (tree.getKind()) { case CLASS: return ((ClassTree) tree).getModifiers().getFlags().contains(STATIC); case METHOD: return ((MethodTree) tree).getModifiers().getFlags().contains(STATIC); case VARIABLE: return ((VariableTree) tree).getModifiers().getFlags().contains(STATIC); case BLOCK: return ((BlockTree) tree).isStatic(); case ENUM: case ANNOTATION_TYPE: case INTERFACE: return true; default: throw new AssertionError("unknown member type:" + tree.getKind()); } } });
public Boolean visitBlock(BlockTree node, TreePath p) { if (p == null) { super.visitBlock(node, p); return false; } if (p.getLeaf().getKind() != Kind.BLOCK) { //single-statement blocks are considered to be equivalent to statements //TODO: some parents may need to be more strict, esp. synchronized and do-while assert node.getStatements().size() == 1; assert !node.isStatic(); if (p.getLeaf() == searchingFor.getLeaf()) return false; return checkLists(node.getStatements(), Collections.singletonList(p.getLeaf()), p.getParentPath()); } BlockTree at = (BlockTree) p.getLeaf(); if (node.isStatic() != at.isStatic()) { return false; } return checkLists(node.getStatements(), at.getStatements(), p); }
@Override public Void visitBlock(BlockTree node, InitializationContext init) { if (getCurrentPath().getParentPath().getLeaf().getKind() == Kind.CLASS) { init = node.isStatic() ? InitializationContext.STATIC : InitializationContext.INSTANCE; } return super.visitBlock(node, init); }
@Override public Tree visitBlock(BlockTree tree, Void p) { BlockTree n = make.Block(tree.getStatements(), tree.isStatic()); model.setType(n, model.getType(tree)); comments.copyComments(tree, n); model.setPos(n, model.getPos(tree)); return n; }
/** * For each class, visits constructors, instance variables, and instance initializers. Delegates * further scanning of these "constructor-scope" constructs to {@link #traverse}. */ @Override public Description matchClass(ClassTree tree, VisitorState state) { // TODO(b/36395371): filter here to exclude some classes (e.g. not immutable) if (TEST_CLASS.matches(tree, state)) { return NO_MATCH; } for (Tree member : tree.getMembers()) { if (isSuppressed(member)) { continue; } if ((member instanceof MethodTree && Matchers.methodIsConstructor().matches((MethodTree) member, state)) || (member instanceof BlockTree && !((BlockTree) member).isStatic()) || (member instanceof VariableTree && !Matchers.isStatic().matches(member, state))) { traverse(member, state); } } return NO_MATCH; }