Skip to content

Commit c7b3402

Browse files
authored
chore(codegen): switch formatter from prettier-java to eclipse (#1814)
* remove java prettier * apply eclipse formatting
1 parent d6962e6 commit c7b3402

File tree

201 files changed

+1927
-2238
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

201 files changed

+1927
-2238
lines changed

build.gradle.kts

Lines changed: 84 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,12 @@
11
/*
2-
* Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
3-
*
4-
* Licensed under the Apache License, Version 2.0 (the "License").
5-
* You may not use this file except in compliance with the License.
6-
* A copy of the License is located at
7-
*
8-
* http://aws.amazon.com/apache2.0
9-
*
10-
* or in the "license" file accompanying this file. This file is distributed
11-
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12-
* express or implied. See the License for the specific language governing
13-
* permissions and limitations under the License.
2+
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
* SPDX-License-Identifier: Apache-2.0
144
*/
15-
5+
import com.diffplug.spotless.FormatterFunc
166
import com.github.spotbugs.snom.Effort
177
import org.jreleaser.model.Active
8+
import java.io.Serializable
9+
import java.util.regex.Pattern
1810

1911
plugins {
2012
`java-library`
@@ -24,6 +16,7 @@ plugins {
2416
jacoco
2517
id("com.github.spotbugs") version "6.3.0"
2618
id("org.jreleaser") version "1.20.0"
19+
id("com.diffplug.spotless") version "8.1.0"
2720
}
2821

2922
allprojects {
@@ -34,7 +27,6 @@ allprojects {
3427
// The root project doesn't produce a JAR.
3528
tasks["jar"].enabled = false
3629

37-
3830
repositories {
3931
mavenLocal()
4032
mavenCentral()
@@ -75,10 +67,11 @@ subprojects {
7567
}
7668

7769
// Reusable license copySpec
78-
val licenseSpec = copySpec {
79-
from("${project.rootDir}/LICENSE")
80-
from("${project.rootDir}/NOTICE")
81-
}
70+
val licenseSpec =
71+
copySpec {
72+
from("${project.rootDir}/LICENSE")
73+
from("${project.rootDir}/NOTICE")
74+
}
8275

8376
// Set up tasks that build source and javadoc jars.
8477
tasks.register<Jar>("sourcesJar") {
@@ -127,7 +120,12 @@ subprojects {
127120
repositories {
128121
maven {
129122
name = "stagingRepository"
130-
url = rootProject.layout.buildDirectory.dir("staging").get().asFile.toURI()
123+
url =
124+
rootProject.layout.buildDirectory
125+
.dir("staging")
126+
.get()
127+
.asFile
128+
.toURI()
131129
}
132130
}
133131

@@ -194,7 +192,9 @@ subprojects {
194192
*
195193
* Configure the running of tests.
196194
*/
195+
197196
// Log on passed, skipped, and failed test events if the `-Plog-tests` property is set.
197+
198198
if (project.hasProperty("log-tests")) {
199199
tasks.test {
200200
testLogging {
@@ -226,7 +226,10 @@ subprojects {
226226
xml.required.set(false)
227227
csv.required.set(false)
228228
html.outputLocation.set(
229-
layout.buildDirectory.dir("reports/jacoco").get().asFile
229+
layout.buildDirectory
230+
.dir("reports/jacoco")
231+
.get()
232+
.asFile,
230233
)
231234
}
232235
}
@@ -248,6 +251,62 @@ subprojects {
248251
excludeFilter.set(excludeFile)
249252
}
250253
}
254+
255+
apply(plugin = "com.diffplug.spotless")
256+
257+
spotless {
258+
java {
259+
// Enforce a common license header on all files
260+
licenseHeaderFile("${project.rootDir}/config/spotless/license-header.txt")
261+
.onlyIfContentMatches("^((?!SKIPLICENSECHECK)[\\s\\S])*\$")
262+
leadingTabsToSpaces()
263+
endWithNewline()
264+
eclipse().configFile("${project.rootDir}/config/spotless/formatting.xml")
265+
266+
// Fixes for some strange formatting applied by eclipse:
267+
// see: https://github.com/kamkie/demo-spring-jsf/blob/bcacb9dc90273a5f8d2569470c5bf67b171c7d62/build.gradle.kts#L159
268+
// These have to be implemented with anonymous classes this way instead of lambdas because of:
269+
// https://github.com/diffplug/spotless/issues/2387
270+
custom(
271+
"Lambda fix",
272+
object : Serializable, FormatterFunc {
273+
override fun apply(input: String): String = input.replace("} )", "})").replace("} ,", "},")
274+
},
275+
)
276+
custom(
277+
"Long literal fix",
278+
object : Serializable, FormatterFunc {
279+
override fun apply(input: String): String = Pattern.compile("([0-9_]+) [Ll]").matcher(input).replaceAll("\$1L")
280+
},
281+
)
282+
283+
// Static first, then everything else alphabetically
284+
removeUnusedImports()
285+
importOrder("\\#", "")
286+
// Ignore generated code for formatter check
287+
targetExclude("*/build/**/*.*")
288+
}
289+
290+
// Formatting for build.gradle.kts files
291+
kotlinGradle {
292+
ktlint()
293+
leadingTabsToSpaces()
294+
trimTrailingWhitespace()
295+
endWithNewline()
296+
licenseHeaderFile(
297+
"${project.rootDir}/config/spotless/license-header.txt",
298+
"import|tasks|apply|plugins|rootProject",
299+
)
300+
}
301+
tasks {
302+
// If the property "noFormat" is set, don't auto-format source file (like in CI)
303+
if (!project.hasProperty("noFormat")) {
304+
build {
305+
dependsOn(spotlessApply)
306+
}
307+
}
308+
}
309+
}
251310
}
252311
}
253312

@@ -290,10 +349,13 @@ jreleaser {
290349
active = Active.ALWAYS
291350
url = "https://central.sonatype.com/api/v1/publisher"
292351
stagingRepositories.add(
293-
rootProject.layout.buildDirectory.dir("staging").get().asFile.absolutePath
352+
rootProject.layout.buildDirectory
353+
.dir("staging")
354+
.get()
355+
.asFile.absolutePath,
294356
)
295357
}
296358
}
297359
}
298360
}
299-
}
361+
}

config/checkstyle/checkstyle.xml

Lines changed: 24 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,11 @@
55
-->
66

77
<!DOCTYPE module PUBLIC
8-
"-//Checkstyle//DTD Checkstyle Configuration 1.3//EN"
9-
"https://checkstyle.org/dtds/configuration_1_3.dtd">
8+
"-//Checkstyle//DTD Checkstyle Configuration 1.3//EN"
9+
"https://checkstyle.org/dtds/configuration_1_3.dtd">
1010

11-
<module name = "Checker">
12-
<property name="charset" value="UTF-8"/>
13-
14-
<property name="severity" value="error"/>
15-
16-
<property name="fileExtensions" value="java, properties, xml"/>
17-
<module name="BeforeExecutionExclusionFileFilter">
18-
<property name="fileNamePattern" value="module\-info\.java$"/>
19-
</module>
11+
<module name="Checker">
12+
<module name="SuppressWarningsFilter"/>
2013

2114
<module name="SuppressionFilter">
2215
<property name="file" value="${config_loc}/suppressions.xml"/>
@@ -33,94 +26,14 @@
3326
<property name="fileExtensions" value="java"/>
3427
</module>
3528

36-
<module name="NewlineAtEndOfFile">
37-
<property name="lineSeparator" value="lf_cr_crlf"/>
38-
</module>
39-
4029
<!-- Checks for whitespace -->
4130
<!-- See http://checkstyle.sf.net/config_whitespace.html -->
4231
<module name="FileTabCharacter"/>
43-
<module name="RegexpSingleline">
44-
<property name="format" value="\s+$"/>
45-
<property name="message" value="Line has trailing spaces."/>
46-
</module>
4732

4833
<module name="TreeWalker">
49-
<module name="OuterTypeFilename"/>
50-
<module name="NoLineWrap">
51-
<property name="tokens" value="PACKAGE_DEF, IMPORT, STATIC_IMPORT"/>
52-
</module>
53-
<module name="LeftCurly" />
54-
<module name="RightCurly" />
55-
<module name="WhitespaceAfter"/>
56-
<module name="WhitespaceAround">
57-
<property name="allowEmptyConstructors" value="true"/>
58-
<property name="allowEmptyLambdas" value="true"/>
59-
<property name="allowEmptyMethods" value="true"/>
60-
<property name="allowEmptyTypes" value="true"/>
61-
<property name="allowEmptyLoops" value="true"/>
62-
<property name="allowEmptyCatches" value="true"/>
63-
<property name="ignoreEnhancedForColon" value="false"/>
64-
<property name="tokens"
65-
value="ASSIGN, BAND, BAND_ASSIGN, BOR, BOR_ASSIGN, BSR, BSR_ASSIGN, BXOR,
66-
BXOR_ASSIGN, COLON, DIV, DIV_ASSIGN, DO_WHILE, EQUAL, GE, GT, LAMBDA, LAND,
67-
LCURLY, LE, LITERAL_CATCH, LITERAL_DO, LITERAL_ELSE, LITERAL_FINALLY,
68-
LITERAL_FOR, LITERAL_IF, LITERAL_RETURN, LITERAL_SWITCH, LITERAL_SYNCHRONIZED,
69-
LITERAL_TRY, LITERAL_WHILE, LOR, LT, MINUS, MINUS_ASSIGN, MOD, MOD_ASSIGN,
70-
NOT_EQUAL, PLUS, PLUS_ASSIGN, QUESTION, RCURLY, SL, SLIST, SL_ASSIGN, SR,
71-
SR_ASSIGN, STAR, STAR_ASSIGN, LITERAL_ASSERT, TYPE_EXTENSION_AND"/>
72-
<message key="ws.notFollowed"
73-
value="WhitespaceAround: ''{0}'' is not followed by whitespace. Empty blocks may only be represented as '{}' when not part of a multi-block statement (4.1.3)"/>
74-
<message key="ws.notPreceded"
75-
value="WhitespaceAround: ''{0}'' is not preceded with whitespace."/>
76-
</module>
77-
<module name="GenericWhitespace">
78-
<message key="ws.followed"
79-
value="GenericWhitespace ''{0}'' is followed by whitespace."/>
80-
<message key="ws.preceded"
81-
value="GenericWhitespace ''{0}'' is preceded with whitespace."/>
82-
<message key="ws.illegalFollow"
83-
value="GenericWhitespace ''{0}'' should followed by whitespace."/>
84-
<message key="ws.notPreceded"
85-
value="GenericWhitespace ''{0}'' is not preceded with whitespace."/>
86-
</module>
87-
<module name="OneStatementPerLine"/>
88-
<module name="ModifierOrder"/>
89-
<module name="EmptyLineSeparator">
90-
<property name="tokens"
91-
value="PACKAGE_DEF, IMPORT, STATIC_IMPORT, CLASS_DEF, INTERFACE_DEF, ENUM_DEF,
92-
STATIC_INIT, INSTANCE_INIT, CTOR_DEF, VARIABLE_DEF, RECORD_DEF,
93-
COMPACT_CTOR_DEF"/>
94-
<property name="allowNoEmptyLineBetweenFields" value="true"/>
95-
</module>
96-
<module name="SeparatorWrap">
97-
<property name="id" value="SeparatorWrapDot"/>
98-
<property name="tokens" value="DOT"/>
99-
<property name="option" value="nl"/>
100-
</module>
101-
<module name="SeparatorWrap">
102-
<property name="id" value="SeparatorWrapComma"/>
103-
<property name="tokens" value="COMMA"/>
104-
<property name="option" value="EOL"/>
105-
</module>
106-
107-
<module name="MethodParamPad">
108-
<property name="tokens"
109-
value="CTOR_DEF, LITERAL_NEW, METHOD_CALL, METHOD_DEF,
110-
SUPER_CTOR_CALL, ENUM_CONSTANT_DEF, RECORD_DEF"/>
111-
</module>
112-
<module name="NoWhitespaceBefore">
113-
<property name="tokens"
114-
value="COMMA, SEMI, POST_INC, POST_DEC, DOT,
115-
LABELED_STAT, METHOD_REF"/>
116-
<property name="allowLineBreaks" value="true"/>
117-
</module>
118-
<module name="ParenPad"/>
119-
<module name="AnnotationLocation">
120-
<property name="allowSamelineMultipleAnnotations" value="false"/>
121-
<property name="allowSamelineSingleParameterlessAnnotation" value="false"/>
122-
<property name="allowSamelineParameterizedAnnotation" value="false"/>
123-
</module>
34+
<!-- Make comments and annotations available for the suppression comment filter. -->
35+
<module name="SuppressWarningsHolder"/>
36+
<module name="SuppressionCommentFilter"/>
12437

12538
<!-- Checks for imports -->
12639
<!-- See http://checkstyle.sf.net/config_import.html -->
@@ -131,13 +44,27 @@
13144
<module name="UnusedImports">
13245
<property name="processJavadoc" value="true"/>
13346
</module>
134-
13547
<module name="CustomImportOrder">
13648
<property name="sortImportsInGroupAlphabetically" value="true"/>
13749
<property name="separateLineBetweenGroups" value="true"/>
13850
<property name="customImportOrderRules" value="STATIC###THIRD_PARTY_PACKAGE"/>
139-
<property name="tokens" value="IMPORT, STATIC_IMPORT, PACKAGE_DEF"/>
14051
</module>
52+
<module name="AvoidStarImport"/>
53+
54+
<!-- Checks for Javadoc comments. -->
55+
<!-- See http://checkstyle.sf.net/config_javadoc.html -->
56+
<module name="JavadocMethod">
57+
<property name="allowMissingParamTags" value="true"/>
58+
<property name="allowMissingReturnTag" value="true"/>
59+
<property name="allowedAnnotations" value="Override, Test"/>
60+
</module>
61+
<module name="JavadocStyle"/>
62+
<module name="NonEmptyAtclauseDescription"/>
63+
<module name="AtclauseOrder">
64+
<property name="tagOrder" value="@param, @return, @throws, @deprecated"/>
65+
<property name="target" value="CLASS_DEF, INTERFACE_DEF, ENUM_DEF, METHOD_DEF, CTOR_DEF, VARIABLE_DEF"/>
66+
</module>
67+
<module name="MissingOverride"/>
14168

14269
<!-- Checks for Naming Conventions. -->
14370
<!-- See http://checkstyle.sf.net/config_naming.html -->
@@ -198,4 +125,4 @@
198125
<property name="allowNonPrintableEscapes" value="true"/>
199126
</module>
200127
</module>
201-
</module>
128+
</module>

0 commit comments

Comments
 (0)