Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check Gradle compatibility when importing fails #1965

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion org.eclipse.jdt.ls.core/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,9 @@
<command
id="java.navigate.resolveTypeHierarchy">
</command>
<command
id="java.project.upgradeGradle">
</command>
</delegateCommandHandler>
</extension>
<extension
Expand Down Expand Up @@ -167,7 +170,7 @@
</extension>
<extension id="org.eclipse.jdt.ls.filesystem" name="JDT LS File System" point="org.eclipse.core.filesystem.filesystems">
<filesystem scheme="file">
<run class="org.eclipse.jdt.ls.core.internal.filesystem.JLSFileSystem"/>
<run class="org.eclipse.jdt.ls.core.internal.filesystem.JLSFileSystem"/>
</filesystem>
</extension>
</plugin>
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,23 @@ public enum EventType {
* classpath updated event.
*/
ClasspathUpdated(100),

/**
* projects imported event.
*/
ProjectsImported(200);

ProjectsImported(200),

/**
* Incompatible issue between Gradle and Jdk event.
*/
IncompatibleGradleJdkIssue(300);

private final int value;

EventType(int value) {
this.value = value;
}

public int getValue() {
return value;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.eclipse.jdt.ls.core.internal.commands.ProjectCommand.ClasspathOptions;
import org.eclipse.jdt.ls.core.internal.handlers.FormatterHandler;
import org.eclipse.jdt.ls.core.internal.handlers.ResolveSourceMappingHandler;
import org.eclipse.jdt.ls.core.internal.managers.GradleProjectImporter;
import org.eclipse.jdt.ls.core.internal.commands.SourceAttachmentCommand;
import org.eclipse.jdt.ls.core.internal.commands.TypeHierarchyCommand;
import org.eclipse.lsp4j.ResolveTypeHierarchyItemParams;
Expand Down Expand Up @@ -114,6 +115,9 @@ public Object executeCommand(String commandId, List<Object> arguments, IProgress
params.setPosition(textParams.getPosition());
TypeHierarchyItem typeHierarchyItem = typeHierarchyCommand.typeHierarchy(params, monitor);
return typeHierarchyItem;
case "java.project.upgradeGradle":
String projectDir = (String) arguments.get(0);
return GradleProjectImporter.upgradeGradleVersion(projectDir, monitor);
default:
break;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*******************************************************************************
* Copyright (c) 2018-2021 Microsoft Corporation and others.
rgrunber marked this conversation as resolved.
Show resolved Hide resolved
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Microsoft Corporation - initial API and implementation
*******************************************************************************/
package org.eclipse.jdt.ls.core.internal.managers;

import org.eclipse.buildship.core.internal.util.gradle.GradleVersion;
import org.eclipse.jdt.core.JavaCore;

public class GradleCompatibilityChecker {

public static String MAX_SUPPORTED_JAVA = JavaCore.VERSION_17;
public static String CURRENT_GRADLE = "7.3.1";

public static boolean compatibilityCheck(GradleVersion gradleVersion, String javaVersion) {
rgrunber marked this conversation as resolved.
Show resolved Hide resolved
if (gradleVersion == null || javaVersion == null || javaVersion.isEmpty()) {
return false;
}
String highestSupportedJava = getHighestSupportedJava(gradleVersion);
return JavaCore.compareJavaVersions(javaVersion, highestSupportedJava) > 0;
}

public static String getHighestSupportedJava(GradleVersion gradleVersion) {
GradleVersion baseVersion = gradleVersion.getBaseVersion();
try {
// https://docs.gradle.org/current/userguide/compatibility.html
if (baseVersion.compareTo(GradleVersion.version("7.3")) >= 0) {
return JavaCore.VERSION_17;
} else if (baseVersion.compareTo(GradleVersion.version("7.0")) >= 0) {
return JavaCore.VERSION_16;
} else if (baseVersion.compareTo(GradleVersion.version("6.7")) >= 0) {
return JavaCore.VERSION_15;
} else if (baseVersion.compareTo(GradleVersion.version("6.3")) >= 0) {
return JavaCore.VERSION_14;
} else if (baseVersion.compareTo(GradleVersion.version("6.0")) >= 0) {
return JavaCore.VERSION_13;
} else if (baseVersion.compareTo(GradleVersion.version("5.4")) >= 0) {
return JavaCore.VERSION_12;
} else if (baseVersion.compareTo(GradleVersion.version("5.0")) >= 0) {
return JavaCore.VERSION_11;
} else if (baseVersion.compareTo(GradleVersion.version("4.7")) >= 0) {
return JavaCore.VERSION_10;
} else if (baseVersion.compareTo(GradleVersion.version("4.3")) >= 0) {
return JavaCore.VERSION_9;
}
return JavaCore.VERSION_1_8;
} catch (IllegalArgumentException e) {
return MAX_SUPPORTED_JAVA;
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*******************************************************************************
* Copyright (c) 2021 Microsoft Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Microsoft Corporation - initial API and implementation
*******************************************************************************/
package org.eclipse.jdt.ls.core.internal.managers;

import org.eclipse.core.resources.IMarker;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.MultiStatus;
import org.eclipse.jdt.ls.core.internal.IConstants;
import org.eclipse.jdt.ls.core.internal.managers.GradleProjectImporter.GradleCompatibilityStatus;

public class GradleCompatibilityErrorMarker {
public static final String ID = IConstants.PLUGIN_ID + ".gradlecompatibilityerrormarker";

public static void createMarker(IResource resource, GradleCompatibilityStatus status) {
rgrunber marked this conversation as resolved.
Show resolved Hide resolved
try {
IMarker marker = resource.createMarker(GradleCompatibilityErrorMarker.ID);
marker.setAttribute(IMarker.LINE_NUMBER, 1);
marker.setAttribute(IMarker.MESSAGE, status.getMessage());
marker.setAttribute(IMarker.SEVERITY, IMarker.SEVERITY_ERROR);
} catch (CoreException e) {
// do nothing
}
}
}
Loading