novoda / gradle-static-analysis-plugin

Easy setup of static analysis tools for Android and Java projects.
Apache License 2.0
410 stars 28 forks source link

Bug fix: define closure strategy #185

Closed tasomaniac closed 5 years ago

tasomaniac commented 5 years ago

Problem

We are using very powerful feature of Closure in Groovy which is to define delegates. I've realized that some properties were just not working if they clash with something that is already defined in my script.

If it is a function call, it would call the function I defined instead of the one in the tool.

Solution

I've researched and learned that Closure's prefer the Owner over the Delegate first. But by providing custom strategy, we can force to use only the delegate.

In this case, the delegate is the static analysis tool we are integrating.

tasomaniac commented 5 years ago

The one I came across is version. It's a property in ktlint but when used, it sets the version property of the Gradle project instead.

tasomaniac commented 5 years ago

Here is what I've just tried in the sample project


void toolVersion(String foo) {
    throw new IllegalStateException()
}

staticAnalysis {

    penalty none

    checkstyle {
        toolVersion '8.8'
    }
}

Configuring the project fails with IllegalStateException since toolVersion inside checkstyle calls the wrong method defined above.

image