Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
import static com.sun.source.tree.Tree.Kind.ANNOTATION;
import static com.sun.source.tree.Tree.Kind.EXTENDS_WILDCARD;
import static com.sun.source.tree.Tree.Kind.STRING_LITERAL;
import static com.sun.tools.javac.code.Flags.COMPACT_RECORD_CONSTRUCTOR;
import static com.sun.tools.javac.code.Flags.RECORD;

import com.google.common.base.Enums;
import com.google.common.base.MoreObjects;
Expand Down Expand Up @@ -75,6 +77,7 @@
import com.sun.source.tree.BindingPatternTree;
import com.sun.source.tree.BlockTree;
import com.sun.source.tree.BreakTree;
import com.sun.source.tree.CaseLabelTree;
import com.sun.source.tree.CaseTree;
import com.sun.source.tree.CatchTree;
import com.sun.source.tree.ClassTree;
Expand Down Expand Up @@ -400,16 +403,8 @@ public Void visitCompilationUnit(CompilationUnitTree node, Void unused) {
return null;
}

@Nullable
private static final Method COMPILATION_UNIT_TREE_GET_MODULE =
maybeGetMethod(CompilationUnitTree.class, "getModule");

protected void handleModule(boolean first, CompilationUnitTree node) {
if (COMPILATION_UNIT_TREE_GET_MODULE == null) {
// Java < 17, see https://bugs.openjdk.java.net/browse/JDK-8255464
return;
}
ModuleTree module = (ModuleTree) invoke(COMPILATION_UNIT_TREE_GET_MODULE, node);
ModuleTree module = node.getModule();
if (module != null) {
if (!first) {
builder.blankLineWanted(BlankLineWanted.YES);
Expand Down Expand Up @@ -1287,32 +1282,11 @@ public Void visitInstanceOf(InstanceOfTree node, Void unused) {
return null;
}

@Nullable
private static final Method BINDING_PATTERN_TREE_GET_VARIABLE =
maybeGetMethod(BindingPatternTree.class, "getVariable");

@Nullable
private static final Method BINDING_PATTERN_TREE_GET_TYPE = maybeGetMethod(BindingPatternTree.class, "getType");

@Nullable
private static final Method BINDING_PATTERN_TREE_GET_BINDING =
maybeGetMethod(BindingPatternTree.class, "getBinding");

@Override
public Void visitBindingPattern(BindingPatternTree node, Void unused) {
sync(node);
if (BINDING_PATTERN_TREE_GET_VARIABLE != null) {
VariableTree variableTree = (VariableTree) invoke(BINDING_PATTERN_TREE_GET_VARIABLE, node);
visitBindingPattern(variableTree.getModifiers(), variableTree.getType(), variableTree.getName());
} else if (BINDING_PATTERN_TREE_GET_TYPE != null && BINDING_PATTERN_TREE_GET_BINDING != null) {
Tree type = (Tree) invoke(BINDING_PATTERN_TREE_GET_TYPE, node);
Name name = (Name) invoke(BINDING_PATTERN_TREE_GET_BINDING, node);
visitBindingPattern(/* modifiers= */ null, type, name);
} else {
throw new RuntimeException(
"BindingPatternTree must have either getVariable() or both getType() and getBinding(),"
+ " but does not");
}
VariableTree variableTree = node.getVariable();
visitBindingPattern(variableTree.getModifiers(), variableTree.getType(), variableTree.getName());
return null;
}

Expand All @@ -1324,11 +1298,9 @@ private static boolean isVarType(Tree type) {
return type.getKind().name().equals("VAR_TYPE");
}

private void visitBindingPattern(@Nullable ModifiersTree modifiers, @Nullable Tree type, Name name) {
private void visitBindingPattern(ModifiersTree modifiers, @Nullable Tree type, Name name) {
builder.open(plusFour);
if (modifiers != null) {
builder.addAll(visitModifiers(modifiers, Direction.HORIZONTAL, Optional.empty()));
}
builder.addAll(visitModifiers(modifiers, Direction.HORIZONTAL, Optional.empty()));
if (type == null || isVarType(type)) {
token("var");
} else {
Expand Down Expand Up @@ -1533,11 +1505,6 @@ public Void visitAnnotatedType(AnnotatedTypeTree node, Void unused) {
return null;
}

// TODO(fawind): Use Flags.COMPACT_RECORD_CONSTRUCTOR once if/when we drop support for Java 11
protected static final long COMPACT_RECORD_CONSTRUCTOR = 1L << 51;

protected static final long RECORD = 1L << 61;

// TODO: use Flags.IMPLICIT_CLASS once this module compiles with JDK 22 or later (JDK 21 calls
// bit 19 UNNAMED_CLASS). javac sets it on the class it synthesizes for a compact source file.
private static final long IMPLICIT_CLASS = 1L << 19;
Expand Down Expand Up @@ -1990,24 +1957,14 @@ boolean visitSingleMemberAnnotation(AnnotationTree node) {
return true;
}

@Nullable
private static final Method CASE_TREE_GET_LABELS = maybeGetMethod(CaseTree.class, "getLabels");

@Override
public Void visitCase(CaseTree node, Void unused) {
sync(node);
markForPartialFormat();
builder.forcedBreak();
List<? extends Tree> labels;
boolean isDefault;
if (CASE_TREE_GET_LABELS != null) {
labels = (List<? extends Tree>) invoke(CASE_TREE_GET_LABELS, node);
isDefault = labels.size() == 1
&& Iterables.getOnlyElement(labels).getKind().name().equals("DEFAULT_CASE_LABEL");
} else {
labels = node.getExpressions();
isDefault = labels.isEmpty();
}
List<? extends CaseLabelTree> labels = node.getLabels();
boolean isDefault =
labels.size() == 1 && getOnlyElement(labels).getKind().name().equals("DEFAULT_CASE_LABEL");
if (isDefault) {
token("default", ZERO);
} else {
Expand Down Expand Up @@ -4069,17 +4026,9 @@ private void addBodyDeclarations(
}
}

@Nullable
private static final Method CLASS_TREE_GET_PERMITS_CLAUSE = maybeGetMethod(ClassTree.class, "getPermitsClause");

/** Gets the permits clause for the given node. This is only available in Java 15 and later. */
/** Gets the permits clause for the given node, empty unless the class or interface is sealed. */
protected List<? extends Tree> getPermitsClause(ClassTree node) {
if (CLASS_TREE_GET_PERMITS_CLAUSE != null) {
return (List<? extends Tree>) invoke(CLASS_TREE_GET_PERMITS_CLAUSE, node);
} else {
// Java < 15
return List.of();
}
return node.getPermitsClause();
}

private void classDeclarationTypeList(String token, List<? extends Tree> types) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@
import com.sun.tools.javac.util.Options;
import java.lang.reflect.Method;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.jspecify.annotations.Nullable;
Expand Down Expand Up @@ -102,39 +101,22 @@
return null;
}

// TODO(fwindheuser): remove this override when pattern matching in switch is no longer a preview
// feature, and TreePathScanner visits CaseTree#getLabels instead of CaseTree#getExpressions
// The TreeScanner of JDK 17 visits CaseTree#getExpressions, which leave out a pattern label and so the type
// it names; from JDK 21 it visits CaseTree#getLabels, and the labels are scanned twice, which changes nothing.
// TODO: remove this override once no release line runs on a JDK older than 21.
@Override
public Void visitCase(CaseTree tree, Void unused) {
if (CASE_TREE_GET_LABELS != null) {
try {
scan((List<? extends Tree>) CASE_TREE_GET_LABELS.invoke(tree), null);
} catch (ReflectiveOperationException e) {
throw new RuntimeException(e.getMessage(), e);
}
}
scan(tree.getLabels(), null);
return super.visitCase(tree, null);
}

@Nullable
private static final Method CASE_TREE_GET_LABELS = caseTreeGetLabels();

@Nullable
private static Method caseTreeGetLabels() {
try {
return CaseTree.class.getMethod("getLabels");
} catch (NoSuchMethodException e) {
return null;
}
}

@Override
public Void scan(@Nullable Tree tree, Void unused) {
if (tree == null) {
return null;
}
scanJavadoc();
return super.scan(tree, unused);

Check warning on line 119 in open-java-format/src/main/java/com/palantir/javaformat/java/RemoveUnusedImports.java

View workflow job for this annotation

GitHub Actions / build

[VoidUsed] Using a Void-typed variable is potentially confusing, and can be replaced with a literal `null`.
}

private void scanJavadoc() {
Expand Down Expand Up @@ -194,7 +176,7 @@
(int) basePos,
(int) basePos + node.getName().length())
: null);
return super.visitIdentifier(node, aVoid);

Check warning on line 179 in open-java-format/src/main/java/com/palantir/javaformat/java/RemoveUnusedImports.java

View workflow job for this annotation

GitHub Actions / build

[VoidUsed] Using a Void-typed variable is potentially confusing, and can be replaced with a literal `null`.
}
}
}
Expand Down Expand Up @@ -295,7 +277,7 @@
}

// ImportTree#isModule() (JEP 511) exists from JDK 23 on; this module compiles with a JDK 21
// compiler, so it can't be referenced directly. Same idiom as CASE_TREE_GET_LABELS above.
// compiler, so it can't be referenced directly.
@Nullable
private static final Method IMPORT_TREE_IS_MODULE =
JavaInputAstVisitor.maybeGetMethod(ImportTree.class, "isModule");
Expand Down
Loading