1 19 package org.netbeans.modules.java.source.pretty; 20 21 import java.io.*; 22 23 import com.sun.tools.javac.util.*; 24 import com.sun.tools.javac.code.*; 25 import com.sun.tools.javac.code.Symbol.*; 26 import com.sun.tools.javac.tree.JCTree; 27 import com.sun.tools.javac.tree.JCTree.*; 28 29 public class DanglingElseChecker extends Visitor { 30 boolean foundDanglingElse; 31 public boolean hasDanglingElse(JCTree t) { 32 if(t==null) return false; 33 foundDanglingElse = false; 34 t.accept(this); 35 return foundDanglingElse; 36 } 37 @Override 38 public void visitTree(JCTree tree) { 39 } 40 @Override 41 public void visitIf(JCIf tree) { 42 if(tree.elsepart==null) foundDanglingElse = true; 43 else tree.elsepart.accept(this); 44 } 45 @Override 46 public void visitWhileLoop(JCWhileLoop tree) { 47 tree.body.accept(this); 48 } 49 @Override 50 public void visitDoLoop(JCDoWhileLoop tree) { 51 tree.body.accept(this); 52 } 53 @Override 54 public void visitForLoop(JCForLoop tree) { 55 tree.body.accept(this); 56 } 57 @Override 58 public void visitSynchronized(JCSynchronized tree) { 59 tree.body.accept(this); 60 } 61 @Override 62 public void visitLabelled(JCLabeledStatement tree) { 63 tree.body.accept(this); 64 } 65 @Override 66 public void visitBlock(JCBlock tree) { 67 if(!tree.stats.isEmpty() && tree.stats.tail.isEmpty()) 70 tree.stats.head.accept(this); 71 } 72 } 73 | Popular Tags |