Skip to content

Commit 81bdeb2

Browse files
Add utility to check java project settings (#264)
Signed-off-by: Jinbo Wang <[email protected]>
1 parent c140561 commit 81bdeb2

File tree

3 files changed

+70
-1
lines changed

3 files changed

+70
-1
lines changed

com.microsoft.java.debug.plugin/plugin.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
<command id="vscode.java.validateLaunchConfig"/>
1313
<command id="vscode.java.resolveMainMethod"/>
1414
<command id="vscode.java.inferLaunchCommandLength"/>
15+
<command id="vscode.java.checkProjectSettings"/>
1516
</delegateCommandHandler>
1617
</extension>
1718
</plugin>

com.microsoft.java.debug.plugin/src/main/java/com/microsoft/java/debug/plugin/internal/JavaDebugDelegateCommandHandler.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*******************************************************************************
2-
* Copyright (c) 2017 Microsoft Corporation and others.
2+
* Copyright (c) 2017-2019 Microsoft Corporation and others.
33
* All rights reserved. This program and the accompanying materials
44
* are made available under the terms of the Eclipse Public License v1.0
55
* which accompanies this distribution, and is available at
@@ -30,6 +30,7 @@ public class JavaDebugDelegateCommandHandler implements IDelegateCommandHandler
3030
public static final String VALIDATE_LAUNCHCONFIG = "vscode.java.validateLaunchConfig";
3131
public static final String RESOLVE_MAINMETHOD = "vscode.java.resolveMainMethod";
3232
public static final String INFER_LAUNCH_COMMAND_LENGTH = "vscode.java.inferLaunchCommandLength";
33+
public static final String CHECK_PROJECT_SETTINGS = "vscode.java.checkProjectSettings";
3334

3435
@Override
3536
public Object executeCommand(String commandId, List<Object> arguments, IProgressMonitor progress) throws Exception {
@@ -57,6 +58,8 @@ public Object executeCommand(String commandId, List<Object> arguments, IProgress
5758
return ResolveMainMethodHandler.resolveMainMethods(arguments);
5859
case INFER_LAUNCH_COMMAND_LENGTH:
5960
return LaunchCommandHandler.getLaunchCommandLength(JsonUtils.fromJson((String) arguments.get(0), LaunchArguments.class));
61+
case CHECK_PROJECT_SETTINGS:
62+
return ProjectSettingsChecker.check(JsonUtils.fromJson((String) arguments.get(0), ProjectSettingsChecker.ProjectSettingsCheckerParams.class));
6063
default:
6164
break;
6265
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*******************************************************************************
2+
* Copyright (c) 2019 Microsoft Corporation and others.
3+
* All rights reserved. This program and the accompanying materials
4+
* are made available under the terms of the Eclipse Public License v1.0
5+
* which accompanies this distribution, and is available at
6+
* http://www.eclipse.org/legal/epl-v10.html
7+
*
8+
* Contributors:
9+
* Microsoft Corporation - initial API and implementation
10+
*******************************************************************************/
11+
12+
package com.microsoft.java.debug.plugin.internal;
13+
14+
import java.util.HashMap;
15+
import java.util.List;
16+
import java.util.Map;
17+
import java.util.Map.Entry;
18+
import java.util.Objects;
19+
20+
import org.apache.commons.lang3.StringUtils;
21+
import org.eclipse.core.runtime.CoreException;
22+
import org.eclipse.jdt.core.IJavaProject;
23+
24+
public class ProjectSettingsChecker {
25+
/**
26+
* Check whether the project settings are all matched as the expected.
27+
* @param params - The checker parameters
28+
* @return true if all settings is matched
29+
*/
30+
public static boolean check(ProjectSettingsCheckerParams params) {
31+
Map<String, String> options = new HashMap<>();
32+
IJavaProject javaProject = null;
33+
if (StringUtils.isNotBlank(params.projectName)) {
34+
javaProject = JdtUtils.getJavaProject(params.projectName);
35+
} else if (StringUtils.isNotBlank(params.className)) {
36+
try {
37+
List<IJavaProject> projects = ResolveClasspathsHandler.getJavaProjectFromType(params.className);
38+
if (!projects.isEmpty()) {
39+
javaProject = projects.get(0);
40+
}
41+
} catch (CoreException e) {
42+
// do nothing
43+
}
44+
}
45+
46+
if (javaProject != null) {
47+
options = javaProject.getOptions(params.inheritedOptions);
48+
}
49+
50+
for (Entry<String, String> expected : params.expectedOptions.entrySet()) {
51+
if (!Objects.equals(options.get(expected.getKey()), expected.getValue())) {
52+
return false;
53+
}
54+
}
55+
56+
return true;
57+
}
58+
59+
public static class ProjectSettingsCheckerParams {
60+
String className;
61+
String projectName;
62+
boolean inheritedOptions;
63+
Map<String, String> expectedOptions;
64+
}
65+
}

0 commit comments

Comments
 (0)