<s> package org . eclipse . jdt . internal . core . builder ; import java . io . ByteArrayInputStream ; import java . io . InputStream ; import java . io . PrintWriter ; import java . util . ArrayList ; import java . util . HashSet ; import java . util . Locale ; import java . util . Map ; import org . codehaus . jdt . groovy . integration . LanguageSupportFactory ; import org . eclipse . core . resources . IContainer ; import org . eclipse . core . resources . IFile ; import org . eclipse . core . resources . IFolder ; import org . eclipse . core . resources . IMarker ; import org . eclipse . core . resources . IResource ; import org . eclipse . core . resources . IResourceProxy ; import org . eclipse . core . resources . IResourceProxyVisitor ; import org . eclipse . core . resources . IResourceStatus ; import org . eclipse . core . runtime . CoreException ; import org . eclipse . core . runtime . IPath ; import org . eclipse . core . runtime . IStatus ; import org . eclipse . core . runtime . Path ; import org . eclipse . jdt . core . IJavaModelMarker ; import org . eclipse . jdt . core . IJavaModelStatusConstants ; import org . eclipse . jdt . core . IMember ; import org . eclipse . jdt . core . ISourceRange ; import org . eclipse . jdt . core . IType ; import org . eclipse . jdt . core . JavaConventions ; import org . eclipse . jdt . core . JavaCore ; import org . eclipse . jdt . core . JavaModelException ; import org . eclipse . jdt . core . compiler . CategorizedProblem ; import org . eclipse . jdt . core . compiler . CharOperation ; import org . eclipse . jdt . core . compiler . IProblem ; import org . eclipse . jdt . core . util . CompilerUtils ; import org . eclipse . jdt . internal . compiler . AbstractAnnotationProcessorManager ; import org . eclipse . jdt . internal . compiler . ClassFile ; import org . eclipse . jdt . internal . compiler . CompilationResult ; import org . eclipse . jdt . internal . compiler . Compiler ; import org . eclipse . jdt . internal . compiler . DefaultErrorHandlingPolicies ; import org . eclipse . jdt . internal . compiler . ICompilerRequestor ; import org . eclipse . jdt . internal . compiler . classfmt . ClassFileConstants ; import org . eclipse . jdt . internal . compiler . env . ICompilationUnit ; import org . eclipse . jdt . internal . compiler . impl . CompilerOptions ; import org . eclipse . jdt . internal . compiler . lookup . TypeConstants ; import org . eclipse . jdt . internal . compiler . problem . AbortCompilation ; import org . eclipse . jdt . internal . compiler . util . SimpleSet ; import org . eclipse . jdt . internal . compiler . util . SuffixConstants ; import org . eclipse . jdt . internal . core . JavaModelManager ; import org . eclipse . jdt . internal . core . util . Messages ; import org . eclipse . jdt . internal . core . util . Util ; public abstract class AbstractImageBuilder implements ICompilerRequestor , ICompilationUnitLocator { protected JavaBuilder javaBuilder ; protected State newState ; protected NameEnvironment nameEnvironment ; protected ClasspathMultiDirectory [ ] sourceLocations ; public BuildNotifier notifier ; protected Compiler compiler ; protected WorkQueue workQueue ; protected ArrayList problemSourceFiles ; protected boolean compiledAllAtOnce ; private boolean inCompiler ; protected boolean keepStoringProblemMarkers ; protected SimpleSet filesWithAnnotations = null ; public static int MAX_AT_ONCE = 2000 ; public final static String [ ] JAVA_PROBLEM_MARKER_ATTRIBUTE_NAMES = { IMarker . MESSAGE , IMarker . SEVERITY , IJavaModelMarker . ID , IMarker . CHAR_START , IMarker . CHAR_END , IMarker . LINE_NUMBER , IJavaModelMarker . ARGUMENTS , IJavaModelMarker . CATEGORY_ID , } ; public final static String [ ] JAVA_TASK_MARKER_ATTRIBUTE_NAMES = { IMarker . MESSAGE , IMarker . PRIORITY , IJavaModelMarker . ID , IMarker . CHAR_START , IMarker . CHAR_END , IMarker . LINE_NUMBER , IMarker . USER_EDITABLE , IMarker . SOURCE_ID , } ; public final static Integer S_ERROR = new Integer ( IMarker . SEVERITY_ERROR ) ; public final static Integer S_WARNING = new Integer ( IMarker . SEVERITY_WARNING ) ; public final static Integer P_HIGH = new Integer ( IMarker . PRIORITY_HIGH ) ; public final static Integer P_NORMAL = new Integer ( IMarker . PRIORITY_NORMAL ) ; public final static Integer P_LOW = new Integer ( IMarker . PRIORITY_LOW ) ; protected AbstractImageBuilder ( JavaBuilder javaBuilder , boolean buildStarting , State newState ) { this . javaBuilder = javaBuilder ; this . nameEnvironment = javaBuilder . nameEnvironment ; this . sourceLocations = this . nameEnvironment . sourceLocations ; this . notifier = javaBuilder . notifier ; this . keepStoringProblemMarkers = true ; if ( buildStarting ) { this . newState = newState == null ? new State ( javaBuilder ) : newState ; this . compiler = newCompiler ( ) ; this . workQueue = new WorkQueue ( ) ; this . problemSourceFiles = new ArrayList ( 3 ) ; if ( this . javaBuilder . participants != null ) { for ( int i = 0 , l = this . javaBuilder . participants . length ; i < l ; i ++ ) { if ( this . javaBuilder . participants [ i ] . isAnnotationProcessor ( ) ) { this . filesWithAnnotations = new SimpleSet ( 1 ) ; break ; } } } } } public void acceptResult ( CompilationResult result ) { SourceFile compilationUnit = ( SourceFile ) result . getCompilationUnit ( ) ; if ( ! this . workQueue . isCompiled ( compilationUnit ) ) { this . workQueue . finished ( compilationUnit ) ; try { updateProblemsFor ( compilationUnit , result ) ; updateTasksFor ( compilationUnit , result ) ; } catch ( CoreException e ) { throw internalException ( e ) ; } if ( result . hasInconsistentToplevelHierarchies ) if ( ! this . problemSourceFiles . contains ( compilationUnit ) ) this . problemSourceFiles . add ( compilationUnit ) ; IType mainType = null ; String mainTypeName = null ; String typeLocator = compilationUnit . typeLocator ( ) ; ClassFile [ ] classFiles = result . getClassFiles ( ) ; int length = classFiles . length ; ArrayList duplicateTypeNames = null ; ArrayList definedTypeNames = new ArrayList ( length ) ; for ( int i = 0 ; i < length ; i ++ ) { ClassFile classFile = classFiles [ i ] ; char [ ] [ ] compoundName = classFile . getCompoundName ( ) ; char [ ] typeName = compoundName [ compoundName . length - 1 ] ; boolean isNestedType = classFile . isNestedType ; if ( isNestedType ) { String qualifiedTypeName = new String ( classFile . outerMostEnclosingClassFile ( ) . fileName ( ) ) ; if ( this . newState . isDuplicateLocator ( qualifiedTypeName , typeLocator ) ) continue ; } else { String qualifiedTypeName = new String ( classFile . fileName ( ) ) ; if ( this . newState . isDuplicateLocator ( qualifiedTypeName , typeLocator ) ) { if ( duplicateTypeNames == null ) duplicateTypeNames = new ArrayList ( ) ; duplicateTypeNames . add ( compoundName ) ; if ( mainType == null ) { try { mainTypeName = compilationUnit . initialTypeName ; mainType = this . javaBuilder . javaProject . findType ( mainTypeName . replace ( '/' , '.' ) ) ; } catch ( JavaModelException e ) { } } IType type ; if ( qualifiedTypeName . equals ( mainTypeName ) ) { type = mainType ; } else { String simpleName = qualifiedTypeName . substring ( qualifiedTypeName . lastIndexOf ( '/' ) + 1 ) ; type = mainType == null ? null : mainType . getCompilationUnit ( ) . getType ( simpleName ) ; } createProblemFor ( compilationUnit . resource , type , Messages . bind ( Messages . build_duplicateClassFile , new String ( typeName ) ) , JavaCore . ERROR ) ; continue ; } this . newState . recordLocatorForType ( qualifiedTypeName , typeLocator ) ; if ( result . checkSecondaryTypes && ! qualifiedTypeName . equals ( compilationUnit . initialTypeName ) ) acceptSecondaryType ( classFile ) ; } try { definedTypeNames . add ( writeClassFile ( classFile , compilationUnit , ! isNestedType ) ) ; } catch ( CoreException e ) { Util . log ( e , "" ) ; if ( e . getStatus ( ) . getCode ( ) == IResourceStatus . CASE_VARIANT_EXISTS ) createProblemFor ( compilationUnit . resource , null , Messages . bind ( Messages . build_classFileCollision , e . getMessage ( ) ) , JavaCore . ERROR ) ; else createProblemFor ( compilationUnit . resource , null , Messages . build_inconsistentClassFile , JavaCore . ERROR ) ; } } if ( result . hasAnnotations && this . filesWithAnnotations != null ) this . filesWithAnnotations . add ( compilationUnit ) ; this . compiler . lookupEnvironment . releaseClassFiles ( classFiles ) ; finishedWith ( typeLocator , result , compilationUnit . getMainTypeName ( ) , definedTypeNames , duplicateTypeNames ) ; this . notifier . compiled ( compilationUnit ) ; } } protected void acceptSecondaryType ( ClassFile classFile ) { } protected void addAllSourceFiles ( final ArrayList sourceFiles ) throws CoreException { final boolean isInterestingProject = LanguageSupportFactory . isInterestingProject ( javaBuilder . getProject ( ) ) ; for ( int i = 0 , l = this . sourceLocations . length ; i < l ; i ++ ) { final ClasspathMultiDirectory sourceLocation = this . sourceLocations [ i ] ; final char [ ] [ ] exclusionPatterns = sourceLocation . exclusionPatterns ; final char [ ] [ ] inclusionPatterns = sourceLocation . inclusionPatterns ; final boolean isAlsoProject = sourceLocation . sourceFolder . equals ( this . javaBuilder . currentProject ) ; final int segmentCount = sourceLocation . sourceFolder . getFullPath ( ) . segmentCount ( ) ; final IContainer outputFolder = sourceLocation . binaryFolder ; final boolean isOutputFolder = sourceLocation . sourceFolder . equals ( outputFolder ) ; sourceLocation . sourceFolder . accept ( new IResourceProxyVisitor ( ) { public boolean visit ( IResourceProxy proxy ) throws CoreException { switch ( proxy . getType ( ) ) { case IResource . FILE : String resourceName = proxy . getName ( ) ; if ( ( ! isInterestingProject && org . eclipse . jdt . internal . core . util . Util . isJavaLikeFileName ( resourceName ) && ! LanguageSupportFactory . isInterestingSourceFile ( resourceName ) ) || ( isInterestingProject && LanguageSupportFactory . isSourceFile ( resourceName , isInterestingProject ) ) ) { IResource resource = proxy . requestResource ( ) ; if ( exclusionPatterns != null || inclusionPatterns != null ) if ( Util . isExcluded ( resource . getFullPath ( ) , inclusionPatterns , exclusionPatterns , false ) ) return false ; sourceFiles . add ( new SourceFile ( ( IFile ) resource , sourceLocation ) ) ; } return false ; case IResource . FOLDER : IPath folderPath = null ; if ( isAlsoProject ) if ( isExcludedFromProject ( folderPath = proxy . requestFullPath ( ) ) ) return false ; if ( exclusionPatterns != null ) { if ( folderPath == null ) folderPath = proxy . requestFullPath ( ) ; if ( Util . isExcluded ( folderPath , inclusionPatterns , exclusionPatterns , true ) ) { return inclusionPatterns != null ; } } if ( ! isOutputFolder ) { if ( folderPath == null ) folderPath = proxy . requestFullPath ( ) ; String packageName = folderPath . lastSegment ( ) ; if ( packageName . length ( ) > 0 ) { String sourceLevel = AbstractImageBuilder . this . javaBuilder . javaProject . getOption ( JavaCore . COMPILER_SOURCE , true ) ; String complianceLevel = AbstractImageBuilder . this . javaBuilder . javaProject . getOption ( JavaCore . COMPILER_COMPLIANCE , true ) ; if ( JavaConventions . validatePackageName ( packageName , sourceLevel , complianceLevel ) . getSeverity ( ) != IStatus . ERROR ) createFolder ( folderPath . removeFirstSegments ( segmentCount ) , outputFolder ) ; } } } return true ; } } , IResource . NONE ) ; this . notifier . checkCancel ( ) ; } } protected void cleanUp ( ) { this . nameEnvironment . cleanup ( ) ; this . javaBuilder = null ; this . nameEnvironment = null ; this . sourceLocations = null ; this . notifier = null ; this . compiler = null ; this . workQueue = null ; this . problemSourceFiles = null ; } protected void compile ( SourceFile [ ] units ) { if ( this . filesWithAnnotations != null && this . filesWithAnnotations . elementSize > 0 ) this . filesWithAnnotations . clear ( ) ; CompilationParticipantResult [ ] participantResults = this . javaBuilder . participants == null ? null : notifyParticipants ( units ) ; if ( participantResults != null && participantResults . length > units . length ) { units = new SourceFile [ participantResults . length ] ; for ( int i = participantResults . length ; -- i >= 0 ; ) units [ i ] = participantResults [ i ] . sourceFile ; } int unitsLength = units . length ; this . compiledAllAtOnce = unitsLength <= MAX_AT_ONCE ; if ( this . compiler != null && this . compiler . options != null && this . compiler . options . buildGroovyFiles == 2 ) { this . compiledAllAtOnce = true ; } if ( this . compiledAllAtOnce ) { if ( JavaBuilder . DEBUG ) for ( int i = 0 ; i < unitsLength ; i ++ ) System . out . println ( "" + units [ i ] . typeLocator ( ) ) ; compile ( units , null , true ) ; } else { SourceFile [ ] remainingUnits = new SourceFile [ unitsLength ] ; System . arraycopy ( units , 0 , remainingUnits , 0 , unitsLength ) ; int doNow = unitsLength < MAX_AT_ONCE ? unitsLength : MAX_AT_ONCE ; SourceFile [ ] toCompile = new SourceFile [ doNow ] ; int remainingIndex = 0 ; boolean compilingFirstGroup = true ; while ( remainingIndex < unitsLength ) { int count = 0 ; while ( remainingIndex < unitsLength && count < doNow ) { SourceFile unit = remainingUnits [ remainingIndex ] ; if ( unit != null && ( compilingFirstGroup || this . workQueue . isWaiting ( unit ) ) ) { if ( JavaBuilder . DEBUG ) System . out . println ( "" + remainingIndex + " : " + unit . typeLocator ( ) ) ; toCompile [ count ++ ] = unit ; } remainingUnits [ remainingIndex ++ ] = null ; } if ( count < doNow ) System . arraycopy ( toCompile , 0 , toCompile = new SourceFile [ count ] , 0 , count ) ; if ( ! compilingFirstGroup ) for ( int a = remainingIndex ; a < unitsLength ; a ++ ) if ( remainingUnits [ a ] != null && this . workQueue . isCompiled ( remainingUnits [ a ] ) ) remainingUnits [ a ] = null ; compile ( toCompile , remainingUnits , compilingFirstGroup ) ; compilingFirstGroup = false ; } } if ( participantResults != null ) { for ( int i = participantResults . length ; -- i >= 0 ; ) if ( participantResults [ i ] != null ) recordParticipantResult ( participantResults [ i ] ) ; processAnnotations ( participantResults ) ; } } protected void compile ( SourceFile [ ] units , SourceFile [ ] additionalUnits , boolean compilingFirstGroup ) { if ( units . length == 0 ) return ; this . notifier . aboutToCompile ( units [ 0 ] ) ; if ( ! this . problemSourceFiles . isEmpty ( ) ) { int toAdd = this . problemSourceFiles . size ( ) ; int length = additionalUnits == null ? 0 : additionalUnits . length ; if ( length == 0 ) additionalUnits = new SourceFile [ toAdd ] ; else System . arraycopy ( additionalUnits , 0 , additionalUnits = new SourceFile [ length + toAdd ] , 0 , length ) ; for ( int i = 0 ; i < toAdd ; i ++ ) additionalUnits [ length + i ] = ( SourceFile ) this . problemSourceFiles . get ( i ) ; } String [ ] initialTypeNames = new String [ units . length ] ; for ( int i = 0 , l = units . length ; i < l ; i ++ ) initialTypeNames [ i ] = units [ i ] . initialTypeName ; this . nameEnvironment . setNames ( initialTypeNames , additionalUnits ) ; this . notifier . checkCancel ( ) ; try { this . inCompiler = true ; this . compiler . compile ( units ) ; } catch ( AbortCompilation ignored ) { } finally { this . inCompiler = false ; } this . notifier . checkCancel ( ) ; } protected void copyResource ( IResource source , IResource destination ) throws CoreException { IPath destPath = destination . getFullPath ( ) ; try { source . copy ( destPath , IResource . FORCE | IResource . DERIVED , null ) ; } catch ( CoreException e ) { source . refreshLocal ( 0 , null ) ; if ( ! source . exists ( ) ) return ; throw e ; } Util . setReadOnly ( destination , false ) ; } protected void createProblemFor ( IResource resource , IMember javaElement , String message , String problemSeverity ) { try { IMarker marker = resource . createMarker ( IJavaModelMarker . JAVA_MODEL_PROBLEM_MARKER ) ; int severity = problemSeverity . equals ( JavaCore . WARNING ) ? IMarker . SEVERITY_WARNING : IMarker . SEVERITY_ERROR ; ISourceRange range = null ; if ( javaElement != null ) { try { range = javaElement . getNameRange ( ) ; } catch ( JavaModelException e ) { if ( e . getJavaModelStatus ( ) . getCode ( ) != IJavaModelStatusConstants . ELEMENT_DOES_NOT_EXIST ) { throw e ; } if ( ! CharOperation . equals ( javaElement . getElementName ( ) . toCharArray ( ) , TypeConstants . PACKAGE_INFO_NAME ) ) { throw e ; } } } int start = range == null ? 0 : range . getOffset ( ) ; int end = range == null ? 1 : start + range . getLength ( ) ; marker . setAttributes ( new String [ ] { IMarker . MESSAGE , IMarker . SEVERITY , IMarker . CHAR_START , IMarker . CHAR_END , IMarker . SOURCE_ID } , new Object [ ] { message , new Integer ( severity ) , new Integer ( start ) , new Integer ( end ) , JavaBuilder . SOURCE_ID } ) ; } catch ( CoreException e ) { throw internalException ( e ) ; } } protected void deleteGeneratedFiles ( IFile [ ] deletedGeneratedFiles ) { } protected SourceFile findSourceFile ( IFile file , boolean mustExist ) { if ( mustExist && ! file . exists ( ) ) return null ; ClasspathMultiDirectory md = this . sourceLocations [ 0 ] ; if ( this . sourceLocations . length > 1 ) { IPath sourceFileFullPath = file . getFullPath ( ) ; for ( int j = 0 , m = this . sourceLocations . length ; j < m ; j ++ ) { if ( this . sourceLocations [ j ] . sourceFolder . getFullPath ( ) . isPrefixOf ( sourceFileFullPath ) ) { md = this . sourceLocations [ j ] ; if ( md . exclusionPatterns == null && md . inclusionPatterns == null ) break ; if ( ! Util . isExcluded ( file , md . inclusionPatterns , md . exclusionPatterns ) ) break ; } } } return new SourceFile ( file , md ) ; } protected void finishedWith ( String sourceLocator , CompilationResult result , char [ ] mainTypeName , ArrayList definedTypeNames , ArrayList duplicateTypeNames ) { if ( duplicateTypeNames == null ) { this . newState . record ( sourceLocator , result . qualifiedReferences , result . simpleNameReferences , result . rootReferences , mainTypeName , definedTypeNames ) ; return ; } char [ ] [ ] simpleRefs = result . simpleNameReferences ; next : for ( int i = 0 , l = duplicateTypeNames . size ( ) ; i < l ; i ++ ) { char [ ] [ ] compoundName = ( char [ ] [ ] ) duplicateTypeNames . get ( i ) ; char [ ] typeName = compoundName [ compoundName . length - 1 ] ; int sLength = simpleRefs . length ; for ( int j = 0 ; j < sLength ; j ++ ) if ( CharOperation . equals ( simpleRefs [ j ] , typeName ) ) continue next ; System . arraycopy ( simpleRefs , 0 , simpleRefs = new char [ sLength + 1 ] [ ] , 0 , sLength ) ; simpleRefs [ sLength ] = typeName ; } this . newState . record ( sourceLocator , result . qualifiedReferences , simpleRefs , result . rootReferences , mainTypeName , definedTypeNames ) ; } protected IContainer createFolder ( IPath packagePath , IContainer outputFolder ) throws CoreException { if ( packagePath . isEmpty ( ) ) return outputFolder ; IFolder folder = outputFolder . getFolder ( packagePath ) ; if ( ! folder . exists ( ) ) { createFolder ( packagePath . removeLastSegments ( 1 ) , outputFolder ) ; folder . create ( IResource . FORCE | IResource . DERIVED , true , null ) ; } return folder ; } public ICompilationUnit fromIFile ( IFile file ) { return findSourceFile ( file , true ) ; } protected void initializeAnnotationProcessorManager ( Compiler newCompiler ) { AbstractAnnotationProcessorManager annotationManager = JavaModelManager . getJavaModelManager ( ) . createAnnotationProcessorManager ( ) ; if ( annotationManager != null ) { annotationManager . configureFromPlatform ( newCompiler , this , this . javaBuilder . javaProject ) ; annotationManager . setErr ( new PrintWriter ( System . err ) ) ; annotationManager . setOut ( new PrintWriter ( System . out ) ) ; } newCompiler . annotationProcessorManager = annotationManager ; } protected RuntimeException internalException ( CoreException t ) { ImageBuilderInternalException imageBuilderException = new ImageBuilderInternalException ( t ) ; if ( this . inCompiler ) return new AbortCompilation ( true , imageBuilderException ) ; return imageBuilderException ; } protected boolean isExcludedFromProject ( IPath childPath ) throws JavaModelException { if ( childPath . segmentCount ( ) > 2 ) return false ; for ( int j = 0 , k = this . sourceLocations . length ; j < k ; j ++ ) { if ( childPath . equals ( this . sourceLocations [ j ] . binaryFolder . getFullPath ( ) ) ) return true ; if ( childPath . equals ( this . sourceLocations [ j ] . sourceFolder . getFullPath ( ) ) ) return true ; } return childPath . equals ( this . javaBuilder . javaProject . getOutputLocation ( ) ) ; } protected Compiler newCompiler ( ) { Map projectOptions = this . javaBuilder . javaProject . getOptions ( true ) ; String option = ( String ) projectOptions . get ( JavaCore . COMPILER_PB_INVALID_JAVADOC ) ; if ( option == null || option . equals ( JavaCore . IGNORE ) ) { option = ( String ) projectOptions . get ( JavaCore . COMPILER_PB_MISSING_JAVADOC_TAGS ) ; if ( option == null || option . equals ( JavaCore . IGNORE ) ) { option = ( String ) projectOptions . get ( JavaCore . COMPILER_PB_MISSING_JAVADOC_COMMENTS ) ; if ( option == null || option . equals ( JavaCore . IGNORE ) ) { option = ( String ) projectOptions . get ( JavaCore . COMPILER_PB_UNUSED_IMPORT ) ; if ( option == null || option . equals ( JavaCore . IGNORE ) ) { projectOptions . put ( JavaCore . COMPILER_DOC_COMMENT_SUPPORT , JavaCore . DISABLED ) ; } } } } CompilerOptions compilerOptions = new CompilerOptions ( projectOptions ) ; compilerOptions . performMethodsFullRecovery = true ; compilerOptions . performStatementsRecovery = true ; CompilerUtils . configureOptionsBasedOnNature ( compilerOptions , javaBuilder . javaProject ) ; Compiler newCompiler = new Compiler ( this . nameEnvironment , DefaultErrorHandlingPolicies . proceedWithAllProblems ( ) , compilerOptions , this , ProblemFactory . getProblemFactory ( Locale . getDefault ( ) ) ) ; CompilerOptions options = newCompiler . options ; String setting = System . getProperty ( "" ) ; newCompiler . useSingleThread = setting != null && setting . equals ( "true" ) ; options . produceReferenceInfo = true ; if ( options . complianceLevel >= ClassFileConstants . JDK1_6 && options . processAnnotations ) { initializeAnnotationProcessorManager ( newCompiler ) ; } return newCompiler ; } protected CompilationParticipantResult [ ] notifyParticipants ( SourceFile [ ] unitsAboutToCompile ) { CompilationParticipantResult [ ] results = new CompilationParticipantResult [ unitsAboutToCompile . length ] ; for ( int i = unitsAboutToCompile . length ; -- i >= 0 ; ) results [ i ] = new CompilationParticipantResult ( unitsAboutToCompile [ i ] ) ; for ( int i = 0 , l = this . javaBuilder . participants . length ; i < l ; i ++ ) this . javaBuilder . participants [ i ] . buildStarting ( results , this instanceof BatchImageBuilder ) ; SimpleSet uniqueFiles = null ; CompilationParticipantResult [ ] toAdd = null ; int added = 0 ; for ( int i = results . length ; -- i >= 0 ; ) { CompilationParticipantResult result = results [ i ] ; if ( result == null ) continue ; IFile [ ] deletedGeneratedFiles = result . deletedFiles ; if ( deletedGeneratedFiles != null ) deleteGeneratedFiles ( deletedGeneratedFiles ) ; IFile [ ] addedGeneratedFiles = result . addedFiles ; if ( addedGeneratedFiles != null ) { for ( int j = addedGeneratedFiles . length ; -- j >= 0 ; ) { SourceFile sourceFile = findSourceFile ( addedGeneratedFiles [ j ] , true ) ; if ( sourceFile == null ) continue ; if ( uniqueFiles == null ) { uniqueFiles = new SimpleSet ( unitsAboutToCompile . length + 3 ) ; for ( int f = unitsAboutToCompile . length ; -- f >= 0 ; ) uniqueFiles . add ( unitsAboutToCompile [ f ] ) ; } if ( uniqueFiles . addIfNotIncluded ( sourceFile ) == sourceFile ) { CompilationParticipantResult newResult = new CompilationParticipantResult ( sourceFile ) ; if ( toAdd == null ) { toAdd = new CompilationParticipantResult [ addedGeneratedFiles . length ] ; } else { int length = toAdd . length ; if ( added == length ) System . arraycopy ( toAdd , 0 , toAdd = new CompilationParticipantResult [ length + addedGeneratedFiles . length ] , 0 , length ) ; } toAdd [ added ++ ] = newResult ; } } } } if ( added > 0 ) { int length = results . length ; System . arraycopy ( results , 0 , results = new CompilationParticipantResult [ length + added ] , 0 , length ) ; System . arraycopy ( toAdd , 0 , results , length , added ) ; } return results ; } protected abstract void processAnnotationResults ( CompilationParticipantResult [ ] results ) ; protected void processAnnotations ( CompilationParticipantResult [ ] results ) { boolean hasAnnotationProcessor = false ; for ( int i = 0 , l = this . javaBuilder . participants . length ; ! hasAnnotationProcessor && i < l ; i ++ ) hasAnnotationProcessor = this . javaBuilder . participants [ i ] . isAnnotationProcessor ( ) ; if ( ! hasAnnotationProcessor ) return ; boolean foundAnnotations = this . filesWithAnnotations != null && this . filesWithAnnotations . elementSize > 0 ; for ( int i = results . length ; -- i >= 0 ; ) results [ i ] . reset ( foundAnnotations && this . filesWithAnnotations . includes ( results [ i ] . sourceFile ) ) ; for ( int i = 0 , l = this . javaBuilder . participants . length ; i < l ; i ++ ) if ( this . javaBuilder . participants [ i ] . isAnnotationProcessor ( ) ) this . javaBuilder . participants [ i ] . processAnnotations ( results ) ; processAnnotationResults ( results ) ; } protected void recordParticipantResult ( CompilationParticipantResult result ) { CategorizedProblem [ ] problems = result . problems ; if ( problems != null && problems . length > 0 ) { this . notifier . updateProblemCounts ( problems ) ; try { storeProblemsFor ( result . sourceFile , problems ) ; } catch ( CoreException e ) { Util . log ( e , "" ) ; } } String [ ] dependencies = result . dependencies ; if ( dependencies != null ) { ReferenceCollection refs = ( ReferenceCollection ) this . newState . references . get ( result . sourceFile . typeLocator ( ) ) ; if ( refs != null ) refs . addDependencies ( dependencies ) ; } } protected void storeProblemsFor ( SourceFile sourceFile , CategorizedProblem [ ] problems ) throws CoreException { if ( sourceFile == null || problems == null || problems . length == 0 ) return ; if ( ! this . keepStoringProblemMarkers ) return ; IResource resource = sourceFile . resource ; HashSet managedMarkerTypes = JavaModelManager . getJavaModelManager ( ) . compilationParticipants . managedMarkerTypes ( ) ; for ( int i = 0 , l = problems . length ; i < l ; i ++ ) { CategorizedProblem problem = problems [ i ] ; int id = problem . getID ( ) ; if ( id == IProblem . IsClassPathCorrect ) { String missingClassfileName = problem . getArguments ( ) [ 0 ] ; if ( JavaBuilder . DEBUG ) System . out . println ( Messages . bind ( Messages . build_incompleteClassPath , missingClassfileName ) ) ; boolean isInvalidClasspathError = JavaCore . ERROR . equals ( this . javaBuilder . javaProject . getOption ( JavaCore . CORE_INCOMPLETE_CLASSPATH , true ) ) ; if ( isInvalidClasspathError && JavaCore . ABORT . equals ( this . javaBuilder . javaProject . getOption ( JavaCore . CORE_JAVA_BUILD_INVALID_CLASSPATH , true ) ) ) { JavaBuilder . removeProblemsAndTasksFor ( this . javaBuilder . currentProject ) ; this . keepStoringProblemMarkers = false ; } IMarker marker = this . javaBuilder . currentProject . createMarker ( IJavaModelMarker . JAVA_MODEL_PROBLEM_MARKER ) ; marker . setAttributes ( new String [ ] { IMarker . MESSAGE , IMarker . SEVERITY , IJavaModelMarker . CATEGORY_ID , IMarker . SOURCE_ID } , new Object [ ] { Messages . bind ( Messages . build_incompleteClassPath , missingClassfileName ) , new Integer ( isInvalidClasspathError ? IMarker . SEVERITY_ERROR : IMarker . SEVERITY_WARNING ) , new Integer ( CategorizedProblem . CAT_BUILDPATH ) , JavaBuilder . SOURCE_ID } ) ; } String markerType = problem . getMarkerType ( ) ; boolean managedProblem = false ; if ( IJavaModelMarker . JAVA_MODEL_PROBLEM_MARKER . equals ( markerType ) || ( managedProblem = managedMarkerTypes . contains ( markerType ) ) ) { IMarker marker = resource . createMarker ( markerType ) ; String [ ] attributeNames = JAVA_PROBLEM_MARKER_ATTRIBUTE_NAMES ; int standardLength = attributeNames . length ; String [ ] allNames = attributeNames ; int managedLength = managedProblem ? 0 : 1 ; String [ ] extraAttributeNames = problem . getExtraMarkerAttributeNames ( ) ; int extraLength = extraAttributeNames == null ? 0 : extraAttributeNames . length ; if ( managedLength > 0 || extraLength > 0 ) { allNames = new String [ standardLength + managedLength + extraLength ] ; System . arraycopy ( attributeNames , 0 , allNames , 0 , standardLength ) ; if ( managedLength > 0 ) allNames [ standardLength ] = IMarker . SOURCE_ID ; System . arraycopy ( extraAttributeNames , 0 , allNames , standardLength + managedLength , extraLength ) ; } Object [ ] allValues = new Object [ allNames . length ] ; int index = 0 ; allValues [ index ++ ] = problem . getMessage ( ) ; allValues [ index ++ ] = problem . isError ( ) ? S_ERROR : S_WARNING ; allValues [ index ++ ] = new Integer ( id ) ; allValues [ index ++ ] = new Integer ( problem . getSourceStart ( ) ) ; int end = problem . getSourceEnd ( ) ; allValues [ index ++ ] = new Integer ( end > 0 ? end + 1 : end ) ; allValues [ index ++ ] = new Integer ( problem . getSourceLineNumber ( ) ) ; allValues [ index ++ ] = Util . getProblemArgumentsForMarker ( problem . getArguments ( ) ) ; allValues [ index ++ ] = new Integer ( problem . getCategoryID ( ) ) ; if ( managedLength > 0 ) allValues [ index ++ ] = JavaBuilder . SOURCE_ID ; if ( extraLength > 0 ) System . arraycopy ( problem . getExtraMarkerAttributeValues ( ) , 0 , allValues , index , extraLength ) ; marker . setAttributes ( allNames , allValues ) ; if ( ! this . keepStoringProblemMarkers ) return ; } } } protected void storeTasksFor ( SourceFile sourceFile , CategorizedProblem [ ] tasks ) throws CoreException { if ( sourceFile == null || tasks == null || tasks . length == 0 ) return ; IResource resource = sourceFile . resource ; for ( int i = 0 , l = tasks . length ; i < l ; i ++ ) { CategorizedProblem task = tasks [ i ] ; if ( task . getID ( ) == IProblem . Task ) { IMarker marker = resource . createMarker ( IJavaModelMarker . TASK_MARKER ) ; Integer priority = P_NORMAL ; String compilerPriority = task . getArguments ( ) [ 2 ] ; if ( JavaCore . COMPILER_TASK_PRIORITY_HIGH . equals ( compilerPriority ) ) priority = P_HIGH ; else if ( JavaCore . COMPILER_TASK_PRIORITY_LOW . equals ( compilerPriority ) ) priority = P_LOW ; String [ ] attributeNames = JAVA_TASK_MARKER_ATTRIBUTE_NAMES ; int standardLength = attributeNames . length ; String [ ] allNames = attributeNames ; String [ ] extraAttributeNames = task . getExtraMarkerAttributeNames ( ) ; int extraLength = extraAttributeNames == null ? 0 : extraAttributeNames . length ; if ( extraLength > 0 ) { allNames = new String [ standardLength + extraLength ] ; System . arraycopy ( attributeNames , 0 , allNames , 0 , standardLength ) ; System . arraycopy ( extraAttributeNames , 0 , allNames , standardLength , extraLength ) ; } Object [ ] allValues = new Object [ allNames . length ] ; int index = 0 ; allValues [ index ++ ] = task . getMessage ( ) ; allValues [ index ++ ] = priority ; allValues [ index ++ ] = new Integer ( task . getID ( ) ) ; allValues [ index ++ ] = new Integer ( task . getSourceStart ( ) ) ; allValues [ index ++ ] = new Integer ( task . getSourceEnd ( ) + 1 ) ; allValues [ index ++ ] = new Integer ( task . getSourceLineNumber ( ) ) ; allValues [ index ++ ] = Boolean . FALSE ; allValues [ index ++ ] = JavaBuilder . SOURCE_ID ; if ( extraLength > 0 ) System . arraycopy ( task . getExtraMarkerAttributeValues ( ) , 0 , allValues , index , extraLength ) ; marker . setAttributes ( allNames , allValues ) ; } } } protected void updateProblemsFor ( SourceFile sourceFile , CompilationResult result ) throws CoreException { CategorizedProblem [ ] problems = result . getProblems ( ) ; if ( problems == null || problems . length == 0 ) return ; this . notifier . updateProblemCounts ( problems ) ; storeProblemsFor ( sourceFile , problems ) ; } protected void updateTasksFor ( SourceFile sourceFile , CompilationResult result ) throws CoreException { CategorizedProblem [ ] tasks = result . getTasks ( ) ; if ( tasks == null || tasks . length == 0 ) return ; storeTasksFor ( sourceFile , tasks ) ; } protected char [ ] writeClassFile ( ClassFile classFile , SourceFile compilationUnit , boolean isTopLevelType ) throws CoreException { String fileName = new String ( classFile . fileName ( ) ) ; IPath filePath = new Path ( fileName ) ; IContainer outputFolder = compilationUnit . sourceLocation . binaryFolder ; IContainer container = outputFolder ; if ( filePath . segmentCount ( ) > 1 ) { container = createFolder ( filePath . removeLastSegments ( 1 ) , outputFolder ) ; filePath = new Path ( filePath . lastSegment ( ) ) ; } IFile file = container . getFile ( filePath . addFileExtension ( SuffixConstants . EXTENSION_class ) ) ; writeClassFileContents ( classFile , file , fileName , isTopLevelType , compilationUnit ) ; return filePath . lastSegment ( ) . toCharArray ( ) ; } protected void writeClassFileContents ( ClassFile classFile , IFile file , String qualifiedFileName , boolean isTopLevelType , SourceFile compilationUnit ) throws CoreException { InputStream input = new ByteArrayInputStream ( classFile . getBytes ( ) ) ; if ( file . exists ( ) ) { if ( JavaBuilder . DEBUG ) System . out . println ( "" + file . getName ( ) ) ; if ( ! file . isDerived ( ) ) file . setDerived ( true , null ) ; file . setContents ( input , true , false , null ) ; } else { if ( JavaBuilder . DEBUG ) System . out . println ( "" + file . getName ( ) ) ; file . create ( input , IResource . FORCE | IResource . DERIVED , null ) ; } } } </s>