diff --git a/com.microsoft.java.debug.plugin/plugin.xml b/com.microsoft.java.debug.plugin/plugin.xml index 634734dd6..c1bcaf4fa 100644 --- a/com.microsoft.java.debug.plugin/plugin.xml +++ b/com.microsoft.java.debug.plugin/plugin.xml @@ -12,6 +12,7 @@ + diff --git a/com.microsoft.java.debug.plugin/src/main/java/com/microsoft/java/debug/plugin/internal/JavaDebugDelegateCommandHandler.java b/com.microsoft.java.debug.plugin/src/main/java/com/microsoft/java/debug/plugin/internal/JavaDebugDelegateCommandHandler.java index df39eb8a7..f80fde1d0 100644 --- a/com.microsoft.java.debug.plugin/src/main/java/com/microsoft/java/debug/plugin/internal/JavaDebugDelegateCommandHandler.java +++ b/com.microsoft.java.debug.plugin/src/main/java/com/microsoft/java/debug/plugin/internal/JavaDebugDelegateCommandHandler.java @@ -1,5 +1,5 @@ /******************************************************************************* -* Copyright (c) 2017 Microsoft Corporation and others. +* Copyright (c) 2017-2019 Microsoft Corporation and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at @@ -30,6 +30,7 @@ public class JavaDebugDelegateCommandHandler implements IDelegateCommandHandler public static final String VALIDATE_LAUNCHCONFIG = "vscode.java.validateLaunchConfig"; public static final String RESOLVE_MAINMETHOD = "vscode.java.resolveMainMethod"; public static final String INFER_LAUNCH_COMMAND_LENGTH = "vscode.java.inferLaunchCommandLength"; + public static final String CHECK_PROJECT_SETTINGS = "vscode.java.checkProjectSettings"; @Override public Object executeCommand(String commandId, List arguments, IProgressMonitor progress) throws Exception { @@ -57,6 +58,8 @@ public Object executeCommand(String commandId, List arguments, IProgress return ResolveMainMethodHandler.resolveMainMethods(arguments); case INFER_LAUNCH_COMMAND_LENGTH: return LaunchCommandHandler.getLaunchCommandLength(JsonUtils.fromJson((String) arguments.get(0), LaunchArguments.class)); + case CHECK_PROJECT_SETTINGS: + return ProjectSettingsChecker.check(JsonUtils.fromJson((String) arguments.get(0), ProjectSettingsChecker.ProjectSettingsCheckerParams.class)); default: break; } diff --git a/com.microsoft.java.debug.plugin/src/main/java/com/microsoft/java/debug/plugin/internal/ProjectSettingsChecker.java b/com.microsoft.java.debug.plugin/src/main/java/com/microsoft/java/debug/plugin/internal/ProjectSettingsChecker.java new file mode 100644 index 000000000..db2b783d9 --- /dev/null +++ b/com.microsoft.java.debug.plugin/src/main/java/com/microsoft/java/debug/plugin/internal/ProjectSettingsChecker.java @@ -0,0 +1,65 @@ +/******************************************************************************* + * Copyright (c) 2019 Microsoft Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Microsoft Corporation - initial API and implementation + *******************************************************************************/ + +package com.microsoft.java.debug.plugin.internal; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Map.Entry; +import java.util.Objects; + +import org.apache.commons.lang3.StringUtils; +import org.eclipse.core.runtime.CoreException; +import org.eclipse.jdt.core.IJavaProject; + +public class ProjectSettingsChecker { + /** + * Check whether the project settings are all matched as the expected. + * @param params - The checker parameters + * @return true if all settings is matched + */ + public static boolean check(ProjectSettingsCheckerParams params) { + Map options = new HashMap<>(); + IJavaProject javaProject = null; + if (StringUtils.isNotBlank(params.projectName)) { + javaProject = JdtUtils.getJavaProject(params.projectName); + } else if (StringUtils.isNotBlank(params.className)) { + try { + List projects = ResolveClasspathsHandler.getJavaProjectFromType(params.className); + if (!projects.isEmpty()) { + javaProject = projects.get(0); + } + } catch (CoreException e) { + // do nothing + } + } + + if (javaProject != null) { + options = javaProject.getOptions(params.inheritedOptions); + } + + for (Entry expected : params.expectedOptions.entrySet()) { + if (!Objects.equals(options.get(expected.getKey()), expected.getValue())) { + return false; + } + } + + return true; + } + + public static class ProjectSettingsCheckerParams { + String className; + String projectName; + boolean inheritedOptions; + Map expectedOptions; + } +}