KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > edu > umd > cs > findbugs > tools > patcomp > CodeGen


1 /*
2  * FindBugs - Find bugs in Java programs
3  * Copyright (C) 2004 University of Maryland
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  */

19
20 package edu.umd.cs.findbugs.tools.patcomp;
21
22 import java.io.IOException JavaDoc;
23
24 /**
25  * From a parsed FindBugs Pattern (.fbp) file,
26  * generate Java source code for a bug pattern detector.
27  *
28  * @author David Hovemeyer
29  */

30 public class CodeGen implements PatternCompilerTreeConstants {
31     private CodeEmitter emitter;
32
33     public CodeGen() {
34     }
35
36     public void generate(SimpleNode root, CodeEmitter emitter) throws IOException JavaDoc {
37         this.emitter = emitter;
38         visit(root);
39         emitter.finish();
40     }
41
42     private void visit(SimpleNode node) throws IOException JavaDoc {
43         switch (node.getId()) {
44         case JJTPRESCREEN:
45             generatePrescreen(node);
46             break;
47         default:
48             generateDefault(node);
49             break;
50         }
51     }
52
53     public void generateDefault(SimpleNode node) throws IOException JavaDoc {
54 // System.out.println("START " + node.toString() +
55
// ": first=" + node.getFirstToken() + ", last=" + node.getLastToken());
56

57         int numChildren = node.jjtGetNumChildren();
58
59         SimpleNode child = null;
60         Token t;
61
62         // For each child, emit tokens preceeding that
63
// child not covered by previous children,
64
// then visit the child.
65
int childNum = 0;
66         while (childNum < numChildren) {
67             SimpleNode nextChild = (SimpleNode) node.jjtGetChild(childNum);
68
69             if (child == null)
70                 t = node.getFirstToken();
71             else if (child.getLastToken() != nextChild.getFirstToken())
72                 t = child.getLastToken().next;
73             else
74                 t = nextChild.getFirstToken();
75
76             while (t != nextChild.getFirstToken()) {
77                 emitter.emitToken(t.image);
78                 t = t.next;
79             }
80
81             child = nextChild;
82
83             visit(child);
84
85             ++childNum;
86         }
87
88         // Emit rest of tokens
89
if (child == null)
90             t = node.getFirstToken();
91         else if (child.getLastToken() != node.getLastToken())
92             t = child.getLastToken().next;
93         else
94             t = null;
95
96         while (t != null) {
97             emitter.emitToken(t.image);
98             if (t == node.getLastToken())
99                 break;
100             t = t.next;
101         }
102
103 // System.out.println("FINISH " + node.toString() +
104
// ": first=" + node.getFirstToken() + ", last=" + node.getLastToken());
105
}
106
107     public void generatePrescreen(SimpleNode node) throws IOException JavaDoc {
108         System.out.println("Generating prescreen code");
109     }
110 }
111
112 // vim:ts=4
113
Popular Tags