Skip to content

Commit 48d52e9

Browse files
committed
Forbid wildcard imports (opensearch-project#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]> (cherry picked from commit e856ecb)
1 parent 13fa4bf commit 48d52e9

File tree

12 files changed

+115
-78
lines changed

12 files changed

+115
-78
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
}
4646

@@ -50,7 +50,8 @@ plugins {
5050
`maven-publish`
5151
id("com.github.jk1.dependency-license-report") version "2.8"
5252
id("org.owasp.dependencycheck") version "10.0.2"
53-
id("com.diffplug.spotless") version "6.25.0"
53+
54+
id("opensearch-java.spotless-conventions")
5455
}
5556

5657
apply(plugin = "org.owasp.dependencycheck")
@@ -297,31 +298,6 @@ tasks.withType<Jar> {
297298
}
298299
}
299300

300-
spotless {
301-
java {
302-
target("**/*.java")
303-
304-
licenseHeaderFile("../LICENSE_HEADER.txt")
305-
.named("PrimaryLicenseHeader")
306-
.onlyIfContentMatches("^((?!Licensed to Elasticsearch)[\\s\\S])*$")
307-
.delimiter("(package |//-----)")
308-
309-
licenseHeaderFile("../LICENSE_HEADER_FORKED.txt")
310-
.named("ForkedLicenseHeader")
311-
.onlyIfContentMatches("Licensed to Elasticsearch")
312-
.delimiter("(package |//-----)")
313-
314-
// Use the default importOrder configuration
315-
importOrder()
316-
removeUnusedImports()
317-
318-
eclipse().configFile("../buildSrc/formatterConfig.xml")
319-
320-
trimTrailingWhitespace()
321-
endWithNewline()
322-
}
323-
}
324-
325301
publishing {
326302
repositories{
327303
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

+6-26
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,27 +260,6 @@ tasks.withType<Jar> {
259260
}
260261
}
261262

262-
spotless {
263-
java {
264-
target("**/*.java")
265-
266-
licenseHeaderFile("../LICENSE_HEADER.txt")
267-
.named("PrimaryLicenseHeader")
268-
.onlyIfContentMatches("^((?!Licensed to Elasticsearch)[\\s\\S])*$")
269-
.delimiter("(package |//-----)")
270-
271-
licenseHeaderFile("../LICENSE_HEADER_FORKED.txt")
272-
.named("ForkedLicenseHeader")
273-
.onlyIfContentMatches("Licensed to Elasticsearch")
274-
.delimiter("(package |//-----)")
275-
276-
// Use the default importOrder configuration
277-
importOrder()
278-
removeUnusedImports()
279-
280-
eclipse().configFile("../buildSrc/formatterConfig-generated.xml")
281-
282-
trimTrailingWhitespace()
283-
endWithNewline()
284-
}
285-
}
263+
spotlessConventions {
264+
eclipseFormatterConfigFile = rootProject.file("buildSrc/formatterConfig-generated.xml")
265+
}

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 {
@@ -28,22 +29,6 @@ dependencies {
2829
implementation("org.apache.httpcomponents.core5", "httpcore5", "5.2.2")
2930
}
3031

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

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)