Skip to content

Commit e856ecb

Browse files
authored
Forbid wildcard imports (#1180)
* Forbid wildcard imports Needs to be implemented as an throwing custom step as Spotless doesn't have built in support to remove/deny wildcards: diffplug/spotless#649 Signed-off-by: Thomas Farr <[email protected]> * Make conventions plugin Signed-off-by: Thomas Farr <[email protected]> * Fix wildcard imports Signed-off-by: Thomas Farr <[email protected]> * Allow overriding eclipse formatter config file Signed-off-by: Thomas Farr <[email protected]> * Fix message formatting Signed-off-by: Thomas Farr <[email protected]> --------- Signed-off-by: Thomas Farr <[email protected]>
1 parent 17c94e0 commit e856ecb

File tree

12 files changed

+114
-69
lines changed

12 files changed

+114
-69
lines changed

build.gradle.kts

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ allprojects {
4040
mavenLocal()
4141
maven(url = "https://aws.oss.sonatype.org/content/repositories/snapshots")
4242
mavenCentral()
43-
maven(url = "https://plugins.gradle.org/m2/")
43+
gradlePluginPortal()
4444
}
4545
}
4646

buildSrc/build.gradle.kts

+9-3
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,19 @@
3030
* GitHub history for details.
3131
*/
3232

33-
dependencies {
34-
implementation("org.ajoberstar.grgit:grgit-gradle:5.2.2")
33+
plugins {
34+
`kotlin-dsl`
3535
}
3636

3737
repositories {
3838
mavenLocal()
3939
maven(url = "https://aws.oss.sonatype.org/content/repositories/snapshots")
4040
mavenCentral()
41-
maven(url = "https://plugins.gradle.org/m2/")
41+
gradlePluginPortal()
4242
}
43+
44+
dependencies {
45+
implementation("org.ajoberstar.grgit:grgit-gradle:5.2.2")
46+
implementation("com.diffplug.spotless", "spotless-plugin-gradle", "6.25.0")
47+
}
48+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
plugins {
2+
id("com.diffplug.spotless")
3+
}
4+
5+
interface SpotlessConventionsPluginExtension {
6+
val eclipseFormatterConfigFile: RegularFileProperty
7+
}
8+
9+
val extension = project.extensions.create<SpotlessConventionsPluginExtension>("spotlessConventions")
10+
11+
extension.eclipseFormatterConfigFile.convention(rootProject.layout.projectDirectory.file("buildSrc/formatterConfig.xml"))
12+
13+
spotless {
14+
java {
15+
target("**/*.java")
16+
17+
licenseHeaderFile(rootProject.file("LICENSE_HEADER.txt"))
18+
.named("PrimaryLicenseHeader")
19+
.onlyIfContentMatches("^((?!Licensed to Elasticsearch)[\\s\\S])*$")
20+
.delimiter("(package |//-----)")
21+
22+
licenseHeaderFile(rootProject.file("LICENSE_HEADER_FORKED.txt"))
23+
.named("ForkedLicenseHeader")
24+
.onlyIfContentMatches("Licensed to Elasticsearch")
25+
.delimiter("(package |//-----)")
26+
27+
// Use the default importOrder configuration
28+
importOrder()
29+
removeUnusedImports()
30+
31+
eclipse().configFile(extension.eclipseFormatterConfigFile)
32+
33+
trimTrailingWhitespace()
34+
endWithNewline()
35+
36+
// NOTE: Any time a custom step below is modified, bump this number.
37+
// Allows up-to-date checks to work correctly with custom steps.
38+
bumpThisNumberIfACustomStepChanges(1)
39+
40+
val wildcardImportRegex = Regex("""^import\s+(?:static\s+)?[^*\s]+\.\*;$""", RegexOption.MULTILINE)
41+
custom("Refuse wildcard imports") { contents ->
42+
// Wildcard imports can't be resolved by spotless itself.
43+
// This will require the developer themselves to adhere to best practices.
44+
val wildcardImports = wildcardImportRegex.findAll(contents)
45+
if (wildcardImports.any()) {
46+
var msg = """
47+
Please replace the following wildcard imports with explicit imports ('spotlessApply' cannot resolve this issue):
48+
""".trimIndent()
49+
wildcardImports.forEach {
50+
msg += "\n\t- ${it.value}"
51+
}
52+
msg += "\n"
53+
throw AssertionError(msg)
54+
}
55+
contents
56+
}
57+
}
58+
}

java-client/build.gradle.kts

+3-27
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ buildscript {
4040
mavenLocal()
4141
maven(url = "https://aws.oss.sonatype.org/content/repositories/snapshots")
4242
mavenCentral()
43-
maven(url = "https://plugins.gradle.org/m2/")
43+
gradlePluginPortal()
4444
}
4545
dependencies {
4646
"classpath"(group = "org.opensearch.gradle", name = "build-tools", version = "3.0.0-SNAPSHOT")
@@ -53,7 +53,8 @@ plugins {
5353
`maven-publish`
5454
id("com.github.jk1.dependency-license-report") version "2.9"
5555
id("org.owasp.dependencycheck") version "10.0.4"
56-
id("com.diffplug.spotless") version "6.25.0"
56+
57+
id("opensearch-java.spotless-conventions")
5758
}
5859
apply(plugin = "opensearch.repositories")
5960
apply(plugin = "org.owasp.dependencycheck")
@@ -299,31 +300,6 @@ tasks.withType<Jar> {
299300
}
300301
}
301302

302-
spotless {
303-
java {
304-
target("**/*.java")
305-
306-
licenseHeaderFile("../LICENSE_HEADER.txt")
307-
.named("PrimaryLicenseHeader")
308-
.onlyIfContentMatches("^((?!Licensed to Elasticsearch)[\\s\\S])*$")
309-
.delimiter("(package |//-----)")
310-
311-
licenseHeaderFile("../LICENSE_HEADER_FORKED.txt")
312-
.named("ForkedLicenseHeader")
313-
.onlyIfContentMatches("Licensed to Elasticsearch")
314-
.delimiter("(package |//-----)")
315-
316-
// Use the default importOrder configuration
317-
importOrder()
318-
removeUnusedImports()
319-
320-
eclipse().configFile("../buildSrc/formatterConfig.xml")
321-
322-
trimTrailingWhitespace()
323-
endWithNewline()
324-
}
325-
}
326-
327303
publishing {
328304
repositories{
329305
if (version.toString().endsWith("SNAPSHOT")) {

java-client/src/main/java/org/opensearch/client/opensearch/_types/mapping/IcuCollationKeywordProperty.java

+5-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,11 @@
1111
import jakarta.json.stream.JsonGenerator;
1212
import java.util.function.Function;
1313
import javax.annotation.Nullable;
14-
import org.opensearch.client.json.*;
14+
import org.opensearch.client.json.JsonpDeserializable;
15+
import org.opensearch.client.json.JsonpDeserializer;
16+
import org.opensearch.client.json.JsonpMapper;
17+
import org.opensearch.client.json.ObjectBuilderDeserializer;
18+
import org.opensearch.client.json.ObjectDeserializer;
1519
import org.opensearch.client.opensearch._types.analysis.IcuCollationAlternate;
1620
import org.opensearch.client.opensearch._types.analysis.IcuCollationCaseFirst;
1721
import org.opensearch.client.opensearch._types.analysis.IcuCollationDecomposition;

java-client/src/test/java/org/opensearch/client/opensearch/integTest/aws/AwsSdk2GetRequestIT.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313
import org.junit.Test;
1414
import org.opensearch.client.opensearch.OpenSearchAsyncClient;
1515
import org.opensearch.client.opensearch.OpenSearchClient;
16-
import org.opensearch.client.opensearch.core.*;
16+
import org.opensearch.client.opensearch.core.GetRequest;
17+
import org.opensearch.client.opensearch.core.GetResponse;
1718

1819
public class AwsSdk2GetRequestIT extends AwsSdk2TransportTestCase {
1920
@Test

java-codegen/build.gradle.kts

+5-17
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ buildscript {
2121
mavenLocal()
2222
maven(url = "https://aws.oss.sonatype.org/content/repositories/snapshots")
2323
mavenCentral()
24-
maven(url = "https://plugins.gradle.org/m2/")
24+
gradlePluginPortal()
2525
}
2626
dependencies {
2727
"classpath"(group = "org.opensearch.gradle", name = "build-tools", version = "3.0.0-SNAPSHOT")
@@ -32,8 +32,9 @@ plugins {
3232
application
3333
id("com.github.jk1.dependency-license-report") version "2.9"
3434
id("org.owasp.dependencycheck") version "10.0.2"
35-
id("com.diffplug.spotless") version "6.25.0"
3635
id("de.undercouch.download") version "5.6.0"
36+
37+
id("opensearch-java.spotless-conventions")
3738
}
3839
apply(plugin = "opensearch.repositories")
3940
apply(plugin = "org.owasp.dependencycheck")
@@ -259,19 +260,6 @@ tasks.withType<Jar> {
259260
}
260261
}
261262

262-
spotless {
263-
java {
264-
target("**/*.java")
265-
266-
licenseHeaderFile("../LICENSE_HEADER.txt")
267-
268-
// Use the default importOrder configuration
269-
importOrder()
270-
removeUnusedImports()
271-
272-
eclipse().configFile("../buildSrc/formatterConfig-generated.xml")
273-
274-
trimTrailingWhitespace()
275-
endWithNewline()
276-
}
263+
spotlessConventions {
264+
eclipseFormatterConfigFile = rootProject.file("buildSrc/formatterConfig-generated.xml")
277265
}

java-codegen/src/main/java/org/opensearch/client/codegen/CodeGenerator.java

+5-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,11 @@
88

99
package org.opensearch.client.codegen;
1010

11-
import static org.opensearch.client.codegen.model.OperationGroupMatcher.*;
11+
import static org.opensearch.client.codegen.model.OperationGroupMatcher.and;
12+
import static org.opensearch.client.codegen.model.OperationGroupMatcher.named;
13+
import static org.opensearch.client.codegen.model.OperationGroupMatcher.namespace;
14+
import static org.opensearch.client.codegen.model.OperationGroupMatcher.not;
15+
import static org.opensearch.client.codegen.model.OperationGroupMatcher.or;
1216

1317
import java.io.File;
1418
import java.io.IOException;

samples/build.gradle.kts

+2-17
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
plugins {
1010
java
1111
application
12-
id("com.diffplug.spotless") version "6.25.0"
12+
13+
id("opensearch-java.spotless-conventions")
1314
}
1415

1516
java {
@@ -26,22 +27,6 @@ dependencies {
2627
implementation("com.fasterxml.jackson.core", "jackson-databind", "2.15.2")
2728
}
2829

29-
spotless {
30-
java {
31-
32-
target("**/*.java")
33-
34-
// Use the default importOrder configuration
35-
importOrder()
36-
removeUnusedImports()
37-
38-
eclipse().configFile("../buildSrc/formatterConfig.xml")
39-
40-
trimTrailingWhitespace()
41-
endWithNewline()
42-
}
43-
}
44-
4530
application {
4631
mainClass.set("org.opensearch.client.samples.Main")
4732
}

samples/src/main/java/org/opensearch/client/samples/FlatObjectBasics.java

+8
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
/*
2+
* SPDX-License-Identifier: Apache-2.0
3+
*
4+
* The OpenSearch Contributors require contributions made to
5+
* this file be licensed under the Apache-2.0 license or a
6+
* compatible open source license.
7+
*/
8+
19
package org.opensearch.client.samples;
210

311
import java.util.List;

samples/src/main/java/org/opensearch/client/samples/IndexTemplates.java

+8-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,14 @@
1313
import org.apache.logging.log4j.Logger;
1414
import org.opensearch.client.opensearch._types.Time;
1515
import org.opensearch.client.opensearch.cluster.PutComponentTemplateRequest;
16-
import org.opensearch.client.opensearch.indices.*;
16+
import org.opensearch.client.opensearch.indices.CreateIndexRequest;
17+
import org.opensearch.client.opensearch.indices.DeleteIndexRequest;
18+
import org.opensearch.client.opensearch.indices.DeleteIndexTemplateRequest;
19+
import org.opensearch.client.opensearch.indices.GetIndicesSettingsRequest;
20+
import org.opensearch.client.opensearch.indices.GetIndicesSettingsResponse;
21+
import org.opensearch.client.opensearch.indices.GetMappingRequest;
22+
import org.opensearch.client.opensearch.indices.GetMappingResponse;
23+
import org.opensearch.client.opensearch.indices.PutIndexTemplateRequest;
1724

1825
/**
1926
* Run with: <c>./gradlew :samples:run -Dsamples.mainClass=IndexTemplates</c>

samples/src/main/java/org/opensearch/client/samples/util/IssueDocument.java

+8
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
/*
2+
* SPDX-License-Identifier: Apache-2.0
3+
*
4+
* The OpenSearch Contributors require contributions made to
5+
* this file be licensed under the Apache-2.0 license or a
6+
* compatible open source license.
7+
*/
8+
19
package org.opensearch.client.samples.util;
210

311
import java.util.List;

0 commit comments

Comments
 (0)