15
15
import java .util .Properties ;
16
16
17
17
import org .eclipse .core .resources .IFile ;
18
+ import org .eclipse .core .resources .IProject ;
18
19
import org .eclipse .core .runtime .IPath ;
20
+ import org .eclipse .jdt .core .IClasspathEntry ;
19
21
import org .eclipse .jdt .core .IJavaProject ;
20
22
import org .eclipse .jdt .core .JavaModelException ;
23
+ import org .eclipse .jdt .core .compiler .BuildContext ;
21
24
import org .eclipse .jdt .core .dom .ASTParser ;
22
25
23
26
import j2s .CorePlugin ;
@@ -42,7 +45,6 @@ public abstract class Java2ScriptCompiler {
42
45
private final static String J2S_COMPILER_STATUS_ENABLED = "enabled" ;
43
46
44
47
private final static String J2S_COMPILER_JAVA_VERSION = "j2s.compiler.java.version" ;
45
- private final static String J2S_COMPILER_JAVA_VERSION_DEFAULT = "8" ;
46
48
47
49
private final static String J2S_OUTPUT_PATH = "j2s.output.path" ;
48
50
private final static String J2S_OUTPUT_PATH_DEFAULT = "bin" ;
@@ -74,10 +76,11 @@ public abstract class Java2ScriptCompiler {
74
76
*
75
77
* process the source file into JavaScript using the JDT abstract syntax tree
76
78
* parser and visitor
79
+ * @param isCleanBuild2
77
80
*
78
81
* @param javaSource
79
82
*/
80
- abstract public boolean initializeProject (IJavaProject project );
83
+ abstract public boolean initializeProject (IJavaProject project , boolean isCleanBuild2 );
81
84
82
85
abstract public boolean compileToJavaScript (IFile javaSource , String trailer );
83
86
@@ -134,34 +137,84 @@ public boolean doBreakOnError() {
134
137
private final HashSet <String > copiedResourcePackages = new HashSet <String >();
135
138
136
139
140
+ static File checkJ2SDir (String dir ) {
141
+ System .out .println ("Checking for .j2s or .j2sjmol in " + dir );
142
+ File f ;
143
+ return ((f = new File (dir , J2S_CONFIG_JMOL )).exists () ? f
144
+ : (f = new File (dir , J2S_CONFIG_SWINGJS )).exists () ? f
145
+ : null );
146
+ }
147
+
137
148
/**
138
- * Check to see if this project is what we need
149
+ * Entry point from compilation participant when Java build is complete and it is our turn.
150
+ *
139
151
* @param project
152
+ * @param files
153
+ * @return
154
+ */
155
+ public static Java2ScriptCompiler newCompiler (IJavaProject project , BuildContext [] files ) {
156
+ if (files .length == 0 )
157
+ return null ;
158
+ String j2stype ;
159
+ File f = getJ2SConfigName (project , files [0 ]);
160
+ return ( f == null ? null
161
+ : J2S_CONFIG_JMOL .equals (j2stype = f .getName ()) ?
162
+ new Java2ScriptLegacyCompiler (f )
163
+ : J2S_CONFIG_SWINGJS .equals (j2stype ) ?
164
+ new Java2ScriptSwingJSCompiler (f ) : null );
165
+ }
166
+
167
+ /**
168
+ * Called by newCompiler only. Checks in the root of the classpath for this file
169
+ * first, then in the project root directory.
170
+ *
171
+ * Check to see if this project is what we need for the FIRST file being
172
+ * activated. Note that this means that if there are multiple classpath entries,
173
+ * EACH should have this .j2smol file in it.
174
+ *
175
+ * @param project
176
+ * @param files
177
+ * @param retFile
140
178
* @return ".j2s" or ".j2sjmol" or null
141
179
*/
142
- public static String getJ2SConfigName (IJavaProject project ) {
180
+ private static File getJ2SConfigName (IJavaProject project , BuildContext files ) {
181
+
182
+ File f = null ;
143
183
try {
144
- String dir = project .getProject ().getLocation ().toOSString ();
145
- return (new File (dir , J2S_CONFIG_JMOL ).exists () ? J2S_CONFIG_JMOL
146
- : new File (dir , J2S_CONFIG_SWINGJS ).exists () ? J2S_CONFIG_SWINGJS
147
- : null );
148
- } catch (@ SuppressWarnings ("unused" ) Exception e ) {
149
- return null ;
184
+ if (files == null )
185
+ return null ;
186
+ String projectDir = project .getProject ().getLocation ().toOSString ();
187
+ IPath path = getFirstSourceClassPathEntry (project .getResolvedClasspath (true ));
188
+ String dir = new File (projectDir ).getParent () + path ; // is relative to workspace, I guess.
189
+ System .out .println ("checking entry for " + dir );
190
+ f = checkJ2SDir (dir );
191
+ if (f == null ) {
192
+ f = checkJ2SDir (projectDir );
193
+ }
194
+ } catch (@ SuppressWarnings ("unused" ) JavaModelException e1 ) {
195
+ // no matter;
196
+ } catch (Exception e ) {
197
+ e .printStackTrace ();
150
198
}
199
+ return f ;
151
200
}
152
201
153
- public static Java2ScriptCompiler newCompiler (IJavaProject project ) {
154
- String j2stype = getJ2SConfigName (project );
155
- return ( J2S_CONFIG_JMOL .equals (j2stype ) ?
156
- new Java2ScriptLegacyCompiler ()
157
- : J2S_CONFIG_SWINGJS .equals (j2stype ) ?
158
- new Java2ScriptSwingJSCompiler () : null );
202
+ private static IPath getFirstSourceClassPathEntry (IClasspathEntry [] path ) {
203
+ for (int i = 0 ; i < path .length ; i ++) {
204
+ IClasspathEntry e = path [i ];
205
+ if (e .getEntryKind () == IClasspathEntry .CPE_SOURCE ) {
206
+ System .out .println (i + " " + e + "\n " + e .getPath ());
207
+ return e .getPath ();
208
+ }
209
+ }
210
+ return null ;
159
211
}
160
212
161
- protected Java2ScriptCompiler (boolean isSwingJS , String j2sConfigFileName ) {
213
+ protected Java2ScriptCompiler (boolean isSwingJS , File f ) {
162
214
this .isSwingJS = isSwingJS ;
163
- this .j2sConfigFileName = j2sConfigFileName ;
164
- System .out .println ("Java2ScriptCompiler " + this + " isSwingJS=" + isSwingJS + " " + j2sConfigFileName );
215
+ activeJ2SFile = f ;
216
+ j2sConfigFileName = f .getName ();
217
+ System .out .println ("Java2ScriptCompiler " + this + " isSwingJS=" + f + " " + j2sConfigFileName );
165
218
// initialized only once for SwingJS and once for legacy version
166
219
}
167
220
@@ -183,9 +236,9 @@ protected void startBuild(boolean isClean) {
183
236
* @param j2sFile
184
237
* @param altLevel
185
238
*/
186
- protected Properties initializeUsing ( File j2sFile , int altLevel ) {
187
- System . out . println ( "J2S using configuration file " + j2sFile );
188
- Properties props = new Properties ();
239
+ protected Properties getPropsForDir ( String dir , String j2sConfigName , int altLevel ) {
240
+ File j2sFile = new File ( dir , j2sConfigName );
241
+ Properties props = new Properties ();
189
242
try (FileInputStream os = new FileInputStream (j2sFile )) {
190
243
props .load (os );
191
244
os .close ();
@@ -204,38 +257,31 @@ protected Properties initializeUsing(File j2sFile, int altLevel) {
204
257
* get all necessary .j2s params for a build
205
258
*
206
259
* @param project
260
+ * @param isCleanBuild
207
261
* @param jls4
208
262
* @return true if this is a j2s project and is enabled
209
263
*
210
264
*/
211
- protected boolean initializeProject (IJavaProject project , int javaLanguageLevel ) {
265
+ protected boolean initializeProject (IJavaProject project , boolean isCleanBuild , int javaLanguageLevel ) {
212
266
this .project = project ;
213
- if (!j2sConfigFileName .equals (getJ2SConfigName (project ))) {
214
- // the file .j2s does not exist in the project directory -- skip this project
267
+ projectFolder = project .getProject ().getLocation ().toOSString ();
268
+ startBuild (isCleanBuild );
269
+ props = getPropsForDir (activeJ2SFile .getParent (), j2sConfigFileName , 0 );
270
+ System .out .println (this .getClass ().getName () + " " + activeJ2SFile + " " + props );
271
+ if (!isEnabled ()) {
215
272
return false ;
216
273
}
217
- projectFolder = project .getProject ().getLocation ().toOSString ();
218
- File j2sFile = new File (projectFolder , j2sConfigFileName );
219
- props = initializeUsing (j2sFile , 0 );
220
- if (props == null )
221
- props = new Properties ();
222
- String status = getProperty (J2S_COMPILER_STATUS , J2S_COMPILER_STATUS_ENABLED );
223
- if (!J2S_COMPILER_STATUS_ENABLE .equalsIgnoreCase (status )
224
- && !J2S_COMPILER_STATUS_ENABLED .equalsIgnoreCase (status )) {
225
- if (getFileContents (j2sFile ).trim ().length () == 0 ) {
226
- writeToFile (j2sFile , getDefaultJ2SFile ());
227
- } else {
228
- // not enabled
229
- return false ;
230
- }
274
+ if (getFileContents (activeJ2SFile ).trim ().length () == 0 ) {
275
+ writeToFile (activeJ2SFile , getDefaultJ2SFile ());
231
276
}
232
277
int jslLevel = javaLanguageLevel ;
233
278
if (jslLevel == 8 ) {
234
279
// SwingJS allows 8 or 11
235
280
try {
236
- String ver = getProperty (J2S_COMPILER_JAVA_VERSION , J2S_COMPILER_JAVA_VERSION_DEFAULT );
281
+ String ver = getProperty (J2S_COMPILER_JAVA_VERSION , "" + jslLevel );
237
282
jslLevel = Integer .parseInt (ver );
238
283
} catch (@ SuppressWarnings ("unused" ) Exception e ) {
284
+ System .out .println ("j2s.compiler.java.version should be one of 4, 8, or 11" );
239
285
// ignore
240
286
}
241
287
}
@@ -260,14 +306,14 @@ protected boolean initializeProject(IJavaProject project, int javaLanguageLevel)
260
306
261
307
outputPath = getProperty (J2S_OUTPUT_PATH , null );
262
308
if (outputPath == null ) {
263
- outputPath = J2S_OUTPUT_PATH_DEFAULT ; // bin
309
+ outputPath = J2S_OUTPUT_PATH_DEFAULT ; // bin
264
310
try {
265
311
IPath loc = project .getOutputLocation ();
266
312
outputPath = loc .toString ().substring (loc .toString ().lastIndexOf ('/' ) + 1 );
267
313
} catch (JavaModelException e1 ) {
268
314
// TODO Auto-generated catch block
269
315
e1 .printStackTrace ();
270
- }
316
+ }
271
317
}
272
318
273
319
if (isDebugging ) {
@@ -294,6 +340,12 @@ protected boolean initializeProject(IJavaProject project, int javaLanguageLevel)
294
340
return true ;
295
341
}
296
342
343
+ private boolean isEnabled () {
344
+ String status = getProperty (J2S_COMPILER_STATUS , J2S_COMPILER_STATUS_ENABLED );
345
+ return (J2S_COMPILER_STATUS_ENABLE .equalsIgnoreCase (status )
346
+ || J2S_COMPILER_STATUS_ENABLED .equalsIgnoreCase (status ));
347
+ }
348
+
297
349
boolean excludeFile (IFile javaSource ) {
298
350
return excludeFile (javaSource .getFullPath ().toString ());
299
351
}
@@ -311,12 +363,16 @@ private boolean excludeFile(String filePath) {
311
363
}
312
364
313
365
protected String getProperty (String key , String def ) {
366
+ if (props == null ) {
367
+ System .out .println ("getting " + key + " props is null" );
368
+ return null ;
369
+ }
314
370
String val = props .getProperty (key );
315
371
if (val == null )
316
372
val = def ;
317
- System .out .println (key + " = " + val );
318
373
if (val != null && val .indexOf ("<" ) == 0 )
319
374
val = null ;
375
+ System .out .println ("getting " + key + " = " + val );
320
376
return val ;
321
377
}
322
378
0 commit comments