2 * SonarQube JavaScript Plugin
3 * Copyright (C) 2011-2016 SonarSource SA
4 * mailto:contact AT sonarsource DOT com
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 3 of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 package de.example.plugins.custom.javascript;
22 import com.google.common.collect.Lists;
23 import com.google.common.collect.Sets;
24 import java.util.ArrayList;
25 import java.util.List;
27 import javax.annotation.Nullable;
28 import org.sonar.api.batch.rule.CheckFactory;
29 import org.sonar.api.batch.rule.Checks;
30 import org.sonar.api.rule.RuleKey;
31 import org.sonar.javascript.se.SeCheck;
32 import org.sonar.plugins.javascript.api.CustomJavaScriptRulesDefinition;
33 import org.sonar.plugins.javascript.api.JavaScriptCheck;
34 import org.sonar.plugins.javascript.api.visitors.TreeVisitor;
37 * Wrapper around Checks Object to ease the manipulation of the different JavaScript rule repositories.
39 public class JavaScriptChecks {
41 private final CheckFactory checkFactory;
42 private Set<Checks<JavaScriptCheck>> checksByRepository = Sets.newHashSet();
44 private JavaScriptChecks(CheckFactory checkFactory) {
45 this.checkFactory = checkFactory;
48 public static JavaScriptChecks createJavaScriptCheck(CheckFactory checkFactory) {
49 return new JavaScriptChecks(checkFactory);
52 public JavaScriptChecks addChecks(String repositoryKey, Iterable<Class> checkClass) {
53 checksByRepository.add(checkFactory
54 .<JavaScriptCheck>create(repositoryKey)
55 .addAnnotatedChecks(checkClass));
60 public JavaScriptChecks addCustomChecks(@Nullable CustomJavaScriptRulesDefinition[] customRulesDefinitions) {
61 if (customRulesDefinitions != null) {
63 for (CustomJavaScriptRulesDefinition rulesDefinition : customRulesDefinitions) {
64 addChecks(rulesDefinition.repositoryKey(), Lists.newArrayList(rulesDefinition.checkClasses()));
71 private List<JavaScriptCheck> all() {
72 List<JavaScriptCheck> allVisitors = Lists.newArrayList();
74 for (Checks<JavaScriptCheck> checks : checksByRepository) {
75 allVisitors.addAll(checks.all());
81 public List<SeCheck> seChecks() {
82 List<SeCheck> checks = new ArrayList<>();
83 for (JavaScriptCheck check : all()) {
84 if (check instanceof SeCheck) {
85 checks.add((SeCheck) check);
92 public List<TreeVisitor> visitorChecks() {
93 List<TreeVisitor> checks = new ArrayList<>();
94 for (JavaScriptCheck check : all()) {
95 if (check instanceof TreeVisitor) {
96 checks.add((TreeVisitor) check);
104 public RuleKey ruleKeyFor(JavaScriptCheck check) {
107 for (Checks<JavaScriptCheck> checks : checksByRepository) {
108 ruleKey = checks.ruleKey(check);
110 if (ruleKey != null) {