Skip to content
This repository was archived by the owner on Dec 5, 2025. It is now read-only.

Commit ef06a56

Browse files
Merge pull request #15 from Salling-Group/rebase-v1-15-0
Rebase v1 15 0
2 parents fe34175 + 2a94df0 commit ef06a56

40 files changed

Lines changed: 585 additions & 373 deletions

.github/dependabot.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
version: 2
2+
updates:
3+
- package-ecosystem: "maven"
4+
directory: "/"
5+
schedule:
6+
interval: "daily"

.github/workflows/ci.yml

Lines changed: 3 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -29,25 +29,17 @@ jobs:
2929
fail-fast: false
3030
matrix:
3131
os: [ ubuntu-latest ]
32-
java: [ 17, 14, 11 ]
32+
java: [ 19, 17, 11 ]
3333
experimental: [ false ]
3434
include:
35-
# Only test on macos and windows with a single recent JDK to avoid a
36-
# combinatorial explosion of test configurations.
37-
- os: macos-latest
38-
java: 17
39-
experimental: false
40-
- os: windows-latest
41-
java: 17
42-
experimental: false
4335
- os: ubuntu-latest
44-
java: 18-ea
36+
java: 20-ea
4537
experimental: true
4638
runs-on: ${{ matrix.os }}
4739
continue-on-error: ${{ matrix.experimental }}
4840
steps:
4941
- name: Cancel previous
50-
uses: styfle/cancel-workflow-action@0.8.0
42+
uses: styfle/cancel-workflow-action@0.9.1
5143
with:
5244
access_token: ${{ github.token }}
5345
- name: 'Check out repository'
@@ -64,26 +56,3 @@ jobs:
6456
- name: 'Test'
6557
shell: bash
6658
run: mvn test -B
67-
68-
publish_snapshot:
69-
name: 'Publish snapshot'
70-
needs: test
71-
if: github.event_name == 'push' && github.repository == 'google/google-java-format' && github.ref == 'refs/heads/master'
72-
runs-on: ubuntu-latest
73-
steps:
74-
- name: 'Check out repository'
75-
uses: actions/checkout@v2
76-
- name: 'Set up JDK 15'
77-
uses: actions/setup-java@v2
78-
with:
79-
java-version: 15
80-
distribution: 'zulu'
81-
cache: 'maven'
82-
server-id: sonatype-nexus-snapshots
83-
server-username: CI_DEPLOY_USERNAME
84-
server-password: CI_DEPLOY_PASSWORD
85-
- name: 'Publish'
86-
env:
87-
CI_DEPLOY_USERNAME: ${{ secrets.CI_DEPLOY_USERNAME }}
88-
CI_DEPLOY_PASSWORD: ${{ secrets.CI_DEPLOY_PASSWORD }}
89-
run: mvn source:jar deploy -B -DskipTests=true -Dinvoker.skip=true -Dmaven.javadoc.skip=true

.mvn/jvm.config

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
--add-exports=jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED
2+
--add-exports=jdk.compiler/com.sun.tools.javac.file=ALL-UNNAMED
3+
--add-exports=jdk.compiler/com.sun.tools.javac.main=ALL-UNNAMED
4+
--add-exports=jdk.compiler/com.sun.tools.javac.model=ALL-UNNAMED
5+
--add-exports=jdk.compiler/com.sun.tools.javac.parser=ALL-UNNAMED
6+
--add-exports=jdk.compiler/com.sun.tools.javac.processing=ALL-UNNAMED
7+
--add-exports=jdk.compiler/com.sun.tools.javac.tree=ALL-UNNAMED
8+
--add-exports=jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED
9+
--add-opens=jdk.compiler/com.sun.tools.javac.code=ALL-UNNAMED
10+
--add-opens=jdk.compiler/com.sun.tools.javac.comp=ALL-UNNAMED

core/pom.xml

Lines changed: 51 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -77,10 +77,15 @@
7777
<groupId>com.google.truth</groupId>
7878
<artifactId>truth</artifactId>
7979
</dependency>
80+
<dependency>
81+
<groupId>com.google.truth.extensions</groupId>
82+
<artifactId>truth-java8-extension</artifactId>
83+
<scope>test</scope>
84+
</dependency>
8085
<dependency>
8186
<groupId>com.google.testing.compile</groupId>
8287
<artifactId>compile-testing</artifactId>
83-
<version>0.15</version>
88+
<version>0.19</version>
8489
<scope>test</scope>
8590
</dependency>
8691
</dependencies>
@@ -121,7 +126,7 @@
121126
<plugin>
122127
<groupId>org.apache.maven.plugins</groupId>
123128
<artifactId>maven-shade-plugin</artifactId>
124-
<version>2.4.3</version>
129+
<version>3.2.4</version>
125130
<executions>
126131
<execution>
127132
<id>shade-all-deps</id>
@@ -197,7 +202,7 @@
197202
<plugin>
198203
<groupId>org.codehaus.mojo</groupId>
199204
<artifactId>build-helper-maven-plugin</artifactId>
200-
<version>3.0.0</version>
205+
<version>3.3.0</version>
201206
<executions>
202207
<execution>
203208
<id>add-source</id>
@@ -228,7 +233,7 @@
228233
<profile>
229234
<id>jdk11</id>
230235
<activation>
231-
<jdk>(,14)</jdk>
236+
<jdk>(,17)</jdk>
232237
</activation>
233238
<build>
234239
<plugins>
@@ -237,14 +242,54 @@
237242
<artifactId>maven-compiler-plugin</artifactId>
238243
<configuration>
239244
<excludes>
240-
<exclude>**/Java14InputAstVisitor.java</exclude>
245+
<exclude>**/Java17InputAstVisitor.java</exclude>
241246
</excludes>
242247
</configuration>
243248
</plugin>
244249
<plugin>
245250
<artifactId>maven-javadoc-plugin</artifactId>
246251
<configuration>
247-
<excludePackageNames>com.google.googlejavaformat.java.java14</excludePackageNames>
252+
<excludePackageNames>com.google.googlejavaformat.java.java17</excludePackageNames>
253+
</configuration>
254+
</plugin>
255+
</plugins>
256+
</build>
257+
</profile>
258+
<profile>
259+
<id>native</id>
260+
<build>
261+
<plugins>
262+
<plugin>
263+
<groupId>org.graalvm.buildtools</groupId>
264+
<artifactId>native-maven-plugin</artifactId>
265+
<version>0.9.13</version>
266+
<extensions>true</extensions>
267+
<executions>
268+
<execution>
269+
<id>build-native</id>
270+
<goals>
271+
<goal>build</goal>
272+
</goals>
273+
<phase>package</phase>
274+
</execution>
275+
<execution>
276+
<id>test-native</id>
277+
<goals>
278+
<goal>test</goal>
279+
</goals>
280+
<phase>test</phase>
281+
</execution>
282+
</executions>
283+
<configuration>
284+
<imageName>google-java-format</imageName>
285+
<classpath>
286+
<param>${project.build.directory}/${project.artifactId}-${project.version}-all-deps.jar</param>
287+
</classpath>
288+
<buildArgs>
289+
<buildArg>-H:IncludeResourceBundles=com.sun.tools.javac.resources.compiler</buildArg>
290+
<buildArg>--no-fallback</buildArg>
291+
<buildArg>--initialize-at-build-time=com.sun.tools.javac.file.Locations</buildArg>
292+
</buildArgs>
248293
</configuration>
249294
</plugin>
250295
</plugins>

core/src/main/java/com/google/googlejavaformat/CommentsHelper.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@
1414

1515
package com.google.googlejavaformat;
1616

17+
import com.google.googlejavaformat.Input.Tok;
18+
import java.util.Optional;
19+
import java.util.regex.Pattern;
20+
1721
/**
1822
* Rewrite comments. This interface is implemented by {@link
1923
* com.google.googlejavaformat.java.JavaCommentsHelper JavaCommentsHelper}.
@@ -28,4 +32,19 @@ public interface CommentsHelper {
2832
* @return the rewritten comment
2933
*/
3034
String rewrite(Input.Tok tok, int maxWidth, int column0);
35+
36+
static Optional<String> reformatParameterComment(Tok tok) {
37+
if (!tok.isSlashStarComment()) {
38+
return Optional.empty();
39+
}
40+
var match = PARAMETER_COMMENT.matcher(tok.getOriginalText());
41+
if (!match.matches()) {
42+
return Optional.empty();
43+
}
44+
return Optional.of(String.format("/* %s= */", match.group(1)));
45+
}
46+
47+
Pattern PARAMETER_COMMENT =
48+
Pattern.compile(
49+
"/\\*\\s*(\\p{javaJavaIdentifierStart}\\p{javaJavaIdentifierPart}*(\\Q...\\E)?)\\s*=\\s*\\*/");
3150
}

core/src/main/java/com/google/googlejavaformat/Doc.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
package com.google.googlejavaformat;
1616

1717
import static com.google.common.collect.Iterables.getLast;
18+
import static com.google.googlejavaformat.CommentsHelper.reformatParameterComment;
1819
import static java.lang.Math.max;
1920

2021
import com.google.common.base.MoreObjects;
@@ -727,7 +728,7 @@ float computeWidth() {
727728
// Account for line comments with missing spaces, see computeFlat.
728729
return tok.length() + 1;
729730
} else {
730-
return tok.length();
731+
return reformatParameterComment(tok).map(String::length).orElse(tok.length());
731732
}
732733
}
733734
return idx != -1 ? Float.POSITIVE_INFINITY : (float) tok.length();
@@ -741,7 +742,7 @@ String computeFlat() {
741742
if (tok.isSlashSlashComment() && !tok.getOriginalText().startsWith("// ")) {
742743
return "// " + tok.getOriginalText().substring("//".length());
743744
}
744-
return tok.getOriginalText();
745+
return reformatParameterComment(tok).orElse(tok.getOriginalText());
745746
}
746747

747748
@Override

core/src/main/java/com/google/googlejavaformat/java/Formatter.java

Lines changed: 5 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@
1414

1515
package com.google.googlejavaformat.java;
1616

17-
import static com.google.common.base.StandardSystemProperty.JAVA_CLASS_VERSION;
18-
import static com.google.common.base.StandardSystemProperty.JAVA_SPECIFICATION_VERSION;
1917
import static java.nio.charset.StandardCharsets.UTF_8;
2018

2119
import com.google.common.collect.ImmutableList;
@@ -42,7 +40,6 @@
4240
import com.sun.tools.javac.util.Options;
4341
import java.io.IOError;
4442
import java.io.IOException;
45-
import java.lang.reflect.Method;
4643
import java.net.URI;
4744
import java.util.ArrayList;
4845
import java.util.Collection;
@@ -139,9 +136,9 @@ public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOExcept
139136
JavacParser parser =
140137
parserFactory.newParser(
141138
javaInput.getText(),
142-
/*keepDocComments=*/ true,
143-
/*keepEndPos=*/ true,
144-
/*keepLineMap=*/ true);
139+
/* keepDocComments= */ true,
140+
/* keepEndPos= */ true,
141+
/* keepLineMap= */ true);
145142
unit = parser.parseCompilationUnit();
146143
unit.sourcefile = source;
147144

@@ -154,10 +151,10 @@ public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOExcept
154151
OpsBuilder builder = new OpsBuilder(javaInput, javaOutput);
155152
// Output the compilation unit.
156153
JavaInputAstVisitor visitor;
157-
if (getMajor() >= 14) {
154+
if (Runtime.version().feature() >= 17) {
158155
try {
159156
visitor =
160-
Class.forName("com.google.googlejavaformat.java.java14.Java14InputAstVisitor")
157+
Class.forName("com.google.googlejavaformat.java.java17.Java17InputAstVisitor")
161158
.asSubclass(JavaInputAstVisitor.class)
162159
.getConstructor(OpsBuilder.class, JavaFormatterOptions.class)
163160
.newInstance(builder, options);
@@ -177,23 +174,6 @@ public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOExcept
177174
javaOutput.flush();
178175
}
179176

180-
// Runtime.Version was added in JDK 9, so use reflection to access it to preserve source
181-
// compatibility with Java 8.
182-
private static int getMajor() {
183-
try {
184-
Method versionMethod = Runtime.class.getMethod("version");
185-
Object version = versionMethod.invoke(null);
186-
return (int) version.getClass().getMethod("major").invoke(version);
187-
} catch (Exception e) {
188-
// continue below
189-
}
190-
int version = (int) Double.parseDouble(JAVA_CLASS_VERSION.value());
191-
if (49 <= version && version <= 52) {
192-
return version - (49 - 5);
193-
}
194-
throw new IllegalStateException("Unknown Java version: " + JAVA_SPECIFICATION_VERSION.value());
195-
}
196-
197177
static boolean errorDiagnostic(Diagnostic<?> input) {
198178
if (input.getKind() != Diagnostic.Kind.ERROR) {
199179
return false;

core/src/main/java/com/google/googlejavaformat/java/JavaCommentsHelper.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,13 @@ public String rewrite(Tok tok, int maxWidth, int column0) {
5353
}
5454
if (tok.isSlashSlashComment()) {
5555
return indentLineComments(lines, column0);
56-
} else if (javadocShaped(lines)) {
57-
return indentJavadoc(lines, column0);
58-
} else {
59-
return preserveIndentation(lines, column0);
6056
}
57+
return CommentsHelper.reformatParameterComment(tok)
58+
.orElseGet(
59+
() ->
60+
javadocShaped(lines)
61+
? indentJavadoc(lines, column0)
62+
: preserveIndentation(lines, column0));
6163
}
6264

6365
// For non-javadoc-shaped block comments, shift the entire block to the correct

core/src/main/java/com/google/googlejavaformat/java/JavaInput.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
import javax.tools.Diagnostic;
5050
import javax.tools.DiagnosticCollector;
5151
import javax.tools.DiagnosticListener;
52+
import javax.tools.JavaFileManager;
5253
import javax.tools.JavaFileObject;
5354
import javax.tools.JavaFileObject.Kind;
5455
import javax.tools.SimpleJavaFileObject;
@@ -311,7 +312,7 @@ private static ImmutableMap<Integer, Integer> makePositionToColumnMap(List<Tok>
311312
for (Tok tok : toks) {
312313
builder.put(tok.getPosition(), tok.getColumn());
313314
}
314-
return builder.build();
315+
return builder.buildOrThrow();
315316
}
316317

317318
/**
@@ -349,7 +350,8 @@ static ImmutableList<Tok> buildToks(String text, ImmutableSet<TokenKind> stopTok
349350
stopTokens = ImmutableSet.<TokenKind>builder().addAll(stopTokens).add(TokenKind.EOF).build();
350351
Context context = new Context();
351352
Options.instance(context).put("--enable-preview", "true");
352-
new JavacFileManager(context, true, UTF_8);
353+
JavaFileManager fileManager = new JavacFileManager(context, false, UTF_8);
354+
context.put(JavaFileManager.class, fileManager);
353355
DiagnosticCollector<JavaFileObject> diagnosticCollector = new DiagnosticCollector<>();
354356
context.put(DiagnosticListener.class, diagnosticCollector);
355357
Log log = Log.instance(context);

0 commit comments

Comments
 (0)