1111package net .sf .j2s .core .builder ;
1212
1313import java .io .ByteArrayInputStream ;
14+ import java .io .InputStream ;
1415import java .io .PrintWriter ;
1516import java .util .ArrayList ;
1617import java .util .HashSet ;
3132import org .eclipse .core .resources .IResourceStatus ;
3233import org .eclipse .core .runtime .CoreException ;
3334import org .eclipse .core .runtime .IPath ;
35+ import org .eclipse .core .runtime .IStatus ;
3436import org .eclipse .core .runtime .Path ;
3537import org .eclipse .jdt .core .IJavaModelMarker ;
3638import org .eclipse .jdt .core .IMember ;
3739import org .eclipse .jdt .core .ISourceRange ;
3840import org .eclipse .jdt .core .IType ;
41+ import org .eclipse .jdt .core .JavaConventions ;
3942import org .eclipse .jdt .core .JavaCore ;
4043import org .eclipse .jdt .core .JavaModelException ;
4144import org .eclipse .jdt .core .compiler .CategorizedProblem ;
@@ -174,7 +177,7 @@ public void acceptResult(CompilationResult result) {
174177
175178 char [][] compoundName = classFile .getCompoundName ();
176179 char [] typeName = compoundName [compoundName .length - 1 ];
177- boolean isNestedType = classFile .enclosingClassFile != null ;
180+ boolean isNestedType = classFile .isNestedType ;
178181
179182 // Look for a possible collision, if one exists, report an error but do not write the class file
180183 if (isNestedType ) {
@@ -206,7 +209,7 @@ public void acceptResult(CompilationResult result) {
206209 continue ;
207210 }
208211 newState .recordLocatorForType (qualifiedTypeName , typeLocator );
209- if (!qualifiedTypeName .equals (compilationUnit .initialTypeName ))
212+ if (result . checkSecondaryTypes && !qualifiedTypeName .equals (compilationUnit .initialTypeName ))
210213 acceptSecondaryType (classFile );
211214 }
212215 try {
@@ -222,6 +225,7 @@ public void acceptResult(CompilationResult result) {
222225 if (result .hasAnnotations && this .filesWithAnnotations != null ) // only initialized if an annotation processor is attached
223226 this .filesWithAnnotations .add (compilationUnit );
224227
228+ this .compiler .lookupEnvironment .releaseClassFiles (classFiles );
225229 finishedWith (typeLocator , result , compilationUnit .getMainTypeName (), definedTypeNames , duplicateTypeNames );
226230 notifier .compiled (compilationUnit );
227231 }
@@ -270,7 +274,13 @@ public boolean visit(IResourceProxy proxy) throws CoreException {
270274 if (!isOutputFolder ) {
271275 if (folderPath == null )
272276 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+ }
274284 }
275285 }
276286 return true ;
@@ -392,6 +402,19 @@ protected void compile(SourceFile[] units, SourceFile[] additionalUnits, boolean
392402 notifier .checkCancel ();
393403}
394404
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+
395418protected void createProblemFor (IResource resource , IMember javaElement , String message , String problemSeverity ) {
396419 try {
397420 IMarker marker = resource .createMarker (IJavaModelMarker .JAVA_MODEL_PROBLEM_MARKER );
@@ -530,7 +553,10 @@ protected Compiler newCompiler() {
530553 this ,
531554 ProblemFactory .getProblemFactory (Locale .getDefault ()));
532555 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+
534560 // enable the compiler reference info support
535561 options .produceReferenceInfo = true ;
536562
@@ -639,7 +665,7 @@ protected void recordParticipantResult(CompilationParticipantResult result) {
639665 storeProblemsFor (result .sourceFile , problems );
640666 } catch (CoreException e ) {
641667 // 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$
643669 }
644670 }
645671
@@ -660,8 +686,8 @@ protected void recordParticipantResult(CompilationParticipantResult result) {
660686 * - its priority reflects the severity of the problem
661687 * - its range is the problem's range
662688 * - 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
665691 * carried from the problem to the marker in extra attributes, if present.
666692 */
667693protected void storeProblemsFor (SourceFile sourceFile , CategorizedProblem [] problems ) throws CoreException {
@@ -728,11 +754,12 @@ protected void storeProblemsFor(SourceFile sourceFile, CategorizedProblem[] prob
728754 allValues [index ++] = problem .isError () ? S_ERROR : S_WARNING ; // severity
729755 allValues [index ++] = new Integer (id ); // ID
730756 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
732759 allValues [index ++] = new Integer (problem .getSourceLineNumber ()); // line
733760 allValues [index ++] = Util .getProblemArgumentsForMarker (problem .getArguments ()); // arguments
734761 allValues [index ++] = new Integer (problem .getCategoryID ()); // category ID
735- // GENERATED_BY attribute for JDT problems
762+ // SOURCE_ID attribute for JDT problems
736763 if (managedLength > 0 )
737764 allValues [index ++] = JavaBuilder .SOURCE_ID ;
738765 // optional extra attributes
@@ -818,27 +845,28 @@ protected char[] writeClassFile(ClassFile classFile, SourceFile compilationUnit,
818845 }
819846
820847 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 );
825849 // answer the name of the class file as in Y or Y$M
826850 return filePath .lastSegment ().toCharArray ();
827851}
828852
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 ());
830858 if (file .exists ()) {
831859 // Deal with shared output folders... last one wins... no collision cases detected
832860 if (JavaBuilder .DEBUG )
833861 System .out .println ("Writing changed class file " + file .getName ());//$NON-NLS-1$
834862 if (!file .isDerived ())
835863 file .setDerived (true );
836- file .setContents (new ByteArrayInputStream ( bytes ) , true , false , null );
864+ file .setContents (input , true , false , null );
837865 } else {
838866 // Default implementation just writes out the bytes for the new class file...
839867 if (JavaBuilder .DEBUG )
840868 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 );
842870 }
843871}
844872}
0 commit comments