Search in sources :

Example 1 with StandardVMType

use of org.eclipse.jdt.internal.launching.StandardVMType in project eclipse.jdt.ls by eclipse.

the class JVMConfiguratorTest method testJVM.

@Test
public void testJVM() throws Exception {
    try {
        Preferences prefs = new Preferences();
        Bundle bundle = Platform.getBundle(JavaLanguageServerTestPlugin.PLUGIN_ID);
        URL url = FileLocator.toFileURL(bundle.getEntry("/fakejdk2/11a"));
        File file = URIUtil.toFile(URIUtil.toURI(url));
        String path = file.getAbsolutePath();
        String javadoc = "file:///javadoc";
        Set<RuntimeEnvironment> runtimes = new HashSet<>();
        RuntimeEnvironment runtime = new RuntimeEnvironment();
        runtime.setPath(path);
        runtime.setName(ENVIRONMENT_NAME);
        runtime.setJavadoc(javadoc);
        runtime.setDefault(true);
        assertTrue(runtime.isValid());
        runtimes.add(runtime);
        prefs.setRuntimes(runtimes);
        file = runtime.getInstallationFile();
        assertTrue(file != null && file.isDirectory());
        IVMInstallType installType = JavaRuntime.getVMInstallType(StandardVMType.ID_STANDARD_VM_TYPE);
        IStatus status = installType.validateInstallLocation(file);
        assertTrue(status.toString(), status.isOK());
        boolean changed = JVMConfigurator.configureJVMs(prefs);
        assertTrue("A VM hasn't been changed", changed);
        JobHelpers.waitForJobsToComplete();
        IVMInstall vm = JVMConfigurator.findVM(runtime.getInstallationFile(), ENVIRONMENT_NAME);
        assertNotNull(vm);
        assertTrue(vm instanceof IVMInstall2);
        String version = ((IVMInstall2) vm).getJavaVersion();
        assertTrue(version.startsWith(JavaCore.VERSION_11));
        StandardVMType svt = (StandardVMType) vm.getVMInstallType();
        LibraryLocation[] libs = vm.getLibraryLocations();
        assertNotNull(libs);
        for (LibraryLocation lib : libs) {
            assertEquals(runtime.getJavadocURL(), lib.getJavadocLocation());
        }
        IVMInstall newDefaultVM = JavaRuntime.getDefaultVMInstall();
        assertNotEquals(originalVm, newDefaultVM);
        assertEquals(vm, newDefaultVM);
        IExecutionEnvironment environment = JVMConfigurator.getExecutionEnvironment(ENVIRONMENT_NAME);
        assertNotNull(environment);
        assertEquals(vm, environment.getDefaultVM());
    } finally {
        IVMInstall vm = JVMConfigurator.findVM(null, ENVIRONMENT_NAME);
        if (vm != null) {
            vm.getVMInstallType().disposeVMInstall(vm.getId());
        }
    }
    IVMInstall vm = JVMConfigurator.findVM(null, ENVIRONMENT_NAME);
    assertNull(vm);
}
Also used : IStatus(org.eclipse.core.runtime.IStatus) Bundle(org.osgi.framework.Bundle) IVMInstallType(org.eclipse.jdt.launching.IVMInstallType) LibraryLocation(org.eclipse.jdt.launching.LibraryLocation) URL(java.net.URL) IExecutionEnvironment(org.eclipse.jdt.launching.environments.IExecutionEnvironment) IVMInstall(org.eclipse.jdt.launching.IVMInstall) StandardVMType(org.eclipse.jdt.internal.launching.StandardVMType) ClientPreferences(org.eclipse.jdt.ls.core.internal.preferences.ClientPreferences) IEclipsePreferences(org.eclipse.core.runtime.preferences.IEclipsePreferences) Preferences(org.eclipse.jdt.ls.core.internal.preferences.Preferences) File(java.io.File) IVMInstall2(org.eclipse.jdt.launching.IVMInstall2) HashSet(java.util.HashSet) AbstractInvisibleProjectBasedTest(org.eclipse.jdt.ls.core.internal.managers.AbstractInvisibleProjectBasedTest) Test(org.junit.Test)

Example 2 with StandardVMType

use of org.eclipse.jdt.internal.launching.StandardVMType in project eclipse.jdt.ls by eclipse.

the class JVMConfigurator method configureJVMs.

public static boolean configureJVMs(Preferences preferences, JavaClientConnection connection) throws CoreException {
    boolean changed = false;
    boolean defaultVMSet = false;
    Set<RuntimeEnvironment> runtimes = preferences.getRuntimes();
    for (RuntimeEnvironment runtime : runtimes) {
        if (runtime.isValid()) {
            File file = runtime.getInstallationFile();
            if (file != null && file.isDirectory()) {
                URL javadocURL = runtime.getJavadocURL();
                IPath sourcePath = runtime.getSourcePath();
                IVMInstall vm = findVM(file, runtime.getName());
                IVMInstallType installType = JavaRuntime.getVMInstallType(StandardVMType.ID_STANDARD_VM_TYPE);
                if (installType == null || installType.getVMInstalls().length == 0) {
                    // https://github.com/eclipse/eclipse.jdt.ls/issues/1646
                    IVMInstallType macInstallType = JavaRuntime.getVMInstallType(MAC_OSX_VM_TYPE);
                    if (macInstallType != null) {
                        installType = macInstallType;
                    }
                }
                VMStandin vmStandin;
                if (vm == null) {
                    long unique = System.currentTimeMillis();
                    while (installType.findVMInstall(String.valueOf(unique)) != null) {
                        unique++;
                    }
                    String vmId = String.valueOf(unique);
                    vmStandin = new VMStandin(installType, vmId);
                    changed = true;
                } else {
                    vmStandin = new VMStandin(vm);
                    changed = changed || !runtime.getName().equals(vm.getName()) || !runtime.getInstallationFile().equals(vm.getInstallLocation());
                }
                IStatus status = installType.validateInstallLocation(file);
                if (!status.isOK()) {
                    if (Objects.equals(file.getName(), "bin")) {
                        sendNotification(connection, "Invalid runtime for " + runtime.getName() + ": 'bin' should be removed from the path (" + runtime.getPath() + ").");
                    } else {
                        sendNotification(connection, "Invalid runtime for " + runtime.getName() + ": The path (" + runtime.getPath() + ") does not point to a JDK.");
                    }
                    JavaLanguageServerPlugin.log(status);
                    continue;
                }
                vmStandin.setName(runtime.getName());
                vmStandin.setInstallLocation(file);
                if (sourcePath != null || javadocURL != null) {
                    LibraryLocation[] libs;
                    if (vm != null && vm.getLibraryLocations() != null) {
                        libs = vm.getLibraryLocations();
                    } else {
                        StandardVMType svt = (StandardVMType) installType;
                        libs = svt.getDefaultLibraryLocations(file);
                    }
                    boolean libChanged = false;
                    if (libs != null) {
                        for (int i = 0; i < libs.length; i++) {
                            LibraryLocation lib = libs[i];
                            IPath systemSourcePath = sourcePath != null ? sourcePath : lib.getSystemLibrarySourcePath();
                            URL javadocLocation = javadocURL != null ? javadocURL : lib.getJavadocLocation();
                            LibraryLocation newLib = new LibraryLocation(lib.getSystemLibraryPath(), systemSourcePath, lib.getPackageRootPath(), javadocLocation, lib.getIndexLocation(), lib.getExternalAnnotationsPath());
                            libChanged = libChanged || !newLib.equals(lib);
                            libs[i] = newLib;
                        }
                    }
                    if (libChanged) {
                        LibraryLocation[] newLibs = Arrays.copyOf(libs, libs.length);
                        vmStandin.setLibraryLocations(newLibs);
                        changed = true;
                    }
                }
                vm = vmStandin.convertToRealVM();
                if (runtime.isDefault()) {
                    defaultVMSet = true;
                    if (!Objects.equals(vm, JavaRuntime.getDefaultVMInstall())) {
                        JavaLanguageServerPlugin.logInfo("Setting runtime " + runtime.getName() + "-" + runtime.getInstallationFile() + " as default global VM");
                        JavaRuntime.setDefaultVMInstall(vm, new NullProgressMonitor());
                        changed = true;
                    }
                }
                if (!setDefaultEnvironmentVM(vm, runtime.getName())) {
                    sendNotification(connection, "Invalid runtime for " + runtime.getName() + ": Runtime at '" + runtime.getPath() + "' is not compatible with the '" + runtime.getName() + "' environment.");
                    JavaLanguageServerPlugin.logError("Runtime at '" + runtime.getPath() + "' is not compatible with the '" + runtime.getName() + "' environment");
                }
            } else {
                sendNotification(connection, "Invalid runtime for " + runtime.getName() + ": The path points to a missing or inaccessible folder (" + runtime.getPath() + ").");
                JavaLanguageServerPlugin.logInfo("Invalid runtime: " + runtime);
            }
        }
    }
    if (!defaultVMSet) {
        changed = configureDefaultVM(preferences.getJavaHome()) || changed;
    }
    if (changed) {
        JavaLanguageServerPlugin.logInfo("JVM Runtimes changed, saving new configuration");
        JavaRuntime.saveVMConfiguration();
    }
    return changed;
}
Also used : NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) IStatus(org.eclipse.core.runtime.IStatus) IPath(org.eclipse.core.runtime.IPath) IVMInstallType(org.eclipse.jdt.launching.IVMInstallType) LibraryLocation(org.eclipse.jdt.launching.LibraryLocation) URL(java.net.URL) IVMInstall(org.eclipse.jdt.launching.IVMInstall) StandardVMType(org.eclipse.jdt.internal.launching.StandardVMType) File(java.io.File) VMStandin(org.eclipse.jdt.launching.VMStandin)

Example 3 with StandardVMType

use of org.eclipse.jdt.internal.launching.StandardVMType in project eclipse.jdt.ls by eclipse.

the class GradleProjectImporter method startSynchronization.

protected IStatus startSynchronization(Path projectFolder, IProgressMonitor monitor) {
    File location = projectFolder.toFile();
    boolean shouldSynchronize = shouldSynchronize(location);
    if (shouldSynchronize) {
        BuildConfiguration build = getBuildConfiguration(projectFolder);
        GradleBuild gradleBuild = GradleCore.getWorkspace().createBuild(build);
        SynchronizationResult result = gradleBuild.synchronize(monitor);
        IStatus resultStatus = result.getStatus();
        if (isFailedStatus(resultStatus)) {
            try {
                BuildEnvironment environment = gradleBuild.withConnection(connection -> connection.getModel(BuildEnvironment.class), monitor);
                GradleEnvironment gradleEnvironment = environment.getGradle();
                String gradleVersion = gradleEnvironment.getGradleVersion();
                File javaHome = getJavaHome(getPreferences());
                String javaVersion;
                if (javaHome == null) {
                    javaVersion = System.getProperty("java.version");
                } else {
                    StandardVMType type = new StandardVMType();
                    javaVersion = type.readReleaseVersion(javaHome);
                }
                if (GradleCompatibilityChecker.isIncompatible(GradleVersion.version(gradleVersion), javaVersion)) {
                    Path projectName = projectFolder.getName(projectFolder.getNameCount() - 1);
                    String message = String.format("Can't use Java %s and Gradle %s to import Gradle project %s.", javaVersion, gradleVersion, projectName.toString());
                    String highestJavaVersion = GradleCompatibilityChecker.getHighestSupportedJava(GradleVersion.version(gradleVersion));
                    return new GradleCompatibilityStatus(resultStatus, message, projectFolder.toUri().toString(), highestJavaVersion);
                }
            } catch (Exception e) {
            // Do nothing
            }
        }
        return resultStatus;
    }
    return Status.OK_STATUS;
}
Also used : IPath(org.eclipse.core.runtime.IPath) Path(java.nio.file.Path) IStatus(org.eclipse.core.runtime.IStatus) CoreException(org.eclipse.core.runtime.CoreException) IOException(java.io.IOException) BuildConfiguration(org.eclipse.buildship.core.BuildConfiguration) GradleEnvironment(org.gradle.tooling.model.build.GradleEnvironment) BuildEnvironment(org.gradle.tooling.model.build.BuildEnvironment) SynchronizationResult(org.eclipse.buildship.core.SynchronizationResult) StandardVMType(org.eclipse.jdt.internal.launching.StandardVMType) File(java.io.File) GradleBuild(org.eclipse.buildship.core.GradleBuild)

Aggregations

File (java.io.File)3 IStatus (org.eclipse.core.runtime.IStatus)3 StandardVMType (org.eclipse.jdt.internal.launching.StandardVMType)3 URL (java.net.URL)2 IPath (org.eclipse.core.runtime.IPath)2 IVMInstall (org.eclipse.jdt.launching.IVMInstall)2 IVMInstallType (org.eclipse.jdt.launching.IVMInstallType)2 LibraryLocation (org.eclipse.jdt.launching.LibraryLocation)2 IOException (java.io.IOException)1 Path (java.nio.file.Path)1 HashSet (java.util.HashSet)1 BuildConfiguration (org.eclipse.buildship.core.BuildConfiguration)1 GradleBuild (org.eclipse.buildship.core.GradleBuild)1 SynchronizationResult (org.eclipse.buildship.core.SynchronizationResult)1 CoreException (org.eclipse.core.runtime.CoreException)1 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)1 IEclipsePreferences (org.eclipse.core.runtime.preferences.IEclipsePreferences)1 IVMInstall2 (org.eclipse.jdt.launching.IVMInstall2)1 VMStandin (org.eclipse.jdt.launching.VMStandin)1 IExecutionEnvironment (org.eclipse.jdt.launching.environments.IExecutionEnvironment)1