Skip to content

Commit

Permalink
Check gradle-wrapper.jar
Browse files Browse the repository at this point in the history
Signed-off-by: Snjezana Peco <[email protected]>
  • Loading branch information
snjeza committed May 13, 2020
1 parent df7ac34 commit 60b4583
Show file tree
Hide file tree
Showing 19 changed files with 965 additions and 13 deletions.
5 changes: 3 additions & 2 deletions org.eclipse.jdt.ls.core/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ Export-Package: org.eclipse.jdt.ls.core.internal;x-friends:="org.eclipse.jdt.ls.
org.eclipse.jdt.ls.core.internal.commands;x-friends:="org.eclipse.jdt.ls.tests",
org.eclipse.jdt.ls.core.internal.contentassist;x-friends:="org.eclipse.jdt.ls.tests",
org.eclipse.jdt.ls.core.internal.corext.codemanipulation;x-friends:="org.eclipse.jdt.ls.tests",
org.eclipse.jdt.ls.core.internal.corext.template.java;x-friends:="org.eclipse.jdt.ls.tests",
org.eclipse.jdt.ls.core.internal.corext.dom;x-internal:=true,
org.eclipse.jdt.ls.core.internal.corext.refactoring;x-internal:=true,
org.eclipse.jdt.ls.core.internal.corext.refactoring.changes;x-internal:=true,
Expand All @@ -46,6 +45,7 @@ Export-Package: org.eclipse.jdt.ls.core.internal;x-friends:="org.eclipse.jdt.ls.
org.eclipse.jdt.ls.core.internal.corext.refactoring.reorg;x-internal:=true,
org.eclipse.jdt.ls.core.internal.corext.refactoring.tagging;x-internal:=true,
org.eclipse.jdt.ls.core.internal.corext.refactoring.util;x-internal:=true,
org.eclipse.jdt.ls.core.internal.corext.template.java;x-friends:="org.eclipse.jdt.ls.tests",
org.eclipse.jdt.ls.core.internal.corext.util;x-internal:=true,
org.eclipse.jdt.ls.core.internal.corrections;x-internal:=true,
org.eclipse.jdt.ls.core.internal.corrections.proposals;x-internal:=true,
Expand All @@ -56,9 +56,10 @@ Export-Package: org.eclipse.jdt.ls.core.internal;x-friends:="org.eclipse.jdt.ls.
org.eclipse.jdt.ls.core.internal.lsp;x-friends:="org.eclipse.jdt.ls.tests",
org.eclipse.jdt.ls.core.internal.managers;x-friends:="org.eclipse.jdt.ls.tests,org.eclipse.jdt.ls.tests.syntaxserver",
org.eclipse.jdt.ls.core.internal.preferences;x-friends:="org.eclipse.jdt.ls.tests,org.eclipse.jdt.ls.tests.syntaxserver",
org.eclipse.jdt.ls.core.internal.semantictokens;x-friends:="org.eclipse.jdt.ls.tests",
org.eclipse.jdt.ls.core.internal.syntaxserver;x-friends:="org.eclipse.jdt.ls.tests.syntaxserver",
org.eclipse.jdt.ls.core.internal.text.correction;x-friends:="org.eclipse.jdt.ls.tests",
org.eclipse.jdt.ls.core.internal.semantictokens;x-friends:="org.eclipse.jdt.ls.tests"
org.eclipse.jdt.ls.internal.gradle.checksums;x-friends:="org.eclipse.jdt.ls.tests"
Bundle-ClassPath: lib/jsoup-1.9.2.jar,
lib/remark-1.2.0.jar,
.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,10 @@ public static void waitForJobs(String jobFamily, IProgressMonitor monitor) {
}
}

public static void waitForLoadingGradleJobs() {
waitForJobs(LoadingGradleVersionJobMatcher.INSTANCE, MAX_TIME_MILLIS);
}

interface IJobMatcher {

boolean matches(Job job);
Expand Down Expand Up @@ -239,6 +243,17 @@ public boolean matches(Job job) {

}

static class LoadingGradleVersionJobMatcher implements IJobMatcher {

public static final IJobMatcher INSTANCE = new LoadingGradleVersionJobMatcher();

@Override
public boolean matches(Job job) {
return job.getClass().getName().matches("org.eclipse.buildship.core.internal.util.gradle.PublishedGradleVersionsWrapper.LoadVersionsJob");
}

}

static class DownloadSourcesJobMatcher implements IJobMatcher {

public static final IJobMatcher INSTANCE = new DownloadSourcesJobMatcher();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

import java.io.File;
import java.nio.file.Paths;
import java.util.Optional;

import org.eclipse.buildship.core.BuildConfiguration;
import org.eclipse.buildship.core.GradleBuild;
Expand Down Expand Up @@ -56,10 +57,17 @@ public void update(IProject project, boolean force, IProgressMonitor monitor) th
return;
}
JavaLanguageServerPlugin.logInfo("Starting Gradle update for " + project.getName());
String projectPath = project.getLocation().toFile().getAbsolutePath();
BuildConfiguration buildConfiguration = GradleProjectImporter.getBuildConfiguration(Paths.get(projectPath));
GradleBuild build = GradleCore.getWorkspace().createBuild(buildConfiguration);
build.synchronize(monitor);
if (force) {
String projectPath = project.getLocation().toFile().getAbsolutePath();
BuildConfiguration buildConfiguration = GradleProjectImporter.getBuildConfiguration(Paths.get(projectPath));
GradleBuild build = GradleCore.getWorkspace().createBuild(buildConfiguration);
build.synchronize(monitor);
} else {
Optional<GradleBuild> build = GradleCore.getWorkspace().getBuild(project);
if (build.isPresent()) {
build.get().synchronize(monitor);
}
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
*******************************************************************************/
package org.eclipse.jdt.ls.core.internal.managers;

import static java.util.Arrays.asList;

import java.io.File;
import java.io.FilenameFilter;
import java.nio.file.Files;
Expand Down Expand Up @@ -39,9 +41,13 @@
import org.eclipse.jdt.launching.IVMInstall;
import org.eclipse.jdt.launching.JavaRuntime;
import org.eclipse.jdt.ls.core.internal.AbstractProjectImporter;
import org.eclipse.jdt.ls.core.internal.ActionableNotification;
import org.eclipse.jdt.ls.core.internal.JavaLanguageServerPlugin;
import org.eclipse.jdt.ls.core.internal.ProjectUtils;
import org.eclipse.jdt.ls.core.internal.preferences.PreferenceManager;
import org.eclipse.jdt.ls.internal.gradle.checksums.ValidationResult;
import org.eclipse.jdt.ls.internal.gradle.checksums.WrapperValidator;
import org.eclipse.lsp4j.MessageType;

/**
* @author Fred Bricon
Expand Down Expand Up @@ -107,13 +113,37 @@ private void importDir(Path rootFolder, IProgressMonitor monitor) {
}

public static GradleDistribution getGradleDistribution(Path rootFolder) {
if (JavaLanguageServerPlugin.getPreferencesManager() != null && JavaLanguageServerPlugin.getPreferencesManager().getPreferences().isGradleWrapperEnabled() && Files.exists(rootFolder.resolve("gradlew"))) {
return GradleDistribution.fromBuild();
PreferenceManager preferencesManager = JavaLanguageServerPlugin.getPreferencesManager();
if (preferencesManager != null && preferencesManager.getPreferences().isGradleWrapperEnabled() && Files.exists(rootFolder.resolve("gradlew"))) {
WrapperValidator validator = new WrapperValidator();
try {
ValidationResult result = validator.checkWrapper(rootFolder.toFile().getAbsolutePath());
if (result.isValid()) {
WrapperGradleDistribution gradleDistribution = GradleDistribution.fromBuild();
return gradleDistribution;
} else {
if (!WrapperValidator.getDisallowed().contains(result.getSha256())) {
ProjectsManager pm = JavaLanguageServerPlugin.getProjectsManager();
if (pm != null && pm.getConnection() != null) {
//@formatter:off
String message = "__GRADLE_WRAPPER_SHA256__";
ActionableNotification notification = new ActionableNotification()
.withSeverity(MessageType.Warning)
.withMessage(message)
.withData(asList(result.getWrapperJar(), result.getSha256()));
pm.getConnection().sendActionableNotification(notification);
//@formatter:on
}
}
}
} catch (CoreException e) {
JavaLanguageServerPlugin.logException(e.getMessage(), e);
}
}
if (JavaLanguageServerPlugin.getPreferencesManager() != null && JavaLanguageServerPlugin.getPreferencesManager().getPreferences().getGradleVersion() != null) {
if (preferencesManager != null && preferencesManager.getPreferences().getGradleVersion() != null) {
List<GradleVersion> versions = CorePlugin.publishedGradleVersions().getVersions();
GradleVersion gradleVersion = null;
String versionString = JavaLanguageServerPlugin.getPreferencesManager().getPreferences().getGradleVersion();
String versionString = preferencesManager.getPreferences().getGradleVersion();
GradleVersion requiredVersion = GradleVersion.version(versionString);
for (GradleVersion version : versions) {
if (version.compareTo(requiredVersion) == 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,10 @@ public void setConnection(JavaLanguageClient client) {
this.client = client;
}

public JavaLanguageClient getConnection() {
return this.client;
}

private String getWorkspaceInfo() {
StringBuilder b = new StringBuilder();
b.append("Projects:\n");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import java.util.UUID;

import org.eclipse.core.internal.resources.PreferenceInitializer;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.preferences.DefaultScope;
import org.eclipse.core.runtime.preferences.IEclipsePreferences;
Expand All @@ -39,9 +40,12 @@
import org.eclipse.jdt.internal.core.manipulation.MembersOrderPreferenceCacheCommon;
import org.eclipse.jdt.ls.core.internal.IConstants;
import org.eclipse.jdt.ls.core.internal.JavaLanguageServerPlugin;
import org.eclipse.jdt.ls.core.internal.ProjectUtils;
import org.eclipse.jdt.ls.core.internal.ResourceUtils;
import org.eclipse.jdt.ls.core.internal.RuntimeEnvironment;
import org.eclipse.jdt.ls.core.internal.contentassist.TypeFilter;
import org.eclipse.jdt.ls.core.internal.managers.ProjectsManager;
import org.eclipse.jdt.ls.internal.gradle.checksums.WrapperValidator;
import org.eclipse.lsp4j.MessageType;

/**
Expand All @@ -60,7 +64,7 @@ public class Preferences {
* Specifies Java Execution Environments.
*/
public static final String JAVA_CONFIGURATION_RUNTIMES = "java.configuration.runtimes";
public static final Set<String> JAVA_CONFIGURATION_RUNTIMES_DEFAULT;
public static final List<String> JAVA_CONFIGURATION_RUNTIMES_DEFAULT;
/**
* Specifies the file path to the formatter xml url.
*/
Expand Down Expand Up @@ -209,6 +213,15 @@ public class Preferences {
*/
public static final String SELECTIONRANGE_ENABLED_KEY = "java.selectionRange.enabled";

/**
* A named preference that holds the allowed gradle wrapper sha256 checksums.
* <p>
* Value is of type <code>String</code>: list of checksums.
* </p>
*/
public static final String JAVA_GRADLE_WRAPPER_SHA256_KEY = "java.gradle.wrapper.sha256.checksums";
public static final List<String> JAVA_GRADLE_WRAPPER_SHA256_DEFAULT;

/**
* A named preference that holds the favorite static members.
* <p>
Expand Down Expand Up @@ -412,6 +425,8 @@ public class Preferences {
private String mavenUserSettings;

private List<String> javaCompletionFavoriteMembers;
private List<String> sha256Allowed;
private List<String> sha256Disallowed;

private List<String> javaImportExclusions = new LinkedList<>();
private ReferencedLibraries referencedLibraries;
Expand All @@ -430,7 +445,8 @@ public class Preferences {

static {
JAVA_IMPORT_EXCLUSIONS_DEFAULT = new LinkedList<>();
JAVA_CONFIGURATION_RUNTIMES_DEFAULT = new HashSet<>();
JAVA_CONFIGURATION_RUNTIMES_DEFAULT = new ArrayList<>();
JAVA_GRADLE_WRAPPER_SHA256_DEFAULT = new ArrayList<>();
JAVA_IMPORT_EXCLUSIONS_DEFAULT.add("**/node_modules/**");
JAVA_IMPORT_EXCLUSIONS_DEFAULT.add("**/.metadata/**");
JAVA_IMPORT_EXCLUSIONS_DEFAULT.add("**/archetype-resources/**");
Expand Down Expand Up @@ -731,6 +747,43 @@ public static Preferences createFrom(Map<String, Object> configuration) {
List<String> javaCompletionFavoriteMembers = getList(configuration, JAVA_COMPLETION_FAVORITE_MEMBERS_KEY, JAVA_COMPLETION_FAVORITE_MEMBERS_DEFAULT);
prefs.setJavaCompletionFavoriteMembers(javaCompletionFavoriteMembers);

List<?> gradleWrapperList = getList(configuration, JAVA_GRADLE_WRAPPER_SHA256_KEY, JAVA_GRADLE_WRAPPER_SHA256_DEFAULT);
List<String> sha256Allowed = new ArrayList<>();
List<String> sha256Disallowed = new ArrayList<>();
for (Object object : gradleWrapperList) {
if (object instanceof Map) {
Map<?, ?> map = (Map<?, ?>) object;
final Sha256 sha256 = prefs.new Sha256();
sha256.allowed = true;
map.forEach((k, v) -> {
if (k instanceof String) {
switch ((String) k) {
case "sha256":
if (v instanceof String) {
sha256.sha256 = (String) v;
}
break;
case "allowed":
if (v instanceof Boolean) {
sha256.allowed = (Boolean) v;
}
break;
default:
break;
}
}
});
if (sha256.sha256 != null) {
if (sha256.allowed) {
sha256Allowed.add(sha256.sha256);
} else {
sha256Disallowed.add(sha256.sha256);
}
}
}
}
prefs.putSha256(sha256Allowed, sha256Disallowed);

String mavenUserSettings = getString(configuration, MAVEN_USER_SETTINGS_KEY, null);
prefs.setMavenUserSettings(mavenUserSettings);

Expand Down Expand Up @@ -771,7 +824,7 @@ public static Preferences createFrom(Map<String, Object> configuration) {
int staticOnDemandThreshold = getInt(configuration, IMPORTS_STATIC_ONDEMANDTHRESHOLD, IMPORTS_STATIC_ONDEMANDTHRESHOLD_DEFAULT);
prefs.setStaticImportOnDemandThreshold(staticOnDemandThreshold);

List<?> runtimeList = getList(configuration, JAVA_CONFIGURATION_RUNTIMES, JAVA_IMPORT_ORDER_DEFAULT);
List<?> runtimeList = getList(configuration, JAVA_CONFIGURATION_RUNTIMES, JAVA_CONFIGURATION_RUNTIMES_DEFAULT);
Set<RuntimeEnvironment> runtimes = new HashSet<>();
boolean[] hasDefault = { false };
for (Object object : runtimeList) {
Expand Down Expand Up @@ -888,6 +941,25 @@ public Preferences setJavaCompletionFavoriteMembers(List<String> javaCompletionF
return this;
}

public Preferences putSha256(List<String> sha256Allowed, List<String> sha256Disallowed) {
List<String> oldAllowed = this.sha256Allowed;
List<String> oldDisallowed = this.sha256Disallowed;
WrapperValidator.getAllowed().clear();
this.sha256Allowed = sha256Allowed;
WrapperValidator.getDisallowed().clear();
if (sha256Disallowed != null) {
WrapperValidator.getDisallowed().addAll(sha256Disallowed);
}
this.sha256Disallowed = sha256Disallowed;
ProjectsManager projectsManager = JavaLanguageServerPlugin.getProjectsManager();
if (projectsManager != null && (!Objects.equals(oldAllowed, this.sha256Allowed) || !Objects.equals(oldDisallowed, this.sha256Disallowed))) {
for (IProject project : ProjectUtils.getGradleProjects()) {
projectsManager.updateProject(project, true);
}
}
return this;
}

private Preferences setMembersSortOrder(String sortOrder) {
if (sortOrder != null) {
IEclipsePreferences fPreferenceStore = InstanceScope.INSTANCE.getNode(IConstants.PLUGIN_ID);
Expand Down Expand Up @@ -1092,6 +1164,10 @@ public String[] getJavaCompletionFavoriteMembers() {
return javaCompletionFavoriteMembers.toArray(new String[0]);
}

public List<String> getSha256Allowed() {
return sha256Allowed;
}

public String getJavaHome() {
return javaHome;
}
Expand Down Expand Up @@ -1367,4 +1443,9 @@ public Preferences setStaticImportOnDemandThreshold(int staticImportOnDemandThre
defEclipsePrefs.put(CodeStyleConfiguration.ORGIMPORTS_STATIC_ONDEMANDTHRESHOLD, String.valueOf(this.staticImportOnDemandThreshold));
return this;
}

class Sha256 {
private String sha256;
private boolean allowed;
}
}
Loading

0 comments on commit 60b4583

Please sign in to comment.