Example usage for org.eclipse.jdt.core IJavaProject getRawClasspath

List of usage examples for org.eclipse.jdt.core IJavaProject getRawClasspath

Introduction

In this page you can find the example usage for org.eclipse.jdt.core IJavaProject getRawClasspath.

Prototype

IClasspathEntry[] getRawClasspath() throws JavaModelException;

Source Link

Document

Returns the raw classpath for the project, as a list of classpath entries.

Usage

From source file:com.javadude.dependencies.actions.DOTReportAction.java

License:Open Source License

public void run(IAction action) {
    // create DOT definition
    String NL = System.getProperty("line.separator");
    String dot = "digraph dependencies {" + NL;
    try {/*from   w  ww.j  ava2s  . com*/
        for (IProject project : ResourcesPlugin.getWorkspace().getRoot().getProjects()) {
            if (project.isOpen() && project.hasNature(JavaCore.NATURE_ID)) {
                IJavaProject javaProject = JavaCore.create(project);
                for (IClasspathEntry entry : javaProject.getRawClasspath()) {
                    if (entry.getEntryKind() == IClasspathEntry.CPE_PROJECT) {
                        dot += '\t' + fixName(project.getName()) + " -> "
                                + fixName(entry.getPath().lastSegment()) + ';' + NL;
                    }
                }
            }
        }
        dot += "}";
        try {
            FileWriter fileWriter = new FileWriter("/dependencies.dot");
            fileWriter.write(dot);
            fileWriter.close();
        } catch (IOException e) {
            DependenciesPlugin.error(111, "Error writing dot file /dependencies.dot", e);
        }
    } catch (CoreException e) {
        DependenciesPlugin.error(222, "Error determining dependencies", e);
    }
}

From source file:com.legstar.eclipse.plugin.cixscom.wizards.AbstractCixsGeneratorWizardPage.java

License:Open Source License

/**
 * From a Java nature Eclipse project, this utility method retrieves either
 * a java source folder or an associated java output folder for binaries.
 * <p/>/*ww w . j ava 2 s.  com*/
 * The algorithm stops at the first source directory encountered.
 * 
 * @param project the current Eclipse project
 * @param source true if we are looking for a source folder, false for an
 *            output folder
 * @return a java folder (either source or output)
 */
protected File getJavaDir(final IProject project, final boolean source) {
    IJavaProject javaProject = JavaCore.create(project);
    if (javaProject != null) {
        IPath rootPath = javaProject.getProject().getLocation().removeLastSegments(1);

        /*
         * If this is a java project, get first Java source and output
         * folders.
         */
        try {
            IClasspathEntry[] cpe = javaProject.getRawClasspath();
            for (int i = 0; i < cpe.length; i++) {
                if (cpe[i].getEntryKind() == IClasspathEntry.CPE_SOURCE) {
                    if (source) {
                        return rootPath.append(cpe[i].getPath()).toFile();
                    } else {
                        if (cpe[i].getOutputLocation() == null) {
                            return rootPath.append(javaProject.getOutputLocation()).toFile();
                        } else {
                            return rootPath.append(cpe[i].getOutputLocation()).toFile();
                        }
                    }
                }
            }
        } catch (JavaModelException e) {
            AbstractWizard.errorDialog(getShell(), Messages.generate_error_dialog_title, getPluginId(),
                    Messages.java_location_lookup_failure_msg,
                    NLS.bind(Messages.invalid_java_project_msg, project.getName(), e.getMessage()));
            AbstractWizard.logCoreException(e, getPluginId());
        }
    }
    /*
     * If everything else failed, assume generated artifacts will go to the
     * project root.
     */
    return project.getLocation().toFile();
}

From source file:com.legstar.eclipse.plugin.common.wizards.AbstractWizardPage.java

License:Open Source License

/**
 * What we do here is that we search a project classpath for any occurrence
 * of a container library.//from w w  w  .j  av  a 2  s. co  m
 * 
 * @param jproject the target java project
 * @param libraryName the name of the container library
 * @return true if the container library is already on the classpath
 */
public boolean lookupContainerLibrary(final IJavaProject jproject, final String libraryName) {
    try {
        IClasspathEntry[] cpe = jproject.getRawClasspath();
        for (int i = 0; i < cpe.length; i++) {
            if (cpe[i].getEntryKind() == IClasspathEntry.CPE_CONTAINER) {
                if (cpe[i].getPath().equals(new Path(libraryName))) {
                    return true;
                }
            }
        }
        return false;
    } catch (JavaModelException e) {
        return false;
    }
}

From source file:com.legstar.eclipse.plugin.common.wizards.AbstractWizardPage.java

License:Open Source License

/**
 * The target Java project needs a container library on its classpath.
 * This assumes a classpath initializer did define the library
 * container and all what is left to do is to update the project with yet
 * another classpath entry./*from  w w  w .  ja v a  2s . co  m*/
 * 
 * @param jproject the target java project
 * @param libraryName the name of the container library
 * @throws JavaModelException if seting up classpath fails
 */
public void setupContainerLibrary(final IJavaProject jproject, final String libraryName)
        throws JavaModelException {
    IClasspathEntry varEntry = JavaCore.newContainerEntry(new Path(libraryName), false);

    java.util.List<IClasspathEntry> sourceEntries = new ArrayList<IClasspathEntry>();
    for (IClasspathEntry entry : jproject.getRawClasspath()) {
        sourceEntries.add(entry);
    }
    sourceEntries.add(varEntry);
    IClasspathEntry[] entries = (IClasspathEntry[]) sourceEntries
            .toArray(new IClasspathEntry[sourceEntries.size()]);
    jproject.setRawClasspath(entries, null);
}

From source file:com.legstar.eclipse.plugin.coxbgen.wizards.CoxbGenWizardPage.java

License:Open Source License

/**
 * Initially, try to set the target src dir as the first source directory of
 * the project containing the Xsd file. If that project is not a Java
 * project leave the field not initialized.
 * /*from  w  ww  .  ja v  a  2 s. c o  m*/
 * @param xsdFile the XML schema file from the workspace
 * @throws CoreException if project is invalid
 */
protected void initTargetSrcDir(final IFile xsdFile) throws CoreException {
    try {
        IJavaProject jproject = JavaCore.create(xsdFile.getProject());
        if (jproject == null) {
            throwCoreException(
                    NLS.bind(Messages.xsd_file_in_invalid_project_msg, xsdFile.getLocation().toOSString()));
        }
        IClasspathEntry[] cpe = jproject.getRawClasspath();
        /* Find the first source location */
        for (int i = 0; i < cpe.length; i++) {
            if (cpe[i].getEntryKind() == IClasspathEntry.CPE_SOURCE) {
                _targetSrcDirText.setText(cpe[i].getPath().toOSString());
                break;
            }
        }
    } catch (JavaModelException e) {
        throwCoreException(e);
    }
}

From source file:com.legstar.eclipse.plugin.coxbgen.wizards.CoxbGenWizardPage.java

License:Open Source License

/**
 * Check if a relative path name is a valid java source directory. Also sets
 * the target binary folder.//from  w ww.java 2 s.c  o m
 * 
 * @param relativePathName the path name
 * @return true if this is a valid java source folder
 */
protected boolean isJavaSrcDir(final String relativePathName) {
    IResource resource = ResourcesPlugin.getWorkspace().getRoot().findMember(new Path(relativePathName));
    if (resource != null) {
        IJavaProject jproject = JavaCore.create(resource.getProject());
        if (jproject != null) {
            try {
                IClasspathEntry[] cpe = jproject.getRawClasspath();
                /* Lookup the pathname */
                for (int i = 0; i < cpe.length; i++) {
                    if (cpe[i].getEntryKind() == IClasspathEntry.CPE_SOURCE) {
                        if (relativePathName.equals(cpe[i].getPath().toOSString())) {
                            if (cpe[i].getOutputLocation() != null) {
                                _targetBinDirLabel.setText(cpe[i].getOutputLocation().toOSString());
                            } else {
                                _targetBinDirLabel.setText(jproject.getOutputLocation().toOSString());
                            }
                            return true;
                        }
                    }
                }
            } catch (JavaModelException e) {
                return false;
            }
        }
    }
    return false;
}

From source file:com.legstar.eclipse.plugin.schemagen.wizards.JavaToXsdWizardPage.java

License:Open Source License

/**
 * Extract classpath entries from given java project and store them as
 * path elements in a list./* ww  w  .  j a v a 2  s  .  c  o m*/
 * 
 * @param selectedPathElementsLocations list of path elements locations
 * @param javaProject the input java project
 */
private void addPathElements(final List<String> selectedPathElementsLocations, final IJavaProject javaProject) {
    try {
        IClasspathEntry[] classPathEntries = javaProject.getRawClasspath();
        addPathElements(selectedPathElementsLocations, classPathEntries, javaProject);
    } catch (JavaModelException e) {
        errorDialog(getShell(), Messages.classpath_init_error_dialog_title, Activator.PLUGIN_ID,
                Messages.classpath_init_failure_short_msg, NLS.bind(Messages.classpath_init_failure_long_msg,
                        javaProject.getElementName(), e.getMessage()));
    }
}

From source file:com.liferay.ide.core.util.CoreUtil.java

License:Open Source License

/**
 * @param project// www . j a  v  a 2s .c  om
 * @return
 */
public static IClasspathEntry[] getClasspathEntries(IProject project) {
    if (project != null) {
        IJavaProject javaProject = JavaCore.create(project);
        try {
            IClasspathEntry[] classPathEntries = javaProject.getRawClasspath();
            return classPathEntries;
        } catch (JavaModelException e) {
        }
    }
    return null;
}

From source file:com.liferay.ide.gradle.core.LiferayGradleProject.java

License:Open Source License

private IFolder createResorcesFolder(IProject project) {
    try {//from www  .j  a  v  a 2 s.c  om
        IJavaProject javaProject = JavaCore.create(project);

        List<IClasspathEntry> existingRawClasspath;

        existingRawClasspath = Arrays.asList(javaProject.getRawClasspath());

        List<IClasspathEntry> newRawClasspath = new ArrayList<IClasspathEntry>();

        IClasspathAttribute[] attributes = new IClasspathAttribute[] {
                JavaCore.newClasspathAttribute("FROM_GRADLE_MODEL", "true") }; //$NON-NLS-1$ //$NON-NLS-2$

        IClasspathEntry resourcesEntry = JavaCore.newSourceEntry(
                project.getFullPath().append("src/main/resources"), new IPath[0], new IPath[0], null,
                attributes);

        for (IClasspathEntry entry : existingRawClasspath) {
            newRawClasspath.add(entry);
        }

        if (!existingRawClasspath.contains(resourcesEntry)) {
            newRawClasspath.add(resourcesEntry);
        }

        javaProject.setRawClasspath(newRawClasspath.toArray(new IClasspathEntry[0]), new NullProgressMonitor());

        project.refreshLocal(IResource.DEPTH_INFINITE, new NullProgressMonitor());

        IFolder[] sourceFolders = getSourceFolders();

        for (IFolder folder : sourceFolders) {
            if (folder.getName().equals("resources")) {
                return folder;
            }
        }
    } catch (CoreException e) {
        GradleCore.logError(e);
    }

    return null;
}

From source file:com.liferay.ide.layouttpl.core.facet.LayoutTplPluginFacetInstall.java

License:Open Source License

protected void removeUnneededClasspathEntries() {
    IFacetedProjectWorkingCopy facetedProject = getFacetedProject();
    IJavaProject javaProject = JavaCore.create(facetedProject.getProject());

    try {/*  w  w  w. ja  va 2  s . co  m*/
        IClasspathEntry[] existingClasspath = javaProject.getRawClasspath();
        List<IClasspathEntry> newClasspath = new ArrayList<IClasspathEntry>();

        for (IClasspathEntry entry : existingClasspath) {
            if (entry.getEntryKind() == IClasspathEntry.CPE_SOURCE) {
                continue;
            } else if (entry.getEntryKind() == IClasspathEntry.CPE_CONTAINER) {
                String path = entry.getPath().toPortableString();

                if (path.contains("org.eclipse.jdt.launching.JRE_CONTAINER") || //$NON-NLS-1$
                        path.contains("org.eclipse.jst.j2ee.internal.web.container") || //$NON-NLS-1$
                        path.contains("org.eclipse.jst.j2ee.internal.module.container")) //$NON-NLS-1$
                {
                    continue;
                }
            }

            newClasspath.add(entry);
        }

        javaProject.setRawClasspath(newClasspath.toArray(new IClasspathEntry[0]), null);

        IResource sourceFolder = javaProject.getProject()
                .findMember(IPluginFacetConstants.PORTLET_PLUGIN_SDK_SOURCE_FOLDER);

        if (sourceFolder.exists()) {
            sourceFolder.delete(true, null);
        }
    } catch (Exception e) {
        // no need to report errors
    }
}