RequireThis rule and fixThis Gradle task

* `gradlew clean check -x test --parallel --continue` - to collect reports
* `gradlew fixThis --parallel` - to fix all possible vulnerabilities. With `-Dfile.encoding=UTF-8` on Windows

Since the `RequireThisCheck` doesn't see parents for anonymous classes (e.g. `Runnable` callback), its report doesn't contains the outer class name with `this.`,
therefore we still have to fix those cases manually.
Thanks to the wrong `replacer` just with `this.` we have uncompilable code enough easy to find problems.
Not so easy to fix for good readability though...

* Upgrade to Grade 2.12
* Upgrade to SonarQube native plugin

The fix contains at about 300 files. So, will be done on merge.

Fix `fixThis.gradle` according PR comments

Apply `fixThis` and also `fixModifiers` for test classes.
 Fix some `this.` inner issues manually.
 Make code polishing for long lines after `fixThis`

Fix conflicts and vulnerabilities after the rebase
This commit is contained in:
Artem Bilan
2016-03-17 17:02:15 -04:00
parent e189307ab6
commit 2b0598291c
348 changed files with 1828 additions and 1781 deletions

View File

@@ -67,10 +67,10 @@
<!-- <property name="max" value="3" /> -->
<!-- </module> -->
<!-- <module name="MultipleVariableDeclarations" /> -->
<!-- <module name="RequireThis"> -->
<!-- <property name="checkMethods" value="false" /> -->
<!-- </module> -->
<!-- <module name="OneStatementPerLine" /> -->
<module name="RequireThis">
<property name="checkMethods" value="false" />
</module>
<!--<module name="OneStatementPerLine" /> -->
<!-- Imports -->
<!-- <module name="AvoidStarImport" /> -->

View File

@@ -18,12 +18,13 @@ task fixModifiers << {
def outSource = ''
file.eachLine { line, ln ->
if (!headerFixed) {
def matcher = line =~ /Copyright (20\d\d)(?:-20\d\d)?/
def matcher = line =~ /Copyright (20\d\d)(?:-(20\d\d))?/
if (matcher.count) {
def years = matcher[0][1]
if (years != now) {
years = years + "-$now"
line = line.replaceFirst(/(20\d\d)(?:-20\d\d)?/, years)
def year1 = matcher[0][1]
if (now != year1) {
if (now != matcher[0][2]) {
line = line.replaceFirst(/(20\d\d)(?:-20\d\d)?/, year1 + "-$now")
}
}
headerFixed = true
}

View File

@@ -1,64 +1,65 @@
task fixThis << {
fileTree("${buildDir}/reports/checkstyle").include('*.xml').each { report ->
println "processing $report"
def xml = new XmlParser(false, false).parse(report)
xml.file.each { f ->
// println "processing $f"
def errors = f.error
def hasThisError = false
def thisErrors = []
errors.each { error ->
if (error.@source == 'com.puppycrawl.tools.checkstyle.checks.coding.RequireThisCheck') {
thisErrors.add(error)
hasThisError = true
}
}
// println hasThisError
// println f.@name
if (hasThisError) {
if (thisErrors) {
def errorInx = 0
def error = thisErrors[errorInx++]
def lx = Integer.valueOf(error.@line)
println error
def file = new File(f.@name)
println "Fixing file $file ..."
boolean headerFixed
def outSource = ''
def ln = 0
file.eachLine { line ->
// println line
ln++
def matcher = line =~ /Copyright (20\d\d)(?:-20\d\d)?/
if (matcher.count) {
def years = matcher[0][1]
if (years != now) {
years = years + "-$now"
file.eachLine { line, ln ->
if (!headerFixed) {
def matcher = line =~ /Copyright (20\d\d)(?:-(20\d\d))?/
if (matcher.count) {
def year1 = matcher[0][1]
if (now != year1) {
if (now != matcher[0][2]) {
line = line.replaceFirst(/(20\d\d)(?:-20\d\d)?/, year1 + "-$now")
}
}
headerFixed = true
}
line.replaceFirst(/(20\d\d)(?:-20\d\d)?/, years)
}
if (error && ln == lx) {
// println line
def beforeIndex = Integer.valueOf(error.@column) - 1
if (error && ln == (error.@line as int)) {
def message = error.@message
def property = message.substring(message.indexOf('\'') + 1, message.lastIndexOf('\''))
def thisPrefix = message.substring(message.indexOf('"') + 1, message.lastIndexOf('"'))
def index = (error.@column as int) - 1
def chars = line.toCharArray()
for (int i = 0; i < beforeIndex; i++) {
for (int i = 0; i < index; i++) {
if (chars[i] == '\t') { // tabs before code == 8
beforeIndex -= 7;
index -= 7;
}
else if (chars[i] != ' ') { // tabs after code start are only counted as 1
break;
}
}
line = line.substring(0, beforeIndex) + "this." + line.substring(beforeIndex)
// println line
error = thisErrors[errorInx++]
while (error && lx == Integer.valueOf(error.@line)) {
line = line.substring(0, index) + thisPrefix + property + line.substring(index + property.length())
println "Fixed line $line"
while (error && ln == (error.@line as int)) {
error = thisErrors[errorInx++]
}
if (error) {
lx = Integer.valueOf(error.@line)
}
}
outSource += line + '\n'
outSource += line + System.lineSeparator()
}
file.write(outSource)
println()
}
}
}