Search in sources :

Example 1 with TextReport

use of net.sourceforge.pmd.benchmark.TextReport in project maven-plugins by apache.

the class PmdReport method executePmd.

private void executePmd() throws MavenReportException {
    if (renderer != null) {
        // PMD has already been run
        getLog().debug("PMD has already been run - skipping redundant execution.");
        return;
    }
    try {
        excludeFromFile.loadExcludeFromFailuresData(excludeFromFailureFile);
    } catch (MojoExecutionException e) {
        throw new MavenReportException("Unable to load exclusions", e);
    }
    // configure ResourceManager
    locator.addSearchPath(FileResourceLoader.ID, project.getFile().getParentFile().getAbsolutePath());
    locator.addSearchPath("url", "");
    locator.setOutputDirectory(targetDirectory);
    renderer = new PmdCollectingRenderer();
    PMDConfiguration pmdConfiguration = getPMDConfiguration();
    String[] sets = new String[rulesets.length];
    try {
        for (int idx = 0; idx < rulesets.length; idx++) {
            String set = rulesets[idx];
            getLog().debug("Preparing ruleset: " + set);
            RuleSetReferenceId id = new RuleSetReferenceId(set);
            File ruleset = locator.getResourceAsFile(id.getRuleSetFileName(), getLocationTemp(set));
            if (null == ruleset) {
                throw new MavenReportException("Could not resolve " + set);
            }
            sets[idx] = ruleset.getAbsolutePath();
        }
    } catch (ResourceNotFoundException e) {
        throw new MavenReportException(e.getMessage(), e);
    } catch (FileResourceCreationException e) {
        throw new MavenReportException(e.getMessage(), e);
    }
    pmdConfiguration.setRuleSets(StringUtils.join(sets, ","));
    try {
        if (filesToProcess == null) {
            filesToProcess = getFilesToProcess();
        }
        if (filesToProcess.isEmpty() && !"java".equals(language)) {
            getLog().warn("No files found to process. Did you add your additional source folders like javascript?" + " (see also build-helper-maven-plugin)");
        }
    } catch (IOException e) {
        throw new MavenReportException("Can't get file list", e);
    }
    String encoding = getSourceEncoding();
    if (StringUtils.isEmpty(encoding) && !filesToProcess.isEmpty()) {
        getLog().warn("File encoding has not been set, using platform encoding " + ReaderFactory.FILE_ENCODING + ", i.e. build is platform dependent!");
        encoding = ReaderFactory.FILE_ENCODING;
    }
    pmdConfiguration.setSourceEncoding(encoding);
    List<DataSource> dataSources = new ArrayList<>(filesToProcess.size());
    for (File f : filesToProcess.keySet()) {
        dataSources.add(new FileDataSource(f));
    }
    if (sets.length > 0) {
        processFilesWithPMD(pmdConfiguration, dataSources);
    } else {
        getLog().debug("Skipping PMD execution as no rulesets are defined.");
    }
    if (renderer.hasErrors()) {
        if (!skipPmdError) {
            getLog().error("PMD processing errors:");
            getLog().error(renderer.getErrorsAsString());
            throw new MavenReportException("Found " + renderer.getErrors().size() + " PMD processing errors");
        }
        getLog().warn("There are " + renderer.getErrors().size() + " PMD processing errors:");
        getLog().warn(renderer.getErrorsAsString());
    }
    removeExcludedViolations(renderer.getViolations());
    // so the "check" goals can check for violations
    if (isXml() && renderer != null) {
        writeNonHtml(renderer.asReport());
    }
    if (benchmark) {
        try (PrintStream benchmarkFileStream = new PrintStream(benchmarkOutputFilename)) {
            (new TextReport()).generate(Benchmarker.values(), benchmarkFileStream);
        } catch (FileNotFoundException fnfe) {
            getLog().error("Unable to generate benchmark file: " + benchmarkOutputFilename, fnfe);
        }
    }
}
Also used : PrintStream(java.io.PrintStream) RuleSetReferenceId(net.sourceforge.pmd.RuleSetReferenceId) FileResourceCreationException(org.codehaus.plexus.resource.loader.FileResourceCreationException) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) ArrayList(java.util.ArrayList) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) TextReport(net.sourceforge.pmd.benchmark.TextReport) DataSource(net.sourceforge.pmd.util.datasource.DataSource) FileDataSource(net.sourceforge.pmd.util.datasource.FileDataSource) FileDataSource(net.sourceforge.pmd.util.datasource.FileDataSource) ResourceNotFoundException(org.codehaus.plexus.resource.loader.ResourceNotFoundException) File(java.io.File) MavenReportException(org.apache.maven.reporting.MavenReportException) PMDConfiguration(net.sourceforge.pmd.PMDConfiguration)

Example 2 with TextReport

use of net.sourceforge.pmd.benchmark.TextReport in project Gargoyle by callakrsos.

the class PMDCheckComposite method simpleFilePmd.

private void simpleFilePmd(File file) {
    try {
        String sourceCode = Files.toString(this.sourceFile, Charset.forName("UTF-8"));
        javaTextArea.setContent(sourceCode);
        GargoylePMDParameters params = new GargoylePMDParameters();
        params.setSourceFileName(file.getAbsolutePath());
        params.setSourceText(sourceCode);
        //			transformParametersIntoConfiguration(params);
        long start = System.nanoTime();
        int violations = doPMD.doPMD(transformParametersIntoConfiguration(params));
        long end = System.nanoTime();
        Benchmarker.mark(Benchmark.TotalPMD, end - start, 0);
        TextReport report = new TextReport();
        try (ByteArrayOutputStream out = new ByteArrayOutputStream()) {
            report.generate(Benchmarker.values(), new PrintStream(out));
            out.flush();
            LOGGER.debug(out.toString("UTF-8"));
        }
        Platform.runLater(() -> {
            xmlEditor.setContent(doPMD.getResultString());
            violationLabel.setText(String.format(VIOLATION_TEXT_FORMAT, violations));
        });
    } catch (IOException e) {
        LOGGER.error(ValueUtil.toString(e));
    }
}
Also used : PrintStream(java.io.PrintStream) GargoylePMDParameters(com.kyj.fx.voeditor.visual.framework.pmd.GargoylePMDParameters) TextReport(net.sourceforge.pmd.benchmark.TextReport) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException)

Example 3 with TextReport

use of net.sourceforge.pmd.benchmark.TextReport in project Gargoyle by callakrsos.

the class PMDCheckComposite method dirFilePmd.

private void dirFilePmd(File file) {
    try {
        //			String sourceCode = Files.toString(this.sourceFile, Charset.forName("UTF-8"));
        //			javaTextArea.setContent(sourceCode);
        GargoylePMDParameters params = new GargoylePMDParameters();
        try {
            Field declaredField = GargoylePMDParameters.class.getDeclaredField("sourceDir");
            if (declaredField != null) {
                declaredField.setAccessible(true);
                declaredField.set(params, file.getAbsolutePath());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        //			transformParametersIntoConfiguration(params);
        long start = System.nanoTime();
        int violations = doPMD.doPMD(transformParametersIntoConfiguration(params));
        long end = System.nanoTime();
        Benchmarker.mark(Benchmark.TotalPMD, end - start, 0);
        TextReport report = new TextReport();
        try (ByteArrayOutputStream out = new ByteArrayOutputStream()) {
            report.generate(Benchmarker.values(), new PrintStream(out));
            out.flush();
            LOGGER.debug(out.toString("UTF-8"));
        }
        Platform.runLater(() -> {
            xmlEditor.setContent(doPMD.getResultString());
            violationLabel.setText(String.format(VIOLATION_TEXT_FORMAT, violations));
        });
    } catch (IOException e) {
        LOGGER.error(ValueUtil.toString(e));
    }
}
Also used : Field(java.lang.reflect.Field) PrintStream(java.io.PrintStream) GargoylePMDParameters(com.kyj.fx.voeditor.visual.framework.pmd.GargoylePMDParameters) TextReport(net.sourceforge.pmd.benchmark.TextReport) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) IOException(java.io.IOException)

Example 4 with TextReport

use of net.sourceforge.pmd.benchmark.TextReport in project Gargoyle by callakrsos.

the class PMDCheckedListComposite method dirFilePmd.

protected void dirFilePmd(File file) {
    try {
        GargoylePMDParameters params = new GargoylePMDParameters();
        try {
            Field declaredField = GargoylePMDParameters.class.getDeclaredField("sourceDir");
            if (declaredField != null) {
                declaredField.setAccessible(true);
                declaredField.set(params, file.getAbsolutePath());
            }
        } catch (Exception e) {
            LOGGER.error(ValueUtil.toString(e));
        }
        long start = System.nanoTime();
        List<ReportListener> listeners = new ArrayList<>();
        listeners.addAll(chartComposite.getAvalilableReportListenerList());
        listeners.add(reportListenerProperty.get());
        listeners.add(violationCountingListener.get());
        doPMD.doPMD(transformParametersIntoConfiguration(params), listeners);
        long end = System.nanoTime();
        Benchmarker.mark(Benchmark.TotalPMD, end - start, 0);
        TextReport report = new TextReport();
        try (ByteArrayOutputStream out = new ByteArrayOutputStream()) {
            report.generate(Benchmarker.values(), new PrintStream(out));
            out.flush();
            LOGGER.debug(out.toString("UTF-8"));
        }
        Platform.runLater(() -> {
            updateViolationLabel();
            chartComposite.build();
        });
    } catch (IOException e) {
        LOGGER.error(ValueUtil.toString(e));
    }
}
Also used : Field(java.lang.reflect.Field) PrintStream(java.io.PrintStream) ReportListener(net.sourceforge.pmd.ReportListener) ArrayList(java.util.ArrayList) GargoylePMDParameters(com.kyj.fx.voeditor.visual.framework.pmd.GargoylePMDParameters) TextReport(net.sourceforge.pmd.benchmark.TextReport) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) IOException(java.io.IOException)

Example 5 with TextReport

use of net.sourceforge.pmd.benchmark.TextReport in project Gargoyle by callakrsos.

the class JavaCodeAreaHelper method doPmd.

/**
	 * PMD 처리
	 * @작성자 : KYJ
	 * @작성일 : 2016. 10. 12.
	 */
private void doPmd() {
    PMDCheckedListComposite pmdCheckedListComposite = new PMDCheckedListComposite(null) {

        @Override
        public void run() {
            simpleFilePmd(null);
        }

        @Override
        protected void simpleFilePmd(File file) {
            try {
                GargoylePMDParameters params = new GargoylePMDParameters();
                String sourceCode = codeArea.getText();
                params.setSourceFileName("Java");
                params.setSourceText(sourceCode);
                //						if (!FileUtil.isJavaFile(file)) {
                //							String fileExtension = FileUtil.getFileExtension(file);
                //							try {
                //								Field declaredField = PMDParameters.class.getDeclaredField("language");
                //								if (declaredField != null) {
                //									declaredField.setAccessible(true);
                //									declaredField.set(params, fileExtension);
                //								}
                //							} catch (Exception e) {
                //								e.printStackTrace();
                //							}
                //						}
                //			transformParametersIntoConfiguration(params);
                long start = System.nanoTime();
                doPMD.doPMD(transformParametersIntoConfiguration(params), reportListenerPropertyProperty().get(), violationCountingListenerProperty().get());
                long end = System.nanoTime();
                Benchmarker.mark(Benchmark.TotalPMD, end - start, 0);
                TextReport report = new TextReport();
                try (ByteArrayOutputStream out = new ByteArrayOutputStream()) {
                    report.generate(Benchmarker.values(), new PrintStream(out));
                    out.flush();
                    LOGGER.debug(out.toString("UTF-8"));
                }
                updateStatus(sourceCode);
            } catch (IOException e) {
                LOGGER.error(ValueUtil.toString(e));
            }
        }
    };
    pmdCheckedListComposite.run();
    CloseableParent<BorderPane> closa = pmdCheckedListComposite;
    Consumer<Stage> option = stage -> {
        stage.setTitle("PMD Check.");
        stage.initOwner(SharedMemory.getPrimaryStage());
        stage.setWidth(1200d);
        stage.setHeight(800d);
    };
    FxUtil.createStageAndShow(closa, option);
}
Also used : EventHandler(javafx.event.EventHandler) ByteArrayOutputStream(java.io.ByteArrayOutputStream) LoggerFactory(org.slf4j.LoggerFactory) PMDCheckedListComposite(com.kyj.fx.voeditor.visual.component.pmd.PMDCheckedListComposite) Benchmark(net.sourceforge.pmd.benchmark.Benchmark) PrintStream(java.io.PrintStream) Logger(org.slf4j.Logger) MenuItem(javafx.scene.control.MenuItem) CloseableParent(com.kyj.fx.voeditor.visual.main.layout.CloseableParent) GargoylePMDParameters(com.kyj.fx.voeditor.visual.framework.pmd.GargoylePMDParameters) IOException(java.io.IOException) ValueUtil(com.kyj.fx.voeditor.visual.util.ValueUtil) CodeArea(org.fxmisc.richtext.CodeArea) File(java.io.File) TextReport(net.sourceforge.pmd.benchmark.TextReport) Menu(javafx.scene.control.Menu) Consumer(java.util.function.Consumer) SeparatorMenuItem(javafx.scene.control.SeparatorMenuItem) FxUtil(com.kyj.fx.voeditor.visual.util.FxUtil) List(java.util.List) ActionEvent(javafx.event.ActionEvent) Stage(javafx.stage.Stage) Benchmarker(net.sourceforge.pmd.benchmark.Benchmarker) BorderPane(javafx.scene.layout.BorderPane) SharedMemory(com.kyj.fx.voeditor.visual.momory.SharedMemory) PrintStream(java.io.PrintStream) BorderPane(javafx.scene.layout.BorderPane) PMDCheckedListComposite(com.kyj.fx.voeditor.visual.component.pmd.PMDCheckedListComposite) TextReport(net.sourceforge.pmd.benchmark.TextReport) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) Stage(javafx.stage.Stage) GargoylePMDParameters(com.kyj.fx.voeditor.visual.framework.pmd.GargoylePMDParameters) File(java.io.File)

Aggregations

IOException (java.io.IOException)6 PrintStream (java.io.PrintStream)6 TextReport (net.sourceforge.pmd.benchmark.TextReport)6 GargoylePMDParameters (com.kyj.fx.voeditor.visual.framework.pmd.GargoylePMDParameters)5 ByteArrayOutputStream (java.io.ByteArrayOutputStream)5 Field (java.lang.reflect.Field)3 File (java.io.File)2 ArrayList (java.util.ArrayList)2 PMDCheckedListComposite (com.kyj.fx.voeditor.visual.component.pmd.PMDCheckedListComposite)1 CloseableParent (com.kyj.fx.voeditor.visual.main.layout.CloseableParent)1 SharedMemory (com.kyj.fx.voeditor.visual.momory.SharedMemory)1 FxUtil (com.kyj.fx.voeditor.visual.util.FxUtil)1 ValueUtil (com.kyj.fx.voeditor.visual.util.ValueUtil)1 FileNotFoundException (java.io.FileNotFoundException)1 List (java.util.List)1 Consumer (java.util.function.Consumer)1 ActionEvent (javafx.event.ActionEvent)1 EventHandler (javafx.event.EventHandler)1 Menu (javafx.scene.control.Menu)1 MenuItem (javafx.scene.control.MenuItem)1