KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > java > source > pretty > DanglingElseChecker


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

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 JavaDoc
38     public void visitTree(JCTree tree) {
39     }
40     @Override JavaDoc
41     public void visitIf(JCIf tree) {
42     if(tree.elsepart==null) foundDanglingElse = true;
43     else tree.elsepart.accept(this);
44     }
45     @Override JavaDoc
46     public void visitWhileLoop(JCWhileLoop tree) {
47     tree.body.accept(this);
48     }
49     @Override JavaDoc
50     public void visitDoLoop(JCDoWhileLoop tree) {
51     tree.body.accept(this);
52     }
53     @Override JavaDoc
54     public void visitForLoop(JCForLoop tree) {
55     tree.body.accept(this);
56     }
57     @Override JavaDoc
58     public void visitSynchronized(JCSynchronized tree) {
59     tree.body.accept(this);
60     }
61     @Override JavaDoc
62     public void visitLabelled(JCLabeledStatement tree) {
63     tree.body.accept(this);
64     }
65     @Override JavaDoc
66     public void visitBlock(JCBlock tree) {
67     // Do dangling else checks on single statement blocks since
68
// they often get eliminated and replaced by their constained statement
69
if(!tree.stats.isEmpty() && tree.stats.tail.isEmpty())
70         tree.stats.head.accept(this);
71     }
72 }
73
Popular Tags