Skip to content

Commit

Permalink
Enable ForbiddenMethodCall (#4334)
Browse files Browse the repository at this point in the history
* Enable ForbiddenMethodCall on detekt code base

* Move Resources extensions from tooling to utils

* Forbid Url.openStream calls and Class.getResourceAsStream
  • Loading branch information
BraisGabin committed Dec 2, 2021
1 parent ed829aa commit 92433ea
Show file tree
Hide file tree
Showing 20 changed files with 39 additions and 19 deletions.
7 changes: 7 additions & 0 deletions config/detekt/detekt.yml
Expand Up @@ -164,6 +164,13 @@ style:
- '@author'
- '@requiresTypeResolution'
excludes: ['**/detekt-rules-style/**/ForbiddenComment.kt']
ForbiddenMethodCall:
active: true
methods:
- 'kotlin.io.print'
- 'kotlin.io.println'
- 'java.net.URL.openStream()'
- 'java.lang.Class.getResourceAsStream()'
ForbiddenVoid:
active: true
LibraryCodeMustSpecifyReturnType:
Expand Down
1 change: 1 addition & 0 deletions detekt-api/build.gradle.kts
Expand Up @@ -10,6 +10,7 @@ plugins {
dependencies {
api(libs.kotlin.compilerEmbeddable)
api(projects.detektPsiUtils)
implementation(projects.detektUtils)

testImplementation(projects.detektTest)
testImplementation(libs.bundles.testImplementation)
Expand Down
@@ -1,5 +1,6 @@
package io.gitlab.arturbosch.detekt.api.internal

import io.github.detekt.utils.openSafeStream
import io.gitlab.arturbosch.detekt.api.Extension
import java.net.URL
import java.util.jar.Manifest
Expand All @@ -18,7 +19,7 @@ fun whichJava(): String = System.getProperty("java.runtime.version")
* Returns the bundled detekt version.
*/
fun whichDetekt(): String? {
fun readVersion(resource: URL): String? = resource.openStream()
fun readVersion(resource: URL): String? = resource.openSafeStream()
.use { Manifest(it).mainAttributes.getValue("DetektVersion") }

return Extension::class.java.classLoader.getResources("META-INF/MANIFEST.MF")
Expand Down
Expand Up @@ -17,6 +17,7 @@ import kotlin.system.exitProcess

fun main(args: Array<String>) {
val result = CliRunner().run(args)
@Suppress("ForbiddenMethodCall")
when (val error = result.error) {
is InvalidConfig, is MaxIssuesReached -> println(error.message)
is UnexpectedError -> {
Expand Down
1 change: 1 addition & 0 deletions detekt-core/build.gradle.kts
Expand Up @@ -13,6 +13,7 @@ dependencies {
implementation(projects.detektReportTxt)
implementation(projects.detektReportXml)
implementation(projects.detektReportSarif)
implementation(projects.detektUtils)

testRuntimeOnly(projects.detektRules)
testRuntimeOnly(projects.detektFormatting)
Expand Down
Expand Up @@ -2,7 +2,7 @@ package io.gitlab.arturbosch.detekt.core.config

import io.github.detekt.tooling.api.spec.ConfigSpec
import io.github.detekt.tooling.api.spec.ProcessingSpec
import io.github.detekt.tooling.internal.openSafeStream
import io.github.detekt.utils.openSafeStream
import io.gitlab.arturbosch.detekt.api.Config
import java.net.URI
import java.net.URL
Expand Down
@@ -1,6 +1,6 @@
package io.gitlab.arturbosch.detekt.core.config

import io.github.detekt.tooling.internal.getSafeResourceAsStream
import io.github.detekt.utils.getSafeResourceAsStream
import io.gitlab.arturbosch.detekt.api.Config

internal object DefaultConfig {
Expand Down
@@ -1,7 +1,7 @@
package io.gitlab.arturbosch.detekt.core.tooling

import io.github.detekt.tooling.api.DefaultConfigurationProvider
import io.github.detekt.tooling.internal.openSafeStream
import io.github.detekt.utils.openSafeStream
import io.gitlab.arturbosch.detekt.api.Config
import io.gitlab.arturbosch.detekt.core.config.DefaultConfig
import java.nio.file.Files
Expand Down
Expand Up @@ -47,8 +47,7 @@ class CompositeConfigSpec : Spek({
"is not of required type Boolean"

assertThatThrownBy {
val value: Boolean = config.valueOrDefault("active", true)
println(value)
config.valueOrDefault("active", true)
}.isInstanceOf(IllegalStateException::class.java)
.hasMessageContaining(expectedErrorMessage)
}
Expand Down
Expand Up @@ -3,7 +3,7 @@
package io.gitlab.arturbosch.detekt.core.config

import io.github.detekt.test.utils.resourceAsPath
import io.github.detekt.tooling.internal.getSafeResourceAsStream
import io.github.detekt.utils.getSafeResourceAsStream
import io.gitlab.arturbosch.detekt.api.Config
import io.gitlab.arturbosch.detekt.test.yamlConfig
import io.gitlab.arturbosch.detekt.test.yamlConfigFromContent
Expand Down
Expand Up @@ -10,9 +10,9 @@ import io.gitlab.arturbosch.detekt.generator.printer.defaultconfig.ConfigPrinter

class DetektPrinter(private val arguments: GeneratorArgs) {

private val markdownWriter = MarkdownWriter()
private val yamlWriter = YamlWriter()
private val propertiesWriter = PropertiesWriter()
private val markdownWriter = MarkdownWriter(System.out)
private val yamlWriter = YamlWriter(System.out)
private val propertiesWriter = PropertiesWriter(System.out)

fun print(pages: List<RuleSetPage>) {
pages.forEach {
Expand Down
@@ -1,9 +1,12 @@
package io.gitlab.arturbosch.detekt.generator.out

import java.io.PrintStream
import java.nio.file.Files
import java.nio.file.Path

internal abstract class AbstractWriter {
internal abstract class AbstractWriter(
private val outputPrinter: PrintStream,
) {

protected abstract val ending: String

Expand All @@ -15,21 +18,21 @@ internal abstract class AbstractWriter {
}
}
Files.write(filePath, content().toByteArray())
println("Wrote: $filePath")
outputPrinter.println("Wrote: $filePath")
}
}

internal class MarkdownWriter : AbstractWriter() {
internal class MarkdownWriter(outputPrinter: PrintStream) : AbstractWriter(outputPrinter) {

override val ending = "md"
}

internal class YamlWriter : AbstractWriter() {
internal class YamlWriter(outputPrinter: PrintStream) : AbstractWriter(outputPrinter) {

override val ending = "yml"
}

internal class PropertiesWriter : AbstractWriter() {
internal class PropertiesWriter(outputPrinter: PrintStream) : AbstractWriter(outputPrinter) {

override val ending = "properties"
}
1 change: 1 addition & 0 deletions detekt-report-html/build.gradle.kts
Expand Up @@ -5,6 +5,7 @@ plugins {
dependencies {
compileOnly(projects.detektApi)
compileOnly(projects.detektMetrics)
implementation(projects.detektUtils)
implementation(libs.kotlinx.html) {
exclude(group = "org.jetbrains.kotlin")
}
Expand Down
Expand Up @@ -2,6 +2,7 @@ package io.github.detekt.report.html

import io.github.detekt.metrics.ComplexityReportGenerator
import io.github.detekt.psi.toUnifiedString
import io.github.detekt.utils.openSafeStream
import io.gitlab.arturbosch.detekt.api.Detektion
import io.gitlab.arturbosch.detekt.api.Finding
import io.gitlab.arturbosch.detekt.api.OutputReport
Expand Down Expand Up @@ -48,7 +49,7 @@ class HtmlOutputReport : OutputReport() {

override fun render(detektion: Detektion) =
javaClass.getResource("/$DEFAULT_TEMPLATE")!!
.openStream()
.openSafeStream()
.bufferedReader()
.use { it.readText() }
.replace(PLACEHOLDER_VERSION, renderVersion())
Expand Down
Expand Up @@ -5,7 +5,6 @@ import io.gitlab.arturbosch.detekt.sample.extensions.processors.fqNamesKey

fun qualifiedNamesReport(detektion: Detektion): String? {
val fqNames = detektion.getData(fqNamesKey)
println("fqNames: $fqNames")
if (fqNames.isNullOrEmpty()) return null

return with(StringBuilder()) {
Expand Down
1 change: 1 addition & 0 deletions detekt-test/build.gradle.kts
Expand Up @@ -6,6 +6,7 @@ plugins {
dependencies {
api(projects.detektApi)
api(projects.detektTestUtils)
implementation(projects.detektUtils)
compileOnly(libs.assertj)
implementation(projects.detektCore)
implementation(projects.detektParser)
Expand Down
@@ -1,10 +1,11 @@
package io.gitlab.arturbosch.detekt.test

import io.github.detekt.test.utils.resource
import io.github.detekt.utils.openSafeStream
import io.gitlab.arturbosch.detekt.api.Config
import io.gitlab.arturbosch.detekt.core.config.YamlConfig
import java.io.StringReader

fun yamlConfig(name: String) = resource(name).toURL().openStream().reader().use(YamlConfig::load)
fun yamlConfig(name: String) = resource(name).toURL().openSafeStream().reader().use(YamlConfig::load)

fun yamlConfigFromContent(content: String): Config = StringReader(content.trimIndent()).use(YamlConfig::load)
3 changes: 3 additions & 0 deletions detekt-utils/build.gradle.kts
@@ -0,0 +1,3 @@
plugins {
id("module")
}
@@ -1,4 +1,4 @@
package io.github.detekt.tooling.internal
package io.github.detekt.utils

import java.io.InputStream
import java.net.URL
Expand Down
1 change: 1 addition & 0 deletions settings.gradle.kts
Expand Up @@ -33,6 +33,7 @@ include("detekt-sample-extensions")
include("detekt-test")
include("detekt-test-utils")
include("detekt-tooling")
include("detekt-utils")

enableFeaturePreview("VERSION_CATALOGS")
enableFeaturePreview("TYPESAFE_PROJECT_ACCESSORS")
Expand Down

0 comments on commit 92433ea

Please sign in to comment.