Java 声纳插件,如何保存违规行为

Java 声纳插件,如何保存违规行为,java,plugins,sonarqube,save,Java,Plugins,Sonarqube,Save,我正在开发一个sonar插件,用tslint分析TrueScript代码 我从[github.com/SonarSource/sonar examples]下载了示例插件,并编辑了examplessensor.java[github.com/SonarSource/sonar examples/tree/master/plugins/sonar reference plugin/src/main/java/com/mycompany/sonar/reference/batch]。现在,我的传感器

我正在开发一个sonar插件,用tslint分析TrueScript代码

我从[github.com/SonarSource/sonar examples]下载了示例插件,并编辑了examplessensor.java[github.com/SonarSource/sonar examples/tree/master/plugins/sonar reference plugin/src/main/java/com/mycompany/sonar/reference/batch]。现在,我的传感器与此文件中的相同

package com.mycompany.sonar.reference.batch;

import java.io.File;
import java.io.IOException;
import java.io.StringWriter;
import java.util.Collection;

import org.apache.commons.io.IOUtils;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.sonar.api.batch.Sensor;
import org.sonar.api.batch.SensorContext;
import org.sonar.api.config.Settings;
import org.sonar.api.resources.Project;
import org.sonar.api.rules.Rule;
import org.sonar.api.rules.Violation;

import pl.sollers.utils.FileSearcher;

public class ExampleSensor implements Sensor {

        private static final Logger LOG = LoggerFactory.getLogger(ExampleSensor.class);

        private Settings settings;

        /**
         * Use of IoC to get Settings
         */
        public ExampleSensor(Settings settings) {
                this.settings = settings;
        }

        public boolean shouldExecuteOnProject(Project project) {
                // This sensor is executed only for ts project type
                return project.getLanguageKey().equals("js");
        }

        public void analyse(Project project, SensorContext sensorContext) {
                // getting all files for analyzing
                Collection<File> files = FileSearcher.getByExtensionRecursively("ts");

                Process tslintProces;
                JSONParser jsonParser = new JSONParser();
                JSONArray warnings;
                Object jsonObj;

                for (File file : files) {
                        try {
                                // run tslint process and analyze file
                                tslintProces = new ProcessBuilder("tslint.cmd", "-c", "./tslint.json", "-t",
                                                "json", "-f", file.getCanonicalPath()).start();

                                // copy tslint output
                                StringWriter writer = new StringWriter();
                                IOUtils.copy(tslintProces.getInputStream(), writer, "UTF-8");
                                String json = writer.toString();

                                // parse output and extract violation message, ruleName, line
                                jsonObj = jsonParser.parse(json);
                                if (jsonObj instanceof JSONArray) {
                                        warnings = (JSONArray) (jsonObj);
                                } else {
                                        throw new Exception("Oczekiwano obiektu klasy JSONArray");
                                }

                                for (int i = 0; i < warnings.size(); i++) {
                                        JSONObject warning = (JSONObject) warnings.get(i);

                                        String message = (String) warning.get("failure");
                                        String ruleName = (String) warning.get("ruleName");

                                        JSONObject position = (JSONObject) warning.get("startPosition");
                                        Long line = (Long) position.get("line");
                                        // nie widzę pola w Sonarze aby użyć numeru znaku w linii
                                        Long character = (Long) position.get("character");

                                        // HELP! I DO NOT KNOW HOW TO STORE VIOLATION IN SONAR
                                        // FOLLOWING LINES DOES NOT WORK
                                        // Rule rule = Rule.create("repositoryKey",
                                                        String.format("%s-%s", "key", ruleName), ruleName);
                                        // org.sonar.api.resources.File resource = new org.sonar.api.resources.File(file
                                                        .getParentFile().getCanonicalPath(), file.getName());

                                        // Violation violation = Violation.create(rule, resource);
                                        // violation.setLineId(line.intValue());
                                        // violation.setMessage(message);
                                        // sensorContext.saveViolation(violation);
                                }
                        } catch (IOException e) {
                                LOG.error(e.getMessage());
                                e.printStackTrace();
                        } catch (ParseException e) {
                                LOG.error(e.getMessage());
                                e.printStackTrace();
                        } catch (Exception e) {
                                LOG.error(e.getMessage());
                        }
                }
        }

        @Override
        public String toString() {
                return getClass().getSimpleName();
        }
}
我在声纳数据库中保存违规行为时遇到问题。这部分代码应该在第80-90行。有人能帮我保存违规行为吗。

您应该使用而不是违规行为,因为它已弃用,将在4.3中删除

 import org.sonar.api.component.ResourcePerspectives;
 public class MySensor extends Sensor {
   private final ResourcePerspectives perspectives;

   public MySensor(ResourcePerspectives p) {
     this.perspectives = p;
   }

   public void analyse(Project project, SensorContext context) {
     Resource myResource; // to be set
     Issuable issuable = perspectives.as(Issuable.class, myResource);
     if (issuable != null) {
       // can be used
       Issue issue = issuable.newIssueBuilder()
         .setRuleKey(RuleKey.of("pmd", "AvoidArrayLoops")
         .setLine(10)
         .build();
       issuable.addIssue(issue);
     }
   }
 }

我试图实现此解决方案,但我被“Issuable Issuable=perspectives.asIssuable.class,myResource;”这一行困住了方法as返回null,我不知道为什么。myResource是org.sonar.api.resources.File对象。我从org.sonar.api.resources.File.fromIOFilefile,project得到这个;我试图实现这个解决方案,但我被困在行Issuable-Issuable=perspectives.asIssuable.class,myResource;其中issuable为空。我的资源来自哪里?列出inputFiles=project.getFileSystem.mainFilests;对于InputFile InputFile:inputFiles{org.sonar.api.resources.File myResource=org.sonar.api.resources.File.fromIOFileinputFile.getFile,project;Issuable Issuable=perspectives.asisuable.class,myResource;//Issuable==null}知道为什么Issuable为null吗?我认为必须首先对资源进行索引。也许你应该调用SensorContext.index。。。方法,然后再引用myResource。这是一个解决办法,因为API文档说它将自SQ4.2起自动编制索引。4.2中有很大的变化。你应该先读。检查pom.xml中使用的API版本。fromIOFile可以返回null。有关详细信息和示例代码,请查看。