KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > spoon > LtLauncher


1 package spoon;
2
3 import java.io.File JavaDoc;
4 import java.lang.reflect.Method JavaDoc;
5 import java.util.ArrayList JavaDoc;
6
7 import spoon.processing.ProcessingManager;
8 import spoon.support.RuntimeProcessingManager;
9
10 import com.martiansoftware.jsap.FlaggedOption;
11 import com.martiansoftware.jsap.JSAP;
12 import com.martiansoftware.jsap.JSAPException;
13
14 /**
15  * This launcher can run a program within a special {@link SpoonClassLoader}
16  * that will process the used classes at load time. Actually this launcher
17  * accepts a list of processors that will be applied before the program is run,
18  * and another list of processors that will be applied at load-time (only once
19  * the classes are used and not before). You can then process bytecode at
20  * load-time by using this launcher with the decompile option. Launch with no
21  * arguments (see {@link #main(String[])}) for detailed usage.
22  */

23 public class LtLauncher extends Launcher {
24
25     /**
26      * Starts a program with this launcher.
27      *
28      * @param args
29      * run with no args to print usage
30      * @throws JSAPException
31      * @throws Exception
32      * any untrapped exception
33      */

34     static public void main(String JavaDoc args[]) throws JSAPException {
35         new LtLauncher(args).run();
36     }
37
38     /**
39      * Constructor with arguments.
40      */

41     public LtLauncher(String JavaDoc[] args) throws JSAPException {
42         super(args);
43     }
44
45     /**
46      * Defines the arguments accepted by this launcher.
47      */

48     protected JSAP defineArgs() throws JSAPException {
49         JSAP jsap = super.defineArgs();
50
51         // Processor qualified name
52
FlaggedOption opt2 = new FlaggedOption("ltprocessors");
53         opt2.setShortFlag('l');
54         opt2.setLongFlag("ltprocessors");
55         opt2.setHelp("List of load-time processors to use");
56         opt2.setStringParser(JSAP.STRING_PARSER);
57         opt2.setRequired(false);
58         jsap.registerParameter(opt2);
59
60         opt2 = new FlaggedOption("sourcepath");
61         opt2.setLongFlag("sourcepath");
62         opt2.setHelp("Specify where to find input source files");
63         opt2.setStringParser(JSAP.STRING_PARSER);
64         opt2.setRequired(false);
65         jsap.registerParameter(opt2);
66
67         return jsap;
68     }
69
70     java.util.List JavaDoc<String JavaDoc> ltprocessors = new ArrayList JavaDoc<String JavaDoc>();
71
72     /**
73      * Adds a processor.
74      */

75     @Override JavaDoc
76     public void addProcessor(String JavaDoc name) {
77         super.addProcessor(name);
78         ltprocessors.add(name);
79     }
80
81     /**
82      * Gets the loadtime processor types.
83      */

84     protected java.util.List JavaDoc<String JavaDoc> getLtProcessorTypes() {
85         if (getArguments().getString("ltprocessors") != null) {
86             for (String JavaDoc processorName : getArguments()
87                     .getString("ltprocessors").split(File.pathSeparator)) {
88                 ltprocessors.add(processorName);
89             }
90         }
91         return ltprocessors;
92     }
93
94     /**
95      * Processes the program within a {@link SpoonClassLoader}.
96      */

97     public void run() {
98         try {
99             getFactory().getEnvironment().debugMessage(
100                     "loading command-line arguments");
101             processArguments();
102
103             getFactory().getEnvironment().debugMessage("Start Pre-processing");
104             build();
105             process();
106
107             // Create a CompilingClassLoader
108
SpoonClassLoader ccl = new SpoonClassLoader();
109             ccl.setFactory(getFactory());
110             if (getArguments().getString("sourcepath") != null)
111                 ccl.setSourcePath(new File JavaDoc(getArguments().getString(
112                         "sourcepath")));
113
114             // Create runtime processing manager
115
ProcessingManager pm = new RuntimeProcessingManager(getFactory());
116             for (String JavaDoc s : getLtProcessorTypes())
117                 pm.addProcessor(s);
118             ccl.setProcessingManager(pm);
119
120             getFactory().getEnvironment().debugMessage("Running");
121             // Gets main class
122
String JavaDoc progClass = getArguments().getString("class");
123             String JavaDoc progArgs[] = getArguments().getStringArray("arguments");
124
125             // Launch main class using reflection
126
Class JavaDoc clas = ccl.loadClass(progClass);
127             Class JavaDoc mainArgType[] = { (new String JavaDoc[0]).getClass() };
128             Method JavaDoc main = clas.getMethod("main", mainArgType);
129             Object JavaDoc argsArray[] = { progArgs };
130             main.invoke(null, argsArray);
131         } catch (Exception JavaDoc e) {
132             e.printStackTrace();
133         }
134     }
135 }
136
Popular Tags