Skip to content

Commit 4564214

Browse files
author
Vincent Potucek
committed
[Experimental] Add rewrite support for RemoveUnusedPrivateMethods & RemoveUnusedImports
1 parent fba01c4 commit 4564214

File tree

37 files changed

+122
-237
lines changed

37 files changed

+122
-237
lines changed

README.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,12 @@ 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 import order is a part of static check. please call `rewriteRun` to optimize the imports of Java codes before filing pull request.
208+
209+
`./gradlew rewriteRun`
205210

206211
#### Spotbugs ####
207212
Spotbugs uses static analysis to look for bugs in the code.

build.gradle

Lines changed: 81 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@
1616
import org.ajoberstar.grgit.Grgit
1717
import java.nio.charset.StandardCharsets
1818

19+
import static java.lang.Boolean.valueOf
20+
import static java.lang.System.getenv
21+
import static java.lang.System.getenv
22+
import static java.util.Objects.isNull
23+
1924
buildscript {
2025
repositories {
2126
mavenCentral()
@@ -29,18 +34,77 @@ buildscript {
2934
}
3035

3136
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"
37+
id "com.diffplug.spotless" version "7.2.1"
38+
id "com.github.ben-manes.versions" version "0.48.0"
39+
id "com.github.spotbugs" version "6.0.25" apply false
40+
id "com.gradleup.shadow" version "8.3.6" apply false
41+
id "idea"
3842
id "io.swagger.core.v3.swagger-gradle-plugin" version "${swaggerVersion}"
43+
id "jacoco"
44+
id "java-library"
45+
id "org.nosphere.apache.rat" version "0.8.1"
46+
id "org.openrewrite.rewrite" version "7.12.1"
47+
id "org.owasp.dependencycheck" version "8.2.1"
48+
id "org.scoverage" version "8.0.3" apply false
49+
}
50+
51+
rewrite {
52+
activeRecipe(
53+
"org.openrewrite.gradle.GradleBestPractices",
54+
"org.openrewrite.java.RemoveUnusedImports",
55+
"org.openrewrite.staticanalysis.RemoveUnusedLocalVariables",
56+
"org.openrewrite.staticanalysis.RemoveUnusedPrivateFields",
57+
"org.openrewrite.staticanalysis.RemoveUnusedPrivateMethods",
58+
//"org.openrewrite.java.OrderImports",
59+
//"org.openrewrite.java.format.RemoveTrailingWhitespace",
60+
//"org.openrewrite.java.migrate.UpgradeToJava17",
61+
//"org.openrewrite.java.migrate.util.MigrateCollectionsSingletonList",
62+
//"org.openrewrite.java.migrate.util.MigrateCollectionsUnmodifiableList",
63+
//"org.openrewrite.java.recipes.JavaRecipeBestPractices",
64+
//"org.openrewrite.java.recipes.RecipeNullabilityBestPractices",
65+
//"org.openrewrite.java.recipes.RecipeTestingBestPractices",
66+
//"org.openrewrite.staticanalysis.CodeCleanup",
67+
//"org.openrewrite.staticanalysis.EmptyBlock",
68+
//"org.openrewrite.staticanalysis.EqualsAvoidsNull",
69+
//"org.openrewrite.staticanalysis.JavaApiBestPractices",
70+
//"org.openrewrite.staticanalysis.MissingOverrideAnnotation",
71+
//"org.openrewrite.staticanalysis.ModifierOrder",
72+
//"org.openrewrite.staticanalysis.NoFinalizer",
73+
//"org.openrewrite.staticanalysis.RemoveCallsToSystemGc",
74+
//"org.openrewrite.staticanalysis.RemoveUnneededAssertion",
75+
//"org.openrewrite.staticanalysis.StringLiteralEquality",
76+
//"org.openrewrite.staticanalysis.UnnecessaryParentheses",
77+
//"org.openrewrite.staticanalysis.UnnecessaryThrows",
78+
//"org.openrewrite.text.EndOfLineAtEndOfFile",
79+
//"tech.picnic.errorprone.refasterrules.CollectionRulesRecipes",
80+
//"tech.picnic.errorprone.refasterrules.FileRulesRecipes",
81+
//"tech.picnic.errorprone.refasterrules.NullRulesRecipes",
82+
//"tech.picnic.errorprone.refasterrules.NullRulesRecipes",
83+
//"tech.picnic.errorprone.refasterrules.StreamRulesRecipes",
84+
//"tech.picnic.errorprone.refasterrules.StringRulesRecipes",
85+
// running active recipes: tech.picnic.errorprone.refasterrules.NullRulesRecipes
86+
//<============-> 99% EXECUTING [1h 38s]
87+
//> :rewriteRun
88+
)
89+
exportDatatables = true
90+
exclusions.add("**.GarbageCollectedMemoryPoolTest.java")
91+
failOnDryRunResults = true
92+
}
3993

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"
94+
spotless {
95+
java {
96+
targetExclude('**/generated/**/*.java','**/generated-test/**/*.java')
97+
importOrder('kafka', 'org.apache.kafka', 'com', 'net', 'org', 'java', 'javax', '', '\\#')
98+
//licenseHeaderFile("$rootDir/checkstyle/java.header", 'package') todo apply me
99+
}
100+
scala {
101+
scalafmt("$versions.scalafmt").configFile("$rootDir/checkstyle/.scalafmt.conf").scalaMajorVersion(versions.baseScala)
102+
//licenseHeaderFile("$rootDir/checkstyle/java.header", 'package') todo apply me
103+
}
104+
}
105+
106+
dependencies {
107+
rewrite("org.openrewrite.recipe:rewrite-third-party:0.24.3", "org.openrewrite.recipe:rewrite-static-analysis:2.13.0")
44108
}
45109

46110
ext {
@@ -157,7 +221,7 @@ ext {
157221
libs.log4j2Api,
158222
libs.log4j2Core
159223
]
160-
224+
161225
}
162226

163227
allprojects {
@@ -224,6 +288,11 @@ allprojects {
224288
delete "${projectDir}/src/generated"
225289
delete "${projectDir}/src/generated-test"
226290
}
291+
292+
// off switch for release: ' -x check' or ' -x rewriteDryRun spotlessJavaCheck'
293+
tasks {
294+
check.dependsOn(rewriteDryRun, spotlessCheck)
295+
}
227296
}
228297

229298
def determineCommitId() {
@@ -551,7 +620,7 @@ subprojects {
551620
maxFailures = userMaxTestRetryFailures
552621
}
553622
}
554-
623+
555624
finalizedBy("copyTestXml")
556625
}
557626

@@ -816,14 +885,6 @@ subprojects {
816885
skipProjects = [ ":jmh-benchmarks", ":trogdor" ]
817886
skipConfigurations = [ "zinc" ]
818887
}
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-
}
827888
}
828889

829890
gradle.taskGraph.whenReady { taskGraph ->
@@ -2811,15 +2872,6 @@ project(':streams:streams-scala') {
28112872
jar {
28122873
dependsOn 'copyDependantLibs'
28132874
}
2814-
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-
}
28232875
}
28242876

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

clients/clients-integration-tests/src/test/java/org/apache/kafka/clients/consumer/ConsumerTopicCreationTest.java

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,15 @@
1919
import org.apache.kafka.clients.admin.Admin;
2020
import org.apache.kafka.clients.admin.TopicListing;
2121
import org.apache.kafka.common.test.ClusterInstance;
22-
import org.apache.kafka.common.test.api.ClusterConfig;
2322
import org.apache.kafka.common.test.api.ClusterTemplate;
2423

2524
import java.time.Duration;
2625
import java.util.List;
2726
import java.util.Locale;
2827
import java.util.Map;
29-
import java.util.Set;
3028

3129
import static org.apache.kafka.clients.consumer.ConsumerConfig.ALLOW_AUTO_CREATE_TOPICS_CONFIG;
3230
import static org.apache.kafka.clients.consumer.ConsumerConfig.GROUP_PROTOCOL_CONFIG;
33-
import static org.apache.kafka.common.test.api.Type.KRAFT;
3431
import static org.apache.kafka.server.config.ServerLogConfigs.AUTO_CREATE_TOPICS_ENABLE_CONFIG;
3532
import static org.junit.jupiter.api.Assertions.assertFalse;
3633
import static org.junit.jupiter.api.Assertions.assertTrue;
@@ -102,19 +99,6 @@ private List<String> getAllTopics(ClusterInstance cluster) throws Exception {
10299
}
103100
}
104101

105-
private static List<ClusterConfig> autoCreateTopicsConfigs() {
106-
return List.of(
107-
ClusterConfig.defaultBuilder()
108-
.setTypes(Set.of(KRAFT))
109-
.setServerProperties(Map.of(AUTO_CREATE_TOPICS_ENABLE_CONFIG, "true"))
110-
.build(),
111-
ClusterConfig.defaultBuilder()
112-
.setTypes(Set.of(KRAFT))
113-
.setServerProperties(Map.of(AUTO_CREATE_TOPICS_ENABLE_CONFIG, "false"))
114-
.build()
115-
);
116-
}
117-
118102
private Consumer<byte[], byte[]> createConsumer(ClusterInstance cluster, GroupProtocol protocol, boolean allowConsumerAutoCreateTopics) {
119103
Map<String, Object> consumerConfig = Map.of(
120104
ALLOW_AUTO_CREATE_TOPICS_CONFIG, allowConsumerAutoCreateTopics,

clients/src/main/java/org/apache/kafka/common/requests/ShareAcknowledgeResponse.java

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -88,12 +88,6 @@ public static ShareAcknowledgeResponse parse(Readable readable, short version) {
8888
);
8989
}
9090

91-
private static boolean matchingTopic(ShareAcknowledgeResponseData.ShareAcknowledgeTopicResponse previousTopic, TopicIdPartition currentTopic) {
92-
if (previousTopic == null)
93-
return false;
94-
return previousTopic.topicId().equals(currentTopic.topicId());
95-
}
96-
9791
public static ShareAcknowledgeResponseData.PartitionData partitionResponse(TopicIdPartition topicIdPartition, Errors error) {
9892
return partitionResponse(topicIdPartition.topicPartition().partition(), error);
9993
}

clients/src/test/java/org/apache/kafka/clients/consumer/internals/ConsumerCoordinatorTest.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,6 @@ public abstract class ConsumerCoordinatorTest {
149149
private final long retryBackoffMaxMs = 1000;
150150
private final int autoCommitIntervalMs = 2000;
151151
private final int requestTimeoutMs = 30000;
152-
private final int throttleMs = 10;
153152
private final MockTime time = new MockTime();
154153
private GroupRebalanceConfig rebalanceConfig;
155154

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

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
import org.apache.kafka.common.KafkaException;
2020
import org.apache.kafka.common.security.oauthbearer.internals.secured.OAuthBearerTest;
2121
import org.apache.kafka.common.utils.MockTime;
22-
import org.apache.kafka.common.utils.Time;
2322

2423
import org.jose4j.jwt.consumer.JwtContext;
2524
import org.jose4j.jwx.JsonWebStructure;
@@ -165,8 +164,6 @@ public void testInvalidAlgorithm() throws IOException {
165164
}
166165

167166
private static class Builder {
168-
169-
private final Time time = new MockTime();
170167
private String algorithm = TOKEN_SIGNING_ALGORITHM_RS256;
171168
private File privateKeyFile;
172169
private Optional<String> passphrase = Optional.empty();

connect/runtime/src/test/java/org/apache/kafka/connect/runtime/WorkerTest.java

Lines changed: 0 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -2977,11 +2977,6 @@ private void verifyConverters() {
29772977
verify(plugins).newInternalConverter(eq(false), anyString(), anyMap());
29782978
}
29792979

2980-
private void mockTaskConverter(ClassLoaderUsage classLoaderUsage, String converterClassConfig, Converter returning) {
2981-
when(plugins.newConverter(any(AbstractConfig.class), eq(converterClassConfig), eq(classLoaderUsage)))
2982-
.thenReturn(returning);
2983-
}
2984-
29852980
private void mockVersionedTaskConverterFromConnector(String converterClassConfig, String converterVersionConfig, Converter returning) {
29862981
when(plugins.newConverter(any(ConnectorConfig.class), eq(converterClassConfig), eq(converterVersionConfig))).thenReturn(returning);
29872982
}
@@ -2998,15 +2993,6 @@ private void verifyVersionedTaskConverterFromWorker(String converterClassConfig,
29982993
verify(plugins).newConverter(any(WorkerConfig.class), eq(converterClassConfig), eq(converterVersionConfig));
29992994
}
30002995

3001-
private void mockTaskHeaderConverter(ClassLoaderUsage classLoaderUsage, HeaderConverter returning) {
3002-
when(plugins.newHeaderConverter(any(AbstractConfig.class), eq(WorkerConfig.HEADER_CONVERTER_CLASS_CONFIG), eq(classLoaderUsage)))
3003-
.thenReturn(returning);
3004-
}
3005-
3006-
private void verifyTaskHeaderConverter() {
3007-
verify(plugins).newHeaderConverter(any(AbstractConfig.class), eq(WorkerConfig.HEADER_CONVERTER_CLASS_CONFIG), eq(ClassLoaderUsage.CURRENT_CLASSLOADER));
3008-
}
3009-
30102996
private void mockVersionedTaskHeaderConverterFromConnector(HeaderConverter returning) {
30112997
when(plugins.newHeaderConverter(any(ConnectorConfig.class), eq(ConnectorConfig.HEADER_CONVERTER_CLASS_CONFIG), eq(ConnectorConfig.HEADER_CONVERTER_VERSION_CONFIG)))
30122998
.thenReturn(returning);
@@ -3034,25 +3020,13 @@ private void verifyGenericIsolation() {
30343020
verify(loaderSwap, atLeastOnce()).close();
30353021
}
30363022

3037-
private void mockConnectorIsolation(String connectorClass, Connector connector) {
3038-
mockGenericIsolation();
3039-
when(plugins.newConnector(connectorClass)).thenReturn(connector);
3040-
when(connector.version()).thenReturn("1.0");
3041-
}
3042-
30433023
private void mockVersionedConnectorIsolation(String connectorClass, VersionRange range, Connector connector) {
30443024
mockGenericIsolation();
30453025
when(plugins.pluginLoader(connectorClass, range)).thenReturn(pluginLoader);
30463026
when(plugins.newConnector(connectorClass, range)).thenReturn(connector);
30473027
when(connector.version()).thenReturn(range == null ? "unknown" : range.toString());
30483028
}
30493029

3050-
private void verifyConnectorIsolation(Connector connector) {
3051-
verifyGenericIsolation();
3052-
verify(plugins).newConnector(anyString());
3053-
verify(connector, atLeastOnce()).version();
3054-
}
3055-
30563030
private void verifyVersionedConnectorIsolation(String connectorClass, VersionRange range, Connector connector) {
30573031
verifyGenericIsolation();
30583032
verify(plugins).pluginLoader(connectorClass, range);
@@ -3077,13 +3051,6 @@ private void mockVersionedTaskIsolation(Class<? extends Connector> connectorClas
30773051
when(task.version()).thenReturn(range == null ? "unknown" : range.toString());
30783052
}
30793053

3080-
private void verifyTaskIsolation(Task task) {
3081-
verifyGenericIsolation();
3082-
verify(plugins).connectorClass(anyString());
3083-
verify(plugins).newTask(any());
3084-
verify(task).version();
3085-
}
3086-
30873054
private void verifyVersionedTaskIsolation(Class<? extends Connector> connectorClass, Class<? extends Task> taskClass, VersionRange range, Task task) {
30883055
verifyGenericIsolation();
30893056
verify(plugins).pluginLoader(connectorClass.getName(), range);

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.

0 commit comments

Comments
 (0)