KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > spoon > Launcher


1 package spoon;
2
3 import spoon.processing.FileGenerator;
4 import spoon.reflect.Factory;
5 import spoon.support.ByteCodeOutputProcessor;
6 import spoon.support.JavaOutputProcessor;
7 import spoon.support.gui.SpoonModelTree;
8
9 import com.martiansoftware.jsap.FlaggedOption;
10 import com.martiansoftware.jsap.JSAP;
11 import com.martiansoftware.jsap.JSAPException;
12 import com.martiansoftware.jsap.Switch;
13 import com.martiansoftware.jsap.stringparsers.FileStringParser;
14
15 /**
16  * This class implements an integrated command-line launcher for processing
17  * programs at compile-time using the JDT-based builder (Eclipse). It takes
18  * arguments that allow building, processing, printing, and compiling Java
19  * programs. Launch with no arguments (see {@link #main(String[])}) for
20  * detailed usage.
21  *
22  *
23  * @see spoon.processing.Environment
24  * @see spoon.reflect.Factory
25  * @see spoon.processing.Builder
26  * @see spoon.processing.ProcessingManager
27  * @see spoon.processing.Processor
28  */

29 public class Launcher extends AbstractLauncher {
30     /**
31      * A default program entry point (instantiates a launcher with the given
32      * arguments and calls {@link #run()}).
33      */

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

41     protected Launcher(String JavaDoc[] args) throws JSAPException {
42         super(args);
43     }
44
45     /**
46      * Adds some specific arguments to the common ones.
47      */

48     @Override JavaDoc
49     protected JSAP defineArgs() throws JSAPException {
50         JSAP jsap = super.defineArgs();
51
52         // Disable output generation
53
Switch sw1 = new Switch("nooutput");
54         sw1.setLongFlag("no");
55         sw1.setHelp("disable output printing");
56         sw1.setDefault("false");
57         jsap.registerParameter(sw1);
58
59         // Compile Output files
60
sw1 = new Switch("compile");
61         sw1.setShortFlag('c');
62         sw1.setLongFlag("compile");
63         sw1.setHelp("compile generated sources");
64         jsap.registerParameter(sw1);
65
66         // build output directory
67
FlaggedOption opt2 = new FlaggedOption("build");
68         opt2.setShortFlag('b');
69         opt2.setLongFlag("build");
70         opt2.setDefault("spoonBuild");
71         opt2.setHelp("specify where to place generated class files");
72         opt2.setStringParser(FileStringParser.getParser());
73         opt2.setRequired(false);
74         jsap.registerParameter(opt2);
75
76         // show GUI
77
sw1 = new Switch("gui");
78         sw1.setShortFlag('g');
79         sw1.setLongFlag("gui");
80         sw1.setHelp("show spoon model after processing");
81         jsap.registerParameter(sw1);
82
83         return jsap;
84     }
85
86     /**
87      * Creates the factory and associated environment for constructing the
88      * model, initialized with the launcher's arguments.
89      */

90     @Override JavaDoc
91     protected Factory createFactory() {
92         Factory f = super.createFactory();
93
94         if (getArguments().getBoolean("compile")) {
95             FileGenerator<?> printer = f.getEnvironment()
96                     .getDefaultFileGenerator();
97             ByteCodeOutputProcessor p = new ByteCodeOutputProcessor(
98                     (JavaOutputProcessor) printer, getArguments().getFile(
99                             "build"));
100             f.getEnvironment().setDefaultFileGenerator(p);
101         }
102
103         return f;
104     }
105
106     /**
107      * Prints out the built model into files.
108      */

109     @Override JavaDoc
110     protected void print() {
111         if (!getArguments().getBoolean("nooutput"))
112             super.print();
113     }
114
115     /**
116      * Starts the Spoon processing.
117      */

118     @Override JavaDoc
119     public void run() {
120         super.run();
121
122         // display GUI
123
if (getArguments().getBoolean("gui"))
124             new SpoonModelTree(getFactory());
125
126     }
127
128 }
129
Popular Tags