Search in sources :

Example 1 with IExecutionEnvironment

use of org.eclipse.jdt.launching.environments.IExecutionEnvironment in project bndtools by bndtools.

the class ImportBndWorkspaceWizard method addSystemLibraryContainer.

/**
     * The Java-Nature doesn't add a JRE-Container, so we add one
     *
     * @param javaProject
     *            the Java project which should get enhanced with LibraryContainer
     * @param javacTarget
     * @param monitor
     *            current IProgressMonitor
     * @throws JavaModelException
     */
private void addSystemLibraryContainer(final IJavaProject javaProject, final String javacTarget, final ImportSettings importSettings, IProgressMonitor monitor) throws JavaModelException {
    List<IClasspathEntry> entries = new ArrayList<>(Arrays.asList(javaProject.getRawClasspath()));
    // entries.addAll(newContainerEntries);
    // only add JRE-Container if none available
    boolean jreContainerAvailable = false;
    Iterator<IClasspathEntry> it = entries.iterator();
    while (it.hasNext()) {
        IClasspathEntry entry = it.next();
        if (entry.getEntryKind() == IClasspathEntry.CPE_CONTAINER && entry.getPath() != null && entry.getPath().toString().startsWith(JavaRuntime.JRE_CONTAINER)) {
            // remove existing entry if user want to infer EE
            if (importSettings.inferExecutionEnvironment) {
                it.remove();
            } else {
                jreContainerAvailable = true;
            }
            break;
        }
    }
    if (!jreContainerAvailable) {
        IClasspathEntry defaultJREContainerEntry = JavaRuntime.getDefaultJREContainerEntry();
        if (importSettings.inferExecutionEnvironment) {
            // fuzzy at the moment but better than nothing. We should find a way to handle CDC
            IExecutionEnvironment environment = JavaRuntime.getExecutionEnvironmentsManager().getEnvironment("J2SE-" + javacTarget);
            if (environment == null) {
                environment = JavaRuntime.getExecutionEnvironmentsManager().getEnvironment("JavaSE-" + javacTarget);
            }
            if (environment != null) {
                entries.add(JavaCore.newContainerEntry(JavaRuntime.newJREContainerPath(environment)));
            } else {
                Plugin.getDefault().getLog().log(new Status(IStatus.WARNING, Plugin.PLUGIN_ID, 0, String.format("Could not infer execution-environment in project '%s' for javac.target '%s'", javaProject.getElementName(), javacTarget), null));
                entries.add(defaultJREContainerEntry);
            }
        } else {
            entries.add(defaultJREContainerEntry);
        }
        javaProject.setRawClasspath(entries.toArray(new IClasspathEntry[entries.size()]), monitor);
    }
}
Also used : MultiStatus(org.eclipse.core.runtime.MultiStatus) IStatus(org.eclipse.core.runtime.IStatus) Status(org.eclipse.core.runtime.Status) IExecutionEnvironment(org.eclipse.jdt.launching.environments.IExecutionEnvironment) IClasspathEntry(org.eclipse.jdt.core.IClasspathEntry) ArrayList(java.util.ArrayList)

Aggregations

ArrayList (java.util.ArrayList)1 IStatus (org.eclipse.core.runtime.IStatus)1 MultiStatus (org.eclipse.core.runtime.MultiStatus)1 Status (org.eclipse.core.runtime.Status)1 IClasspathEntry (org.eclipse.jdt.core.IClasspathEntry)1 IExecutionEnvironment (org.eclipse.jdt.launching.environments.IExecutionEnvironment)1