From d217475442c8b463724626325f27ff5bfe06fc14 Mon Sep 17 00:00:00 2001 From: Julien Lancelot Date: Thu, 11 Jul 2013 17:10:08 +0200 Subject: [PATCH] Add some CheckForNull and Nullable annotations --- .../java/org/sonar/core/i18n/I18nManager.java | 12 ++++------ .../org/sonar/core/i18n/RuleI18nManager.java | 6 +++++ .../org/sonar/core/i18n/package-info.java | 23 +++++++++++++++++++ .../main/java/org/sonar/api/i18n/I18n.java | 6 ++++- .../java/org/sonar/api/i18n/RuleI18n.java | 5 ++++ .../java/org/sonar/api/i18n/package-info.java | 23 +++++++++++++++++++ .../main/java/org/sonar/api/rules/Rule.java | 8 +++++-- .../sonar/server/rule/RubyRuleService.java | 3 +++ .../org/sonar/server/rule/package-info.java | 23 +++++++++++++++++++ 9 files changed, 99 insertions(+), 10 deletions(-) create mode 100644 sonar-core/src/main/java/org/sonar/core/i18n/package-info.java create mode 100644 sonar-plugin-api/src/main/java/org/sonar/api/i18n/package-info.java create mode 100644 sonar-server/src/main/java/org/sonar/server/rule/package-info.java diff --git a/sonar-core/src/main/java/org/sonar/core/i18n/I18nManager.java b/sonar-core/src/main/java/org/sonar/core/i18n/I18nManager.java index 5f39c191dab..e87bac2b705 100644 --- a/sonar-core/src/main/java/org/sonar/core/i18n/I18nManager.java +++ b/sonar-core/src/main/java/org/sonar/core/i18n/I18nManager.java @@ -31,17 +31,13 @@ import org.sonar.api.platform.PluginMetadata; import org.sonar.api.platform.PluginRepository; import org.sonar.api.utils.SonarException; +import javax.annotation.CheckForNull; import javax.annotation.Nullable; import java.io.IOException; import java.io.InputStream; import java.text.MessageFormat; -import java.util.Enumeration; -import java.util.Locale; -import java.util.Map; -import java.util.MissingResourceException; -import java.util.ResourceBundle; -import java.util.Set; +import java.util.*; public class I18nManager implements I18n, ServerExtension, BatchExtension { private static final Logger LOG = LoggerFactory.getLogger(I18nManager.class); @@ -87,7 +83,8 @@ public class I18nManager implements I18n, ServerExtension, BatchExtension { fileContentCache=null; } - public String message(Locale locale, String key, String defaultValue, Object... parameters) { + @CheckForNull + public String message(Locale locale, String key, @Nullable String defaultValue, Object... parameters) { String bundleKey = propertyToBundles.get(key); String value = null; if (bundleKey != null) { @@ -159,6 +156,7 @@ public class I18nManager implements I18n, ServerExtension, BatchExtension { return propertyToBundles.keySet(); } + @CheckForNull private String formatMessage(@Nullable String message, Object... parameters) { if (message == null || parameters.length == 0) { return message; diff --git a/sonar-core/src/main/java/org/sonar/core/i18n/RuleI18nManager.java b/sonar-core/src/main/java/org/sonar/core/i18n/RuleI18nManager.java index a728bf45172..3316edc27c4 100644 --- a/sonar-core/src/main/java/org/sonar/core/i18n/RuleI18nManager.java +++ b/sonar-core/src/main/java/org/sonar/core/i18n/RuleI18nManager.java @@ -26,6 +26,8 @@ import org.sonar.api.ServerExtension; import org.sonar.api.i18n.RuleI18n; import org.sonar.api.rules.Rule; +import javax.annotation.CheckForNull; + import java.util.List; import java.util.Locale; @@ -51,10 +53,12 @@ public class RuleI18nManager implements RuleI18n, ServerExtension, BatchExtensio this.ruleKeys = list.toArray(new RuleKey[list.size()]); } + @CheckForNull public String getName(String repositoryKey, String ruleKey, Locale locale) { return message(repositoryKey, ruleKey, locale, NAME_SUFFIX); } + @CheckForNull public String getName(Rule rule, Locale locale) { String name = message(rule.getRepositoryKey(), rule.getKey(), locale, NAME_SUFFIX); return name != null ? name : rule.getName(); @@ -92,10 +96,12 @@ public class RuleI18nManager implements RuleI18n, ServerExtension, BatchExtensio return description; } + @CheckForNull public String getParamDescription(String repositoryKey, String ruleKey, String paramKey, Locale locale) { return message(repositoryKey, ruleKey, locale, ".param." + paramKey); } + @CheckForNull String message(String repositoryKey, String ruleKey, Locale locale, String suffix) { String propertyKey = new StringBuilder().append(RULE_PREFIX).append(repositoryKey).append(".").append(ruleKey).append(suffix).toString(); return i18nManager.message(locale, propertyKey, null); diff --git a/sonar-core/src/main/java/org/sonar/core/i18n/package-info.java b/sonar-core/src/main/java/org/sonar/core/i18n/package-info.java new file mode 100644 index 00000000000..102c799b2be --- /dev/null +++ b/sonar-core/src/main/java/org/sonar/core/i18n/package-info.java @@ -0,0 +1,23 @@ +/* + * SonarQube, open source software quality management tool. + * Copyright (C) 2008-2013 SonarSource + * mailto:contact AT sonarsource DOT com + * + * SonarQube 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. + * + * SonarQube 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. + */ +@ParametersAreNonnullByDefault +package org.sonar.core.i18n; + +import javax.annotation.ParametersAreNonnullByDefault; \ No newline at end of file diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/i18n/I18n.java b/sonar-plugin-api/src/main/java/org/sonar/api/i18n/I18n.java index 7ce619b9c42..2588ea5d58f 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/i18n/I18n.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/i18n/I18n.java @@ -22,6 +22,9 @@ package org.sonar.api.i18n; import org.sonar.api.BatchComponent; import org.sonar.api.ServerComponent; +import javax.annotation.CheckForNull; +import javax.annotation.Nullable; + import java.util.Locale; /** @@ -45,6 +48,7 @@ public interface I18n extends ServerComponent, BatchComponent { * @param parameters the parameters used to format the message from the translated pattern. * @return the message formatted with the translated pattern and the given parameters */ - String message(final Locale locale, final String key, final String defaultValue, final Object... parameters); + @CheckForNull + String message(final Locale locale, final String key, @Nullable final String defaultValue, final Object... parameters); } diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/i18n/RuleI18n.java b/sonar-plugin-api/src/main/java/org/sonar/api/i18n/RuleI18n.java index 5cb5e002187..9b06cee3180 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/i18n/RuleI18n.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/i18n/RuleI18n.java @@ -23,6 +23,8 @@ import org.sonar.api.BatchComponent; import org.sonar.api.ServerComponent; import org.sonar.api.rules.Rule; +import javax.annotation.CheckForNull; + import java.util.Locale; /** @@ -43,6 +45,7 @@ public interface RuleI18n extends ServerComponent, BatchComponent { * @param locale the locale to translate into * @return the translated name of the rule, or the default English one if the given locale is not supported, or null */ + @CheckForNull String getName(String repositoryKey, String ruleKey, Locale locale); /** @@ -55,6 +58,7 @@ public interface RuleI18n extends ServerComponent, BatchComponent { * @param locale the locale to translate into * @return the translated name of the rule, or the default English one if the given locale is not supported, or the rule name. */ + @CheckForNull String getName(Rule rule, Locale locale); /** @@ -83,6 +87,7 @@ public interface RuleI18n extends ServerComponent, BatchComponent { * @return the translated name of the rule parameter, or the default English one if the given locale is not supported, or null if * no translation can be found. */ + @CheckForNull String getParamDescription(String repositoryKey, String ruleKey, String paramKey, Locale locale); } diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/i18n/package-info.java b/sonar-plugin-api/src/main/java/org/sonar/api/i18n/package-info.java new file mode 100644 index 00000000000..616332a1c32 --- /dev/null +++ b/sonar-plugin-api/src/main/java/org/sonar/api/i18n/package-info.java @@ -0,0 +1,23 @@ +/* + * SonarQube, open source software quality management tool. + * Copyright (C) 2008-2013 SonarSource + * mailto:contact AT sonarsource DOT com + * + * SonarQube 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. + * + * SonarQube 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. + */ +@ParametersAreNonnullByDefault +package org.sonar.api.i18n; + +import javax.annotation.ParametersAreNonnullByDefault; \ No newline at end of file diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/rules/Rule.java b/sonar-plugin-api/src/main/java/org/sonar/api/rules/Rule.java index 79eae5c38b2..02612902def 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/rules/Rule.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/rules/Rule.java @@ -32,6 +32,8 @@ import org.sonar.api.rule.RuleKey; import org.sonar.api.utils.SonarException; import org.sonar.check.Cardinality; +import javax.annotation.CheckForNull; +import javax.annotation.Nullable; import javax.persistence.*; import java.util.ArrayList; @@ -205,6 +207,7 @@ public final class Rule { this.id = id; } + @CheckForNull public String getName() { return name; } @@ -212,7 +215,7 @@ public final class Rule { /** * Sets the rule name */ - public Rule setName(String name) { + public Rule setName(@Nullable String name) { this.name = removeNewLineCharacters(name); return this; } @@ -504,7 +507,8 @@ public final class Rule { .toString(); } - private String removeNewLineCharacters(String text) { + @CheckForNull + private String removeNewLineCharacters(@Nullable String text) { String removedCRLF = StringUtils.remove(text, "\n"); removedCRLF = StringUtils.remove(removedCRLF, "\r"); removedCRLF = StringUtils.remove(removedCRLF, "\n\r"); diff --git a/sonar-server/src/main/java/org/sonar/server/rule/RubyRuleService.java b/sonar-server/src/main/java/org/sonar/server/rule/RubyRuleService.java index 0bd88c37885..974e3865c1b 100644 --- a/sonar-server/src/main/java/org/sonar/server/rule/RubyRuleService.java +++ b/sonar-server/src/main/java/org/sonar/server/rule/RubyRuleService.java @@ -25,6 +25,8 @@ import org.sonar.api.rules.Rule; import org.sonar.core.i18n.RuleI18nManager; import org.sonar.server.user.UserSession; +import javax.annotation.CheckForNull; + /** * Used through ruby code
Internal.rules
*/ @@ -36,6 +38,7 @@ public class RubyRuleService implements ServerComponent, Startable { this.i18n = i18n; } + @CheckForNull public String ruleL10nName(Rule rule) { String name = i18n.getName(rule.getRepositoryKey(), rule.getKey(), UserSession.get().locale()); if (name == null) { diff --git a/sonar-server/src/main/java/org/sonar/server/rule/package-info.java b/sonar-server/src/main/java/org/sonar/server/rule/package-info.java new file mode 100644 index 00000000000..1ea6c764bfb --- /dev/null +++ b/sonar-server/src/main/java/org/sonar/server/rule/package-info.java @@ -0,0 +1,23 @@ +/* + * SonarQube, open source software quality management tool. + * Copyright (C) 2008-2013 SonarSource + * mailto:contact AT sonarsource DOT com + * + * SonarQube 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. + * + * SonarQube 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. + */ +@ParametersAreNonnullByDefault +package org.sonar.server.rule; + +import javax.annotation.ParametersAreNonnullByDefault; \ No newline at end of file -- 2.39.5