https://kotlinlang.org logo
#detekt
Title
# detekt
b

bbaldino

07/08/2020, 5:23 PM
I can't seem to get the
ForbiddenMethodCall
to work, I've got it configured like so:
Copy code
style:
  active: true
  ForbiddenMethodCall:
    active: true
    methods: ['java.util.concurrent.ExecutorService.submit', 'kotlin.io.println']
in a custom
detekt.yml
(which doesn't contain much else). I'm using the maven plugin and have it pointing to that config file. (I added
println
in there to try and test after the first one wasn't working.) It's not triggering for
println
calls or
ExecutorService.submit
calls. I tried adding another rule and that did work...so maybe I've just got something misconfigured for
ForbiddenMethodCall
?
g

gammax

07/08/2020, 5:25 PM
Do you have symbol resolution enabled?
b

bbaldino

07/08/2020, 5:26 PM
Nope, unless it's on by default...didn't know about that
g

gammax

07/08/2020, 5:28 PM
Yeah so
ForbiddenMethodClass
uses symbol resolution (that is experimental). To enable it you need to create a custom
detektMain
task and provide
classpath
and `jvmTarget`: https://detekt.github.io/detekt/groovydsl.html#using-type-resolution
b

bbaldino

07/08/2020, 5:29 PM
Thanks! Looking now at how to configure that in the maven plugin.
How do people usually retrieve the classpath?
g

gammax

07/08/2020, 5:39 PM
So you’re on Android right (saw your message in #ktlint), then it gets a bit tricker because of build variants. Ideally this should help: https://github.com/detekt/detekt/issues/2292
b

bbaldino

07/08/2020, 5:40 PM
Oh, nope sorry this isn't on android...just normal jvm.
g

gammax

07/08/2020, 5:40 PM
On a side note for us: we really need to work on this page https://detekt.github.io/detekt/type-and-symbol-solving.html
b

bbaldino

07/08/2020, 5:40 PM
But I am using maven, not gradle
g

gammax

07/08/2020, 5:40 PM
this isn’t on android...just normal jvm.
oops my bad, you’re right.
b

bbaldino

07/08/2020, 5:42 PM
I was able to use
${java.class.path}
, but I see lots of complaints about kotlin stdlib in detekt output
🤔 1
g

gammax

07/08/2020, 5:42 PM
Does the rule work?
b

bbaldino

07/08/2020, 5:42 PM
Nope, doesn't catch the issues (but they're in kotlin code, so that's likely why)
Ok, I have a couple java files in this repo too and I made a call to
ExecutorService#submit
there and it did catch it...so I think it's just a matter of the classpath not including the kotlin stdlib?
g

gammax

07/08/2020, 5:47 PM
AFAIK Detekt should not even inspect Java source files 🤨
b

bbaldino

07/08/2020, 5:48 PM
OH! You're right, it did catch them in the kotlin code!
It does dump a ton of errors though like:
/Vp8UtilsTest.kt:56:32: error: cannot access built-in declaration '<http://kotlin.Int|kotlin.Int>'. Ensure that you have a dependency on the Kotlin standard library
It's weird that it works but seems to choke on all kotlin stdlib types
g

gammax

07/08/2020, 5:54 PM
I don’t really know what is going on tbh. If you can open an issue on detekt with some reproducible test, we could look into it 👍
b

bbaldino

07/08/2020, 5:55 PM
Ok, will take a shot at that
Ah, and I just noticed the rule forbidding println isn't detecting a use of println
I made a small repro project, but I feel like this is probably and issue with the classpath not including the kotlin classes
@gammax what is the configuration for setting the classpath in gradle?
I tried getting the output from
mvn dependency:build-classpath
and hard-coding that into the
classPath
field in the config for the maven plugin (it gives a string like
/Users/bbaldino/.m2/repository/org/jetbrains/kotlin/kotlin-stdlib-jdk8/1.3.72/kotlin-stdlib-jdk8-1.3.72.jar:...
), but I get complaints about
warning: classpath entry points to a non-existent location
...I dunno if that's from some sandboxing or something.
Ok, if I copy
kotlin-stdlib-1.3.72.jar
into the project directory and do
<classPath>${java.class.path};${project.basedir}/kotlin-stdlib-1.3.72.jar</classPath>
that does appear to work...
90 Views