-
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
28 changed files
with
891 additions
and
108 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
113 changes: 113 additions & 0 deletions
113
code-assert/src/main/java/guru/nidi/codeassert/detekt/DetektAnalyzer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
/* | ||
* 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 java.io.File; | ||
import java.net.URL; | ||
import java.util.*; | ||
|
||
import static guru.nidi.codeassert.config.Language.KOTLIN; | ||
import static io.gitlab.arturbosch.detekt.api.Severity.*; | ||
import static java.lang.Boolean.FALSE; | ||
|
||
public class DetektAnalyzer implements Analyzer<List<TypedDetektFinding>> { | ||
private final AnalyzerConfig config; | ||
private final DetektCollector collector; | ||
|
||
public DetektAnalyzer(AnalyzerConfig config, DetektCollector collector) { | ||
this.config = config; | ||
this.collector = collector; | ||
} | ||
|
||
public DetektResult analyze() { | ||
final URL defaultConfig = DetektAnalyzer.class.getResource("default-detekt-config.yml"); | ||
final Config detektConfig = new NoFormat(YamlConfig.Companion.loadResource(defaultConfig)); | ||
final File baseDir = new File(AnalyzerConfig.Path.commonBase(config.getSourcePaths(KOTLIN)).getPath()); | ||
final ProcessingSettings settings = new ProcessingSettings(baseDir.toPath(), | ||
detektConfig, Collections.emptyList(), false, false, Collections.emptyList()); | ||
final KtTreeCompiler compiler = KtTreeCompiler.Companion.instance(settings); | ||
final Detektion detektion = DetektFacade.INSTANCE.instance(settings).run(compiler); | ||
return createResult(baseDir, detektion); | ||
} | ||
|
||
private DetektResult createResult(File baseDir, Detektion detektion) { | ||
final List<TypedDetektFinding> filtered = new ArrayList<>(); | ||
final UsageCounter counter = new UsageCounter(); | ||
for (final Map.Entry<String, List<Finding>> entry : detektion.getFindings().entrySet()) { | ||
for (final Finding finding : entry.getValue()) { | ||
final TypedDetektFinding typed = new TypedDetektFinding(baseDir, finding.getEntity(), entry.getKey(), | ||
finding.getId(), finding.getIssue().getSeverity(), finding.getIssue().getDescription()); | ||
if (counter.accept(collector.accept(typed))) { | ||
filtered.add(typed); | ||
} | ||
} | ||
} | ||
collector.printUnusedWarning(counter); | ||
Collections.sort(filtered, TypedDetektFindingComparator.INSTANCE); | ||
return new DetektResult(this, filtered, 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> T valueOrDefault(String s, T t) { | ||
return "autoCorrect".equals(s) ? (T) FALSE : delegate.valueOrDefault(s, t); | ||
} | ||
} | ||
|
||
private static class SeverityComparator implements Comparator<Severity> { | ||
static final SeverityComparator INSTANCE = new SeverityComparator(); | ||
|
||
private static final List<Severity> SEVERITIES = Arrays.asList( | ||
Style, CodeSmell, Minor, Performance, Maintainability, Warning, Security, Defect); | ||
|
||
public int compare(Severity s1, Severity s2) { | ||
return SEVERITIES.indexOf(s2) - SEVERITIES.indexOf(s1); | ||
} | ||
} | ||
|
||
private static class TypedDetektFindingComparator implements Comparator<TypedDetektFinding> { | ||
static final TypedDetektFindingComparator INSTANCE = new TypedDetektFindingComparator(); | ||
|
||
public int compare(TypedDetektFinding f1, TypedDetektFinding f2) { | ||
int res = SeverityComparator.INSTANCE.compare(f1.severity, f2.severity); | ||
if (res != 0) { | ||
return res; | ||
} | ||
res = f1.type.compareTo(f2.type); | ||
if (res != 0) { | ||
return res; | ||
} | ||
return f1.name.compareTo(f2.name); | ||
} | ||
} | ||
} |
68 changes: 68 additions & 0 deletions
68
code-assert/src/main/java/guru/nidi/codeassert/detekt/DetektCollector.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
/* | ||
* 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.config.*; | ||
import guru.nidi.codeassert.util.ListUtils; | ||
|
||
import java.io.File; | ||
import java.util.List; | ||
|
||
import static guru.nidi.codeassert.config.Language.KOTLIN; | ||
|
||
public class DetektCollector extends BaseCollector<TypedDetektFinding, Ignore, DetektCollector> { | ||
@Override | ||
public DetektCollector config(final CollectorConfig<Ignore>... configs) { | ||
return new DetektCollector() { | ||
@Override | ||
public ActionResult accept(TypedDetektFinding issue) { | ||
return accept(issue, DetektCollector.this, configs); | ||
} | ||
|
||
public List<Ignore> unused(UsageCounter counter) { | ||
return unused(counter, DetektCollector.this, configs); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return DetektCollector.this.toString() + "\n" + ListUtils.join("\n", configs); | ||
} | ||
}; | ||
} | ||
|
||
@Override | ||
public ActionResult accept(TypedDetektFinding issue) { | ||
return ActionResult.accept(null, 1); | ||
} | ||
|
||
@Override | ||
protected ActionResult doAccept(TypedDetektFinding issue, Ignore action) { | ||
final File file = new File(issue.basedir, issue.entity.getLocation().getFile()); | ||
final String className = guessClassFromFile(file.getAbsolutePath(), KOTLIN); | ||
return action.accept(new NamedLocation(issue.name, KOTLIN, className, null, true)); | ||
} | ||
|
||
@Override | ||
public List<Ignore> unused(UsageCounter counter) { | ||
return unusedNullAction(counter, false); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return ""; | ||
} | ||
|
||
} |
Oops, something went wrong.