Skip to content

Commit 0b15b77

Browse files
authored
Include static imports in spotlessApply (#16237)
1 parent 935ef1f commit 0b15b77

3 files changed

Lines changed: 75 additions & 20 deletions

File tree

buildscripts/checkstyle.xml

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -64,26 +64,6 @@
6464
<property name="message"
6565
value="Please statically import methods from Assertions (OpenTelemetryAssertions extends Assertions)"/>
6666
</module>
67-
<module name="RegexpSinglelineJava">
68-
<property name="format" value="^(?!.*import).*\bObjects\.requireNonNull\b"/>
69-
<property name="message" value="Please statically import Objects.requireNonNull"/>
70-
</module>
71-
<module name="RegexpSinglelineJava">
72-
<property name="format" value="^(?!.*import).*\bElementMatchers\.[a-z]"/>
73-
<property name="message" value="Please statically import methods from ElementMatchers"/>
74-
</module>
75-
<module name="RegexpSinglelineJava">
76-
<property name="format" value="^(?!.*import).*\bAgentElementMatchers\.[a-z]"/>
77-
<property name="message" value="Please statically import methods from AgentElementMatchers"/>
78-
</module>
79-
<module name="RegexpSinglelineJava">
80-
<property name="format" value="^(?!.*import).*\bTimeUnit\.[A-Z]"/>
81-
<property name="message" value="Please statically import constants from TimeUnit"/>
82-
</module>
83-
<module name="RegexpSinglelineJava">
84-
<property name="format" value="^(?!.*import).*\bStandardCharsets\.[A-Z]"/>
85-
<property name="message" value="Please statically import constants from StandardCharsets"/>
86-
</module>
8767
<module name="OuterTypeFilename"/>
8868
<module name="IllegalTokenText">
8969
<property name="tokens" value="STRING_LITERAL, CHAR_LITERAL"/>
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/*
2+
* Copyright The OpenTelemetry Authors
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
package io.opentelemetry.instrumentation.gradle
7+
8+
import com.diffplug.spotless.FormatterFunc
9+
import java.io.Serializable
10+
11+
/**
12+
* Spotless custom step that rewrites qualified references (e.g. {@code Objects.requireNonNull})
13+
* into unqualified references and adds the corresponding static import. Runs before
14+
* googleJavaFormat so that imports are sorted and any newly-unused regular imports are removed.
15+
*/
16+
class StaticImportFormatter : FormatterFunc, Serializable {
17+
18+
override fun apply(input: String): String {
19+
// (className, fullyQualifiedName, memberPattern)
20+
val rules = listOf(
21+
Triple("Objects", "java.util.Objects", "requireNonNull"),
22+
Triple(
23+
"ElementMatchers",
24+
"net.bytebuddy.matcher.ElementMatchers",
25+
"[a-z][a-zA-Z0-9]*"
26+
),
27+
Triple(
28+
"AgentElementMatchers",
29+
"io.opentelemetry.javaagent.extension.matcher.AgentElementMatchers",
30+
"[a-z][a-zA-Z0-9]*"
31+
),
32+
Triple("TimeUnit", "java.util.concurrent.TimeUnit", "[A-Z][A-Z_0-9]*"),
33+
Triple(
34+
"StandardCharsets",
35+
"java.nio.charset.StandardCharsets",
36+
"[A-Z][A-Z_0-9]*"
37+
),
38+
)
39+
40+
var content = input
41+
val importsToAdd = mutableSetOf<String>()
42+
43+
for ((className, pkg, memberPattern) in rules) {
44+
val regex = Regex("\\b${className}\\.(${memberPattern})\\b")
45+
val lines = content.lines().toMutableList()
46+
for (i in lines.indices) {
47+
if (lines[i].trimStart().startsWith("import ")) continue
48+
for (match in regex.findAll(lines[i])) {
49+
importsToAdd.add("import static ${pkg}.${match.groupValues[1]};")
50+
}
51+
lines[i] = regex.replace(lines[i], "$1")
52+
}
53+
content = lines.joinToString("\n")
54+
}
55+
56+
if (importsToAdd.isNotEmpty()) {
57+
val lines = content.lines().toMutableList()
58+
val firstImportIndex = lines.indexOfFirst { it.trimStart().startsWith("import ") }
59+
if (firstImportIndex >= 0) {
60+
for ((offset, imp) in importsToAdd.sorted().withIndex()) {
61+
lines.add(firstImportIndex + offset, imp)
62+
}
63+
content = lines.joinToString("\n")
64+
}
65+
}
66+
67+
return content
68+
}
69+
70+
companion object {
71+
private const val serialVersionUID = 1L
72+
}
73+
}

conventions/src/main/kotlin/otel.spotless-conventions.gradle.kts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
import com.diffplug.gradle.spotless.SpotlessExtension
2+
import io.opentelemetry.instrumentation.gradle.StaticImportFormatter
23

34
plugins {
45
id("com.diffplug.spotless")
56
}
67

78
spotless {
89
java {
10+
custom("staticImports", StaticImportFormatter())
911
googleJavaFormat()
1012
licenseHeaderFile(
1113
rootProject.file("buildscripts/spotless.license.java"),

0 commit comments

Comments
 (0)