Skip to content

Support update Java project options. #3162

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

Merged
merged 2 commits into from
May 28, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.Map.Entry;
import java.util.Objects;
import java.util.stream.Stream;
Expand Down Expand Up @@ -721,6 +722,8 @@ public JdkUpdateResult(boolean success, String message) {
public static void updateProjectSettings(String projectUri, Map<String, Object> options) throws CoreException, URISyntaxException {
IJavaProject javaProject = getJavaProjectFromUri(projectUri);
IProject project = javaProject.getProject();
Map<String, String> newOptions = new HashMap<>();
final Set<String> validOptionKeys = JavaCore.getDefaultOptions().keySet();
for (Map.Entry<String, Object> entry : options.entrySet()) {
switch (entry.getKey()) {
case M2E_SELECTED_PROFILES:
Expand All @@ -744,8 +747,16 @@ public static void updateProjectSettings(String projectUri, Map<String, Object>
}
break;
default:
if (validOptionKeys.contains(entry.getKey())) {
newOptions.put(entry.getKey(), entry.getValue().toString());
}
break;
}
}
if (!newOptions.isEmpty()) {
Map<String, String> javaProjectOptions = javaProject.getOptions(false);
javaProjectOptions.putAll(newOptions);
javaProject.setOptions(javaProjectOptions);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -467,6 +467,24 @@ public void testUpdateMavenProfiles() throws Exception {
assertEquals("my profile", options.get(KEY));
}

@Test
public void testUpdateProjectOptions() throws Exception {
importProjects("maven/salut");
IProject project = WorkspaceHelper.getProject("salut");
String uriString = project.getLocationURI().toString();
List<String> settingKeys = Arrays.asList(JavaCore.COMPILER_CODEGEN_METHOD_PARAMETERS_ATTR);
Map<String, Object> options = ProjectCommand.getProjectSettings(uriString, settingKeys);

assertEquals(JavaCore.DO_NOT_GENERATE, options.get(JavaCore.COMPILER_CODEGEN_METHOD_PARAMETERS_ATTR));

Map<String, Object> updateOptions = new HashMap<>();
updateOptions.put(JavaCore.COMPILER_CODEGEN_METHOD_PARAMETERS_ATTR, JavaCore.GENERATE);
ProjectCommand.updateProjectSettings(uriString, updateOptions);

options = ProjectCommand.getProjectSettings(uriString, settingKeys);
assertEquals(JavaCore.GENERATE, options.get(JavaCore.COMPILER_CODEGEN_METHOD_PARAMETERS_ATTR));
}

private SymbolInformation buildClassSymbol(IProject project, String fqClassName) throws Exception {
String uriString = buildClassfileUri(project, fqClassName);
SymbolInformation si = new SymbolInformation();
Expand Down
Loading