11
11
package net .sf .j2s .core .builder ;
12
12
13
13
import java .io .ByteArrayInputStream ;
14
+ import java .io .InputStream ;
14
15
import java .io .PrintWriter ;
15
16
import java .util .ArrayList ;
16
17
import java .util .HashSet ;
31
32
import org .eclipse .core .resources .IResourceStatus ;
32
33
import org .eclipse .core .runtime .CoreException ;
33
34
import org .eclipse .core .runtime .IPath ;
35
+ import org .eclipse .core .runtime .IStatus ;
34
36
import org .eclipse .core .runtime .Path ;
35
37
import org .eclipse .jdt .core .IJavaModelMarker ;
36
38
import org .eclipse .jdt .core .IMember ;
37
39
import org .eclipse .jdt .core .ISourceRange ;
38
40
import org .eclipse .jdt .core .IType ;
41
+ import org .eclipse .jdt .core .JavaConventions ;
39
42
import org .eclipse .jdt .core .JavaCore ;
40
43
import org .eclipse .jdt .core .JavaModelException ;
41
44
import org .eclipse .jdt .core .compiler .CategorizedProblem ;
@@ -174,7 +177,7 @@ public void acceptResult(CompilationResult result) {
174
177
175
178
char [][] compoundName = classFile .getCompoundName ();
176
179
char [] typeName = compoundName [compoundName .length - 1 ];
177
- boolean isNestedType = classFile .enclosingClassFile != null ;
180
+ boolean isNestedType = classFile .isNestedType ;
178
181
179
182
// Look for a possible collision, if one exists, report an error but do not write the class file
180
183
if (isNestedType ) {
@@ -206,7 +209,7 @@ public void acceptResult(CompilationResult result) {
206
209
continue ;
207
210
}
208
211
newState .recordLocatorForType (qualifiedTypeName , typeLocator );
209
- if (!qualifiedTypeName .equals (compilationUnit .initialTypeName ))
212
+ if (result . checkSecondaryTypes && !qualifiedTypeName .equals (compilationUnit .initialTypeName ))
210
213
acceptSecondaryType (classFile );
211
214
}
212
215
try {
@@ -222,6 +225,7 @@ public void acceptResult(CompilationResult result) {
222
225
if (result .hasAnnotations && this .filesWithAnnotations != null ) // only initialized if an annotation processor is attached
223
226
this .filesWithAnnotations .add (compilationUnit );
224
227
228
+ this .compiler .lookupEnvironment .releaseClassFiles (classFiles );
225
229
finishedWith (typeLocator , result , compilationUnit .getMainTypeName (), definedTypeNames , duplicateTypeNames );
226
230
notifier .compiled (compilationUnit );
227
231
}
@@ -270,7 +274,13 @@ public boolean visit(IResourceProxy proxy) throws CoreException {
270
274
if (!isOutputFolder ) {
271
275
if (folderPath == null )
272
276
folderPath = proxy .requestFullPath ();
273
- createFolder (folderPath .removeFirstSegments (segmentCount ), outputFolder );
277
+ String packageName = folderPath .lastSegment ();
278
+ if (packageName .length () > 0 ) {
279
+ String sourceLevel = javaBuilder .javaProject .getOption (JavaCore .COMPILER_SOURCE , true );
280
+ String complianceLevel = javaBuilder .javaProject .getOption (JavaCore .COMPILER_COMPLIANCE , true );
281
+ if (JavaConventions .validatePackageName (packageName , sourceLevel , complianceLevel ).getSeverity () != IStatus .ERROR )
282
+ createFolder (folderPath .removeFirstSegments (segmentCount ), outputFolder );
283
+ }
274
284
}
275
285
}
276
286
return true ;
@@ -392,6 +402,19 @@ protected void compile(SourceFile[] units, SourceFile[] additionalUnits, boolean
392
402
notifier .checkCancel ();
393
403
}
394
404
405
+ protected void copyResource (IResource source , IResource destination ) throws CoreException {
406
+ IPath destPath = destination .getFullPath ();
407
+ try {
408
+ source .copy (destPath , IResource .FORCE | IResource .DERIVED , null );
409
+ } catch (CoreException e ) {
410
+ // handle the case when the source resource is deleted
411
+ source .refreshLocal (0 , null );
412
+ if (!source .exists ()) return ; // source resource was deleted so skip it
413
+ throw e ;
414
+ }
415
+ Util .setReadOnly (destination , false ); // just in case the original was read only
416
+ }
417
+
395
418
protected void createProblemFor (IResource resource , IMember javaElement , String message , String problemSeverity ) {
396
419
try {
397
420
IMarker marker = resource .createMarker (IJavaModelMarker .JAVA_MODEL_PROBLEM_MARKER );
@@ -530,7 +553,10 @@ protected Compiler newCompiler() {
530
553
this ,
531
554
ProblemFactory .getProblemFactory (Locale .getDefault ()));
532
555
CompilerOptions options = newCompiler .options ;
533
-
556
+ // temporary code to allow the compiler to revert to a single thread
557
+ String setting = System .getProperty ("jdt.compiler.useSingleThread" ); //$NON-NLS-1$
558
+ newCompiler .useSingleThread = setting != null && setting .equals ("true" ); //$NON-NLS-1$
559
+
534
560
// enable the compiler reference info support
535
561
options .produceReferenceInfo = true ;
536
562
@@ -639,7 +665,7 @@ protected void recordParticipantResult(CompilationParticipantResult result) {
639
665
storeProblemsFor (result .sourceFile , problems );
640
666
} catch (CoreException e ) {
641
667
// must continue with compile loop so just log the CoreException
642
- e . printStackTrace ();
668
+ Util . log ( e , "JavaBuilder logging CompilationParticipant's CoreException to help debugging" ); //$NON-NLS-1$
643
669
}
644
670
}
645
671
@@ -660,8 +686,8 @@ protected void recordParticipantResult(CompilationParticipantResult result) {
660
686
* - its priority reflects the severity of the problem
661
687
* - its range is the problem's range
662
688
* - it has an extra attribute "ID" which holds the problem's id
663
- * - it's GENERATED_BY attribute is positioned to JavaBuilder.GENERATED_BY if
664
- * the problem was generated by JDT; else the GENERATED_BY attribute is
689
+ * - it's {@link IMarker#SOURCE_ID} attribute is positioned to {@link JavaBuilder#SOURCE_ID} if
690
+ * the problem was generated by JDT; else the {@link IMarker#SOURCE_ID} attribute is
665
691
* carried from the problem to the marker in extra attributes, if present.
666
692
*/
667
693
protected void storeProblemsFor (SourceFile sourceFile , CategorizedProblem [] problems ) throws CoreException {
@@ -728,11 +754,12 @@ protected void storeProblemsFor(SourceFile sourceFile, CategorizedProblem[] prob
728
754
allValues [index ++] = problem .isError () ? S_ERROR : S_WARNING ; // severity
729
755
allValues [index ++] = new Integer (id ); // ID
730
756
allValues [index ++] = new Integer (problem .getSourceStart ()); // start
731
- allValues [index ++] = new Integer (problem .getSourceEnd () + 1 ); // end
757
+ int end = problem .getSourceEnd ();
758
+ allValues [index ++] = new Integer (end > 0 ? end + 1 : end ); // end
732
759
allValues [index ++] = new Integer (problem .getSourceLineNumber ()); // line
733
760
allValues [index ++] = Util .getProblemArgumentsForMarker (problem .getArguments ()); // arguments
734
761
allValues [index ++] = new Integer (problem .getCategoryID ()); // category ID
735
- // GENERATED_BY attribute for JDT problems
762
+ // SOURCE_ID attribute for JDT problems
736
763
if (managedLength > 0 )
737
764
allValues [index ++] = JavaBuilder .SOURCE_ID ;
738
765
// optional extra attributes
@@ -818,27 +845,28 @@ protected char[] writeClassFile(ClassFile classFile, SourceFile compilationUnit,
818
845
}
819
846
820
847
IFile file = container .getFile (filePath .addFileExtension (SuffixConstants .EXTENSION_class ));
821
- writeClassFileBytes (classFile .getBytes (), file , fileName , isTopLevelType , compilationUnit .updateClassFile );
822
- if (classFile .isShared ) {
823
- this .compiler .lookupEnvironment .classFilePool .release (classFile );
824
- }
848
+ writeClassFileContents (classFile , file , fileName , isTopLevelType , compilationUnit );
825
849
// answer the name of the class file as in Y or Y$M
826
850
return filePath .lastSegment ().toCharArray ();
827
851
}
828
852
829
- protected void writeClassFileBytes (byte [] bytes , IFile file , String qualifiedFileName , boolean isTopLevelType , boolean updateClassFile ) throws CoreException {
853
+ protected void writeClassFileContents (ClassFile classFile , IFile file , String qualifiedFileName , boolean isTopLevelType , SourceFile compilationUnit ) throws CoreException {
854
+ // InputStream input = new SequenceInputStream(
855
+ // new ByteArrayInputStream(classFile.header, 0, classFile.headerOffset),
856
+ // new ByteArrayInputStream(classFile.contents, 0, classFile.contentsOffset));
857
+ InputStream input = new ByteArrayInputStream (classFile .getBytes ());
830
858
if (file .exists ()) {
831
859
// Deal with shared output folders... last one wins... no collision cases detected
832
860
if (JavaBuilder .DEBUG )
833
861
System .out .println ("Writing changed class file " + file .getName ());//$NON-NLS-1$
834
862
if (!file .isDerived ())
835
863
file .setDerived (true );
836
- file .setContents (new ByteArrayInputStream ( bytes ) , true , false , null );
864
+ file .setContents (input , true , false , null );
837
865
} else {
838
866
// Default implementation just writes out the bytes for the new class file...
839
867
if (JavaBuilder .DEBUG )
840
868
System .out .println ("Writing new class file " + file .getName ());//$NON-NLS-1$
841
- file .create (new ByteArrayInputStream ( bytes ) , IResource .FORCE | IResource .DERIVED , null );
869
+ file .create (input , IResource .FORCE | IResource .DERIVED , null );
842
870
}
843
871
}
844
872
}
0 commit comments