38
38
import org .eclipse .jdt .core .compiler .batch .BatchCompiler ;
39
39
import org .eclipse .jdt .internal .compiler .ClassFile ;
40
40
import org .eclipse .jdt .internal .compiler .CompilationResult ;
41
- import org .eclipse .jdt .internal .compiler .DefaultErrorHandlingPolicies ;
42
41
import org .eclipse .jdt .internal .compiler .ICompilerRequestor ;
43
- import org .eclipse .jdt .internal .compiler .IErrorHandlingPolicy ;
44
42
import org .eclipse .jdt .internal .compiler .classfmt .ClassFileReader ;
45
43
import org .eclipse .jdt .internal .compiler .classfmt .ClassFormatException ;
46
44
import org .eclipse .jdt .internal .compiler .env .ICompilationUnit ;
56
54
import java .io .IOException ;
57
55
import java .io .InputStream ;
58
56
import java .io .PrintWriter ;
57
+ import java .io .StringWriter ;
59
58
import java .io .Writer ;
60
- import java .net .MalformedURLException ;
61
- import java .net .URL ;
62
- import java .net .URLClassLoader ;
63
59
import java .util .ArrayList ;
64
60
import java .util .Iterator ;
65
61
import java .util .LinkedHashMap ;
66
- import java .util .LinkedList ;
67
62
import java .util .List ;
68
63
import java .util .Map ;
69
64
import java .util .Properties ;
@@ -89,35 +84,28 @@ public EclipseJavaCompiler()
89
84
public CompilerResult performCompile ( CompilerConfiguration config )
90
85
throws CompilerException
91
86
{
92
- List <CompilerMessage > errors = new LinkedList <CompilerMessage >();
93
87
94
- List <String > classpathEntries = config .getClasspathEntries ();
95
-
96
- URL [] urls = new URL [1 + classpathEntries .size ()];
97
-
98
- int i = 0 ;
99
-
100
- try
101
- {
102
- urls [i ++] = new File ( config .getOutputLocation () ).toURL ();
103
-
104
- for ( String entry : classpathEntries )
105
- {
106
- urls [i ++] = new File ( entry ).toURL ();
107
- }
108
- }
109
- catch ( MalformedURLException e )
110
- {
111
- throw new CompilerException ( "Error while converting the classpath entries to URLs." , e );
112
- }
113
-
114
- ClassLoader classLoader = new URLClassLoader ( urls );
115
-
116
- SourceCodeLocator sourceCodeLocator = new SourceCodeLocator ( config .getSourceLocations () );
117
-
118
- INameEnvironment env = new EclipseCompilerINameEnvironment ( sourceCodeLocator , classLoader , errors );
88
+ //URL[] urls = new URL[1 + classpathEntries.size()];
89
+ //
90
+ //int i = 0;
91
+ //
92
+ //try
93
+ //{
94
+ // urls[i++] = new File( config.getOutputLocation() ).toURL();
95
+ //
96
+ // for ( String entry : classpathEntries )
97
+ // {
98
+ // urls[i++] = new File( entry ).toURL();
99
+ // }
100
+ //}
101
+ //catch ( MalformedURLException e )
102
+ //{
103
+ // throw new CompilerException( "Error while converting the classpath entries to URLs.", e );
104
+ //}
119
105
120
- IErrorHandlingPolicy policy = DefaultErrorHandlingPolicies .proceedWithAllProblems ();
106
+ //ClassLoader classLoader = new URLClassLoader( urls );
107
+ //
108
+ //SourceCodeLocator sourceCodeLocator = new SourceCodeLocator( config.getSourceLocations() );
121
109
122
110
// ----------------------------------------------------------------------
123
111
// Build settings from configuration
@@ -192,6 +180,15 @@ public CompilerResult performCompile( CompilerConfiguration config )
192
180
this .errorsAsWarnings = true ;
193
181
}
194
182
183
+ //-- Handle the properties silliness
184
+ String props = extras .get ("properties" );
185
+ if (null == props )
186
+ props = extras .get ("properties" );
187
+ if (null != props ) {
188
+ File propFile = new File (props );
189
+ if (! propFile .exists () || ! propFile .isFile ())
190
+ throw new IllegalArgumentException ("Properties file " + propFile + " does not exist" );
191
+ }
195
192
//settings.putAll( extras );
196
193
197
194
//if ( settings.containsKey( "properties" ) )
@@ -257,6 +254,20 @@ public CompilerResult performCompile( CompilerConfiguration config )
257
254
}
258
255
}
259
256
257
+ // Output path
258
+ args .add ("-d" );
259
+ args .add (config .getOutputLocation ());
260
+
261
+ //-- Write .class files even when error occur, but make sure methods with compile errors do abort when called
262
+ args .add ("-proceedOnError:Fatal" );
263
+
264
+ //-- classpath
265
+ List <String > classpathEntries = config .getClasspathEntries ();
266
+ if (classpathEntries .size () != 0 ) {
267
+ args .add ("-classpath" );
268
+ args .add (getPathString (classpathEntries ));
269
+ }
270
+
260
271
// ----------------------------------------------------------------------
261
272
// Compile!
262
273
// ----------------------------------------------------------------------
@@ -280,10 +291,11 @@ public CompilerResult performCompile( CompilerConfiguration config )
280
291
281
292
System .out .println (">>>> ECJ: " + args );
282
293
283
- PrintWriter devNull = new PrintWriter (new NullWriter ());
294
+ StringWriter sw = new StringWriter ();
295
+ PrintWriter devNull = new PrintWriter (sw );
284
296
285
297
//BatchCompiler.compile(args.toArray(new String[args.size()]), new PrintWriter(System.err), new PrintWriter(System.out), new CompilationProgress() {
286
- BatchCompiler .compile (args .toArray (new String [args .size ()]), devNull , devNull , new CompilationProgress () {
298
+ boolean worked = BatchCompiler .compile (args .toArray (new String [args .size ()]), devNull , devNull , new CompilationProgress () {
287
299
@ Override public void begin (int i ) {
288
300
289
301
}
@@ -305,16 +317,24 @@ public CompilerResult performCompile( CompilerConfiguration config )
305
317
}
306
318
});
307
319
308
- List <CompilerMessage > messageList = new EcjResponseParser ().parse (errorF , errorsAsWarnings );
309
-
320
+ List <CompilerMessage > messageList ;
310
321
boolean hasError = false ;
311
- for (CompilerMessage compilerMessage : errors ) {
322
+ if (errorF .length () < 80 ) {
323
+ messageList = new ArrayList <>();
324
+ messageList .add (new CompilerMessage ("Internal compiler error" ));
325
+ System .err .println (">> " + sw .toString ());
326
+ return new CompilerResult (false , messageList );
327
+ }
328
+ messageList = new EcjResponseParser ().parse (errorF , errorsAsWarnings );
329
+
330
+ for (CompilerMessage compilerMessage : messageList ) {
312
331
if (compilerMessage .isError ()) {
313
332
hasError = true ;
314
333
break ;
315
334
}
316
335
}
317
336
return new CompilerResult (! hasError , messageList );
337
+
318
338
} catch (Exception x ) {
319
339
throw new RuntimeException (x ); // sigh
320
340
} finally {
0 commit comments