2626import org .apache .maven .project .MavenProject ;
2727import unquietcode .tools .flapi .Descriptor ;
2828import unquietcode .tools .flapi .DescriptorMaker ;
29- import unquietcode .tools .flapi .ExtractRuntime ;
30- import unquietcode .tools .flapi .plugin .compile .CharSequenceJavaFileObject ;
31- import unquietcode .tools .flapi .plugin .compile .ClassFileManager ;
3229
33- import javax .tools .*;
34- import java .io .ByteArrayOutputStream ;
3530import java .io .File ;
36- import java .io .IOException ;
37- import java .io .OutputStream ;
38- import java .lang .reflect .InvocationTargetException ;
39- import java .lang .reflect .Method ;
40- import java .net .URISyntaxException ;
4131import java .net .URL ;
4232import java .net .URLClassLoader ;
43- import java .util .*;
33+ import java .util .ArrayList ;
34+ import java .util .List ;
4435
4536/**
4637 * Given a static method which returns a {@link Descriptor} object,
@@ -120,193 +111,50 @@ public void execute() throws MojoExecutionException, MojoFailureException {
120111 getLog ().warn ("'flapi.descriptor.class' is deprecated, please use 'flapi.descriptor.classes'" );
121112 }
122113
123- boolean atLeastOne = false ;
124-
125- for (String descriptorClass : descriptorClasses .split ("," )) {
126- descriptorClass = descriptorClass .trim ();
127-
128- if (descriptorClass .isEmpty ()) {
129- continue ;
114+ // set up shared plugin helper
115+ final PluginHelper helper = new PluginHelper (classesDirectory , sourcesDirectory ) {
116+ protected @ Override Exception handleError (String message , Throwable cause ) throws Exception {
117+ throw new MojoExecutionException (message , cause );
130118 }
131119
132- if ( descriptorClass . trim (). equals ( "change.me" )) {
133- continue ;
120+ protected @ Override Exception handleFailure ( String message , Throwable cause ) throws Exception {
121+ throw new MojoFailureException ( message , cause ) ;
134122 }
135123
136- getLog ().info ("processing descriptor " +descriptorClass );
137- execute (descriptorClass );
138- atLeastOne = true ;
139- }
140-
141- if (!atLeastOne ) {
142- getLog ().warn ("No descriptor classes were specified." );
143- }
144- }
145-
146- private void execute (String descriptorClass ) throws MojoExecutionException , MojoFailureException {
147- Method method ;
148- DescriptorMaker descriptorMaker ;
149-
150- // instantiate the class
151- URLClassLoader classLoader ;
152- Class <?> _descriptorClass ;
153-
154- try {
155- classLoader = getCompiledClassloader ();
156- _descriptorClass = classLoader .loadClass (descriptorClass );
157- } catch (Exception ex ) {
158- throw new MojoExecutionException ("could not load class" , ex );
159- }
160-
161- // ensure that it implements the interface
162- if (!DescriptorMaker .class .isAssignableFrom (_descriptorClass )) {
163- throw new MojoExecutionException ("object must implement the DescriptorMaker interface" );
164- }
165-
166- // lookup the method
167- try {
168- method = _descriptorClass .getMethod ("descriptor" );
169- } catch (NoSuchMethodException ex ) {
170- throw new MojoExecutionException ("method cannot be found" , ex );
171- }
172-
173- // instantiate the object
174- try {
175- descriptorMaker = (DescriptorMaker ) _descriptorClass .newInstance ();
176- } catch (Exception ex ) {
177- throw new MojoExecutionException ("could not instantiate DescriptorMaker object" , ex );
178- }
179-
180- // execute and get the descriptor
181- Descriptor descriptor ;
182- try {
183- descriptor = (Descriptor ) method .invoke (descriptorMaker );
184- } catch (IllegalAccessException ex ) {
185- throw new MojoExecutionException ("method not accessible" , ex );
186- } catch (InvocationTargetException ex ) {
187- throw new MojoExecutionException ("error while executing method" , ex .getTargetException ());
188- }
189-
190- // ensure not null
191- if (descriptor == null ) {
192- throw new MojoExecutionException ("method returned null" );
193- }
194-
195- // compile and write out the classes
196- if (writeClasses ) {
197- new File (classesDirectory ).mkdirs ();
198- compileAndWriteClasses (descriptor , classLoader );
199-
200- if (includeRuntime ) {
201- ExtractRuntime .writeRequiredClasses (classesDirectory );
124+ protected @ Override void logInfo (String message ) {
125+ getLog ().info (message );
202126 }
203- }
204-
205- // write out the source files
206- if (writeSources ) {
207- new File (sourcesDirectory ).mkdirs ();
208- descriptor .writeToFolder (sourcesDirectory );
209127
210- if ( includeRuntime ) {
211- ExtractRuntime . writeRequiredSources ( sourcesDirectory );
128+ protected @ Override void logWarn ( String message ) {
129+ getLog (). warn ( message );
212130 }
213- }
214- }
215-
216- private URLClassLoader getCompiledClassloader () throws Exception {
217- List <URL > urls = new ArrayList <URL >();
218-
219- for (Object object : project .getTestClasspathElements ()) {
220- String path = (String ) object ;
221- System .out .println (path );
222- urls .add (new File (path ).toURI ().toURL ());
223- }
224-
225- return new URLClassLoader (urls .toArray (new URL [urls .size ()]), getClass ().getClassLoader ());
226- }
227-
228-
229- List <JavaFileObject > getSourceFiles (Descriptor descriptor ) {
230- Map <String , OutputStream > streams = descriptor .writeToStreams (new Iterator <OutputStream >() {
231- public OutputStream next () { return new ByteArrayOutputStream (); }
232- public boolean hasNext () { return true ; }
233- public void remove () { throw new UnsupportedOperationException ("nope" ); }
234- });
235-
236- final List <JavaFileObject > files = new ArrayList <JavaFileObject >();
237-
238- for (Map .Entry <String , OutputStream > entry : streams .entrySet ()) {
239- String name = entry .getKey ();
240- name = name .substring (0 , name .length ()-5 ); // removes .java
241-
242- ByteArrayOutputStream stream = (ByteArrayOutputStream ) entry .getValue ();
243- JavaFileObject file = new CharSequenceJavaFileObject (name , stream .toString ());
244- files .add (file );
245- }
246-
247- return files ;
248- }
249-
250- private ClassLoader compileAndWriteClasses (Descriptor descriptor , URLClassLoader classLoader ) throws MojoExecutionException {
251- JavaCompiler compiler = ToolProvider .getSystemJavaCompiler ();
252- DiagnosticCollector <JavaFileObject > diagnostics = new DiagnosticCollector <JavaFileObject >();
253- JavaFileManager fileManager = new ClassFileManager (compiler .getStandardFileManager (null , null , null ), classesDirectory );
254131
255- List <String > options = new ArrayList <String >();
256- options .add ("-classpath" );
257- options .add (makeClasspath (classLoader ));
258- options .add ("-source" );
259- options .add ("1.6" );
260- options .add ("-target" );
261- options .add ("1.6" );
262-
263- Iterable <? extends JavaFileObject > compilationUnits = getSourceFiles (descriptor );
264- JavaCompiler .CompilationTask task = compiler .getTask (null , fileManager , diagnostics , options , null , compilationUnits );
265- task .call ();
266-
267- try {
268- fileManager .close ();
269- } catch (IOException e ) {
270- // nothing
271- }
272-
273- boolean atLeastOneError = false ;
274-
275- for (Diagnostic <? extends JavaFileObject > error : diagnostics .getDiagnostics ()) {
276-
277- if (error .getKind () != Diagnostic .Kind .NOTE ) {
278- StringBuilder message = new StringBuilder ()
279- .append (error .getSource ().getName ())
280- .append (" (" ).append (error .getLineNumber ()).append ("," ).append (error .getColumnNumber ()).append (")\n " )
281- .append (error .getMessage (Locale .getDefault ()))
282- ;
283- getLog ().warn (message .toString ());
284- atLeastOneError = true ;
132+ protected @ Override void logError (String message ) {
133+ getLog ().error (message );
285134 }
286- }
287-
288- if (atLeastOneError ) {
289- throw new MojoExecutionException ("The compilation was completed with errors." );
290- }
291135
292- return fileManager . getClassLoader ( StandardLocation . CLASS_PATH );
293- }
136+ protected @ Override URLClassLoader getCompiledClassloader () throws Exception {
137+ List < URL > urls = new ArrayList <>();
294138
295- private static String makeClasspath (URLClassLoader classLoader ) {
296- StringBuilder buffer = new StringBuilder ("\" " );
139+ for (Object object : project .getTestClasspathElements ()) {
140+ String path = (String ) object ;
141+ urls .add (new File (path ).toURI ().toURL ());
142+ }
297143
298- for (URL url : classLoader .getURLs ()) {
299- final File file ;
300- try {
301- file = new File (url .toURI ());
302- } catch (URISyntaxException e ) {
303- throw new RuntimeException (e );
144+ return new URLClassLoader (urls .toArray (new URL [urls .size ()]), getClass ().getClassLoader ());
304145 }
146+ };
305147
306- buffer .append (file );
307- buffer .append (System .getProperty ("path.separator" ));
308- }
148+ // configure it
149+ helper .setIncludeRuntime (includeRuntime );
150+ helper .setWriteClasses (writeClasses );
151+ helper .setWriteSources (writeSources );
309152
310- return buffer .append ("\" " ).toString ();
153+ // run it
154+ try {
155+ helper .processDescriptors (descriptorClasses .split ("," ));
156+ } catch (Exception e ) {
157+ throw new MojoExecutionException ("error while running plugin" , e );
158+ }
311159 }
312160}
0 commit comments