|
| 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