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

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

Introduction

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

Prototype

@Override
    public char[] getMainTypeName() 

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   www.jav  a  2 s  .  c  om*/
            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.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   ww  w .  jav a  2s.co 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
        }
    }
}