Skip to content

Commit faeb653

Browse files
author
Vincent Potucek
committed
PoC: Add rewrite support for errorprone.refasterrules.FileRulesRecipes
1 parent 81bdf0b commit faeb653

File tree

21 files changed

+254
-85
lines changed

21 files changed

+254
-85
lines changed

.github/workflows/build.yml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,12 @@ jobs:
140140
# --scan: Publish the build scan. This will only work on PRs from apache/kafka and trunk
141141
# --no-scan: For public fork PRs, we won't attempt to publish the scan
142142
run: |
143-
./gradlew --build-cache --info $SCAN_ARG check releaseTarGz -x test
143+
./gradlew --build-cache --info $SCAN_ARG check releaseTarGz -x test -x rewriteDryRun
144+
- name: Rewrite
145+
env:
146+
SCAN_ARG: ${{ inputs.is-public-fork && '--no-scan' || '--scan' }}
147+
run: |
148+
./gradlew --build-cache --info $SCAN_ARG rewriteDryRun
144149
- name: Archive check reports
145150
if: always()
146151
uses: actions/upload-artifact@v4

README.md

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,17 @@ For experiments (or regression testing purposes) add `-PcheckstyleVersion=X.y.z`
201201
#### Spotless ####
202202
The import order is a part of static check. please call `spotlessApply` to optimize the imports of Java codes before filing pull request.
203203

204-
./gradlew spotlessApply
204+
`./gradlew spotlessApply`
205+
206+
#### Rewrite
207+
The build system incorporates [Moderne](https://moderne.io/) rewrite capabilities for automated code transformations.
208+
209+
- **Convention** (e.g., JUnit's naming rules)
210+
- **Refactor** safely (e.g., rename methods, migrate APIs)
211+
- **Modernize** (e.g., Java 8 → Java 17 features)
212+
- **Patterns** (e.g., replace `Vector` with `ArrayList`)
213+
214+
`./gradlew rewriteRun`
205215

206216
#### Spotbugs ####
207217
Spotbugs uses static analysis to look for bugs in the code.

build.gradle

Lines changed: 19 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
// See the License for the specific language governing permissions and
1414
// limitations under the License.
1515

16+
import com.github.spotbugs.snom.SpotBugsTask
1617
import org.ajoberstar.grgit.Grgit
1718
import java.nio.charset.StandardCharsets
1819

@@ -29,20 +30,25 @@ buildscript {
2930
}
3031

3132
plugins {
32-
id 'com.github.ben-manes.versions' version '0.48.0'
33-
id 'idea'
34-
id 'jacoco'
35-
id 'java-library'
36-
id 'org.owasp.dependencycheck' version '8.2.1'
37-
id 'org.nosphere.apache.rat' version "0.8.1"
33+
id "com.diffplug.spotless" version "6.25.0" apply false
34+
id "com.github.ben-manes.versions" version "0.48.0"
35+
id "com.github.spotbugs" version "6.0.25" apply false
36+
id "com.gradleup.shadow" version "8.3.6" apply false
37+
id "idea"
3838
id "io.swagger.core.v3.swagger-gradle-plugin" version "${swaggerVersion}"
39-
40-
id "com.github.spotbugs" version '6.0.25' apply false
41-
id 'org.scoverage' version '8.0.3' apply false
42-
id 'com.gradleup.shadow' version '8.3.6' apply false
43-
id 'com.diffplug.spotless' version "6.25.0"
39+
id "jacoco"
40+
id "java-library"
41+
id "org.nosphere.apache.rat" version "0.8.1"
42+
id "org.openrewrite.rewrite" version "7.12.1" apply false
43+
id "org.owasp.dependencycheck" version "8.2.1"
44+
id "org.scoverage" version "8.0.3" apply false
4445
}
4546

47+
apply from: "$rootDir/gradle/.qa/checkstyle.gradle"
48+
apply from: "$rootDir/gradle/.qa/rewrite.gradle"
49+
apply from: "$rootDir/gradle/.qa/spotbugs.gradle"
50+
apply from: "$rootDir/gradle/.qa/spotless.gradle"
51+
4652
ext {
4753
gradleVersion = versions.gradle
4854
minClientJavaVersion = 11
@@ -157,7 +163,7 @@ ext {
157163
libs.log4j2Api,
158164
libs.log4j2Core
159165
]
160-
166+
161167
}
162168

163169
allprojects {
@@ -312,8 +318,6 @@ subprojects {
312318
task allDepInsight(type: DependencyInsightReportTask) {showingAllVariants = false} doLast {}
313319

314320
apply plugin: 'java-library'
315-
apply plugin: 'checkstyle'
316-
apply plugin: "com.github.spotbugs"
317321

318322
// We use the shadow plugin for the jmh-benchmarks module and the `-all` jar can get pretty large, so
319323
// don't publish it
@@ -749,32 +753,7 @@ subprojects {
749753
}
750754
}
751755

752-
checkstyle {
753-
configDirectory = rootProject.layout.projectDirectory.dir("checkstyle")
754-
configProperties = checkstyleConfigProperties("import-control.xml")
755-
toolVersion = versions.checkstyle
756-
}
757-
758-
configure(checkstyleMain) {
759-
group = 'Verification'
760-
description = 'Run checkstyle on all main Java sources'
761-
}
762-
763-
configure(checkstyleTest) {
764-
group = 'Verification'
765-
description = 'Run checkstyle on all test Java sources'
766-
}
767-
768-
test.dependsOn('checkstyleMain', 'checkstyleTest')
769-
770-
spotbugs {
771-
toolVersion = versions.spotbugs
772-
excludeFilter = file("$rootDir/gradle/spotbugs-exclude.xml")
773-
ignoreFailures = false
774-
}
775-
test.dependsOn('spotbugsMain')
776-
777-
tasks.withType(com.github.spotbugs.snom.SpotBugsTask).configureEach {
756+
tasks.withType(SpotBugsTask).configureEach {
778757
reports.configure {
779758
// Continue supporting `xmlFindBugsReport` for compatibility
780759
xml.enabled(project.hasProperty('xmlSpotBugsReport') || project.hasProperty('xmlFindBugsReport'))
@@ -816,20 +795,6 @@ subprojects {
816795
skipProjects = [ ":jmh-benchmarks", ":trogdor" ]
817796
skipConfigurations = [ "zinc" ]
818797
}
819-
apply plugin: 'com.diffplug.spotless'
820-
spotless {
821-
java {
822-
targetExclude('**/generated/**/*.java','**/generated-test/**/*.java')
823-
importOrder('kafka', 'org.apache.kafka', 'com', 'net', 'org', 'java', 'javax', '', '\\#')
824-
removeUnusedImports()
825-
}
826-
}
827-
}
828-
829-
gradle.taskGraph.whenReady { taskGraph ->
830-
taskGraph.getAllTasks().findAll { it.name.contains('spotbugsScoverage') || it.name.contains('spotbugsTest') }.each { task ->
831-
task.enabled = false
832-
}
833798
}
834799

835800
def fineTuneEclipseClasspathFile(eclipse, project) {
@@ -2812,14 +2777,6 @@ project(':streams:streams-scala') {
28122777
dependsOn 'copyDependantLibs'
28132778
}
28142779

2815-
apply plugin: 'com.diffplug.spotless'
2816-
spotless {
2817-
scala {
2818-
target '**/*.scala'
2819-
scalafmt("$versions.scalafmt").configFile('../../checkstyle/.scalafmt.conf').scalaMajorVersion(versions.baseScala)
2820-
licenseHeaderFile '../../checkstyle/java.header', 'package'
2821-
}
2822-
}
28232780
}
28242781

28252782
project(':streams:integration-tests') {

clients/src/test/java/org/apache/kafka/common/security/oauthbearer/internals/secured/OAuthBearerTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
import java.net.URL;
4747
import java.nio.ByteBuffer;
4848
import java.nio.channels.FileChannel;
49+
import java.nio.file.Files;
4950
import java.nio.file.StandardOpenOption;
5051
import java.security.KeyPair;
5152
import java.security.KeyPairGenerator;
@@ -236,7 +237,7 @@ protected JwtConsumer jwtConsumer(PublicKey publicKey) {
236237
}
237238

238239
protected File generatePrivateKey(PrivateKey privateKey) throws IOException {
239-
File file = File.createTempFile("private-", ".key");
240+
File file = Files.createTempFile("private-", ".key").toFile();
240241
byte[] bytes = Base64.getEncoder().encode(privateKey.getEncoded());
241242

242243
try (FileChannel channel = FileChannel.open(file.toPath(), EnumSet.of(StandardOpenOption.WRITE))) {

connect/file/src/test/java/org/apache/kafka/connect/file/FileStreamSourceTaskTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public class FileStreamSourceTaskTest {
5353

5454
@BeforeEach
5555
public void setup() throws IOException {
56-
tempFile = File.createTempFile("file-stream-source-task-test", null);
56+
tempFile = Files.createTempFile("file-stream-source-task-test", null).toFile();
5757
config = new HashMap<>();
5858
config.put(FileStreamSourceConnector.FILE_CONFIG, tempFile.getAbsolutePath());
5959
config.put(FileStreamSourceConnector.TOPIC_CONFIG, TOPIC);

connect/runtime/src/test/java/org/apache/kafka/connect/storage/FileOffsetBackingStoreTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ public void setup() {
8181
when(converter.toConnectData(anyString(), any(byte[].class))).thenReturn(new SchemaAndValue(null,
8282
Arrays.asList("connector", Collections.singletonMap("partitionKey", "dummy"))));
8383
store = new FileOffsetBackingStore(converter);
84-
tempFile = assertDoesNotThrow(() -> File.createTempFile("fileoffsetbackingstore", null));
84+
tempFile = assertDoesNotThrow(() -> Files.createTempFile("fileoffsetbackingstore", null).toFile());
8585
Map<String, String> props = new HashMap<>();
8686
props.put(StandaloneConfig.OFFSET_STORAGE_FILE_FILENAME_CONFIG, tempFile.getAbsolutePath());
8787
props.put(StandaloneConfig.KEY_CONVERTER_CLASS_CONFIG, "org.apache.kafka.connect.json.JsonConverter");

connect/runtime/src/test/resources/test-plugins/bad-packaging/test/plugins/StaticInitializerThrowsRestExtension.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,10 @@
1818
package test.plugins;
1919

2020
import java.io.IOException;
21-
import java.util.List;
2221
import java.util.Map;
2322

24-
import org.apache.kafka.common.config.ConfigDef;
25-
import org.apache.kafka.connect.connector.Task;
2623
import org.apache.kafka.connect.rest.ConnectRestExtension;
2724
import org.apache.kafka.connect.rest.ConnectRestExtensionContext;
28-
import org.apache.kafka.connect.sink.SinkConnector;
2925

3026
/**
3127
* Fake plugin class for testing classloading isolation.

connect/runtime/src/test/resources/test-plugins/classpath-converter/org/apache/kafka/connect/converters/ByteArrayConverter.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,14 @@
1919
import org.apache.kafka.common.config.ConfigDef;
2020
import org.apache.kafka.common.utils.AppInfoParser;
2121
import org.apache.kafka.connect.components.Versioned;
22-
import org.apache.kafka.connect.connector.Task;
2322
import org.apache.kafka.connect.data.Schema;
2423
import org.apache.kafka.connect.data.SchemaAndValue;
2524
import org.apache.kafka.connect.errors.DataException;
26-
import org.apache.kafka.connect.sink.SinkConnector;
2725
import org.apache.kafka.connect.storage.Converter;
2826
import org.apache.kafka.connect.storage.ConverterConfig;
2927
import org.apache.kafka.connect.storage.HeaderConverter;
3028

3129
import java.nio.ByteBuffer;
32-
import java.util.List;
3330
import java.util.Map;
3431

3532
public class ByteArrayConverter implements Converter, HeaderConverter, Versioned {

connect/runtime/src/test/resources/test-plugins/sampling-config-provider/test/plugins/SamplingConfigProvider.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,7 @@
2727
import org.apache.kafka.common.config.provider.ConfigProvider;
2828
import org.apache.kafka.common.config.ConfigData;
2929
import org.apache.kafka.common.config.ConfigChangeCallback;
30-
import org.apache.kafka.connect.data.Schema;
31-
import org.apache.kafka.connect.data.SchemaAndValue;
32-
import org.apache.kafka.connect.storage.Converter;
3330
import org.apache.kafka.connect.runtime.isolation.SamplingTestPlugin;
34-
import org.apache.kafka.connect.storage.HeaderConverter;
3531

3632
/**
3733
* Fake plugin class for testing classloading isolation.

connect/runtime/src/test/resources/test-plugins/sampling-connector/test/plugins/SamplingConnector.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717

1818
package test.plugins;
1919

20-
import java.io.IOException;
2120
import java.util.Collections;
2221
import java.util.ArrayList;
2322
import java.util.List;

0 commit comments

Comments
 (0)