Java tutorial
/* * Java Debugging Rules * Copyright (C) 2015-2018 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.debugging.java; import com.google.common.base.Charsets; import com.google.common.io.Resources; import com.google.gson.Gson; import org.sonar.api.rule.RuleStatus; import org.sonar.api.server.rule.RulesDefinition; import org.sonar.plugins.java.Java; import org.sonar.squidbridge.annotations.AnnotationBasedRulesDefinition; import javax.annotation.Nullable; import java.io.IOException; import java.net.URL; import java.util.Locale; public class JavaDebuggingRulesDefinition implements RulesDefinition { public static final String REPOSITORY_KEY = "java-debugging-rules"; public static final String REPOSITORY_NAME = "Java Debugging Rules"; private final Gson gson = new Gson(); @Override public void define(Context context) { NewRepository repository = context.createRepository(REPOSITORY_KEY, Java.KEY).setName(REPOSITORY_NAME); new AnnotationBasedRulesDefinition(repository, Java.KEY) .addRuleClasses(/* don't fail if no SQALE annotations */ false, RulesList.getChecks()); for (NewRule rule : repository.rules()) { String metadataKey = rule.key(); rule.setInternalKey(metadataKey); rule.setHtmlDescription(readRuleDefinitionResource(metadataKey + ".html")); addMetadata(rule, metadataKey); } repository.done(); } @Nullable private static String readRuleDefinitionResource(String fileName) { URL resource = JavaDebuggingRulesDefinition.class .getResource("/org/sonar/l10n/java/rules/debugging/" + fileName); if (resource == null) { return null; } try { return Resources.toString(resource, Charsets.UTF_8); } catch (IOException e) { throw new IllegalStateException("Failed to read: " + resource, e); } } private void addMetadata(NewRule rule, String metadataKey) { String json = readRuleDefinitionResource(metadataKey + ".json"); if (json != null) { RuleMetadata metadata = gson.fromJson(json, RuleMetadata.class); rule.setSeverity(metadata.defaultSeverity.toUpperCase(Locale.US)); rule.setName(metadata.title); rule.setTags(metadata.tags); rule.setStatus(RuleStatus.valueOf(metadata.status.toUpperCase(Locale.US))); } } private static class RuleMetadata { String title; String status; String[] tags; String defaultSeverity; } }