1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.commons.jci.examples.commandline;
19
20 import java.io.File;
21 import java.net.URL;
22 import java.net.URLClassLoader;
23 import java.util.Iterator;
24
25 import org.apache.commons.cli.CommandLine;
26 import org.apache.commons.cli.CommandLineParser;
27 import org.apache.commons.cli.GnuParser;
28 import org.apache.commons.cli.Option;
29 import org.apache.commons.cli.OptionBuilder;
30 import org.apache.commons.cli.Options;
31 import org.apache.commons.jci.compilers.CompilationResult;
32 import org.apache.commons.jci.compilers.JavaCompiler;
33 import org.apache.commons.jci.compilers.JavaCompilerFactory;
34 import org.apache.commons.jci.compilers.JavaCompilerSettings;
35 import org.apache.commons.jci.problems.CompilationProblem;
36 import org.apache.commons.jci.problems.CompilationProblemHandler;
37 import org.apache.commons.jci.readers.FileResourceReader;
38 import org.apache.commons.jci.readers.ResourceReader;
39 import org.apache.commons.jci.stores.FileResourceStore;
40 import org.apache.commons.jci.stores.ResourceStore;
41
42
43
44
45
46
47 public final class CommandlineCompiler {
48
49 public static void main( String[] args ) throws Exception {
50
51 final Options options = new Options();
52
53 options.addOption(
54 OptionBuilder.withArgName("a.jar:b.jar")
55 .hasArg()
56 .withValueSeparator( ':' )
57 .withDescription("Specify where to find user class files")
58 .create( "classpath" ));
59
60 options.addOption(
61 OptionBuilder.withArgName("release")
62 .hasArg()
63 .withDescription("Provide source compatibility with specified release")
64 .create( "source" ));
65
66 options.addOption(
67 OptionBuilder.withArgName("release")
68 .hasArg()
69 .withDescription("Generate class files for specific VM version")
70 .create( "target" ));
71
72 options.addOption(
73 OptionBuilder.withArgName("path")
74 .hasArg()
75 .withDescription("Specify where to find input source files")
76 .create( "sourcepath" ));
77
78 options.addOption(
79 OptionBuilder.withArgName("directory")
80 .hasArg()
81 .withDescription("Specify where to place generated class files")
82 .create( "d" ));
83
84 options.addOption(
85 OptionBuilder.withArgName("num")
86 .hasArg()
87 .withDescription("Stop compilation after these number of errors")
88 .create( "Xmaxerrs" ));
89
90 options.addOption(
91 OptionBuilder.withArgName("num")
92 .hasArg()
93 .withDescription("Stop compilation after these number of warning")
94 .create( "Xmaxwarns" ));
95
96 options.addOption(
97 OptionBuilder.withDescription("Generate no warnings")
98 .create( "nowarn" ));
99
100
101
102
103 final CommandLineParser parser = new GnuParser();
104 final CommandLine cmd = parser.parse(options, args, true);
105
106 ClassLoader classloader = CommandlineCompiler.class.getClassLoader();
107 File sourcepath = new File(".");
108 File targetpath = new File(".");
109 int maxerrs = 10;
110 int maxwarns = 10;
111 final boolean nowarn = cmd.hasOption("nowarn");
112
113
114 final JavaCompiler compiler = new JavaCompilerFactory().createCompiler("eclipse");
115 final JavaCompilerSettings settings = compiler.createDefaultSettings();
116
117
118 for (Iterator it = cmd.iterator(); it.hasNext();) {
119 final Option option = (Option) it.next();
120
121 if ("classpath".equals(option.getOpt())) {
122 final String[] values = option.getValues();
123 final URL[] urls = new URL[values.length];
124 for (int i = 0; i < urls.length; i++) {
125 urls[i] = new File(values[i]).toURL();
126 }
127 classloader = new URLClassLoader(urls);
128 } else if ("source".equals(option.getOpt())) {
129 settings.setSourceVersion(option.getValue());
130 } else if ("target".equals(option.getOpt())) {
131 settings.setTargetVersion(option.getValue());
132 } else if ("sourcepath".equals(option.getOpt())) {
133 sourcepath = new File(option.getValue());
134 } else if ("d".equals(option.getOpt())) {
135 targetpath = new File(option.getValue());
136 } else if ("Xmaxerrs".equals(option.getOpt())) {
137 maxerrs = Integer.parseInt(option.getValue());
138 } else if ("Xmaxwarns".equals(option.getOpt())) {
139 maxwarns = Integer.parseInt(option.getValue());
140 }
141 }
142
143 final ResourceReader reader = new FileResourceReader(sourcepath);
144 final ResourceStore store = new FileResourceStore(targetpath);
145
146 final int maxErrors = maxerrs;
147 final int maxWarnings = maxwarns;
148 compiler.setCompilationProblemHandler(new CompilationProblemHandler() {
149 int errors = 0;
150 int warnings = 0;
151 public boolean handle(final CompilationProblem pProblem) {
152
153 if (pProblem.isError()) {
154 System.err.println(pProblem);
155
156 errors++;
157
158 if (errors >= maxErrors) {
159 return false;
160 }
161 } else {
162 if (!nowarn) {
163 System.err.println(pProblem);
164 }
165
166 warnings++;
167
168 if (warnings >= maxWarnings) {
169 return false;
170 }
171 }
172
173 return true;
174 }
175 });
176
177 final String[] resource = cmd.getArgs();
178
179 for (int i = 0; i < resource.length; i++) {
180 System.out.println("compiling " + resource[i]);
181 }
182
183 final CompilationResult result = compiler.compile(resource, reader, store, classloader);
184
185 System.out.println( result.getErrors().length + " errors");
186 System.out.println( result.getWarnings().length + " warnings");
187
188 }
189 }