Example usage for org.eclipse.jdt.core JavaConventions validateClasspath

List of usage examples for org.eclipse.jdt.core JavaConventions validateClasspath

Introduction

In this page you can find the example usage for org.eclipse.jdt.core JavaConventions validateClasspath.

Prototype

public static IJavaModelStatus validateClasspath(IJavaProject javaProject, IClasspathEntry[] rawClasspath,
        IPath projectOutputLocation) 

Source Link

Document

Validate a given classpath and output location for a project, using the following rules:
  • Classpath entries cannot collide with each other; that is, all entry paths must be unique.

    Usage

    From source file:com.redhat.ceylon.eclipse.code.preferences.CeylonBuildPathsBlock.java

    License:Open Source License

    private void updateBuildPathStatus() {
        List<CPListElement> elements = fClassPathList.getElements();
        IClasspathEntry[] entries = new IClasspathEntry[elements.size()];
    
        for (int i = elements.size() - 1; i >= 0; i--) {
            CPListElement currElement = elements.get(i);
            entries[i] = currElement.getClasspathEntry();
        }// ww  w.  j a v  a  2 s.c o m
    
        IJavaModelStatus status = JavaConventions.validateClasspath(fCurrJProject, entries,
                fJavaOutputLocationPath);
        if (!status.isOK()) {
            fBuildPathStatus.setError(status.getMessage());
            return;
        }
        fBuildPathStatus.setOK();
    }
    

    From source file:com.redhat.ceylon.eclipse.code.wizard.AddResourceFolderWizardPage.java

    License:Open Source License

    private StatusInfo updateRootStatus() {
        IJavaProject javaProject = fNewElement.getJavaProject();
        IProject project = javaProject.getProject();
    
        StatusInfo pathNameStatus = validatePathName(fRootDialogField.getText(), fParent);
    
        if (!pathNameStatus.isOK())
            return pathNameStatus;
    
        if (fLinkedMode) {
            IStatus linkNameStatus = validateLinkLocation(fRootDialogField.getText());
            if (linkNameStatus.matches(IStatus.ERROR)) {
                StatusInfo result = new StatusInfo();
                result.setError(linkNameStatus.getMessage());
                return result;
            }//from  w w  w  .j  av a 2s  .  c  o  m
        }
    
        StatusInfo result = new StatusInfo();
        result.setOK();
    
        IPath projPath = project.getFullPath();
        IPath path = fParent.getFullPath().append(fRootDialogField.getText());
    
        restoreCPElements();
    
        int projectEntryIndex = -1;
        boolean createFolderForExisting = false;
    
        IFolder folder = fParent.getFolder(new Path(fRootDialogField.getText()));
        for (int i = 0; i < fExistingEntries.size(); i++) {
            IClasspathEntry curr = fExistingEntries.get(i).getClasspathEntry();
            if (curr.getEntryKind() == IClasspathEntry.CPE_SOURCE) {
                if (path.equals(curr.getPath()) && fExistingEntries.get(i) != fNewElement) {
                    if (folder.exists()) {
                        result.setError("The folder is already a source or resource folder.");
                        return result;
                    } else {
                        createFolderForExisting = true;
                    }
                }
                if (projPath.equals(curr.getPath())) {
                    projectEntryIndex = i;
                }
            }
        }
    
        if (folder.exists() && !folder.getFullPath().equals(fOrginalPath))
            return new StatusInfo(IStatus.ERROR,
                    Messages.format(NewWizardMessages.NewFolderDialog_folderNameEmpty_alreadyExists,
                            BasicElementLabels.getPathLabel(folder.getFullPath(), false)));
    
        boolean isProjectASourceFolder = projectEntryIndex != -1;
    
        fModifiedElements.clear();
        updateFilters(fNewElement.getPath(), path);
    
        fNewElement.setPath(path);
        if (fLinkedMode) {
            fNewElement.setLinkTarget(fLinkFields.getLinkTarget());
        }
        fRemovedElements.clear();
        Set<CPListElement> modified = new HashSet<CPListElement>();
        boolean isProjectSourceFolderReplaced = false;
        if (fAddExclusionPatterns.isSelected()) {
            if (fOrginalPath == null) {
                addExclusionPatterns(fNewElement, fExistingEntries, modified);
                fModifiedElements.addAll(modified);
                if (!createFolderForExisting)
                    CPListElement.insert(fNewElement, fExistingEntries);
            }
        } else {
            if (isProjectASourceFolder) {
                if (fRemoveProjectFolder.isSelected()) {
                    fOldProjectSourceFolder = fExistingEntries.get(projectEntryIndex);
                    fRemovedElements.add(fOldProjectSourceFolder);
                    fExistingEntries.set(projectEntryIndex, fNewElement);
                    isProjectSourceFolderReplaced = true;
                } else {
                    if (!createFolderForExisting)
                        CPListElement.insert(fNewElement, fExistingEntries);
                }
            } else {
                if (!createFolderForExisting)
                    CPListElement.insert(fNewElement, fExistingEntries);
            }
        }
    
        if ((!fAllowConflict && fCanCommitConflictingBuildpath) || createFolderForExisting)
            return new StatusInfo();
    
        fNewOutputLocation = null;
        IJavaModelStatus status = JavaConventions.validateClasspath(javaProject,
                CPListElement.convertToClasspathEntries(fExistingEntries), fOutputLocation);
        if (!status.isOK()) {
            if (fOutputLocation.equals(projPath)) {
                //Try to change the output folder
                fNewOutputLocation = projPath.append(
                        PreferenceConstants.getPreferenceStore().getString(PreferenceConstants.SRCBIN_BINNAME));
                IStatus status2 = JavaConventions.validateClasspath(javaProject,
                        CPListElement.convertToClasspathEntries(fExistingEntries), fNewOutputLocation);
                if (status2.isOK()) {
                    if (isProjectSourceFolderReplaced) {
                        result.setInfo(
                                Messages.format(NewWizardMessages.NewSourceFolderWizardPage_warning_ReplaceSFandOL,
                                        BasicElementLabels.getPathLabel(fNewOutputLocation, false)));
                    } else {
                        result.setInfo(
                                Messages.format(NewWizardMessages.NewSourceFolderWizardPage_warning_ReplaceOL,
                                        BasicElementLabels.getPathLabel(fNewOutputLocation, false)));
                    }
                    return result;
                }
            }
            //Don't know what the problem is, report to user
            fNewOutputLocation = null;
            if (fCanCommitConflictingBuildpath) {
                result.setInfo(NewWizardMessages.AddSourceFolderWizardPage_conflictWarning + status.getMessage());
            } else {
                result.setError(status.getMessage());
            }
            return result;
        }
        if (!modified.isEmpty()) {
            //Added exclusion patterns to solve problem
            if (modified.size() == 1) {
                CPListElement elem = (CPListElement) modified.toArray()[0];
                String changed = BasicElementLabels.getPathLabel(elem.getPath(), false);
                String excl = BasicElementLabels.getPathLabel(fNewElement.getPath(), false);
                result.setInfo(Messages.format(NewWizardMessages.AddSourceFolderWizardPage_addSinglePattern,
                        new Object[] { excl, changed }));
            } else {
                result.setInfo(
                        Messages.format(NewWizardMessages.NewSourceFolderWizardPage_warning_AddedExclusions_plural,
                                String.valueOf(modified.size())));
            }
            return result;
        }
        if (isProjectSourceFolderReplaced) {
            result.setInfo(NewWizardMessages.AddSourceFolderWizardPage_replaceSourceFolderInfo);
            return result;
        }
    
        return result;
    }
    

    From source file:com.redhat.ceylon.eclipse.code.wizard.ClassPathDetector.java

    License:Open Source License

    /**
     * Method detectClasspath./*from   ww w.ja v  a 2 s.  c  om*/
     * 
     * @param monitor The progress monitor (not null)
     * @throws CoreException in case of any failure
     */
    private void detectClasspath(IProgressMonitor monitor) throws CoreException {
        try {
            monitor.beginTask("Detecting Ceylon project paths", 4);
    
            fMonitor = monitor;
            fProject.accept(this, IResource.NONE);
            monitor.worked(1);
    
            ArrayList<IClasspathEntry> cpEntries = new ArrayList<IClasspathEntry>();
    
            detectSourceFolders(cpEntries);
            if (monitor.isCanceled()) {
                throw new OperationCanceledException();
            }
            monitor.worked(1);
    
            IPath outputLocation = detectOutputFolder();
            if (monitor.isCanceled()) {
                throw new OperationCanceledException();
            }
            monitor.worked(1);
    
            detectLibraries(cpEntries, outputLocation);
            if (monitor.isCanceled()) {
                throw new OperationCanceledException();
            }
            monitor.worked(1);
    
            if (cpEntries.isEmpty() && fClassFiles.isEmpty()) {
                return;
            }
            IClasspathEntry[] jreEntries = PreferenceConstants.getDefaultJRELibrary();
            for (int i = 0; i < jreEntries.length; i++) {
                cpEntries.add(jreEntries[i]);
            }
    
            IClasspathEntry[] entries = cpEntries.toArray(new IClasspathEntry[cpEntries.size()]);
            if (!JavaConventions.validateClasspath(JavaCore.create(fProject), entries, outputLocation).isOK()) {
                return;
            }
    
            fResultClasspath = entries;
            fResultOutputFolder = outputLocation;
        } finally {
            monitor.done();
        }
    }
    

    From source file:com.siteview.mde.internal.core.ClasspathComputer.java

    License:Open Source License

    public static IClasspathEntry[] getClasspath(IProject project, IMonitorModelBase model, Map sourceLibraryMap,
            boolean clear, boolean overrideCompliance) throws CoreException {
        IJavaProject javaProject = JavaCore.create(project);
        ArrayList result = new ArrayList();
        IBuild build = getBuild(project);//from  ww  w .ja  va 2 s  .  com
    
        // add JRE and set compliance options
        String ee = getExecutionEnvironment(model.getBundleDescription());
        result.add(createEntryUsingPreviousEntry(javaProject, ee, MDECore.JRE_CONTAINER_PATH));
        setComplianceOptions(JavaCore.create(project), ee, overrideCompliance);
    
        // add pde container
        result.add(createEntryUsingPreviousEntry(javaProject, ee, MDECore.REQUIRED_PLUGINS_CONTAINER_PATH));
    
        // add own libraries/source
        addSourceAndLibraries(project, model, build, clear, sourceLibraryMap, result);
    
        IClasspathEntry[] entries = (IClasspathEntry[]) result.toArray(new IClasspathEntry[result.size()]);
        IJavaModelStatus validation = JavaConventions.validateClasspath(javaProject, entries,
                javaProject.getOutputLocation());
        if (!validation.isOK()) {
            MDECore.logErrorMessage(validation.getMessage());
            throw new CoreException(validation);
        }
        return (IClasspathEntry[]) result.toArray(new IClasspathEntry[result.size()]);
    }
    

    From source file:org.eclim.plugin.jdt.project.JavaProjectManager.java

    License:Open Source License

    /**
     * Sets the classpath for the supplied project.
     *
     * @param javaProject The project./*w w w . j a va  2s  .  co m*/
     * @param entries The classpath entries.
     * @param classpath The file path of the .classpath file.
     * @return Array of Error or null if no errors reported.
     */
    protected List<Error> setClasspath(IJavaProject javaProject, IClasspathEntry[] entries, String classpath)
            throws Exception {
        FileOffsets offsets = FileOffsets.compile(classpath);
        String classpathValue = IOUtils.toString(new FileInputStream(classpath));
        ArrayList<Error> errors = new ArrayList<Error>();
        for (IClasspathEntry entry : entries) {
            IJavaModelStatus status = JavaConventions.validateClasspathEntry(javaProject, entry, true);
            if (!status.isOK()) {
                errors.add(createErrorForEntry(javaProject, entry, status, offsets, classpath, classpathValue));
            }
        }
    
        IJavaModelStatus status = JavaConventions.validateClasspath(javaProject, entries,
                javaProject.getOutputLocation());
    
        // always set the classpathValue anyways, so that the user can correct the
        // file.
        //if(status.isOK() && errors.isEmpty()){
        javaProject.setRawClasspath(entries, null);
        javaProject.makeConsistent(null);
        //}
    
        if (!status.isOK()) {
            errors.add(new Error(status.getMessage(), classpath, 1, 1, false));
        }
        return errors;
    }
    

    From source file:org.eclipse.ajdt.internal.ui.wizards.PathBlock.java

    License:Open Source License

    /**
     * checks to see if the path is well-formed,
     * duplicates, etc.// ww w.  j  a v  a 2  s .c o  m
     */
    protected void updateJavaBuildPathStatus() {
        List elements = fPathList.getElements();
        List /* IClasspathEntry */<IClasspathEntry> entries = new ArrayList<IClasspathEntry>();
    
        for (int i = elements.size() - 1; i >= 0; i--) {
            CPListElement currElement = (CPListElement) elements.get(i);
            // ignore elements that are part of a container 
            // since user does not have direct control over removing them
            if (!inClasspathContainer(currElement)) {
                entries.add(currElement.getClasspathEntry());
            }
        }
    
        IPath outPath;
        try {
            outPath = fCurrJProject.getOutputLocation();
        } catch (JavaModelException e) {
            outPath = fCurrJProject.getPath();
        }
    
        IClasspathEntry[] entriesArr = entries.toArray(new IClasspathEntry[0]);
        IJavaModelStatus status = JavaConventions.validateClasspath(fCurrJProject, entriesArr, outPath);
    
        if (!status.isOK()) {
            fJavaBuildPathStatus.setError(status.getMessage());
            return;
        }
    
        IJavaModelStatus dupStatus = checkForDuplicates(fCurrJProject, entriesArr);
        if (!dupStatus.isOK()) {
            fJavaBuildPathStatus.setError(dupStatus.getMessage());
            return;
        }
    
        fJavaBuildPathStatus.setOK();
    }
    

    From source file:org.eclipse.pde.internal.core.ClasspathComputer.java

    License:Open Source License

    public static IClasspathEntry[] getClasspath(IProject project, IPluginModelBase model,
            Map<?, ?> sourceLibraryMap, boolean clear, boolean overrideCompliance) throws CoreException {
        IJavaProject javaProject = JavaCore.create(project);
        ArrayList<IClasspathEntry> result = new ArrayList<IClasspathEntry>();
        IBuild build = getBuild(project);/*w  ww .  j a  v  a  2s  .  co m*/
    
        // add JRE and set compliance options
        String ee = getExecutionEnvironment(model.getBundleDescription());
        result.add(createEntryUsingPreviousEntry(javaProject, ee, PDECore.JRE_CONTAINER_PATH));
        setComplianceOptions(JavaCore.create(project), ee, overrideCompliance);
    
        // add pde container
        result.add(createEntryUsingPreviousEntry(javaProject, ee, PDECore.REQUIRED_PLUGINS_CONTAINER_PATH));
    
        // add own libraries/source
        addSourceAndLibraries(project, model, build, clear, sourceLibraryMap, result);
    
        IClasspathEntry[] entries = result.toArray(new IClasspathEntry[result.size()]);
        IJavaModelStatus validation = JavaConventions.validateClasspath(javaProject, entries,
                javaProject.getOutputLocation());
        if (!validation.isOK()) {
            PDECore.logErrorMessage(validation.getMessage());
            throw new CoreException(validation);
        }
        return result.toArray(new IClasspathEntry[result.size()]);
    }
    

    From source file:org.hyperic.hypclipse.internal.ClasspathComputer.java

    License:Open Source License

    public static IClasspathEntry[] getClasspath(IProject project, IPluginModelBase model, boolean clear,
            boolean overrideCompliance) throws CoreException {
        IJavaProject javaProject = JavaCore.create(project);
        ArrayList<IClasspathEntry> result = new ArrayList<IClasspathEntry>();
        IBuild build = getBuild(project);//  www  . j a v  a  2 s. co  m
    
        //      // add JRE and set compliance options
        //      String ee = getExecutionEnvironment(model.getBundleDescription());
        //      result.add(createEntryUsingPreviousEntry(javaProject, ee, PDECore.JRE_CONTAINER_PATH));
        //      setComplianceOptions(JavaCore.create(project), ExecutionEnvironmentAnalyzer.getCompliance(ee), overrideCompliance);
        //
        //      // add pde container
        //      result.add(createEntryUsingPreviousEntry(javaProject, ee, PDECore.REQUIRED_PLUGINS_CONTAINER_PATH));
        //
        //      // add own libraries/source
        //      addSourceAndLibraries(project, model, build, clear, result);
        //
        IClasspathEntry[] entries = result.toArray(new IClasspathEntry[0]);
        IJavaModelStatus validation = JavaConventions.validateClasspath(javaProject, entries,
                javaProject.getOutputLocation());
        if (!validation.isOK()) {
            HQDEPlugin.logErrorMessage(validation.getMessage());
            throw new CoreException(validation);
        }
        return result.toArray(new IClasspathEntry[0]);
    }
    

    From source file:org.jboss.tools.seam.internal.core.project.facet.WtpUtils.java

    License:Open Source License

    public static IResource createSourceFolder(IProject project, IPath path, IPath exclude, IPath outputFolder) {
        IJavaProject javaProject;/*w ww . j  av  a 2  s  .co  m*/
        IClasspathEntry[] javaProjectEntries;
        IPath outputLocation;
    
        if (project == null || !project.exists()) {
            return null;
        }
        if (!project.isOpen()) {
            return null;
        }
        try {
            if (!project.hasNature(JavaCore.NATURE_ID))
                return null;
    
            javaProject = JavaCore.create(project);
            javaProjectEntries = javaProject.getRawClasspath();
            outputLocation = javaProject.getOutputLocation();
    
            IPath projPath = javaProject.getProject().getFullPath();
            IPath newSourceFolderPath = projPath.append(path);
            IPath excludeSourceFolderPath = projPath.append(exclude);
    
            ArrayList<IClasspathEntry> newEntries = new ArrayList<IClasspathEntry>(javaProjectEntries.length + 1);
            int projectEntryIndex = -1;
    
            for (int i = 0; i < javaProjectEntries.length; i++) {
                IClasspathEntry curr = javaProjectEntries[i];
                IClasspathEntry resolved = curr;
                if (resolved.getEntryKind() == IClasspathEntry.CPE_VARIABLE) {
                    try {
                        resolved = JavaCore.getResolvedClasspathEntry(resolved);
                    } catch (AssertionFailedException e) {
                        continue;
                    }
                }
                if (resolved.getEntryKind() == IClasspathEntry.CPE_SOURCE) {
                    if (newSourceFolderPath.equals(resolved.getPath())) {
                        return null;
                    }
                    if (projPath.equals(resolved.getPath())) {
                        projectEntryIndex = i;
                    }
                    if (excludeSourceFolderPath.equals(resolved.getPath())) {
                        continue;
                    }
                }
                newEntries.add(curr);
            }
            if (outputFolder != null) {
                CoreUtility.createDerivedFolder(project.getFolder(outputFolder), true, true,
                        new NullProgressMonitor());
            }
            IFolder newSourceFolder = javaProject.getProject().getFolder(path);
            if (!newSourceFolder.exists()) {
                CoreUtility.createFolder(newSourceFolder, true, true, new NullProgressMonitor());
            }
    
            IClasspathEntry newEntry = JavaCore.newSourceEntry(newSourceFolderPath, new Path[] {}, new Path[] {},
                    outputFolder != null ? project.getFullPath().append(outputFolder) : null);
    
            if (projectEntryIndex != -1) {
                newEntries.set(projectEntryIndex, newEntry);
            } else {
                insertClasspathEntry(newEntry, newEntries);
            }
    
            IClasspathEntry[] newClasspathEntries = newEntries.toArray(new IClasspathEntry[newEntries.size()]);
            IPath newOutputLocation = outputLocation;
    
            IJavaModelStatus result = JavaConventions.validateClasspath(javaProject, newClasspathEntries,
                    newOutputLocation);
            if (!result.isOK()) {
                if (outputLocation.equals(projPath)) {
                    newOutputLocation = projPath.append(
                            PreferenceConstants.getPreferenceStore().getString(PreferenceConstants.SRCBIN_BINNAME));
                    result = JavaConventions.validateClasspath(javaProject, newClasspathEntries, newOutputLocation);
                    if (!result.isOK()) {
                        return null;
                    }
                } else {
                    return null;
                }
            }
    
            javaProject.setRawClasspath(newClasspathEntries, newOutputLocation, new NullProgressMonitor());
            return newSourceFolder;
        } catch (CoreException e) {
            SeamCorePlugin.getPluginLog().logError(e);
        }
        return null;
    }