Skip to content

Commit 85f9745

Browse files
eamonnmcmanusgoogle-java-format Team
authored andcommitted
Reduce member visibility where possible.
Remove some unused code discovered during this exercise. Implement a TODO about using the javac `Flags` API directly. PiperOrigin-RevId: 891022366
1 parent 7b0d3a3 commit 85f9745

22 files changed

+166
-390
lines changed

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

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -74,19 +74,7 @@ public enum FillMode {
7474
public static final int MAX_LINE_WIDTH = 1000;
7575

7676
/** State for writing. */
77-
public static final class State {
78-
final int lastIndent;
79-
final int indent;
80-
final int column;
81-
final boolean mustBreak;
82-
83-
State(int lastIndent, int indent, int column, boolean mustBreak) {
84-
this.lastIndent = lastIndent;
85-
this.indent = indent;
86-
this.column = column;
87-
this.mustBreak = mustBreak;
88-
}
89-
77+
public record State(int lastIndent, int indent, int column, boolean mustBreak) {
9078
public State(int indent0, int column0) {
9179
this(indent0, indent0, column0, false);
9280
}
@@ -98,16 +86,6 @@ State withColumn(int column) {
9886
State withMustBreak(boolean mustBreak) {
9987
return new State(lastIndent, indent, column, mustBreak);
10088
}
101-
102-
@Override
103-
public String toString() {
104-
return MoreObjects.toStringHelper(this)
105-
.add("lastIndent", lastIndent)
106-
.add("indent", indent)
107-
.add("column", column)
108-
.add("mustBreak", mustBreak)
109-
.toString();
110-
}
11189
}
11290

11391
private static final Range<Integer> EMPTY_RANGE = Range.closedOpen(-1, -1);

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

Lines changed: 18 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -17,60 +17,36 @@
1717
import static com.google.common.base.Preconditions.checkArgument;
1818
import static com.google.common.base.Preconditions.checkNotNull;
1919

20-
/** An error that prevented formatting from succeeding. */
21-
public class FormatterDiagnostic {
22-
private final int lineNumber;
23-
private final String message;
24-
private final int column;
25-
26-
public static FormatterDiagnostic create(String message) {
27-
return new FormatterDiagnostic(-1, -1, message);
28-
}
29-
30-
public static FormatterDiagnostic create(int lineNumber, int column, String message) {
31-
checkArgument(lineNumber >= 0);
32-
checkArgument(column >= 0);
20+
/**
21+
* An error that prevented formatting from succeeding.
22+
*
23+
* @param line the line number on which the error occurred, or {@code -1} if the error does not have
24+
* a line number.
25+
* @param column the 1-indexed column number on which the error occurred, or {@code -1} if the error
26+
* does not have a column.
27+
* @param message a description of the problem that prevented formatting from succeeding.
28+
*/
29+
public record FormatterDiagnostic(int line, int column, String message) {
30+
public FormatterDiagnostic {
31+
checkArgument(line >= -1);
32+
checkArgument(column >= -1);
3333
checkNotNull(message);
34-
return new FormatterDiagnostic(lineNumber, column, message);
35-
}
36-
37-
private FormatterDiagnostic(int lineNumber, int column, String message) {
38-
this.lineNumber = lineNumber;
39-
this.column = column;
40-
this.message = message;
41-
}
42-
43-
/**
44-
* Returns the line number on which the error occurred, or {@code -1} if the error does not have a
45-
* line number.
46-
*/
47-
public int line() {
48-
return lineNumber;
49-
}
50-
51-
/**
52-
* Returns the 1-indexed column number on which the error occurred, or {@code -1} if the error
53-
* does not have a column.
54-
*/
55-
public int column() {
56-
return column;
5734
}
5835

59-
/** Returns a description of the problem that prevented formatting from succeeding. */
60-
public String message() {
61-
return message;
36+
public FormatterDiagnostic(String message) {
37+
this(-1, -1, message);
6238
}
6339

6440
@Override
6541
public String toString() {
6642
StringBuilder sb = new StringBuilder();
67-
if (lineNumber >= 0) {
68-
sb.append(lineNumber).append(':');
43+
if (line >= 0) {
44+
sb.append(line).append(':');
6945
}
7046
if (column >= 0) {
7147
sb.append(column).append(':');
7248
}
73-
if (lineNumber >= 0 || column >= 0) {
49+
if (line >= 0 || column >= 0) {
7450
sb.append(' ');
7551
}
7652
sb.append("error: ").append(message);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ public String toString() {
141141
* numbers.
142142
*/
143143
public FormatterDiagnostic createDiagnostic(int inputPosition, String message) {
144-
return FormatterDiagnostic.create(
144+
return new FormatterDiagnostic(
145145
getLineNumber(inputPosition), getColumnNumber(inputPosition), message);
146146
}
147147
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
import java.util.List;
2323
import java.util.Map;
2424

25-
/** This interface defines methods common to an {@link Input} or an {@link Output}. */
25+
/** This class defines methods common to an {@link Input} or an {@link Output}. */
2626
public abstract class InputOutput {
2727
private ImmutableList<String> lines = ImmutableList.of();
2828

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ static Result create(
5656
private final CommandLineOptions parameters;
5757
private final JavaFormatterOptions options;
5858

59-
public FormatFileCallable(
59+
FormatFileCallable(
6060
CommandLineOptions parameters, Path path, String input, JavaFormatterOptions options) {
6161
this.path = path;
6262
this.input = input;

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public final class FormatterException extends Exception {
3232
private final ImmutableList<FormatterDiagnostic> diagnostics;
3333

3434
public FormatterException(String message) {
35-
this(FormatterDiagnostic.create(message));
35+
this(new FormatterDiagnostic(message));
3636
}
3737

3838
public FormatterException(FormatterDiagnostic diagnostic) {
@@ -57,7 +57,7 @@ public static FormatterException fromJavacDiagnostics(
5757
}
5858

5959
private static FormatterDiagnostic toFormatterDiagnostic(Diagnostic<?> input) {
60-
return FormatterDiagnostic.create(
60+
return new FormatterDiagnostic(
6161
(int) input.getLineNumber(), (int) input.getColumnNumber(), input.getMessage(ENGLISH));
6262
}
6363

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

Lines changed: 19 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -63,13 +63,12 @@ public static String reorderImports(String text) throws FormatterException {
6363
}
6464

6565
private String reorderImports() throws FormatterException {
66-
int firstImportStart;
6766
Optional<Integer> maybeFirstImport = findIdentifier(0, IMPORT_OR_CLASS_START);
6867
if (!maybeFirstImport.isPresent() || !tokenAt(maybeFirstImport.get()).equals("import")) {
6968
// No imports, so nothing to do.
7069
return text;
7170
}
72-
firstImportStart = maybeFirstImport.get();
71+
int firstImportStart = maybeFirstImport.get();
7372
int unindentedFirstImportStart = unindent(firstImportStart);
7473

7574
ImportsAndIndex imports = scanImports(firstImportStart);
@@ -189,37 +188,28 @@ private ImportOrderer(String text, ImmutableList<Tok> toks, Style style) {
189188
}
190189
}
191190

192-
enum ImportType {
191+
private enum ImportType {
193192
STATIC,
194193
MODULE,
195194
NORMAL
196195
}
197196

198-
/** An import statement. */
199-
class Import {
200-
private final String imported;
201-
private final String trailing;
202-
private final ImportType importType;
203-
204-
Import(String imported, String trailing, ImportType importType) {
205-
this.imported = imported;
206-
this.trailing = trailing;
207-
this.importType = importType;
208-
}
209-
210-
/** The name being imported, for example {@code java.util.List}. */
211-
String imported() {
212-
return imported;
213-
}
214-
215-
/** Returns the {@link ImportType}. */
216-
ImportType importType() {
217-
return importType;
218-
}
219-
197+
/**
198+
* An import statement.
199+
*
200+
* @param imported the name being imported, for example {@code java.util.List}.
201+
* @param trailing the {@code //} comment lines after the final {@code ;}, up to and including the
202+
* line terminator of the last one. Note: In case two imports were separated by a space (which
203+
* is disallowed by the style guide), the trailing whitespace of the first import does not
204+
* include a line terminator.
205+
* @param importType the {@link ImportType} of the import.
206+
* @param lineSeparator the line separator to use when formatting the import.
207+
*/
208+
private record Import(
209+
String imported, String trailing, ImportType importType, String lineSeparator) {
220210
/** The top-level package of the import. */
221211
String topLevel() {
222-
return DOT_SPLITTER.split(imported()).iterator().next();
212+
return DOT_SPLITTER.split(imported).iterator().next();
223213
}
224214

225215
/** True if this is an Android import per AOSP style. */
@@ -236,18 +226,8 @@ boolean isJava() {
236226
};
237227
}
238228

239-
/**
240-
* The {@code //} comment lines after the final {@code ;}, up to and including the line
241-
* terminator of the last one. Note: In case two imports were separated by a space (which is
242-
* disallowed by the style guide), the trailing whitespace of the first import does not include
243-
* a line terminator.
244-
*/
245-
String trailing() {
246-
return trailing;
247-
}
248-
249229
/** True if this is a third-party import per AOSP style. */
250-
public boolean isThirdParty() {
230+
boolean isThirdParty() {
251231
return !(isAndroid() || isJava());
252232
}
253233

@@ -280,15 +260,7 @@ private String tokString(int start, int end) {
280260
return sb.toString();
281261
}
282262

283-
private static class ImportsAndIndex {
284-
final ImmutableSortedSet<Import> imports;
285-
final int index;
286-
287-
ImportsAndIndex(ImmutableSortedSet<Import> imports, int index) {
288-
this.imports = imports;
289-
this.index = index;
290-
}
291-
}
263+
private record ImportsAndIndex(ImmutableSortedSet<Import> imports, int index) {}
292264

293265
/**
294266
* Scans a sequence of import lines. The parsing uses this approximate grammar:
@@ -366,7 +338,7 @@ private ImportsAndIndex scanImports(int i) throws FormatterException {
366338
// Extra semicolons are not allowed by the JLS but are accepted by javac.
367339
i++;
368340
}
369-
imports.add(new Import(importedName, trailing.toString(), importType));
341+
imports.add(new Import(importedName, trailing.toString(), importType, lineSeparator));
370342
// Remember the position just after the import we just saw, before skipping blank lines.
371343
// If the next thing after the blank lines is not another import then we don't want to
372344
// include those blank lines in the text to be replaced.

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,12 @@
2727
import java.util.regex.Pattern;
2828

2929
/** {@code JavaCommentsHelper} extends {@link CommentsHelper} to rewrite Java comments. */
30-
public final class JavaCommentsHelper implements CommentsHelper {
30+
final class JavaCommentsHelper implements CommentsHelper {
3131

3232
private final String lineSeparator;
3333
private final JavaFormatterOptions options;
3434

35-
public JavaCommentsHelper(String lineSeparator, JavaFormatterOptions options) {
35+
JavaCommentsHelper(String lineSeparator, JavaFormatterOptions options) {
3636
this.lineSeparator = lineSeparator;
3737
this.options = options;
3838
}

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@
5959
import org.jspecify.annotations.Nullable;
6060

6161
/** {@code JavaInput} extends {@link Input} to represent a Java input document. */
62-
public final class JavaInput extends Input {
62+
final class JavaInput extends Input {
6363
/**
6464
* A {@code JavaInput} is a sequence of {@link Tok}s that cover the Java input. A {@link Tok} is
6565
* either a token (if {@code isToken()}), or a non-token, which is a comment (if {@code
@@ -275,7 +275,7 @@ public String toString() {
275275
* @param text the input text
276276
* @throws FormatterException if the input cannot be parsed
277277
*/
278-
public JavaInput(String text) throws FormatterException {
278+
JavaInput(String text) throws FormatterException {
279279
this.text = checkNotNull(text);
280280
setLines(ImmutableList.copyOf(Newlines.lineIterator(text)));
281281
ImmutableList<Tok> toks = buildToks(text);
@@ -608,7 +608,7 @@ private static boolean isParamComment(Tok tok) {
608608
* @return the {@code 0}-based {@link Range} of tokens
609609
* @throws FormatterException if the upper endpoint of the range is outside the file
610610
*/
611-
Range<Integer> characterRangeToTokenRange(Range<Integer> characterRange)
611+
private Range<Integer> characterRangeToTokenRange(Range<Integer> characterRange)
612612
throws FormatterException {
613613
if (characterRange.upperEndpoint() > text.length()) {
614614
throw new FormatterException(
@@ -699,11 +699,11 @@ public int getColumnNumber(int inputPosition) {
699699

700700
// TODO(cushon): refactor JavaInput so the CompilationUnit can be passed into
701701
// the constructor.
702-
public void setCompilationUnit(JCCompilationUnit unit) {
702+
void setCompilationUnit(JCCompilationUnit unit) {
703703
this.unit = unit;
704704
}
705705

706-
public RangeSet<Integer> characterRangesToTokenRanges(Collection<Range<Integer>> characterRanges)
706+
RangeSet<Integer> characterRangesToTokenRanges(Collection<Range<Integer>> characterRanges)
707707
throws FormatterException {
708708
RangeSet<Integer> tokenRangeSet = TreeRangeSet.create();
709709
for (Range<Integer> characterRange : characterRanges) {

0 commit comments

Comments
 (0)