1 /* FOREGEJ - FOrmatting REfactoring GEnerating Java
2 *
3 * Copyright (C) 2003 Andreas Arrgard
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 package com.octagroup.foregej.java.tools;
20 import java.util.Iterator;
21 import java.util.Vector;
22 import com.octagroup.foregej.java.lang.ast.AST_CLASS_DEF;
23 import com.octagroup.foregej.java.lang.ast.AST_CTOR_DEF;
24 import com.octagroup.foregej.java.lang.ast.AST_INTERFACE_DEF;
25 import com.octagroup.foregej.java.lang.ast.AST_METHOD_DEF;
26 import com.octagroup.foregej.java.lang.ast.AST_OBJBLOCK;
27 import com.octagroup.foregej.java.lang.ast.AST_VARIABLE_DEF;
28 import com.octagroup.foregej.java.lang.ast.CompilationUnit;
29 /***
30 * This class is used to "run" the tools.
31 * <p>
32 * All we have to do is to pass a compilation unit to this class and all
33 * the tools will be called.
34 * </p>
35 */
36 public class ToolRunner
37 {
38 /***
39 * These are all the tools that we will use to process the
40 * CompilationUnits
41 */
42 private Vector tools_=new Vector();
43 /***
44 * The singleton instance
45 */
46 private static ToolRunner instance_s=null;
47 /***
48 * Returns the singleton instance.
49 *
50 * @return the singleton instance.
51 */
52 public static ToolRunner getInstance()
53 {
54 if(instance_s==null) {
55 instance_s=new ToolRunner();
56 }
57 return instance_s;
58 }
59 /***
60 * Adds a tool to the tool runner.
61 * <p>
62 * When the <code>process</code>metod is invoked a new instance of the
63 * tool will be created.
64 * </p>
65 *
66 * @param tool
67 */
68 public void addTool(Class tool)
69 {
70 tools_.add(tool);
71 }
72 /***
73 * This method processes a CompilationUnit
74 * <p>
75 * It creates a new instance of the registered tools and starts to
76 * process the compilation unt with that tool.
77 * </p>
78 *
79 * @param cu the CompilationUnit to process.
80 */
81 public void process(CompilationUnit cu)
82 {
83 try{
84 Iterator tools=tools_.iterator();
85 while(tools.hasNext()){
86 Class tool=(Class)tools.next();
87 process(cu, (Tool)tool.newInstance());
88 }
89 }catch (InstantiationException e) {
90 throw new RuntimeException("ToolRunner.process: InstantiationException:"+e.getMessage());
91 }catch (IllegalAccessException e) {
92 throw new RuntimeException("ToolRunner.process: IllegalAccessException:"+e.getMessage());
93 }
94 }
95 /***
96 * Processes a CompilationUnit with a tool
97 *
98 * @param cu the CompilationUnit to process.
99 * @param tool the tool to process the compilation unit with.
100 */
101 private void process(CompilationUnit cu,Tool tool)
102 {
103 boolean cont=tool.process(cu);
104 if(cont) {
105 AST_INTERFACE_DEF interfaze=cu.getAstInterfaceDef();
106 AST_CLASS_DEF clazz=cu.getAstClassDef();
107 if(interfaze!=null) {
108 process(interfaze, tool);
109 } else if(clazz!=null) {
110 process(clazz, tool);
111 } else {
112 throw new IllegalStateException("ToolRunner.process: CompilationUnit did not contain a class or interface.");
113 }
114 }
115 }
116 /***
117 * Processes a Class with a tool
118 *
119 * @param clazz the Class to process.
120 * @param tool the tool to process the class with.
121 */
122 private void process(AST_CLASS_DEF clazz,Tool tool)
123 {
124 boolean cont=tool.process(clazz);
125 if(cont) {
126 process(clazz.getAstBody(), tool);
127 }
128 }
129 /***
130 * Processes an Interface with a tool
131 *
132 * @param interfaze the Interface to process.
133 * @param tool the tool to process the interface with.
134 */
135 private void process(AST_INTERFACE_DEF interfaze,Tool tool)
136 {
137 boolean cont=tool.process(interfaze);
138 if(cont) {
139 process(interfaze.getAstBody(), tool);
140 }
141 }
142 /***
143 * Helper method that passes all the variables, cobstructors and
144 * methods on to the tool.
145 *
146 * @param objBlock the ast that contains all the methods
147 * @param tool the tool that performs the processing.
148 */
149 private void process(AST_OBJBLOCK objBlock,Tool tool)
150 {
151 Iterator variables=objBlock.getAstVariables().iterator();
152 while(variables.hasNext()){
153 AST_VARIABLE_DEF variable=(AST_VARIABLE_DEF)variables.next();
154 tool.process(variable);
155 }
156 Iterator ctors=objBlock.getAstConstructors().iterator();
157 while(ctors.hasNext()){
158 AST_CTOR_DEF ctor=(AST_CTOR_DEF)ctors.next();
159 tool.process(ctor);
160 }
161 Iterator methods=objBlock.getAstMethods().iterator();
162 while(methods.hasNext()){
163 AST_METHOD_DEF method=(AST_METHOD_DEF)methods.next();
164 tool.process(method);
165 }
166 }
167 }
This page was automatically generated by Maven