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

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

Introduction

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

Prototype

void setRawClasspath(IClasspathEntry[] entries, IProgressMonitor monitor) throws JavaModelException;

Source Link

Document

Sets the classpath of this project using a list of classpath entries.

Usage

From source file:gov.redhawk.ide.codegen.java.AbstractJavaCodeGenerator.java

License:Open Source License

@Override
public IStatus cleanupSourceFolders(final IProject project, final IProgressMonitor monitor) {
    final IJavaProject jp = JavaCore.create(project);
    final HashSet<IClasspathEntry> paths = new HashSet<IClasspathEntry>();
    try {/*from  w ww .  jav a  2s.co m*/
        for (final IClasspathEntry path : jp.getRawClasspath()) {
            IPath p = path.getPath();
            if (path.getEntryKind() == IClasspathEntry.CPE_SOURCE) {
                if (p.segment(0).equals(project.getFullPath().segment(0))) {
                    p = p.removeFirstSegments(1);
                    if (project.getFolder(p).exists()) {
                        paths.add(path);
                    }
                }
            } else {
                paths.add(path);
            }
        }
        jp.setRawClasspath(paths.toArray(new IClasspathEntry[paths.size()]), monitor);
    } catch (final JavaModelException e) {
        return new Status(IStatus.WARNING, JavaGeneratorPlugin.PLUGIN_ID,
                "Unable to adjust the list of source code folders for the project");
    }
    return new Status(IStatus.OK, JavaGeneratorPlugin.PLUGIN_ID, "Cleaned up source folders");
}

From source file:gov.redhawk.ide.codegen.java.JavaGeneratorUtils.java

License:Open Source License

/**
 * Returns a javadoc starting line with the specified tab indentation level.
 * /* w ww .j a  v a 2  s  . c o m*/
 * @param indent the number of tabs to indent
 * @return a String for a new javadoc comment
 */
public static IJavaProject addJavaProjectNature(final IProject project, final IProgressMonitor monitor)
        throws CoreException {
    final SubMonitor progress = SubMonitor.convert(monitor, 2);
    IJavaProject javaProject = null;

    progress.subTask("Checking project natures");
    final IProjectDescription desc = project.getDescription();
    if (!project.hasNature(JavaCore.NATURE_ID)) {
        // Add the Java Nature
        final String[] natures = desc.getNatureIds();
        final String[] newNatures = new String[natures.length + 1];
        System.arraycopy(natures, 0, newNatures, 0, natures.length);
        newNatures[natures.length] = JavaCore.NATURE_ID;
        desc.setNatureIds(newNatures);
        project.setDescription(desc, progress.newChild(1));

        // Get the resulting Java Project
        javaProject = JavaCore.create(project);

        final IClasspathEntry[] defaultClasspath = new IClasspathEntry[1];
        defaultClasspath[0] = JavaRuntime.getDefaultJREContainerEntry();
        javaProject.setRawClasspath(defaultClasspath, progress.newChild(1));
    } else {
        javaProject = JavaCore.create(project);
    }
    return javaProject;
}

From source file:gov.redhawk.ide.codegen.java.JavaGeneratorUtils.java

License:Open Source License

public static void addRedhawkJavaClassPaths(final IJavaProject jproject, final IProgressMonitor monitor)
        throws CoreException {
    final SubMonitor progress = SubMonitor.convert(monitor, 1);
    final Set<IClasspathEntry> entries = new LinkedHashSet<IClasspathEntry>(
            Arrays.asList(jproject.getRawClasspath()));

    IClasspathEntry e;/*from w  w  w . j a v  a 2 s.  c o  m*/
    e = JavaRuntime.getDefaultJREContainerEntry();
    if (!entries.contains(e)) {
        entries.add(e);
    }

    entries.add(JavaCore.newContainerEntry(ScaCore.OSSIE_LIB_CONTAINER_PATH));
    entries.add(JavaCore.newContainerEntry(ScaCore.SOFT_PKG_REF_CONTAINER_PATH));

    jproject.setRawClasspath(entries.toArray(new IClasspathEntry[entries.size()]), progress.newChild(1));
}

From source file:gov.redhawk.ide.codegen.java.JavaGeneratorUtils.java

License:Open Source License

/**
 * @deprecated The code in {@link #addRedhawkJavaClassPaths} will correctly include all of the .jar files
 * @param jproject/*from   w  w  w.j av a  2s. c  o m*/
 * @param ports
 * @param monitor
 * @throws CoreException
 */
@Deprecated
public static void addRedhawkPortClassPaths(final IJavaProject jproject, final Ports ports,
        final IProgressMonitor monitor) throws CoreException {
    final Set<String> packages = new LinkedHashSet<String>();
    for (final Provides p : ports.getProvides()) {
        final String[] ints = p.getRepID().split(":")[1].split("/");
        packages.add(ints[ints.length - 2]);
    }
    for (final Uses u : ports.getUses()) {
        final String[] ints = u.getRepID().split(":")[1].split("/");
        packages.add(ints[ints.length - 2]);
    }

    final SubMonitor progress = SubMonitor.convert(monitor, packages.size() + 1);
    final Set<IClasspathEntry> entries = new LinkedHashSet<IClasspathEntry>(
            Arrays.asList(jproject.getRawClasspath()));

    for (final String pack : packages) {
        final String jarFile = "OSSIEHOME/lib/" + pack + "Interfaces.jar";
        final IClasspathEntry e = JavaCore.newVariableEntry(new Path(jarFile), null, null);
        if (!entries.contains(e)) {
            entries.add(e);
        }
        progress.worked(1);
    }

    jproject.setRawClasspath(entries.toArray(new IClasspathEntry[entries.size()]), progress.newChild(1));
}

From source file:gov.redhawk.ide.codegen.java.JavaGeneratorUtils.java

License:Open Source License

public static void addSourceClassPaths(final IJavaProject jproject, final IPath srcPath, final IPath binPath,
        final IProgressMonitor monitor) throws CoreException {
    final SubMonitor progress = SubMonitor.convert(monitor, 1);
    final Set<IClasspathEntry> entries = new LinkedHashSet<IClasspathEntry>(
            Arrays.asList(jproject.getRawClasspath()));

    // Add source code to the java project classpath
    for (final IClasspathEntry path : entries) {
        if ((path.getEntryKind() == IClasspathEntry.CPE_SOURCE)
                && path.getPath().equals(jproject.getProject().getFullPath())) {
            continue;
        }/*from   w ww.ja  va  2 s .  co  m*/
        entries.add(path);
    }

    final IClasspathEntry e = JavaCore.newSourceEntry(srcPath, new Path[0], binPath);
    entries.add(e);

    jproject.setRawClasspath(entries.toArray(new IClasspathEntry[entries.size()]), progress.newChild(1));
}

From source file:hornetq_project_gen.importWizards.hornetq.ImportWizard.java

License:Open Source License

public boolean performFinish() {
    MavenModelHelper helper = mainPage.getHelper();

    if (helper == null)
        return false;

    IProgressMonitor progressMonitor = new GenericProgressMonitor();
    IWorkspaceRoot rootWs = ResourcesPlugin.getWorkspace().getRoot();
    try {/*from   w ww.j  a v  a  2  s .  c o  m*/
        IProject project = rootWs.getProject(mainPage.getProjectName());
        project.create(progressMonitor);
        project.open(progressMonitor);
        //add nature
        IProjectDescription description = project.getDescription();
        description.setNatureIds(new String[] { JavaCore.NATURE_ID });
        project.setDescription(description, progressMonitor);

        IJavaProject javaProject = JavaCore.create(project);
        //setup bin output folder
        if (javaProject.getOutputLocation() == null) {
            IFolder binFolder = project.getFolder("bin");
            binFolder.create(false, true, null);
            javaProject.setOutputLocation(binFolder.getFullPath(), null);
        }

        //all source files
        Map<String, IClasspathEntry> sources = new HashMap<String, IClasspathEntry>();
        helper.getProjectSources(project, sources);

        //all external jars
        helper.getDependencies(sources);

        //jre lib
        IClasspathEntry[] jreEntries = PreferenceConstants.getDefaultJRELibrary();
        for (int i = 0; i < jreEntries.length; i++) {
            sources.put(jreEntries[i].getPath().toOSString(), jreEntries[i]);
        }

        //copy missing entries
        helper.findMissingEntries(project, sources);

        //copy resources
        helper.copyResources(project, sources);

        //set project classpath
        IClasspathEntry[] allEntries = sources.values().toArray(new IClasspathEntry[0]);
        javaProject.setRawClasspath(allEntries, progressMonitor);

        //add a builder
        final String BUILDER_ID = "hornetq_project_gen.hqbuilder";
        IProjectDescription desc = project.getDescription();
        ICommand[] commands = desc.getBuildSpec();
        boolean found = false;

        for (int i = 0; i < commands.length; ++i) {
            if (commands[i].getBuilderName().equals(BUILDER_ID)) {
                found = true;
                break;
            }
        }
        if (!found) {
            //add builder to project
            ICommand command = desc.newCommand();
            command.setBuilderName(BUILDER_ID);
            ICommand[] newCommands = new ICommand[commands.length + 1];

            // Add it before other builders.
            System.arraycopy(commands, 0, newCommands, 1, commands.length);
            newCommands[0] = command;
            desc.setBuildSpec(newCommands);
            project.setDescription(desc, null);
        }

    } catch (CoreException e) {
        e.printStackTrace();
        return false;
    }

    return true;
}

From source file:hornetq_project_gen.importWizards.wildfly.WildFlyImportWizard.java

License:Open Source License

public boolean performFinish() {
    MavenModelHelper helper = mainPage.getHelper();

    if (helper == null)
        return false;

    IProgressMonitor progressMonitor = new GenericProgressMonitor();
    IWorkspaceRoot rootWs = ResourcesPlugin.getWorkspace().getRoot();
    try {// www. ja va  2s .  c o  m
        IProject project = rootWs.getProject(mainPage.getProjectName());
        project.create(progressMonitor);
        project.open(progressMonitor);
        //add nature
        IProjectDescription description = project.getDescription();
        description.setNatureIds(new String[] { JavaCore.NATURE_ID });
        project.setDescription(description, progressMonitor);

        IJavaProject javaProject = JavaCore.create(project);
        //setup bin output folder
        if (javaProject.getOutputLocation() == null) {
            IFolder binFolder = project.getFolder("bin");
            binFolder.create(false, true, null);
            javaProject.setOutputLocation(binFolder.getFullPath(), null);
        }

        //all source files
        Map<String, IClasspathEntry> sources = new HashMap<String, IClasspathEntry>();
        helper.getProjectSources(project, sources);

        //all external jars
        helper.getDependencies(sources);

        //jre lib
        IClasspathEntry[] jreEntries = PreferenceConstants.getDefaultJRELibrary();
        for (int i = 0; i < jreEntries.length; i++) {
            sources.put(jreEntries[i].getPath().toOSString(), jreEntries[i]);
        }

        //need jconsole for cli
        String jHome = System.getProperty("java.home");
        File jreHome = new File(jHome);
        File libDir = new File(jreHome.getParentFile(), "lib");
        File jConsoleJar = new File(libDir, "jconsole.jar");

        String key = jConsoleJar.getAbsolutePath();
        if (!sources.containsKey(key)) {
            IPath jarPath = Path.fromOSString(key);
            IClasspathEntry entry = JavaCore.newLibraryEntry(jarPath, null, null, false);
            sources.put(key, entry);
        }

        //copy missing entries
        helper.findMissingEntries(project, sources);

        //copy resources
        helper.copyResources(project, sources);

        //set project classpath
        IClasspathEntry[] allEntries = sources.values().toArray(new IClasspathEntry[0]);
        javaProject.setRawClasspath(allEntries, progressMonitor);

        //add a builder
        final String BUILDER_ID = "hornetq_project_gen.hqbuilder";
        IProjectDescription desc = project.getDescription();
        ICommand[] commands = desc.getBuildSpec();
        boolean found = false;

        for (int i = 0; i < commands.length; ++i) {
            if (commands[i].getBuilderName().equals(BUILDER_ID)) {
                found = true;
                break;
            }
        }
        if (!found) {
            //add builder to project
            ICommand command = desc.newCommand();
            command.setBuilderName(BUILDER_ID);
            ICommand[] newCommands = new ICommand[commands.length + 1];

            // Add it before other builders.
            System.arraycopy(commands, 0, newCommands, 1, commands.length);
            newCommands[0] = command;
            desc.setBuildSpec(newCommands);
            project.setDescription(desc, null);
        }

    } catch (CoreException e) {
        e.printStackTrace();
        return false;
    }

    return true;
}

From source file:hydrograph.ui.expression.editor.composites.CategoriesDialogSourceComposite.java

License:Apache License

private void createBrowseButton(Composite headerComposite) {
    Button browseButton = new Button(headerComposite, SWT.NONE);
    browseButton.setText("Browse");
    browseButton.setToolTipText(Messages.EXTERNAL_JAR_DIALOG_BROWSE_BUTTON_TOOLTIP);
    browseButton.addSelectionListener(new SelectionAdapter() {
        public void widgetSelected(SelectionEvent e) {
            IFolder libsFolder = BuildExpressionEditorDataSturcture.INSTANCE.getCurrentProject()
                    .getFolder(PathConstant.PROJECT_LIB_FOLDER);
            FileDialog dialog = new FileDialog(Display.getCurrent().getActiveShell(), SWT.OK);
            dialog.setFilterExtensions(filters);
            String path = dialog.open();
            if (path != null) {
                File file = new File(path);
                if (file.isFile()) {
                    IFile newFile = libsFolder.getFile(file.getName());
                    if (!newFile.exists()) {
                        try (InputStream fis = new FileInputStream(file.getAbsoluteFile())) {
                            newFile.create(fis, true, null);
                            addFileToBuildPath(newFile);
                            loadComboJarListFromBuildPath(comboJarList, newFile.getName());

                        } catch (CoreException | IOException e1) {
                            LOGGER.error(
                                    "Exception occurred while copying jar file from local-file-system to project",
                                    e1);
                            new CustomMessageBox(SWT.ERROR, Messages.JAR_FILE_COPY_ERROR, Messages.ERROR_TITLE)
                                    .open();
                        }/* w  ww .ja  v a 2 s.  c o  m*/
                    } else {
                        new CustomMessageBox(SWT.ERROR, Messages.DUPLICATE_JAR_FILE_COPY_ERROR,
                                Messages.ERROR_TITLE).open();
                    }
                }
            }
        }

        private void addFileToBuildPath(IFile jarFile) throws CoreException {
            LOGGER.error("Adding jar file " + jarFile.getName() + " to build Path");
            IJavaProject javaProject = JavaCore
                    .create(BuildExpressionEditorDataSturcture.INSTANCE.getCurrentProject());
            IClasspathEntry[] oldClasspathEntry = javaProject.getRawClasspath();
            IClasspathEntry[] newClasspathEntry = new IClasspathEntry[oldClasspathEntry.length + 1];
            for (int index = 0; index < oldClasspathEntry.length; index++) {
                if (oldClasspathEntry[index].getPath().equals(jarFile.getFullPath()))
                    return;
                newClasspathEntry[index] = javaProject.getRawClasspath()[index];
            }
            newClasspathEntry[oldClasspathEntry.length] = JavaCore.newLibraryEntry(jarFile.getFullPath(), null,
                    null);
            javaProject.setRawClasspath(newClasspathEntry, new NullProgressMonitor());
            javaProject.close();
        }
    });
}

From source file:hydrograph.ui.expression.editor.composites.CategoriesDialogSourceComposite.java

License:Apache License

private void createDelButton(Composite headerComposite) {
    deleteButton = new Button(headerComposite, SWT.NONE);
    deleteButton.setBounds(0, 0, 75, 25);
    deleteButton.setToolTipText(Messages.EXTERNAL_JAR_DIALOG_DELETE_BUTTON_TOOLTIP);
    try {//w  w w. jav  a 2s  . c  om
        deleteButton.setImage(ImagePathConstant.DELETE_BUTTON.getImageFromRegistry());
    } catch (Exception exception) {
        LOGGER.error("Exception occurred while attaching image to button", exception);
        deleteButton.setText("Delete");
    }

    deleteButton.addSelectionListener(new SelectionListener() {
        @Override
        public void widgetSelected(SelectionEvent e) {
            if (comboJarList.getSelectionIndex() > -1) {
                String jarName = comboJarList.getItem(comboJarList.getSelectionIndex());
                if (userIsSure(jarName)) {
                    try {
                        removeJarFromBuildPath(jarName);
                        comboJarList.remove(jarName);
                        sourcePackageList.removeAll();
                        refresh(jarName);
                        enableOrDisableAddLabelsOnComboSelection();
                    } catch (CoreException e1) {
                        LOGGER.error(
                                "Exception occurred while removing jar file" + jarName + "from build Path");
                    }
                }
            }
        }

        private boolean userIsSure(String jarName) {
            MessageBox messageBox = new MessageBox(Display.getCurrent().getActiveShell(),
                    SWT.ICON_QUESTION | SWT.YES | SWT.NO);
            messageBox.setMessage("Do you really want to remove " + jarName + " file?\nCannot be undone.");
            messageBox.setText("Remove Resource");
            int response = messageBox.open();
            if (response == SWT.YES)
                return true;
            return false;
        }

        private void refresh(String jarName) {
            boolean isAnyItemRemovedFromTargetList = false;
            String[] items = targetComposite.getTargetList().getItems();
            targetComposite.getTargetList().removeAll();
            for (String item : items) {
                String jarFileName = StringUtils.trim(StringUtils.substringAfter(item, Constants.DASH));
                if (!StringUtils.equalsIgnoreCase(jarFileName, jarName)) {
                    targetComposite.getTargetList().add(item);
                } else
                    isAnyItemRemovedFromTargetList = true;
            }
            if (isAnyItemRemovedFromTargetList) {
                addCategoreisDialog.createPropertyFileForSavingData();
            }
        }

        private void removeJarFromBuildPath(String jarName) throws CoreException {
            LOGGER.debug("Removing jar file" + jarName + "from build Path");
            IJavaProject javaProject = JavaCore
                    .create(BuildExpressionEditorDataSturcture.INSTANCE.getCurrentProject());
            IFile jarFile = javaProject.getProject().getFolder(PathConstant.PROJECT_LIB_FOLDER)
                    .getFile(jarName);
            IClasspathEntry[] oldClasspathEntry = javaProject.getRawClasspath();
            IClasspathEntry[] newClasspathEntry = new IClasspathEntry[oldClasspathEntry.length - 1];
            if (jarFile.exists()) {
                int index = 0;
                for (IClasspathEntry classpathEntry : oldClasspathEntry) {
                    if (classpathEntry.getPath().equals(jarFile.getFullPath())) {
                        continue;
                    }
                    newClasspathEntry[index] = classpathEntry;
                    index++;
                }
                javaProject.setRawClasspath(newClasspathEntry, new NullProgressMonitor());
                jarFile.delete(true, new NullProgressMonitor());
            }
            javaProject.close();
        }

        @Override
        public void widgetDefaultSelected(SelectionEvent e) {/* Do-Nothing */
        }
    });
}

From source file:hydrograph.ui.expression.editor.launcher.LaunchExpressionEditor.java

License:Apache License

private void addClassPathEntry(IPath tempSrcFolder) throws JavaModelException {
    IJavaProject javaProject = JavaCore.create(BuildExpressionEditorDataSturcture.INSTANCE.getCurrentProject());
    oldClasspathEntry = javaProject.getRawClasspath();
    IClasspathEntry[] newClasspathEntry = new IClasspathEntry[oldClasspathEntry.length + 1];
    for (int index = 0; index < newClasspathEntry.length - 1; index++) {
        if (oldClasspathEntry[index].getPath().equals(tempSrcFolder))
            return;
        newClasspathEntry[index] = javaProject.getRawClasspath()[index];
    }// w ww.  j a  v a 2 s  .co  m
    newClasspathEntry[newClasspathEntry.length - 1] = JavaCore.newSourceEntry(tempSrcFolder);
    javaProject.setRawClasspath(newClasspathEntry, new NullProgressMonitor());
}