All Downloads are FREE. Search and download functionalities are using the official Maven repository.

guru.nidi.codeassert.detekt.DetektAnalyzer Maven / Gradle / Ivy

There is a newer version: 0.9.15
Show newest version
/*
 * Copyright © 2015 Stefan Niederhauser ([email protected])
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package guru.nidi.codeassert.detekt;

import guru.nidi.codeassert.Analyzer;
import guru.nidi.codeassert.config.AnalyzerConfig;
import guru.nidi.codeassert.config.UsageCounter;
import io.gitlab.arturbosch.detekt.api.*;
import io.gitlab.arturbosch.detekt.core.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.Comparator;
import java.util.List;
import java.util.concurrent.Executors;

import static guru.nidi.codeassert.config.Language.KOTLIN;
import static io.gitlab.arturbosch.detekt.api.Severity.*;
import static java.util.Arrays.asList;
import static java.util.Collections.emptyList;
import static java.util.stream.Collectors.toList;

public class DetektAnalyzer implements Analyzer> {
    private static final Logger LOG = LoggerFactory.getLogger(DetektAnalyzer.class);
    private static final List SEVERITIES = asList(
            Style, CodeSmell, Minor, Performance, Maintainability, Warning, Security, Defect);
    private static final Comparator SEVERITY_COMPARATOR = Comparator.comparingInt(SEVERITIES::indexOf);
    private static final Comparator FINDING_COMPARATOR = Comparator
            .comparing((TypedDetektFinding f) -> f.severity, SEVERITY_COMPARATOR)
            .thenComparing(f -> f.type)
            .thenComparing(f -> f.name);

    private final AnalyzerConfig config;
    private final DetektCollector collector;
    private final Config detektConfig;
    private final List ruleSetProviders;

    public DetektAnalyzer(AnalyzerConfig config, DetektCollector collector) {
        this(config, collector, null, emptyList());
    }

    private DetektAnalyzer(AnalyzerConfig config, DetektCollector collector, Config detektConfig,
                           List ruleSetProviders) {
        this.config = config;
        this.collector = collector;
        this.detektConfig = detektConfig;
        this.ruleSetProviders = ruleSetProviders;
    }

    public DetektAnalyzer withConfig(Config detektConfig) {
        return new DetektAnalyzer(config, collector, detektConfig, ruleSetProviders);
    }

    public DetektAnalyzer withRuleSets(RuleSetProvider... providers) {
        return new DetektAnalyzer(config, collector, detektConfig, asList(providers));
    }

    public DetektResult analyze() {
        final File baseDir = new File(AnalyzerConfig.Path.commonBase(config.getSourcePaths(KOTLIN)).getPath());
        try {
            final PrintStream printStream = new PrintStream(new LoggingOutputStream(), true, "utf-8");
            final ProcessingSettings settings = new ProcessingSettings(
                    baseDir.toPath(), calcDetektConfig(), emptyList(), false, false, emptyList(),
                    Executors.newSingleThreadExecutor(), printStream, printStream);
            final DetektFacade df = DetektFacade.Companion.create(settings, ruleSetProviders(settings), emptyList());
            return createResult(baseDir, df.run());
        } catch (UnsupportedEncodingException e) {
            //cannot happen
            throw new AssertionError(e);
        }
    }

    private Config calcDetektConfig() {
        return new NoFormat(detektConfig == null
                ? YamlConfig.Companion.loadResource(DetektAnalyzer.class.getResource("default-detekt-config.yml"))
                : detektConfig);
    }

    private List ruleSetProviders(ProcessingSettings settings) {
        final List res = new RuleSetLocator(settings).load();
        res.addAll(ruleSetProviders);
        return res;
    }

    private DetektResult createResult(File baseDir, Detektion detektion) {
        final UsageCounter counter = new UsageCounter();
        final List findings = detektion.getFindings().entrySet().stream()
                .flatMap(entry -> entry.getValue().stream()
                        .map(finding -> new TypedDetektFinding(baseDir, entry.getKey(), finding))
                        .filter(typed -> counter.accept(collector.accept(typed))))
                .sorted(FINDING_COMPARATOR)
                .collect(toList());
        collector.printUnusedWarning(counter);
        return new DetektResult(this, findings, collector.unusedActions(counter));
    }

    private static class NoFormat implements Config {
        private final Config delegate;

        NoFormat(Config delegate) {
            this.delegate = delegate;
        }

        @Override
        public Config subConfig(String s) {
            return delegate.subConfig(s);
        }

        @Override
        public  T valueOrDefault(String s, T t) {
            return "autoCorrect".equals(s) ? (T) Boolean.FALSE : delegate.valueOrDefault(s, t);
        }

        @Override
        public  T valueOrNull(String s) {
            return "autoCorrect".equals(s) ? (T) Boolean.FALSE : delegate.valueOrNull(s);
        }
    }

    private static class LoggingOutputStream extends OutputStream {
        private final byte[] buf = new byte[1024];
        private int pos;

        @Override
        public void write(int b) {
            buf[pos++] = (byte) b;
        }

        @Override
        public void flush() throws IOException {
            super.flush();
            LOG.warn(new String(buf, 0, pos, StandardCharsets.UTF_8));
            pos = 0;
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy