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.centimia.orm.jaqu.plugin.ToggleNatureAction.java

License:Open Source License

private void configureClassPath(IProject project) throws CoreException {
    IJavaProject javaProject = JavaCore.create(project);
    IClasspathEntry[] classpathEntries = javaProject.getRawClasspath();
    for (IClasspathEntry entry : classpathEntries) {
        if (CLASSPATH_CONTAINER_PATH.equals(entry.getPath().toString())) {
            return;
        }//from  ww  w .  ja  v a 2 s.  c om
    }
    // Add the Jaqu library.
    ClassPathInitializer classPathInitializer = new ClassPathInitializer();
    classPathInitializer.initialize(new Path(CLASSPATH_CONTAINER_PATH), javaProject);
    //JavaCore.setClasspathContainer(new Path(CLASSPATH_CONTAINER_PATH), new IJavaProject[] { javaProject },  new IClasspathContainer[] { new JaquClasspathContainer(javaProject, new Path(CLASSPATH_CONTAINER_PATH))}, null);

    List<IClasspathEntry> list = new ArrayList<IClasspathEntry>();
    list.addAll(Arrays.asList(javaProject.getRawClasspath()));
    list.add(JavaCore.newContainerEntry(new Path(CLASSPATH_CONTAINER_PATH)));
    javaProject.setRawClasspath(list.toArray(new IClasspathEntry[list.size()]), null);
}

From source file:com.centimia.orm.jaqu.plugin.ToggleNatureAction.java

License:Open Source License

private void deconfigureClassPath(IProject project) throws JavaModelException {
    IJavaProject javaProject = JavaCore.create(project);
    IClasspathEntry[] classpathEntries = javaProject.getRawClasspath();
    for (IClasspathEntry entry : classpathEntries) {
        if (CLASSPATH_CONTAINER_PATH.equals(entry.getPath().toString())) {
            List<IClasspathEntry> list = new ArrayList<IClasspathEntry>();
            list.addAll(Arrays.asList(javaProject.getRawClasspath()));
            list.remove(entry);/* w  w  w.ja va  2  s .  c om*/
            javaProject.setRawClasspath(list.toArray(new IClasspathEntry[list.size()]), null);
        }
    }
}

From source file:com.centurylink.mdw.plugin.project.assembly.ProjectConfigurator.java

License:Apache License

private void addJarsToClasspath(IJavaProject javaProject, IFolder libFolder, IProgressMonitor monitor)
        throws JavaModelException {
    List<IClasspathEntry> classpathEntries = new ArrayList<IClasspathEntry>();
    for (IClasspathEntry existingEntry : javaProject.getRawClasspath())
        classpathEntries.add(existingEntry);

    File libDir = new File(libFolder.getRawLocation().toOSString());
    if (libDir.exists() && libDir.isDirectory()) {
        File[] jarFiles = libDir.listFiles(new FilenameFilter() {
            public boolean accept(File dir, String name) {
                return name.endsWith(".jar") && !name.endsWith("_src.jar");
            }//w w w  . j  a v  a 2 s  .  com
        });

        for (File jarFile : jarFiles) {
            IPath path = libFolder.getFile(jarFile.getName()).getFullPath();
            IClasspathEntry newEntry = JavaCore.newLibraryEntry(path, null, null);
            boolean already = false;
            for (IClasspathEntry existing : javaProject.getRawClasspath()) {
                if (existing.getPath().equals(newEntry.getPath())) {
                    already = true;
                    break;
                }
            }
            if (!already)
                classpathEntries.add(newEntry);
        }

        javaProject.setRawClasspath(classpathEntries.toArray(new IClasspathEntry[0]), monitor);
        J2EEComponentClasspathUpdater.getInstance().queueUpdateModule(javaProject.getProject());
    }
}

From source file:com.cisco.yangide.m2e.yang.tests.YangGenerationTest.java

License:Open Source License

public void test_p001_simple() throws Exception {
    ResolverConfiguration configuration = new ResolverConfiguration();
    IProject project1 = importProject("projects/yang/yang-p001/pom.xml", configuration);
    waitForJobsToComplete();//from  w ww.ja  v a 2 s.c  om

    project1.build(IncrementalProjectBuilder.FULL_BUILD, monitor);
    project1.build(IncrementalProjectBuilder.INCREMENTAL_BUILD, monitor);
    waitForJobsToComplete();

    // assertNoErrors(project1);

    IJavaProject javaProject1 = JavaCore.create(project1);
    IClasspathEntry[] cp1 = javaProject1.getRawClasspath();

    assertNotNull(ClasspathHelpers.getClasspathEntry(cp1, "/yang-p001/target/generated-sources/sal"));
    assertNotNull(ClasspathHelpers.getClasspathEntry(cp1, "/yang-p001/src/main/yang"));

    IFile file = project1.getFile(
            "target/generated-sources/sal/org/opendaylight/yang/gen/v1/urn/simple/string/demo/rev130618/TypedefString.java");
    assertTrue(file.isSynchronized(IResource.DEPTH_ZERO));
    assertTrue(file.isDerived(IResource.CHECK_ANCESTORS));
}

From source file:com.cisco.yangide.ui.wizards.YangProjectWizard.java

License:Open Source License

@Override
public boolean performFinish() {
    boolean res = super.performFinish();
    if (!res) {//ww w .  j av  a2  s  .  co m
        return false;
    }
    final boolean doCreateDemoFile = yangPage.createExampleFile();
    final IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(getModel().getArtifactId());
    final String yangRoot = yangPage.getRootDir();
    final IFolder folder = project.getFolder(yangRoot);

    final String yangVersion = yangPage.getYangVersion();

    final List<CodeGeneratorConfig> generators = yangPage.getCodeGenerators();

    Job updateJob = new Job("Yang Project update") {
        @Override
        public IStatus run(IProgressMonitor monitor) {
            try {
                createFolder(folder);

                IFile pomFile = project.getFile("pom.xml");
                Model model = MavenPlugin.getMavenModelManager().readMavenModel(pomFile);
                updateModel(model, yangVersion, generators, yangRoot);

                pomFile.delete(true, new NullProgressMonitor());
                MavenPlugin.getMavenModelManager().createMavenModel(pomFile, model);
                MavenPlugin.getProjectConfigurationManager().updateProjectConfiguration(project,
                        new NullProgressMonitor());

                if (doCreateDemoFile) {
                    InputStream demoFileContents = null;
                    try {
                        Path demoPath = new Path("resources/yang/acme-system.yang");
                        demoFileContents = FileLocator.openStream(YangUIPlugin.getDefault().getBundle(),
                                demoPath, false);

                        folder.getFile("acme-system.yang").create(demoFileContents, true, null);
                    } finally {
                        if (demoFileContents != null) {
                            demoFileContents.close();
                        }
                    }
                }
                // Add yang folder to java classpath
                IJavaProject javaProject = JavaCore.create(project);
                List<IClasspathEntry> classpath = new ArrayList<>(Arrays.asList(javaProject.getRawClasspath()));
                IClasspathEntry yangSrc = JavaCore.newSourceEntry(folder.getFullPath());
                boolean hasSame = false;
                for (IClasspathEntry ee : classpath) {
                    if (ee.getPath().equals(yangSrc.getPath())) {
                        hasSame = true;
                        break;
                    }
                }
                if (!hasSame) {
                    classpath.add(yangSrc);
                    javaProject.setRawClasspath(classpath.toArray(new IClasspathEntry[0]),
                            new NullProgressMonitor());
                }

            } catch (CoreException e) {
                YangUIPlugin.log(e.getMessage(), e);
            } catch (IOException e) {
                YangUIPlugin.log(e.getMessage(), e);
            }
            return Status.OK_STATUS;
        }
    };
    updateJob.setRule(MavenPlugin.getProjectConfigurationManager().getRule());
    updateJob.schedule();
    return true;
}

From source file:com.ebmwebsourcing.petals.common.internal.provisional.utils.JavaUtils.java

License:Open Source License

/**
 * Updates the class path of a Java project with libraries embedded by the studio.
 * @param jp the Java project/* w w w.  j  a va  2 s  . c o m*/
 * @param folders the folder names
 * @throws IOException
 * @throws JavaModelException
 */
public static void updateClasspathWithProjectLibraries(IJavaProject jp, IProgressMonitor monitor,
        String... folders) throws IOException, JavaModelException {

    if (folders == null)
        return;

    // Keep the current entries
    ArrayList<IClasspathEntry> entries = new ArrayList<IClasspathEntry>();
    entries.addAll(Arrays.asList(jp.getRawClasspath()));
    FilenameFilter filter = new FilenameFilter() {
        @Override
        public boolean accept(File dir, String name) {
            return name.endsWith(".jar") || name.endsWith(".zip");
        }
    };

    for (String folder : folders) {
        File pojoLibPath = ResourceUtils.getPluginBinaryPath("com.ebmwebsourcing.petals.libs.esb", folder); //$NON-NLS-1$
        if (pojoLibPath == null) {
            PetalsCommonPlugin.log("Could not find the Petals libraries in the distribution.", IStatus.ERROR);
            throw new IOException("Petals libraries could not be located.");
        }

        // Add the libraries in the project class path
        File[] jarFiles = pojoLibPath.listFiles(filter);
        if (jarFiles != null) {
            for (File jarFile : jarFiles) {
                IPath path = new Path(jarFile.getAbsolutePath());
                IClasspathEntry entry = JavaCore.newLibraryEntry(path, null, null);
                entries.add(entry);
            }
        }
    }

    IClasspathEntry[] newEntries = CollectionUtils.convertToArray(entries, IClasspathEntry.class);
    if (!jp.hasClasspathCycle(newEntries))
        jp.setRawClasspath(newEntries, monitor);
}

From source file:com.ebmwebsourcing.petals.common.internal.provisional.utils.JavaUtils.java

License:Open Source License

/**
 * Makes an Eclipse project a Java project.
 * <p>/*from   ww  w.j a  va 2s. com*/
 * If the project does not exist, or is not accessible, then nothing is done.<br />
 * If the project does not have the Java nature, this nature is added.
 * </p>
 * <p>
 * The default output directory is "bin". If this directory does not exist, it is created.
 * If the creation fails, then no output directory is set.
 * </p>
 * <p>
 * The default source directory is "src/main/java". If this directory does not exist, it is created.
 * If the creation fails, then no source directory is set.
 * </p>
 *
 * @param project the project to transform into a Java project
 * @return the created Java project
 * @throws CoreException if something went wrong
 */
public static IJavaProject createJavaProject(IProject project) throws CoreException {

    IJavaProject jp = null;
    if (project.isAccessible()) {

        // Add the Java nature?
        if (!project.hasNature(JavaCore.NATURE_ID)) {

            IProjectDescription description = project.getDescription();
            String[] natures = description.getNatureIds();
            String[] newNatures = new String[natures.length + 1];

            System.arraycopy(natures, 0, newNatures, 0, natures.length);
            newNatures[natures.length] = JavaCore.NATURE_ID;
            description.setNatureIds(newNatures);
            project.setDescription(description, null);
        }

        // Set the default class path
        jp = JavaCore.create(project);
        IProgressMonitor monitor = new NullProgressMonitor();

        //
        // Output location
        IPath ppath = project.getFullPath();
        IPath binPath = ppath.append(PetalsConstants.LOC_BIN_FOLDER);
        File binFile = ResourcesPlugin.getWorkspace().getRoot().getLocation().append(binPath).toFile();
        if (binFile.exists() && binFile.isDirectory() || !binFile.exists() && binFile.mkdirs()) {
            jp.setOutputLocation(binPath, monitor);
        }

        Set<IClasspathEntry> entries = new HashSet<IClasspathEntry>();
        entries.addAll(Arrays.asList(jp.getRawClasspath()));
        entries.add(JavaRuntime.getDefaultJREContainerEntry());

        //
        // Remove the "src" entry
        IClasspathEntry srcEntry = null;
        for (IClasspathEntry entry : entries) {
            if (entry.getEntryKind() == IClasspathEntry.CPE_SOURCE) {
                srcEntry = entry;
                break;
            }
        }

        //
        // Specify a new source entry
        if (srcEntry != null)
            entries.remove(srcEntry);

        String[] srcPaths = new String[] { PetalsConstants.LOC_SRC_FOLDER,
                PetalsConstants.LOC_JAVA_RES_FOLDER };
        for (String s : srcPaths) {
            IPath srcPath = ppath.append(s);
            File srcFile = ResourcesPlugin.getWorkspace().getRoot().getLocation().append(srcPath).toFile();
            if (srcFile.exists() && srcFile.isDirectory() || !srcFile.exists() && srcFile.mkdirs()) {

                project.refreshLocal(IResource.DEPTH_INFINITE, monitor);
                srcEntry = JavaCore.newSourceEntry(srcPath);
                entries.add(srcEntry);
            } else {
                PetalsCommonPlugin.log("Could not set '" + s + "' as a source folder.", IStatus.ERROR);
            }
        }

        jp.setRawClasspath(entries.toArray(new IClasspathEntry[entries.size()]), monitor);
    }

    return jp;
}

From source file:com.ebmwebsourcing.petals.common.internal.provisional.utils.JavaUtils.java

License:Open Source License

/**
 * Gets the source folders of a IJavaProject.
 * @param javaProject//  w  w  w .  ja  v  a  2  s .co  m
 * @return the list of source folders in this Java project
 */
public static List<IClasspathEntry> getSourceFolders(IJavaProject javaProject) {

    List<IClasspathEntry> result = new ArrayList<IClasspathEntry>();
    try {
        for (IClasspathEntry entry : javaProject.getRawClasspath()) {
            if (entry.getEntryKind() == IClasspathEntry.CPE_SOURCE)
                result.add(entry);
        }

    } catch (JavaModelException e) {
        PetalsCommonPlugin.log(e, IStatus.ERROR);
    }

    return result;
}

From source file:com.ebmwebsourcing.petals.common.internal.provisional.utils.JavaUtils.java

License:Open Source License

/**
 * Get the class path from Java project.
 *
 * @param javaProject/*from  www  . j a  va2s .c  om*/
 * @param getReferencedProjectClasspath
 * @param binaryDirectory
 * @return the class path as a list of string locations.
 */
public static List<String> getClasspath(IJavaProject javaProject, boolean getReferencedProjectClasspath,
        boolean binaryDirectory) {

    List<String> paths = new ArrayList<String>();
    try {
        if (javaProject != null) {

            // Get the raw class path
            IClasspathEntry[] entries = javaProject.getRawClasspath();
            for (IClasspathEntry entry : entries) {
                switch (entry.getEntryKind()) {

                case IClasspathEntry.CPE_PROJECT:
                    if (!getReferencedProjectClasspath)
                        break;

                    String projectName = entry.getPath().toString();
                    IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(projectName);
                    IJavaProject jProject = JavaCore.create(project);

                    List<String> subPaths = getClasspath(jProject, true, binaryDirectory);
                    paths.addAll(subPaths);
                    break;

                case IClasspathEntry.CPE_LIBRARY:
                    IPath path = entry.getPath();
                    paths.add(path.toString());
                    break;

                case IClasspathEntry.CPE_VARIABLE:
                    entry = JavaCore.getResolvedClasspathEntry(entry);
                    if (entry != null) {
                        path = entry.getPath();
                        paths.add(path.toString());
                    }
                    break;

                }
            }

            // Add the "bin" directory?
            if (binaryDirectory && javaProject.getOutputLocation() != null) {
                IPath path = ResourcesPlugin.getWorkspace().getRoot().getLocation();
                path = path.append(javaProject.getOutputLocation());
                paths.add(path.toString());
            }
        }

    } catch (JavaModelException e) {
        PetalsCommonPlugin.log(e, IStatus.ERROR);
    }

    return paths;
}

From source file:com.ebmwebsourcing.petals.common.internal.provisional.utils.JavaUtils.java

License:Open Source License

/**
 * Creates a new source folder.//from ww  w .j  a  va2 s  .  co  m
 * <p>
 * If the folder does not exist, it is created.<br />
 * If the directory is already in the class path, then this method does nothing.
 * </p>
 *
 * @param jp the Java project
 * @param folderName the folder name
 * @param monitor the progress monitor
 * @return the created folder
 * @throws CoreException if something went wrong
 */
public static IFolder createSourceFolder(IJavaProject jp, String folderName, IProgressMonitor monitor)
        throws CoreException {

    IFolder newSourceFolder = jp.getProject().getFolder(folderName);
    if (!newSourceFolder.exists())
        newSourceFolder.create(true, true, monitor);

    IClasspathEntry srcEntry = JavaCore.newSourceEntry(newSourceFolder.getFullPath());
    Set<IClasspathEntry> entries = new HashSet<IClasspathEntry>();
    entries.addAll(Arrays.asList(jp.getRawClasspath()));
    entries.add(srcEntry);
    jp.setRawClasspath(entries.toArray(new IClasspathEntry[entries.size()]), monitor);

    return newSourceFolder;
}