All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.sonar.plugins.csharp.CSharpSonarWayProfile Maven / Gradle / Ivy

There is a newer version: 10.2.0.105762
Show newest version
/*
 * SonarC#
 * Copyright (C) 2014-2024 SonarSource SA
 * mailto:info AT sonarsource DOT com
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 3 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */
package org.sonar.plugins.csharp;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashSet;
import java.util.Set;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.sonarsource.dotnet.shared.plugins.AbstractSonarWayProfile;

public class CSharpSonarWayProfile extends AbstractSonarWayProfile {
  private static final Logger LOG = LoggerFactory.getLogger(CSharpSonarWayProfile.class);

  CSharpSonarWayProfile() {
    super(CSharpPlugin.LANGUAGE_KEY, CSharpPlugin.PLUGIN_KEY, CSharpPlugin.REPOSITORY_KEY);
  }

  @Override
  protected void activateSecurityRules(NewBuiltInQualityProfile sonarWay) {
    final String securityRepositoryKey = getSecurityRepositoryKey();
    try {
      getSecurityRuleKeys().forEach(key -> sonarWay.activateRule(securityRepositoryKey, key));
    } catch (IllegalArgumentException|IllegalStateException e) {
      LOG.warn("Could not activate C# security rules", e);
    }
  }

  private static Set getSecurityRuleKeys() {
    try {
      Class csRulesClass = Class.forName("com.sonar.plugins.security.api.CsRules");
      Method getRuleKeysMethod = csRulesClass.getMethod("getRuleKeys");
      return (Set) getRuleKeysMethod.invoke(null);
    } catch (ClassNotFoundException|NoSuchMethodException e) {
      LOG.debug("com.sonar.plugins.security.api.CsRules#getRuleKeys is not found, no security rules added to Sonar way cs profile: {}", e.getMessage());
    } catch (IllegalAccessException|InvocationTargetException e) {
      LOG.debug("[{}] No security rules added to Sonar way cs profile: {}", e.getClass().getName(), e.getMessage());
    }

    return new HashSet<>();
  }

  private static String getSecurityRepositoryKey() {
    try {
      Class csRulesClass = Class.forName("com.sonar.plugins.security.api.CsRules");
      Method getRuleKeysMethod = csRulesClass.getMethod("getRepositoryKey");
      return (String) getRuleKeysMethod.invoke(null);
    } catch (ClassNotFoundException|NoSuchMethodException|IllegalAccessException|InvocationTargetException e) {
      LOG.debug("com.sonar.plugins.security.api.CsRules#getRepositoryKey is not found, will use default repository key: {}", e.getMessage());
    }
    return CSharpPlugin.REPOSITORY_KEY;
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy