|
| 1 | +/* |
| 2 | + * SonarQube Java |
| 3 | + * Copyright (C) 2012-2025 SonarSource Sàrl |
| 4 | + * mailto:info AT sonarsource DOT com |
| 5 | + * |
| 6 | + * This program is free software; you can redistribute it and/or |
| 7 | + * modify it under the terms of the Sonar Source-Available License Version 1, as published by SonarSource SA. |
| 8 | + * |
| 9 | + * This program is distributed in the hope that it will be useful, |
| 10 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 12 | + * See the Sonar Source-Available License for more details. |
| 13 | + * |
| 14 | + * You should have received a copy of the Sonar Source-Available License |
| 15 | + * along with this program; if not, see https://sonarsource.com/license/ssal/ |
| 16 | + */ |
| 17 | +package org.sonar.plugins.java; |
| 18 | + |
| 19 | +import java.lang.reflect.InvocationTargetException; |
| 20 | +import java.lang.reflect.Method; |
| 21 | +import java.util.HashSet; |
| 22 | +import java.util.Set; |
| 23 | +import java.util.stream.Collectors; |
| 24 | +import javax.annotation.Nullable; |
| 25 | +import org.slf4j.Logger; |
| 26 | +import org.slf4j.LoggerFactory; |
| 27 | +import org.sonar.api.rule.RuleKey; |
| 28 | +import org.sonar.api.server.profile.BuiltInQualityProfilesDefinition; |
| 29 | +import org.sonar.java.GeneratedCheckList; |
| 30 | +import org.sonar.java.annotations.VisibleForTesting; |
| 31 | +import org.sonar.plugins.java.api.ProfileRegistrar; |
| 32 | +import org.sonarsource.analyzer.commons.BuiltInQualityProfileJsonLoader; |
| 33 | + |
| 34 | + |
| 35 | +/** |
| 36 | + * Defines a Java quality profile including rules from sonar-java, Java SE, DBD and Security. |
| 37 | + */ |
| 38 | +abstract class BuiltInJavaQualityProfile implements BuiltInQualityProfilesDefinition { |
| 39 | + private static final Logger LOG = LoggerFactory.getLogger(BuiltInJavaQualityProfile.class); |
| 40 | + |
| 41 | + static final String SECURITY_RULES_CLASS_NAME = "com.sonar.plugins.security.api.JavaRules"; |
| 42 | + static final String DBD_RULES_CLASS_NAME = "com.sonarsource.plugins.dbd.api.JavaRules"; |
| 43 | + static final String SECURITY_RULE_KEYS_METHOD_NAME = "getSecurityRuleKeys"; |
| 44 | + static final String DBD_RULE_KEYS_METHOD_NAME = "getDataflowBugDetectionRuleKeys"; |
| 45 | + static final String GET_REPOSITORY_KEY = "getRepositoryKey"; |
| 46 | + static final String SECURITY_REPOSITORY_KEY = "javasecurity"; |
| 47 | + static final String DBD_REPOSITORY_KEY = "javabugs"; |
| 48 | + |
| 49 | + |
| 50 | + protected final ProfileRegistrar[] profileRegistrars; |
| 51 | + |
| 52 | + BuiltInJavaQualityProfile(@Nullable ProfileRegistrar[] profileRegistrars) { |
| 53 | + this.profileRegistrars = profileRegistrars; |
| 54 | + } |
| 55 | + |
| 56 | + abstract String getProfileName(); |
| 57 | + |
| 58 | + abstract String getPathToJsonProfile(); |
| 59 | + |
| 60 | + abstract boolean isDefault(); |
| 61 | + |
| 62 | + @Override |
| 63 | + public void define(Context context) { |
| 64 | + // Create a new profile |
| 65 | + BuiltInQualityProfilesDefinition.NewBuiltInQualityProfile profile = context.createBuiltInQualityProfile(getProfileName(), Java.KEY); |
| 66 | + // Load rules from local JSON |
| 67 | + Set<RuleKey> ruleKeys = registerRulesFromJson(getPathToJsonProfile(), profileRegistrars); |
| 68 | + |
| 69 | + // FIXME as part of SONARJAVA-6207 |
| 70 | + // Former activation mechanism, it should be removed once sonar-security and sonar-dataflow-bug-detection |
| 71 | + // support the new mechanism: |
| 72 | + // <code> registrarContext.internal().registerDefaultQualityProfileRules(ruleKeys); </code> |
| 73 | + // For now, it still uses reflexion if rules are not yet defined |
| 74 | + if (ruleKeys.stream().noneMatch(rule -> SECURITY_REPOSITORY_KEY.equals(rule.repository()))) { |
| 75 | + ruleKeys.addAll(getSecurityRuleKeys()); |
| 76 | + } |
| 77 | + if (ruleKeys.stream().noneMatch(rule -> DBD_REPOSITORY_KEY.equals(rule.repository()))) { |
| 78 | + ruleKeys.addAll(getDataflowBugDetectionRuleKeys()); |
| 79 | + } |
| 80 | + |
| 81 | + ruleKeys.forEach(ruleKey -> profile.activateRule(ruleKey.repository(), ruleKey.rule())); |
| 82 | + profile.setDefault(isDefault()); |
| 83 | + profile.done(); |
| 84 | + } |
| 85 | + |
| 86 | + static Set<RuleKey> registerRulesFromJson(String pathToJsonProfile, @Nullable ProfileRegistrar[] profileRegistrars) { |
| 87 | + Set<RuleKey> ruleKeys = new HashSet<>(loadRuleKeys(pathToJsonProfile)); |
| 88 | + if (profileRegistrars != null) { |
| 89 | + for (ProfileRegistrar profileRegistrar : profileRegistrars) { |
| 90 | + profileRegistrar.register(ruleKeys::addAll); |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + return ruleKeys; |
| 95 | + } |
| 96 | + |
| 97 | + static Set<RuleKey> loadRuleKeys(final String pathToJsonProfile) { |
| 98 | + return BuiltInQualityProfileJsonLoader.loadActiveKeysFromJsonProfile(pathToJsonProfile).stream() |
| 99 | + .map(rule -> RuleKey.of(GeneratedCheckList.REPOSITORY_KEY, rule)) |
| 100 | + .collect(Collectors.toSet()); |
| 101 | + } |
| 102 | + |
| 103 | + @VisibleForTesting |
| 104 | + Set<RuleKey> getSecurityRuleKeys() { |
| 105 | + return getExternalRuleKeys(SECURITY_RULES_CLASS_NAME, SECURITY_RULE_KEYS_METHOD_NAME, "security"); |
| 106 | + } |
| 107 | + |
| 108 | + @VisibleForTesting |
| 109 | + Set<RuleKey> getDataflowBugDetectionRuleKeys() { |
| 110 | + return getExternalRuleKeys(DBD_RULES_CLASS_NAME, DBD_RULE_KEYS_METHOD_NAME, "dataflow bug detection"); |
| 111 | + } |
| 112 | + |
| 113 | + @SuppressWarnings("unchecked") |
| 114 | + @VisibleForTesting |
| 115 | + Set<RuleKey> getExternalRuleKeys(String className, String ruleKeysMethod, String rulesCategory) { |
| 116 | + try { |
| 117 | + Class<?> javaRulesClass = Class.forName(className); |
| 118 | + Method getRuleKeysMethod = javaRulesClass.getMethod(ruleKeysMethod); |
| 119 | + Set<String> ruleKeys = (Set<String>) getRuleKeysMethod.invoke(null); |
| 120 | + Method getRepositoryKeyMethod = javaRulesClass.getMethod(GET_REPOSITORY_KEY); |
| 121 | + String repositoryKey = (String) getRepositoryKeyMethod.invoke(null); |
| 122 | + return ruleKeys.stream().map(k -> RuleKey.of(repositoryKey, k)).collect(Collectors.toSet()); |
| 123 | + } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException | InvocationTargetException e) { |
| 124 | + LOG.debug(String.format("[%s], no %s rules added to %s java profile: %s", e.getClass().getSimpleName(), rulesCategory, getProfileName(), e.getMessage())); |
| 125 | + } |
| 126 | + return new HashSet<>(); |
| 127 | + } |
| 128 | +} |
0 commit comments