@Override public Boolean visitLabeledStatement(LabeledStatementTree node, TreePath p) { if (p == null) { return super.visitLabeledStatement(node, p); } LabeledStatementTree lst = (LabeledStatementTree)p.getLeaf(); String ident = lst.getLabel().toString(); if (ident.startsWith("$")) { // NOI18N if (bindState.variables2Names.containsKey(ident)) { if (!node.getLabel().contentEquals(bindState.variables2Names.get(ident))) { return false; } } else { bindState.variables2Names.put(ident, node.getLabel().toString()); } } else { if (!node.getLabel().toString().equals(ident)) { return false; } } return scan(node.getStatement(), lst.getStatement(), p); }
private String clashes(TreePath path, final String newName, CompilationInfo info) { TreePath parent = path.getParentPath(); while(parent != null) { if(parent.getLeaf().getKind() == Tree.Kind.LABELED_STATEMENT) { LabeledStatementTree parentLabel = (LabeledStatementTree) parent.getLeaf(); if(newName.equals(parentLabel.getLabel().toString())) { return NbBundle.getMessage(RenameRefactoringPlugin.class, "ERR_LabelClash", newName); } } parent = parent.getParentPath(); } final String[] result = new String[1]; new ErrorAwareTreeScanner<Void, Void>() { @Override public Void visitLabeledStatement(LabeledStatementTree tree, Void p) { if(newName.equals(tree.getLabel().toString())) { result[0] = NbBundle.getMessage(RenameRefactoringPlugin.class, "ERR_LabelClash", newName); } return super.visitLabeledStatement(tree, p); } }.scan(path.getLeaf(), null); return result[0]; }
private boolean testBlock(StringWriter writer, SourcePositions sp, String text, CompilationUnitTree cut, BlockTree blockTree) { boolean success = true; for (StatementTree st : blockTree.getStatements()) { if (isLegal(st)) { success &= testStatement(writer, sp, text, cut, st); } if (st instanceof IfTree) { IfTree ifTree = (IfTree) st; success &= testBranch(writer, sp, text, cut, ifTree.getThenStatement()); success &= testBranch(writer, sp, text, cut, ifTree.getElseStatement()); } else if (st instanceof WhileLoopTree) { WhileLoopTree whileLoopTree = (WhileLoopTree) st; success &= testBranch(writer, sp, text, cut, whileLoopTree.getStatement()); } else if (st instanceof DoWhileLoopTree) { DoWhileLoopTree doWhileLoopTree = (DoWhileLoopTree) st; success &= testBranch(writer, sp, text, cut, doWhileLoopTree.getStatement()); } else if (st instanceof ForLoopTree) { ForLoopTree forLoopTree = (ForLoopTree) st; success &= testBranch(writer, sp, text, cut, forLoopTree.getStatement()); } else if (st instanceof LabeledStatementTree) { LabeledStatementTree labelTree = (LabeledStatementTree) st; success &= testBranch(writer, sp, text, cut, labelTree.getStatement()); } else if (st instanceof SwitchTree) { SwitchTree switchTree = (SwitchTree) st; for (CaseTree caseTree : switchTree.getCases()) { for (StatementTree statementTree : caseTree.getStatements()) { success &= testBranch(writer, sp, text, cut, statementTree); } } } } return success; }
@Override public Node visitLabeledStatement(LabeledStatementTree tree, Void p) { // This method can set the break target after generating all Nodes // in the contained statement, but it can't set the continue target, // which may be in the middle of a sequence of nodes. Labeled loops // must look up and use the continue Labels. Name labelName = tree.getLabel(); Label breakL = new Label(labelName + "_break"); Label continueL = new Label(labelName + "_continue"); breakLabels.put(labelName, breakL); continueLabels.put(labelName, continueL); scan(tree.getStatement(), p); addLabelForNextNode(breakL); breakLabels.remove(labelName); continueLabels.remove(labelName); return null; }
@Override public List<InjectionBinding> visitLabeledStatement( LabeledStatementTree node, List<InjectionBinding> injectionBindings ) { if (node.getLabel().contentEquals("inject")) { // using an inject label, like so: inject : { // will cause all variables initialized to null to become injected. final Predicate<VariableTree> oldPredicate = injectNulls; try { injectNulls = v->true; return super.visitLabeledStatement(node, injectionBindings); } finally { injectNulls = oldPredicate; } } } return super.visitLabeledStatement(node, injectionBindings); }
@Override public Tree visitLabeledStatement(LabeledStatementTree tree, Void p) { LabeledStatementTree n = make.LabeledStatement(tree.getLabel(), tree.getStatement()); model.setType(n, model.getType(tree)); comments.copyComments(tree, n); model.setPos(n, model.getPos(tree)); return n; }
public Void visitLabeledStatement(LabeledStatementTree node, Object p) { System.err.println("visitLabeledStatement: " + node.getLabel()); super.visitLabeledStatement(node, p); LabeledStatementTree copy = make.setLabel(node, node.getLabel() + "0"); this.copy.rewrite(node, copy); return null; }
@Override public Void visitLabeledStatement(LabeledStatementTree tree, List<Node> d) { List<Node> below = new ArrayList<Node>(); addCorrespondingType(below); addCorrespondingComments(below); super.visitLabeledStatement(tree, below); d.add(new TreeNode(info, getCurrentPath(), below)); return null; }
@Override public Void visitLabeledStatement(LabeledStatementTree node, Void unused) { sync(node); builder.open(ZERO); visit(node.getLabel()); token(":"); builder.forcedBreak(); builder.close(); scan(node.getStatement(), null); return null; }
@Override public Void visitLabeledStatement(LabeledStatementTree tree, VisitorState visitorState) { VisitorState state = visitorState.withPath(getCurrentPath()); for (LabeledStatementTreeMatcher matcher : labeledStatementMatchers) { if (!isSuppressed(matcher, state)) { try { reportMatch(matcher.matchLabeledStatement(tree, state), tree, state); } catch (Throwable t) { handleError(matcher, t); } } } return super.visitLabeledStatement(tree, state); }
@Override public Result visitLabeledStatement(LabeledStatementTree node, BreakContext cxt) { cxt.enter(node.getLabel()); try { return node.getStatement().accept(this, cxt); } finally { cxt.exit(node.getLabel()); } }
@Override public Choice<State<JCLabeledStatement>> visitLabeledStatement( final LabeledStatementTree node, State<?> state) { return chooseSubtrees( state, s -> unifyStatement(node.getStatement(), s), stmt -> maker().Labelled((Name) node.getLabel(), stmt)); }
/** * The target of a jump statement (break or continue) is (1) the enclosing loop if the jump is * unlabeled (2) the enclosing LabeledStatementTree with matching label if the jump is labeled * (3) the enclosing switch statement if the jump is a break * * <p>If the target of a break or continue statement is encountered before reaching a finally * block, return NO_MATCH. */ @Override protected MatchResult matchAncestor(Tree leaf, Tree prevTree) { // (1) if (label == null) { switch (leaf.getKind()) { case WHILE_LOOP: case DO_WHILE_LOOP: case FOR_LOOP: case ENHANCED_FOR_LOOP: return MatchResult.NO_MATCH; default: break; } } // (2) if (label != null && leaf instanceof LabeledStatementTree && label.equals(((LabeledStatementTree) leaf).getLabel())) { return MatchResult.NO_MATCH; } // (3) if (jumpType == JumpType.BREAK && leaf instanceof SwitchTree) { return MatchResult.NO_MATCH; } return super.matchAncestor(leaf, prevTree); }
/** * The target of a jump statement (break or continue) is * (1) the enclosing loop if the jump is unlabeled * (2) the enclosing LabeledStatementTree with matching label if the jump is labeled * (3) the enclosing switch statement if the jump is a break * * If the target of a break or continue statement is encountered before reaching a finally * block, return NO_MATCH. */ @Override protected MatchResult matchAncestor(Tree leaf, Tree prevTree) { // (1) if (label == null) { switch (leaf.getKind()) { case WHILE_LOOP: case DO_WHILE_LOOP: case FOR_LOOP: case ENHANCED_FOR_LOOP: return MatchResult.NO_MATCH; default: break; } } // (2) if (label != null && leaf instanceof LabeledStatementTree && label.equals(((LabeledStatementTree) leaf).getLabel())) { return MatchResult.NO_MATCH; } // (3) if (jumpType == JumpType.BREAK && leaf instanceof SwitchTree) { return MatchResult.NO_MATCH; } return super.matchAncestor(leaf, prevTree); }
/** * Returns the label {@link Name} of the leaf in the argument path, or * null if the leaf is not a labeled statement. */ protected /*@Nullable*/ Name getLabel(TreePath path) { if (path.getParentPath() != null) { Tree parent = path.getParentPath().getLeaf(); if (parent.getKind() == Tree.Kind.LABELED_STATEMENT) { return ((LabeledStatementTree) parent).getLabel(); } } return null; }
/** * Returns the label {@link Name} of the leaf in the argument path, or null if the leaf is * not a labeled statement. */ protected /*@Nullable*/ Name getLabel(TreePath path) { if (path.getParentPath() != null) { Tree parent = path.getParentPath().getLeaf(); if (parent.getKind() == Tree.Kind.LABELED_STATEMENT) { return ((LabeledStatementTree) parent).getLabel(); } } return null; }
@Override public Void visitLabeledStatement(LabeledStatementTree expected, Tree actual) { Optional<LabeledStatementTree> other = checkTypeAndCast(expected, actual); if (!other.isPresent()) { addTypeMismatch(expected, actual); return null; } checkForDiff(expected.getLabel().contentEquals(other.get().getLabel()), "Expected statement label to be <%s> but was <%s>.", expected.getLabel(), other.get().getLabel()); scan(expected.getStatement(), other.get().getStatement()); return null; }
@Override public Optional<TreePath> visitLabeledStatement(@Nullable LabeledStatementTree node, Void v) { if (node == null) { return Optional.absent(); } else if (isMatch(node, node.getLabel())) { return currentPathPlus(node); } return super.visitLabeledStatement(node, v); }
@Override public Object visitLabeledStatement(LabeledStatementTree node, Object p) { return scan(node.getStatement(), p); }
private List<int[]> detectBreakOrContinueTarget(CompilationInfo info, Document document, TreePath breakOrContinue, int caretPosition) { List<int[]> result = new ArrayList<int[]>(); StatementTree target = info.getTreeUtilities().getBreakContinueTarget(breakOrContinue); if (target == null) return null; TokenSequence<JavaTokenId> ts = info.getTokenHierarchy().tokenSequence(JavaTokenId.language()); ts.move((int) info.getTrees().getSourcePositions().getStartPosition(info.getCompilationUnit(), target)); if (ts.moveNext()) { result.add(new int[] {ts.offset(), ts.offset() + ts.token().length()}); } StatementTree statement = target.getKind() == Kind.LABELED_STATEMENT ? ((LabeledStatementTree) target).getStatement() : target; Tree block = null; switch (statement.getKind()) { case SWITCH: block = statement; break; case WHILE_LOOP: if (((WhileLoopTree) statement).getStatement().getKind() == Kind.BLOCK) block = ((WhileLoopTree) statement).getStatement(); break; case FOR_LOOP: if (((ForLoopTree) statement).getStatement().getKind() == Kind.BLOCK) block = ((ForLoopTree) statement).getStatement(); break; case ENHANCED_FOR_LOOP: if (((EnhancedForLoopTree) statement).getStatement().getKind() == Kind.BLOCK) block = ((EnhancedForLoopTree) statement).getStatement(); break; case DO_WHILE_LOOP: if (((DoWhileLoopTree) statement).getStatement().getKind() == Kind.BLOCK) block = ((DoWhileLoopTree) statement).getStatement(); break; } if (block != null) { ts.move((int) info.getTrees().getSourcePositions().getEndPosition(info.getCompilationUnit(), block)); if (ts.movePrevious() && ts.token().id() == JavaTokenId.RBRACE) { result.add(new int[] {ts.offset(), ts.offset() + ts.token().length()}); } } if (target.getKind() == Kind.LABELED_STATEMENT && isIn(caretPosition, Utilities.findIdentifierSpan(info, document, breakOrContinue))) { result.addAll(detectLabel(info, document, info.getTrees().getPath(info.getCompilationUnit(), target))); } return result; }
private static int[] computeNameSpan(Tree tree, HintContext context) { switch (tree.getKind()) { case LABELED_STATEMENT: return context.getInfo().getTreeUtilities().findNameSpan((LabeledStatementTree) tree); case METHOD: return context.getInfo().getTreeUtilities().findNameSpan((MethodTree) tree); case ANNOTATION_TYPE: case CLASS: case ENUM: case INTERFACE: return context.getInfo().getTreeUtilities().findNameSpan((ClassTree) tree); case VARIABLE: return context.getInfo().getTreeUtilities().findNameSpan((VariableTree) tree); case MEMBER_SELECT: //XXX: MemberSelectTree mst = (MemberSelectTree) tree; int[] span = context.getInfo().getTreeUtilities().findNameSpan(mst); if (span == null) { int end = (int) context.getInfo().getTrees().getSourcePositions().getEndPosition(context.getInfo().getCompilationUnit(), tree); span = new int[] {end - mst.getIdentifier().length(), end}; } return span; case METHOD_INVOCATION: return computeNameSpan(((MethodInvocationTree) tree).getMethodSelect(), context); case BLOCK: Collection<? extends TreePath> prefix = context.getMultiVariables().get("$$1$"); if (prefix != null) { BlockTree bt = (BlockTree) tree; if (bt.getStatements().size() > prefix.size()) { return computeNameSpan(bt.getStatements().get(prefix.size()), context); } } default: int start = (int) context.getInfo().getTrees().getSourcePositions().getStartPosition(context.getInfo().getCompilationUnit(), tree); if ( StatementTree.class.isAssignableFrom(tree.getKind().asInterface()) && tree.getKind() != Kind.EXPRESSION_STATEMENT && tree.getKind() != Kind.BLOCK) { TokenSequence<?> ts = context.getInfo().getTokenHierarchy().tokenSequence(); ts.move(start); if (ts.moveNext()) { return new int[] {ts.offset(), ts.offset() + ts.token().length()}; } } return new int[] { start, Math.min((int) context.getInfo().getTrees().getSourcePositions().getEndPosition(context.getInfo().getCompilationUnit(), tree), findLineEnd(context.getInfo(), start)), }; } }
@Override public State visitLabeledStatement(LabeledStatementTree node, Void p) { registerBreakTarget(node); registerBreakTarget(node.getStatement()); return super.visitLabeledStatement(node, p); }
/** * Note: if the labeled statement is 1st, in efab.scan(), the visit method is called without * prior scan(tree, param). This LabeledStatement is actually a target of break+continue, so * it must be also added to seenTrees.s */ @Override public Void visitLabeledStatement(LabeledStatementTree node, Collection<TreePath> p) { seenTrees.add(node); return super.visitLabeledStatement(node, p); }
public Boolean visitContinue(ContinueTree node, ConstructorData p) { StatementTree loop = info.getTreeUtilities().getBreakContinueTarget(getCurrentPath()); if (loop == null) { super.visitContinue(node, p); return null; } Tree resumePoint; if (loop.getKind() == Kind.LABELED_STATEMENT) { loop = ((LabeledStatementTree) loop).getStatement(); } switch (loop.getKind()) { case WHILE_LOOP: resumePoint = ((WhileLoopTree) loop).getCondition(); break; case FOR_LOOP: { ForLoopTree flt = (ForLoopTree)loop; resumePoint = null; if (flt.getUpdate() != null && !flt.getUpdate().isEmpty()) { // resume will react on the 1st Tree of the update statement (always processed left to right) resumePoint = flt.getUpdate().get(0); } if (resumePoint == null) { resumePoint = flt.getCondition(); } if (resumePoint == null) { resumePoint = flt.getStatement(); } } break; case DO_WHILE_LOOP: resumePoint = ((DoWhileLoopTree) loop).getCondition(); break; case ENHANCED_FOR_LOOP: resumePoint = ((EnhancedForLoopTree) loop).getStatement(); break; default: resumePoint = null; break; } if (resumePoint != null) { recordResume(resumeBefore, resumePoint, variable2State); } variable2State = new HashMap< Element, State>(); super.visitContinue(node, p); return null; }
public Boolean visitLabeledStatement(LabeledStatementTree node, ConstructorData p) { super.visitLabeledStatement(node, p); return null; }
@Override public List<? extends TypeMirror> visitLabeledStatement(LabeledStatementTree node, Object p) { return null; }
@Override public Mirror visitLabeledStatement(LabeledStatementTree arg0, EvaluationContext evaluationContext) { Assert.error(arg0, "unsupported"); return null; }
@Override public JCTree visitLabeledStatement(LabeledStatementTree node, Void p) { return node.getStatement().accept(this, p); }
@Override public R visitLabeledStatement(LabeledStatementTree lst, P p) { return null; }
@Override public List<T> visitLabeledStatement(LabeledStatementTree node, T p) { return checkForCriteria(node); }
@Override public Object visitLabeledStatement(LabeledStatementTree t, Trees p) { info("LabeledStatementTree" + CL + t.getKind() + SP + t); return super.visitLabeledStatement(t, p); }
@Override public Boolean visitLabeledStatement(LabeledStatementTree tree, Void unused) { // break/continue targets have already been resolved by javac, so // there's nothing to do here return scan(tree.getStatement()); }
@Override public ULabeledStatement visitLabeledStatement(LabeledStatementTree tree, Void v) { return ULabeledStatement.create(tree.getLabel(), template(tree.getStatement())); }
@Override public Choice<Unifier> visitLabeledStatement(LabeledStatementTree node, Unifier unifier) { unifier.putBinding(key(), node.getLabel()); return getStatement().unify(node.getStatement(), unifier); }
@Override public PurityResult visitLabeledStatement(LabeledStatementTree node, PurityResult p) { return scan(node.getStatement(), p); }