From bf3dbbbdd6379904fe7e18a20ce78a62fe5bf927 Mon Sep 17 00:00:00 2001 From: Reynold Morel Date: Mon, 19 Aug 2024 09:34:39 -0400 Subject: [PATCH] Adding Checkstyle linter --- .gitignore | 4 +- config/checkstyle/checkstyle.xml | 365 ++++++++++++++++++ config/checkstyle/suppressions.xml | 5 + gradle/verification-metadata.xml | 242 ++++++++++++ rskj-core/build.gradle | 79 +++- .../main/java/co/rsk/core/ExampleObject.java | 50 +++ 6 files changed, 737 insertions(+), 8 deletions(-) create mode 100644 config/checkstyle/checkstyle.xml create mode 100644 config/checkstyle/suppressions.xml create mode 100644 rskj-core/src/main/java/co/rsk/core/ExampleObject.java diff --git a/.gitignore b/.gitignore index 80b59516973..39e1c2131b8 100644 --- a/.gitignore +++ b/.gitignore @@ -53,10 +53,10 @@ blocksminer jacocoHtml/ # CheckStyle Reports -config/checkstyle/reports/ +rskj-core/config/checkstyle/reports/ # PMD -config/pmd/reports/ +rskj-core/config/pmd/reports/ blocksminer1.txt diff --git a/config/checkstyle/checkstyle.xml b/config/checkstyle/checkstyle.xml new file mode 100644 index 00000000000..73498f813c9 --- /dev/null +++ b/config/checkstyle/checkstyle.xml @@ -0,0 +1,365 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/config/checkstyle/suppressions.xml b/config/checkstyle/suppressions.xml new file mode 100644 index 00000000000..f18d7beffe2 --- /dev/null +++ b/config/checkstyle/suppressions.xml @@ -0,0 +1,5 @@ + + + + + diff --git a/gradle/verification-metadata.xml b/gradle/verification-metadata.xml index 510e633ddef..4f6ea37e38f 100644 --- a/gradle/verification-metadata.xml +++ b/gradle/verification-metadata.xml @@ -1113,6 +1113,248 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/rskj-core/build.gradle b/rskj-core/build.gradle index 0078115a127..5d42bbc995f 100644 --- a/rskj-core/build.gradle +++ b/rskj-core/build.gradle @@ -1,10 +1,73 @@ plugins { id 'application' + id 'checkstyle' } apply plugin: 'maven-publish' apply plugin: 'jacoco' +checkstyle { + toolVersion = '8.45' + configFile = file("$rootDir/config/checkstyle/checkstyle.xml") +} + + +// Start time for Checkstyle checks +def checkstyleStartTime = '2024-08-19 09:52:59 EST' + +def getModifiedFiles(File baseDir, String timestampString) { + def dateFormat = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss z") + Date timestamp = dateFormat.parse(timestampString) + + def modifiedFiles = [] + fileTree(baseDir).matching { + include '**/*.java' + }.each { File file -> + if (file.lastModified() > timestamp.time) { + modifiedFiles.add(file) + } + } + + return modifiedFiles +} + +def configureCheckstyleTask(Checkstyle task, File sourceDir, String timestampString) { + def newFiles = getModifiedFiles(sourceDir, timestampString) + task.source = files(newFiles) + task.classpath = files( + sourceSets.main.output.classesDirs, + sourceSets.main.output.resourcesDir + ) + task.reports { + xml.required.set(true) + html.required.set(true) + } +} + +tasks.withType(Checkstyle).configureEach { Checkstyle task -> + switch (task.name) { + case 'checkstyleMain': + configureCheckstyleTask(task, file('src/main/java'), checkstyleStartTime) + break + case 'checkstyleTest': + configureCheckstyleTask(task, file('src/test/java'), checkstyleStartTime) + break + case 'checkstyleIntegrationTest': + configureCheckstyleTask(task, file('src/integrationTest/java'), checkstyleStartTime) + break + case 'checkstyleJmh': + configureCheckstyleTask(task, file('src/jmh/java'), checkstyleStartTime) + break + } +} + +checkstyleMain.mustRunAfter clean +checkstyleTest.mustRunAfter clean + +checkstyleMain.dependsOn 'classes' +checkstyleTest.dependsOn 'testClasses' + + configurations { jmh } @@ -252,8 +315,6 @@ javadoc { options.encoding = "UTF-8" } -def generatedResources = "$buildDir/generated-resources" - publishing { publications { rskj(MavenPublication) { @@ -275,8 +336,9 @@ task generateResources { def buildInfoFile = 'build-info.properties' doLast { - mkdir generatedResources - def generated = new File(generatedResources as String, buildInfoFile) + def generatedDir = new File(buildDir, 'generatedResources') + mkdir(generatedDir) + def generated = new File(generatedDir, buildInfoFile) def commitHash = gitCommitHash() def currentBranch = gitCurrentBranch() generated.text = """ @@ -297,7 +359,7 @@ task javadocJar(type: Jar) { } jar { - dependsOn 'generateResources' + dependsOn generateResources def commitHash = gitCommitHash() def currentBranch = gitCurrentBranch() manifest { @@ -306,7 +368,7 @@ jar { } from sourceSets.main.output.classesDirs from sourceSets.main.output.resourcesDir - from generatedResources + from new File(buildDir, 'generatedResources') // Reference the generated resources directory } task generatePom(dependsOn: jar) { @@ -417,3 +479,8 @@ static def amendPathIfNeeded(details) { details.path = newPath } } + +tasks.named('check').configure { + dependsOn 'checkstyleMain' + dependsOn 'checkstyleTest' +} diff --git a/rskj-core/src/main/java/co/rsk/core/ExampleObject.java b/rskj-core/src/main/java/co/rsk/core/ExampleObject.java new file mode 100644 index 00000000000..ccb4fd8be47 --- /dev/null +++ b/rskj-core/src/main/java/co/rsk/core/ExampleObject.java @@ -0,0 +1,50 @@ +package co.rsk.core; + +/** + * Testing + * This comment demonstrates the use of Javadoc comment. + */ +public class ExampleObject { + private int number; + private String text; + + public ExampleObject(int number, String text) { + this.number = number; + this.text = text; + } + + public int getNumber() { + return number; + } + + public void setNumber(int number) { + this.number = number; + } + + public String getText() { + return text; + } + + public void setText(String text) { + this.text = text; + } + + @Override + public String toString() { + return "DummyClass{" + + "number=" + number + + ", text='" + text + '\'' + + '}'; + } + + /** + * Example method def comment. + */ + public int testDummyProcess(int a, int b) { + int x = a + b; + int help = x * 2; + int test = help + 10; + + return test; + } +}