Example usage for org.eclipse.jdt.internal.core.builder SourceFile typeLocator

List of usage examples for org.eclipse.jdt.internal.core.builder SourceFile typeLocator

Introduction

In this page you can find the example usage for org.eclipse.jdt.internal.core.builder SourceFile typeLocator.

Prototype

String typeLocator() 

Source Link

Usage

From source file:org.eclipse.jdt.internal.core.builder.AbstractImageBuilder.java

License:Open Source License

public void acceptResult(CompilationResult result) {
    // In Batch mode, we write out the class files, hold onto the dependency info
    // & additional types and report problems.

    // In Incremental mode, when writing out a class file we need to compare it
    // against the previous file, remembering if structural changes occured.
    // Before reporting the new problems, we need to update the problem count &
    // remove the old problems. Plus delete additional class files that no longer exist.

    SourceFile compilationUnit = (SourceFile) result.getCompilationUnit(); // go directly back to the sourceFile
    if (!this.workQueue.isCompiled(compilationUnit)) {
        this.workQueue.finished(compilationUnit);

        try {/*from  ww w.j a  v a 2s . c o m*/
            updateProblemsFor(compilationUnit, result); // record compilation problems before potentially adding duplicate errors
            updateTasksFor(compilationUnit, result); // record tasks
        } catch (CoreException e) {
            throw internalException(e);
        }

        if (result.hasInconsistentToplevelHierarchies)
            // ensure that this file is always retrieved from source for the rest of the build
            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;

            // Look for a possible collision, if one exists, report an error but do not write the class file
            if (isNestedType) {
                String qualifiedTypeName = new String(classFile.outerMostEnclosingClassFile().fileName());
                if (this.newState.isDuplicateLocator(qualifiedTypeName, typeLocator))
                    continue;
            } else {
                String qualifiedTypeName = new String(classFile.fileName()); // the qualified type name "p1/p2/A"
                if (this.newState.isDuplicateLocator(qualifiedTypeName, typeLocator)) {
                    if (duplicateTypeNames == null)
                        duplicateTypeNames = new ArrayList();
                    duplicateTypeNames.add(compoundName);
                    if (mainType == null) {
                        try {
                            mainTypeName = compilationUnit.initialTypeName; // slash separated qualified name "p1/p1/A"
                            mainType = this.javaBuilder.javaProject.findType(mainTypeName.replace('/', '.'));
                        } catch (JavaModelException e) {
                            // ignore
                        }
                    }
                    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, "JavaBuilder handling CoreException"); //$NON-NLS-1$
                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) // only initialized if an annotation processor is attached
            this.filesWithAnnotations.add(compilationUnit);

        this.compiler.lookupEnvironment.releaseClassFiles(classFiles);
        finishedWith(typeLocator, result, compilationUnit.getMainTypeName(), definedTypeNames,
                duplicateTypeNames);
        this.notifier.compiled(compilationUnit);
    }
}

From source file:org.eclipse.jdt.internal.core.builder.AbstractImageBuilder.java

License:Open Source License

protected void compile(SourceFile[] units) {
    if (this.filesWithAnnotations != null && this.filesWithAnnotations.elementSize > 0)
        // will add files that have annotations in acceptResult() & then processAnnotations() before exitting this method
        this.filesWithAnnotations.clear();

    // notify CompilationParticipants which source files are about to be compiled
    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;
    }/*www.  j ava 2 s  . c  om*/

    int unitsLength = units.length;
    this.compiledAllAtOnce = unitsLength <= MAX_AT_ONCE;

    // GROOVY start
    // currently can't easily fault in files from the other group.  Easier to
    // do this than fix that right now.
    if (this.compiler != null && this.compiler.options != null && this.compiler.options.buildGroovyFiles == 2) {
        // System.out.println("although more than "+MAX_AT_ONCE+" still compiling "+unitsLength+" files at once");
        this.compiledAllAtOnce = true;
    }
    // GROOVY end
    if (this.compiledAllAtOnce) {
        // do them all now
        if (JavaBuilder.DEBUG)
            for (int i = 0; i < unitsLength; i++)
                System.out.println("About to compile " + units[i].typeLocator()); //$NON-NLS-1$
        compile(units, null, true);
    } else {
        SourceFile[] remainingUnits = new SourceFile[unitsLength]; // copy of units, removing units when about to compile
        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) {
                // Although it needed compiling when this method was called, it may have
                // already been compiled when it was referenced by another unit.
                SourceFile unit = remainingUnits[remainingIndex];
                if (unit != null && (compilingFirstGroup || this.workQueue.isWaiting(unit))) {
                    if (JavaBuilder.DEBUG)
                        System.out.println("About to compile #" + remainingIndex + " : " + unit.typeLocator()); //$NON-NLS-1$ //$NON-NLS-2$
                    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; // use the class file for this source file since its been compiled
            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);
    }
}

From source file:org.eclipse.jdt.internal.core.builder.BatchImageBuilder.java

License:Open Source License

protected void storeProblemsFor(SourceFile sourceFile, CategorizedProblem[] problems) throws CoreException {
    if (sourceFile == null || problems == null || problems.length == 0)
        return;/* w  ww  .  j a  v a2s  . c  o m*/

    for (int i = problems.length; --i >= 0;) {
        CategorizedProblem problem = problems[i];
        if (problem != null && problem.getID() == IProblem.UndefinedType) {
            if (this.typeLocatorsWithUndefinedTypes == null)
                this.typeLocatorsWithUndefinedTypes = new StringSet(3);
            this.typeLocatorsWithUndefinedTypes.add(sourceFile.typeLocator());
            break;
        }
    }

    super.storeProblemsFor(sourceFile, problems);
}

From source file:org.eclipse.jdt.internal.core.builder.IncrementalImageBuilder.java

License:Open Source License

protected void compile(SourceFile[] units, SourceFile[] additionalUnits, boolean compilingFirstGroup) {
    if (compilingFirstGroup && additionalUnits != null) {
        // add any source file from additionalUnits to units if it defines secondary types
        // otherwise its possible during testing with MAX_AT_ONCE == 1 that a secondary type
        // can cause an infinite loop as it alternates between not found and defined, see bug 146324
        ArrayList extras = null;/*from  w  w  w  .  j  a  va2s .c om*/
        for (int i = 0, l = additionalUnits.length; i < l; i++) {
            SourceFile unit = additionalUnits[i];
            if (unit != null && this.newState.getDefinedTypeNamesFor(unit.typeLocator()) != null) {
                if (JavaBuilder.DEBUG)
                    System.out.println("About to compile file with secondary types " + unit.typeLocator()); //$NON-NLS-1$
                if (extras == null)
                    extras = new ArrayList(3);
                extras.add(unit);
            }
        }
        if (extras != null) {
            int oldLength = units.length;
            int toAdd = extras.size();
            System.arraycopy(units, 0, units = new SourceFile[oldLength + toAdd], 0, oldLength);
            for (int i = 0; i < toAdd; i++)
                units[oldLength++] = (SourceFile) extras.get(i);
        }
    }
    super.compile(units, additionalUnits, compilingFirstGroup);
}

From source file:org.eclipse.jdt.internal.core.builder.IncrementalImageBuilder.java

License:Open Source License

protected void deleteGeneratedFiles(IFile[] deletedGeneratedFiles) {
    // delete generated files and recompile any affected source files
    try {/*  w  w w .ja v a 2 s . c  om*/
        for (int j = deletedGeneratedFiles.length; --j >= 0;) {
            IFile deletedFile = deletedGeneratedFiles[j];
            if (deletedFile.exists())
                continue; // only delete .class files for source files that were actually deleted

            SourceFile sourceFile = findSourceFile(deletedFile, false);
            String typeLocator = sourceFile.typeLocator();
            int mdSegmentCount = sourceFile.sourceLocation.sourceFolder.getFullPath().segmentCount();
            IPath typePath = sourceFile.resource.getFullPath().removeFirstSegments(mdSegmentCount)
                    .removeFileExtension();
            addDependentsOf(typePath, true); // add dependents of the source file since its now deleted
            this.previousSourceFiles = null; // existing source files did not see it as deleted since they were compiled before it was
            char[][] definedTypeNames = this.newState.getDefinedTypeNamesFor(typeLocator);
            if (definedTypeNames == null) { // defined a single type matching typePath
                removeClassFile(typePath, sourceFile.sourceLocation.binaryFolder);
            } else {
                if (definedTypeNames.length > 0) { // skip it if it failed to successfully define a type
                    IPath packagePath = typePath.removeLastSegments(1);
                    for (int d = 0, l = definedTypeNames.length; d < l; d++)
                        removeClassFile(packagePath.append(new String(definedTypeNames[d])),
                                sourceFile.sourceLocation.binaryFolder);
                }
            }
            this.newState.removeLocator(typeLocator);
        }
    } catch (CoreException e) {
        // must continue with compile loop so just log the CoreException
        Util.log(e, "JavaBuilder logging CompilationParticipant's CoreException to help debugging"); //$NON-NLS-1$
    }
}

From source file:org.eclipse.jdt.internal.core.builder.IncrementalImageBuilder.java

License:Open Source License

/**
 * @see org.eclipse.jdt.internal.core.builder.AbstractImageBuilder#writeClassFileContents(org.eclipse.jdt.internal.compiler.ClassFile, org.eclipse.core.resources.IFile, java.lang.String, boolean, org.eclipse.jdt.internal.core.builder.SourceFile)
 *//*from w ww . j a v  a 2  s  .c o m*/
protected void writeClassFileContents(ClassFile classfile, IFile file, String qualifiedFileName,
        boolean isTopLevelType, SourceFile compilationUnit) throws CoreException {
    // Before writing out the class file, compare it to the previous file
    // If structural changes occurred then add dependent source files
    byte[] bytes = classfile.getBytes();
    if (file.exists()) {
        if (writeClassFileCheck(file, qualifiedFileName, bytes) || compilationUnit.updateClassFile) { // see 46093
            if (JavaBuilder.DEBUG)
                System.out.println("Writing changed class file " + file.getName());//$NON-NLS-1$
            if (!file.isDerived())
                file.setDerived(true, null);
            file.setContents(new ByteArrayInputStream(bytes), true, false, null);
        } else if (JavaBuilder.DEBUG) {
            System.out.println("Skipped over unchanged class file " + file.getName());//$NON-NLS-1$
        }
    } else {
        if (isTopLevelType)
            addDependentsOf(new Path(qualifiedFileName), true); // new type
        if (JavaBuilder.DEBUG)
            System.out.println("Writing new class file " + file.getName());//$NON-NLS-1$
        try {
            file.create(new ByteArrayInputStream(bytes), IResource.FORCE | IResource.DERIVED, null);
        } catch (CoreException e) {
            if (e.getStatus().getCode() == IResourceStatus.CASE_VARIANT_EXISTS) {
                IStatus status = e.getStatus();
                if (status instanceof IResourceStatus) {
                    IPath oldFilePath = ((IResourceStatus) status).getPath();
                    char[] oldTypeName = oldFilePath.removeFileExtension().lastSegment().toCharArray();
                    char[][] previousTypeNames = this.newState
                            .getDefinedTypeNamesFor(compilationUnit.typeLocator());
                    boolean fromSameFile = false;
                    if (previousTypeNames == null) {
                        fromSameFile = CharOperation.equals(compilationUnit.getMainTypeName(), oldTypeName);
                    } else {
                        for (int i = 0, l = previousTypeNames.length; i < l; i++) {
                            if (CharOperation.equals(previousTypeNames[i], oldTypeName)) {
                                fromSameFile = true;
                                break;
                            }
                        }
                    }
                    if (fromSameFile) {
                        // file is defined by the same compilationUnit, but won't be deleted until later so do it now
                        IFile collision = file.getParent().getFile(new Path(oldFilePath.lastSegment()));
                        collision.delete(true, false, null);
                        boolean success = false;
                        try {
                            file.create(new ByteArrayInputStream(bytes), IResource.FORCE | IResource.DERIVED,
                                    null);
                            success = true;
                        } catch (CoreException ignored) {
                            // ignore the second exception
                        }
                        if (success)
                            return;
                    }
                }
                // catch the case that a type has been renamed and collides on disk with an as-yet-to-be-deleted type
                throw new AbortCompilation(true, new AbortIncrementalBuildException(qualifiedFileName));
            }
            throw e; // rethrow
        }
    }
}