@@ -27,6 +27,8 @@ import javax.annotation.CheckForNull; | |||
import javax.annotation.Nullable; | |||
import org.sonar.api.rule.RuleKey; | |||
import org.sonar.api.server.rule.RulesDefinition; | |||
import org.sonar.api.server.rule.internal.DefaultNewRepository; | |||
import org.sonar.api.server.rule.internal.DefaultRepository; | |||
import static java.util.Collections.emptyList; | |||
import static java.util.Collections.unmodifiableList; | |||
@@ -75,7 +77,7 @@ public class RulesDefinitionContext extends RulesDefinition.Context { | |||
return emptyList(); | |||
} | |||
void registerRepository(DefaultNewRepository newRepository) { | |||
public void registerRepository(DefaultNewRepository newRepository) { | |||
RulesDefinition.Repository existing = repositoriesByKey.get(newRepository.key()); | |||
if (existing != null) { | |||
String existingLanguage = existing.language(); |
@@ -20,8 +20,11 @@ | |||
package org.sonar.api.server.rule; | |||
import java.net.URL; | |||
import java.util.ArrayList; | |||
import java.util.Collection; | |||
import java.util.HashMap; | |||
import java.util.List; | |||
import java.util.Map; | |||
import java.util.Set; | |||
import javax.annotation.CheckForNull; | |||
import javax.annotation.Nullable; | |||
@@ -34,8 +37,14 @@ import org.sonar.api.rule.RuleStatus; | |||
import org.sonar.api.rules.RuleType; | |||
import org.sonar.api.server.ServerSide; | |||
import org.sonar.api.server.debt.DebtRemediationFunction; | |||
import org.sonar.api.server.rule.internal.DefaultNewRepository; | |||
import org.sonar.api.server.rule.internal.DefaultRepository; | |||
import org.sonarsource.api.sonarlint.SonarLintSide; | |||
import static java.util.Collections.emptyList; | |||
import static java.util.Collections.unmodifiableList; | |||
import static org.sonar.api.utils.Preconditions.checkState; | |||
/** | |||
* Defines some coding rules of the same repository. For example the Java Findbugs plugin provides an implementation of | |||
* this extension point in order to define the rules that it supports. | |||
@@ -344,10 +353,78 @@ public interface RulesDefinition { | |||
} | |||
} | |||
/** | |||
* This implementation will be removed as soon as analyzers stop instantiating it. | |||
* Use RulesDefinitionContext in sonar-plugin-api-impl. | |||
*/ | |||
class Context extends AbstractContext { | |||
private final Map<String, Repository> repositoriesByKey = new HashMap<>(); | |||
private String currentPluginKey; | |||
@Override | |||
public RulesDefinition.NewRepository createRepository(String key, String language) { | |||
return new DefaultNewRepository(this, key, language, false); | |||
} | |||
@Override | |||
public RulesDefinition.NewRepository createExternalRepository(String engineId, String language) { | |||
return new DefaultNewRepository(this, RuleKey.EXTERNAL_RULE_REPO_PREFIX + engineId, language, true); | |||
} | |||
@Override | |||
@Deprecated | |||
public RulesDefinition.NewRepository extendRepository(String key, String language) { | |||
return createRepository(key, language); | |||
} | |||
@Override | |||
@CheckForNull | |||
public RulesDefinition.Repository repository(String key) { | |||
return repositoriesByKey.get(key); | |||
} | |||
@Override | |||
public List<RulesDefinition.Repository> repositories() { | |||
return unmodifiableList(new ArrayList<>(repositoriesByKey.values())); | |||
} | |||
@Override | |||
@Deprecated | |||
public List<RulesDefinition.ExtendedRepository> extendedRepositories(String repositoryKey) { | |||
return emptyList(); | |||
} | |||
@Override | |||
@Deprecated | |||
public List<RulesDefinition.ExtendedRepository> extendedRepositories() { | |||
return emptyList(); | |||
} | |||
public void registerRepository(DefaultNewRepository newRepository) { | |||
RulesDefinition.Repository existing = repositoriesByKey.get(newRepository.key()); | |||
if (existing != null) { | |||
String existingLanguage = existing.language(); | |||
checkState(existingLanguage.equals(newRepository.language()), | |||
"The rule repository '%s' must not be defined for two different languages: %s and %s", | |||
newRepository.key(), existingLanguage, newRepository.language()); | |||
} | |||
repositoriesByKey.put(newRepository.key(), new DefaultRepository(newRepository, existing)); | |||
} | |||
public String currentPluginKey() { | |||
return currentPluginKey; | |||
} | |||
@Override | |||
public void setCurrentPluginKey(@Nullable String pluginKey) { | |||
this.currentPluginKey = pluginKey; | |||
} | |||
} | |||
/** | |||
* Instantiated by core but not by plugins, except for their tests. | |||
*/ | |||
abstract class Context { | |||
abstract class AbstractContext { | |||
/* | |||
* New builder for {@link org.sonar.api.server.rule.RulesDefinition.Repository}. | |||
* <br> | |||
@@ -651,49 +728,49 @@ public interface RulesDefinition { | |||
} | |||
@Immutable | |||
interface Rule { | |||
abstract class Rule { | |||
Repository repository(); | |||
public abstract Repository repository(); | |||
/** | |||
* @since 6.6 the plugin the rule was declared in | |||
*/ | |||
@CheckForNull | |||
String pluginKey(); | |||
public abstract String pluginKey(); | |||
String key(); | |||
public abstract String key(); | |||
String name(); | |||
public abstract String name(); | |||
/** | |||
* @since 7.1 | |||
*/ | |||
RuleScope scope(); | |||
public abstract RuleScope scope(); | |||
/** | |||
* @see NewRule#setType(RuleType) | |||
* @since 5.5 | |||
*/ | |||
RuleType type(); | |||
public abstract RuleType type(); | |||
String severity(); | |||
public abstract String severity(); | |||
@CheckForNull | |||
String htmlDescription(); | |||
public abstract String htmlDescription(); | |||
@CheckForNull | |||
String markdownDescription(); | |||
public abstract String markdownDescription(); | |||
boolean template(); | |||
public abstract boolean template(); | |||
/** | |||
* Should this rule be enabled by default. For example in SonarLint standalone. | |||
* | |||
* @since 6.0 | |||
*/ | |||
boolean activatedByDefault(); | |||
public abstract boolean activatedByDefault(); | |||
RuleStatus status(); | |||
public abstract RuleStatus status(); | |||
/** | |||
* @see #type() | |||
@@ -702,29 +779,29 @@ public interface RulesDefinition { | |||
*/ | |||
@CheckForNull | |||
@Deprecated | |||
String debtSubCharacteristic(); | |||
public abstract String debtSubCharacteristic(); | |||
@CheckForNull | |||
DebtRemediationFunction debtRemediationFunction(); | |||
public abstract DebtRemediationFunction debtRemediationFunction(); | |||
/** | |||
* @deprecated since 5.5, replaced by {@link #gapDescription()} | |||
*/ | |||
@Deprecated | |||
@CheckForNull | |||
String effortToFixDescription(); | |||
public abstract String effortToFixDescription(); | |||
@CheckForNull | |||
String gapDescription(); | |||
public abstract String gapDescription(); | |||
@CheckForNull | |||
Param param(String key); | |||
public abstract Param param(String key); | |||
List<Param> params(); | |||
public abstract List<Param> params(); | |||
Set<String> tags(); | |||
public abstract Set<String> tags(); | |||
Set<String> securityStandards(); | |||
public abstract Set<String> securityStandards(); | |||
/** | |||
* Deprecated rules keys for this rule. | |||
@@ -784,13 +861,13 @@ public interface RulesDefinition { | |||
* @see NewRule#addDeprecatedRuleKey(String, String) | |||
* @since 7.1 | |||
*/ | |||
Set<RuleKey> deprecatedRuleKeys(); | |||
public abstract Set<RuleKey> deprecatedRuleKeys(); | |||
/** | |||
* @see RulesDefinition.NewRule#setInternalKey(String) | |||
*/ | |||
@CheckForNull | |||
String internalKey(); | |||
public abstract String internalKey(); | |||
} | |||
@@ -17,7 +17,7 @@ | |||
* 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.api.impl.server; | |||
package org.sonar.api.server.rule.internal; | |||
import javax.annotation.Nullable; | |||
import org.sonar.api.server.debt.DebtRemediationFunction; | |||
@@ -26,10 +26,10 @@ import org.sonar.api.server.rule.RulesDefinition; | |||
import org.sonar.api.utils.MessageException; | |||
/** | |||
* Factory of {@link org.sonar.api.server.debt.DebtRemediationFunction} that keeps | |||
* Factory of {@link DebtRemediationFunction} that keeps | |||
* a context of rule for better error messages. Used only when declaring rules. | |||
* | |||
* @see org.sonar.api.server.rule.RulesDefinition | |||
* @see RulesDefinition | |||
*/ | |||
class DefaultDebtRemediationFunctions implements RulesDefinition.DebtRemediationFunctions { | |||
@@ -17,7 +17,7 @@ | |||
* 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.api.impl.server; | |||
package org.sonar.api.server.rule.internal; | |||
import javax.annotation.Nullable; | |||
import org.apache.commons.lang.StringUtils; |
@@ -17,7 +17,7 @@ | |||
* 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.api.impl.server; | |||
package org.sonar.api.server.rule.internal; | |||
import java.util.Collection; | |||
import java.util.HashMap; | |||
@@ -30,14 +30,14 @@ import org.sonar.api.server.rule.RulesDefinition; | |||
import static org.sonar.api.utils.Preconditions.checkArgument; | |||
public class DefaultNewRepository implements RulesDefinition.NewRepository { | |||
private final RulesDefinitionContext context; | |||
private final RulesDefinition.Context context; | |||
private final String key; | |||
private final boolean isExternal; | |||
private final String language; | |||
private String name; | |||
private final Map<String, RulesDefinition.NewRule> newRules = new HashMap<>(); | |||
DefaultNewRepository(RulesDefinitionContext context, String key, String language, boolean isExternal) { | |||
public DefaultNewRepository(RulesDefinition.Context context, String key, String language, boolean isExternal) { | |||
this.context = context; | |||
this.key = key; | |||
this.name = key; | |||
@@ -55,15 +55,15 @@ public class DefaultNewRepository implements RulesDefinition.NewRepository { | |||
return key; | |||
} | |||
String language() { | |||
public String language() { | |||
return language; | |||
} | |||
Map<String, RulesDefinition.NewRule> newRules() { | |||
public Map<String, RulesDefinition.NewRule> newRules() { | |||
return newRules; | |||
} | |||
String name() { | |||
public String name() { | |||
return name; | |||
} | |||
@@ -17,7 +17,7 @@ | |||
* 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.api.impl.server; | |||
package org.sonar.api.server.rule.internal; | |||
import java.io.IOException; | |||
import java.net.URL; |
@@ -17,7 +17,7 @@ | |||
* 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.api.impl.server; | |||
package org.sonar.api.server.rule.internal; | |||
import javax.annotation.Nullable; | |||
import javax.annotation.concurrent.Immutable; |
@@ -17,7 +17,7 @@ | |||
* 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.api.impl.server; | |||
package org.sonar.api.server.rule.internal; | |||
import java.util.ArrayList; | |||
import java.util.HashMap; | |||
@@ -35,14 +35,14 @@ import static java.util.Collections.unmodifiableList; | |||
import static java.util.Collections.unmodifiableMap; | |||
@Immutable | |||
class DefaultRepository implements RulesDefinition.Repository { | |||
public class DefaultRepository implements RulesDefinition.Repository { | |||
private final String key; | |||
private final String language; | |||
private final String name; | |||
private final boolean isExternal; | |||
private final Map<String, RulesDefinition.Rule> rulesByKey; | |||
DefaultRepository(DefaultNewRepository newRepository, @Nullable RulesDefinition.Repository mergeInto) { | |||
public DefaultRepository(DefaultNewRepository newRepository, @Nullable RulesDefinition.Repository mergeInto) { | |||
this.key = newRepository.key(); | |||
this.language = newRepository.language(); | |||
this.isExternal = newRepository.isExternal(); |
@@ -17,7 +17,7 @@ | |||
* 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.api.impl.server; | |||
package org.sonar.api.server.rule.internal; | |||
import java.util.ArrayList; | |||
import java.util.Collections; | |||
@@ -40,7 +40,7 @@ import static java.lang.String.format; | |||
import static java.util.Collections.unmodifiableList; | |||
@Immutable | |||
public class DefaultRule implements RulesDefinition.Rule { | |||
public class DefaultRule extends RulesDefinition.Rule { | |||
private final String pluginKey; | |||
private final RulesDefinition.Repository repository; | |||
private final String repoKey; | |||
@@ -91,6 +91,7 @@ public class DefaultRule implements RulesDefinition.Rule { | |||
this.deprecatedRuleKeys = Collections.unmodifiableSet(new TreeSet<>(newRule.deprecatedRuleKeys())); | |||
} | |||
@Override | |||
public RulesDefinition.Repository repository() { | |||
return repository; | |||
} |
@@ -17,7 +17,7 @@ | |||
* 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.api.impl.server; | |||
package org.sonar.api.server.rule.internal; | |||
import org.junit.Rule; | |||
import org.junit.Test; |
@@ -17,9 +17,10 @@ | |||
* 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.api.impl.server; | |||
package org.sonar.api.server.rule.internal; | |||
import org.junit.Test; | |||
import org.sonar.api.impl.server.RulesDefinitionContext; | |||
import static org.assertj.core.api.Assertions.assertThat; | |||
import static org.mockito.Mockito.mock; |
@@ -18,7 +18,7 @@ | |||
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | |||
*/ | |||
package org.sonar.api.impl.server; | |||
package org.sonar.api.server.rule.internal; | |||
import org.junit.Test; | |||
import org.sonar.api.rule.RuleKey; |