public void visitIf(JCIf tree) { try { print("if "); if (PARENS.equals(treeTag(tree.cond))) { printExpr(tree.cond); } else { print("("); printExpr(tree.cond); print(")"); } print(" "); printStat(tree.thenpart); if (tree.elsepart != null) { print(" else "); printStat(tree.elsepart); } } catch (IOException e) { throw new UncheckedIOException(e); } }
public void visitIf(JCIf tree) { try { print("if "); if (tree.cond.getTag() == JCTree.PARENS) { printExpr(tree.cond); } else { print("("); printExpr(tree.cond); print(")"); } print(" "); printStat(tree.thenpart); if (tree.elsepart != null) { print(" else "); printStat(tree.elsepart); } } catch (IOException e) { throw new UncheckedIOException(e); } }
public void visitIf(JCIf tree) { try { print("if "); if (getTag(tree.cond) == PARENS) { printExpr(tree.cond); } else { print("("); printExpr(tree.cond); print(")"); } print(" "); printStat(tree.thenpart); if (tree.elsepart != null) { print(" else "); printStat(tree.elsepart); } } catch (IOException e) { throw new UncheckedIOException(e); } }
@Override public void visitIf(JCIf tree) { aPrint("if "); if (tree.cond instanceof JCParens) { print(tree.cond); } else { print("("); print(tree.cond); print(")"); } print(" "); if (tree.thenpart instanceof JCBlock) { println("{"); indent++; print(((JCBlock) tree.thenpart).stats, ""); indent--; if (tree.elsepart == null) { aPrintln("}", tree); } else { aPrint("}"); } } else { print(tree.thenpart); } if (tree.elsepart != null) { aPrint(" else "); print(tree.elsepart); } }
/** * Checks if the statement is of the form 'if (x == null) {throw WHATEVER;}, * where the block braces are optional. If it is of this form, returns "x". * If it is not of this form, returns null. */ public String returnVarNameIfNullCheck(JCStatement stat) { if (!(stat instanceof JCIf)) return null; /* Check that the if's statement is a throw statement, possibly in a block. */ { JCStatement then = ((JCIf) stat).thenpart; if (then instanceof JCBlock) { List<JCStatement> stats = ((JCBlock) then).stats; if (stats.length() == 0) return null; then = stats.get(0); } if (!(then instanceof JCThrow)) return null; } /* Check that the if's conditional is like 'x == null'. Return from this method (don't generate a nullcheck) if 'x' is equal to our own variable's name: There's already a nullcheck here. */ { JCExpression cond = ((JCIf) stat).cond; while (cond instanceof JCParens) cond = ((JCParens) cond).expr; if (!(cond instanceof JCBinary)) return null; JCBinary bin = (JCBinary) cond; if (!CTC_EQUAL.equals(treeTag(bin))) return null; if (!(bin.lhs instanceof JCIdent)) return null; if (!(bin.rhs instanceof JCLiteral)) return null; if (!CTC_BOT.equals(typeTag(bin.rhs))) return null; return ((JCIdent) bin.lhs).name.toString(); } }
public void visitIf(JCIf that) { try { print("JCIf:"); } catch (Exception e) { } super.visitIf(that); }
@Override public JCIf inline(Inliner inliner) throws CouldNotResolveImportException { return inliner.maker().If( getCondition().inline(inliner), getThenStatement().inline(inliner), (getElseStatement() == null) ? null : getElseStatement().inline(inliner)); }
@Override public void visitIf(JCIf tree) { printNode(tree); child("cond", tree.cond); child("thenpart", tree.thenpart); child("elsepart", tree.elsepart); indent--; }
@Override public void visitIf(JCIf node) { If i = new If(); JCExpression cond = node.getCondition(); setConversionPositionInfo(i, "()", getPosition(cond)); i.rawCondition(toTree(removeParens(cond))); i.rawStatement(toTree(node.getThenStatement())); i.rawElseStatement(toTree(node.getElseStatement())); set(node, i); }
@Override public Choice<State<JCIf>> visitIf(final IfTree node, State<?> state) { return chooseSubtrees( state, s -> unifyExpression(node.getCondition(), s), s -> unifyStatement(node.getThenStatement(), s), s -> unifyStatement(node.getElseStatement(), s), maker()::If); }
@Override public Description matchMethodInvocation(MethodInvocationTree tree, VisitorState state) { if (!matcher.matches(tree, state)) { return Description.NO_MATCH; } Description.Builder description = buildDescription(tree); MethodSymbol sym = ASTHelpers.getSymbol(tree); if (sym != null) { description.setMessage(String.format(MESSAGE_TEMPLATE, sym)); } // If this looks like the "Wait until a condition becomes true" case from the wiki content, // rewrite the enclosing if to a while. Other fixes are too complicated to construct // mechanically, so we provide detailed instructions in the wiki content. if (!waitMethodWithTimeout.matches(tree, state)) { JCIf enclosingIf = ASTHelpers.findEnclosingNode(state.getPath().getParentPath(), JCIf.class); if (enclosingIf != null && enclosingIf.getElseStatement() == null) { CharSequence ifSource = state.getSourceForNode(enclosingIf); if (ifSource == null) { // Source isn't available, so we can't construct a fix return description.build(); } String replacement = ifSource.toString().replaceFirst("if", "while"); return description.addFix(SuggestedFix.replace(enclosingIf, replacement)).build(); } } return description.build(); }
public void visitIf(JCIf tree) { attribExpr(tree.cond, env, syms.booleanType); attribStat(tree.thenpart, env); if (tree.elsepart != null) attribStat(tree.elsepart, env); chk.checkEmptyIf(tree); result = null; }
protected int diffIf(JCIf oldT, JCIf newT, int[] bounds) { int localPointer = bounds[0]; int start = printer.toString().length(); int[] condBounds = getCommentCorrectedBounds(oldT.cond); copyTo(localPointer, condBounds[0]); localPointer = diffTree(oldT.cond, newT.cond, null, condBounds); copyTo(localPointer, localPointer = condBounds[1]); int[] partBounds = new int[] { localPointer, endPos(oldT.thenpart) }; printer.conditionStartHack = start; localPointer = diffTree(oldT.thenpart, newT.thenpart, partBounds, oldT.getKind(), newT.elsepart == null); printer.conditionStartHack = (-1); if (oldT.elsepart == null && newT.elsepart != null) { copyTo(localPointer, localPointer = partBounds[1]); printer.printElse(newT, newT.thenpart.getKind() == Kind.BLOCK); } else if (oldT.elsepart != null && newT.elsepart == null) { // remove else part copyTo(localPointer, partBounds[1]); copyTo(getBounds(oldT.elsepart)[1], bounds[1]); return bounds[1]; } else { if (oldT.elsepart != null) { if (oldT.thenpart.getKind() != newT.thenpart.getKind() && newT.thenpart.getKind() == Kind.BLOCK) { tokenSequence.move(localPointer); moveToDifferentThan(tokenSequence, Direction.FORWARD, EnumSet.of(JavaTokenId.WHITESPACE)); if (localPointer != tokenSequence.offset()) { if (diffContext.style.spaceBeforeElse()) { printer.print(" "); } } localPointer = tokenSequence.offset(); } partBounds = new int[] { localPointer, endPos(oldT.elsepart) }; localPointer = diffTree(oldT.elsepart, newT.elsepart, partBounds, oldT.getKind()); tokenSequence.move(localPointer); if (tokenSequence.movePrevious() && tokenSequence.token().id() == JavaTokenId.LINE_COMMENT) { printer.newline(); } } } if (localPointer < bounds[1]) copyTo(localPointer, localPointer = bounds[1]); return localPointer; }
private boolean matchIf(JCIf t1, JCIf t2) { return treesMatch(t1.cond, t2.cond) && treesMatch(t1.thenpart, t2.thenpart) && treesMatch(t1.elsepart, t2.elsepart); }
@Override public void visitIf(JCIf tree) { //skip body (to prevents same statements to be analyzed twice) scan(tree.getCondition()); }
@Override public void visitIf(JCIf tree) { scan(tree.getCondition()); }
public JCIf If(JCExpression cond, JCStatement thenpart, JCStatement elsepart) { return invoke(If, cond, thenpart, elsepart); }
public AJCIf(JCIf ltree) { super(ltree.cond, ltree.thenpart, ltree.elsepart); }
public AJCIf(JCIf ltree, String lcomment) { this(ltree); setComment(lcomment); }
public JCIf build() { return treeMaker.If(condition, thenBlock, elseBlock); }
@Override public Description matchMethodInvocation(MethodInvocationTree tree, VisitorState state) { if (!waitMatcher.matches(tree, state)) { return Description.NO_MATCH; } if (!SUPPLY_FIX) { return describeMatch(tree, Fix.NO_FIX); } SuggestedFix fix = new SuggestedFix(); // if -> while case JCIf enclosingIf = ASTHelpers.findEnclosingNode(state.getPath().getParentPath(), JCIf.class); if (enclosingIf != null && enclosingIf.getElseStatement() == null) { // Assume first 2 characters of the IfTree are "if", replace with while. fix.replace(enclosingIf.getStartPosition(), enclosingIf.getStartPosition() + 2, "while"); return describeMatch(tree, fix); } // loop outside synchronized block -> move synchronized outside @SuppressWarnings("unchecked") List<Class<? extends StatementTree>> loopClasses = Arrays.asList(WhileLoopTree.class, ForLoopTree.class, EnhancedForLoopTree.class, DoWhileLoopTree.class); StatementTree enclosingLoop = null; for (Class<? extends StatementTree> loopClass : loopClasses) { enclosingLoop = ASTHelpers.findEnclosingNode(state.getPath().getParentPath(), loopClass); if (enclosingLoop != null) { break; } } if (enclosingLoop != null) { SynchronizedTree enclosingSynchronized = ASTHelpers.findEnclosingNode( state.getPath().getParentPath(), SynchronizedTree.class); if (enclosingSynchronized != null) { String blockStatements = enclosingSynchronized.getBlock().toString(); int openBracketIndex = blockStatements.indexOf('{'); int closeBracketIndex = blockStatements.lastIndexOf('}'); blockStatements = blockStatements.substring(openBracketIndex + 1, closeBracketIndex).trim(); fix.replace(enclosingSynchronized, blockStatements); fix.prefixWith(enclosingLoop, "synchronized " + enclosingSynchronized.getExpression() + " {\n"); fix.postfixWith(enclosingLoop, "\n}"); return describeMatch(tree, fix); } } // Intent is to wait forever -> wrap in while (true) // Heuristic: this is the last statement in a method called main, inside a synchronized block. /* if (enclosingIf == null && (ASTHelpers.findEnclosingNode(state.getPath().getParentPath(), WhileLoopTree.class) == null) && (ASTHelpers.findEnclosingNode(state.getPath().getParentPath(), ForLoopTree.class) == null) && (ASTHelpers.findEnclosingNode(state.getPath().getParentPath(), EnhancedForLoopTree.class) == null) && (ASTHelpers.findEnclosingNode(state.getPath().getParentPath(), DoWhileLoopTree.class) == null)) { TreeMaker treeMaker = TreeMaker.instance(state.context); JCLiteral trueLiteral = treeMaker.Literal(true); treeMaker.WhileLoop(trueLiteral, } */ return describeMatch(tree, fix); }
@Override public void handle(AnnotationValues<Cleanup> annotation, JCAnnotation ast, JavacNode annotationNode) { if (inNetbeansEditor(annotationNode)) return; deleteAnnotationIfNeccessary(annotationNode, Cleanup.class); String cleanupName = annotation.getInstance().value(); if (cleanupName.length() == 0) { annotationNode.addError("cleanupName cannot be the empty string."); return; } if (annotationNode.up().getKind() != Kind.LOCAL) { annotationNode.addError("@Cleanup is legal only on local variable declarations."); return; } JCVariableDecl decl = (JCVariableDecl)annotationNode.up().get(); if (decl.init == null) { annotationNode.addError("@Cleanup variable declarations need to be initialized."); return; } JavacNode ancestor = annotationNode.up().directUp(); JCTree blockNode = ancestor.get(); final List<JCStatement> statements; if (blockNode instanceof JCBlock) { statements = ((JCBlock)blockNode).stats; } else if (blockNode instanceof JCCase) { statements = ((JCCase)blockNode).stats; } else if (blockNode instanceof JCMethodDecl) { statements = ((JCMethodDecl)blockNode).body.stats; } else { annotationNode.addError("@Cleanup is legal only on a local variable declaration inside a block."); return; } boolean seenDeclaration = false; ListBuffer<JCStatement> newStatements = ListBuffer.lb(); ListBuffer<JCStatement> tryBlock = ListBuffer.lb(); for (JCStatement statement : statements) { if (!seenDeclaration) { if (statement == decl) seenDeclaration = true; newStatements.append(statement); } else { tryBlock.append(statement); } } if (!seenDeclaration) { annotationNode.addError("LOMBOK BUG: Can't find this local variable declaration inside its parent."); return; } doAssignmentCheck(annotationNode, tryBlock.toList(), decl.name); TreeMaker maker = annotationNode.getTreeMaker(); JCFieldAccess cleanupMethod = maker.Select(maker.Ident(decl.name), annotationNode.toName(cleanupName)); List<JCStatement> cleanupCall = List.<JCStatement>of(maker.Exec( maker.Apply(List.<JCExpression>nil(), cleanupMethod, List.<JCExpression>nil()))); JCMethodInvocation preventNullAnalysis = preventNullAnalysis(maker, annotationNode, maker.Ident(decl.name)); JCBinary isNull = maker.Binary(CTC_NOT_EQUAL, preventNullAnalysis, maker.Literal(CTC_BOT, null)); JCIf ifNotNullCleanup = maker.If(isNull, maker.Block(0, cleanupCall), null); JCBlock finalizer = recursiveSetGeneratedBy(maker.Block(0, List.<JCStatement>of(ifNotNullCleanup)), ast); newStatements.append(setGeneratedBy(maker.Try(setGeneratedBy(maker.Block(0, tryBlock.toList()), ast), List.<JCCatch>nil(), finalizer), ast)); if (blockNode instanceof JCBlock) { ((JCBlock)blockNode).stats = newStatements.toList(); } else if (blockNode instanceof JCCase) { ((JCCase)blockNode).stats = newStatements.toList(); } else if (blockNode instanceof JCMethodDecl) { ((JCMethodDecl)blockNode).body.stats = newStatements.toList(); } else throw new AssertionError("Should not get here"); ancestor.rebuild(); }
JCIf build();