Skip to content

Commit

Permalink
Stop supporting JDK 14 in google-java-format, and update to remove us…
Browse files Browse the repository at this point in the history
…es of reflection for APIs that can be accessed directly in JDK 17

PiperOrigin-RevId: 484376678
  • Loading branch information
cushon authored and google-java-format Team committed Oct 27, 2022
1 parent 198cc39 commit c1ea933
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 73 deletions.
6 changes: 3 additions & 3 deletions core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@
<profile>
<id>jdk11</id>
<activation>
<jdk>(,14)</jdk>
<jdk>(,17)</jdk>
</activation>
<build>
<plugins>
Expand All @@ -236,14 +236,14 @@
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<excludes>
<exclude>**/Java14InputAstVisitor.java</exclude>
<exclude>**/Java17InputAstVisitor.java</exclude>
</excludes>
</configuration>
</plugin>
<plugin>
<artifactId>maven-javadoc-plugin</artifactId>
<configuration>
<excludePackageNames>com.google.googlejavaformat.java.java14</excludePackageNames>
<excludePackageNames>com.google.googlejavaformat.java.java17</excludePackageNames>
</configuration>
</plugin>
</plugins>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,10 +151,10 @@ public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOExcept
OpsBuilder builder = new OpsBuilder(javaInput, javaOutput);
// Output the compilation unit.
JavaInputAstVisitor visitor;
if (Runtime.version().feature() >= 14) {
if (Runtime.version().feature() >= 17) {
try {
visitor =
Class.forName("com.google.googlejavaformat.java.java14.Java14InputAstVisitor")
Class.forName("com.google.googlejavaformat.java.java17.Java17InputAstVisitor")
.asSubclass(JavaInputAstVisitor.class)
.getConstructor(OpsBuilder.class, int.class)
.newInstance(builder, options.indentationMultiplier());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
* the License.
*/

package com.google.googlejavaformat.java.java14;
package com.google.googlejavaformat.java.java17;

import static com.google.common.collect.ImmutableList.toImmutableList;
import static com.google.common.collect.Iterables.getOnlyElement;
Expand All @@ -25,6 +25,7 @@
import com.sun.source.tree.AnnotationTree;
import com.sun.source.tree.BindingPatternTree;
import com.sun.source.tree.BlockTree;
import com.sun.source.tree.CaseLabelTree;
import com.sun.source.tree.CaseTree;
import com.sun.source.tree.ClassTree;
import com.sun.source.tree.CompilationUnitTree;
Expand All @@ -39,39 +40,23 @@
import com.sun.tools.javac.tree.JCTree;
import com.sun.tools.javac.tree.JCTree.JCVariableDecl;
import com.sun.tools.javac.tree.TreeInfo;
import java.lang.reflect.Method;
import java.util.List;
import java.util.Optional;
import javax.lang.model.element.Name;

/**
* Extends {@link JavaInputAstVisitor} with support for AST nodes that were added or modified for
* Java 14.
* Extends {@link JavaInputAstVisitor} with support for AST nodes that were added or modified in
* Java 17.
*/
public class Java14InputAstVisitor extends JavaInputAstVisitor {
private static final Method COMPILATION_UNIT_TREE_GET_MODULE =
maybeGetMethod(CompilationUnitTree.class, "getModule");
private static final Method CLASS_TREE_GET_PERMITS_CLAUSE =
maybeGetMethod(ClassTree.class, "getPermitsClause");
private static final Method BINDING_PATTERN_TREE_GET_VARIABLE =
maybeGetMethod(BindingPatternTree.class, "getVariable");
private static final Method BINDING_PATTERN_TREE_GET_TYPE =
maybeGetMethod(BindingPatternTree.class, "getType");
private static final Method BINDING_PATTERN_TREE_GET_BINDING =
maybeGetMethod(BindingPatternTree.class, "getBinding");
private static final Method CASE_TREE_GET_LABELS = maybeGetMethod(CaseTree.class, "getLabels");
public class Java17InputAstVisitor extends JavaInputAstVisitor {

public Java14InputAstVisitor(OpsBuilder builder, int indentMultiplier) {
public Java17InputAstVisitor(OpsBuilder builder, int indentMultiplier) {
super(builder, indentMultiplier);
}

@Override
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 All @@ -84,30 +69,15 @@ protected void handleModule(boolean first, CompilationUnitTree node) {

@Override
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 super.getPermitsClause(node);
}
return node.getPermitsClause();
}

@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 LinkageError(
"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 Down Expand Up @@ -248,17 +218,9 @@ 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
&& 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", plusTwo);
} else {
Expand Down Expand Up @@ -305,20 +267,4 @@ public Void visitCase(CaseTree node, Void unused) {
}
return null;
}

private static Method maybeGetMethod(Class<?> c, String name) {
try {
return c.getMethod(name);
} catch (ReflectiveOperationException e) {
return null;
}
}

private static Object invoke(Method m, Object target) {
try {
return m.invoke(target);
} catch (ReflectiveOperationException e) {
throw new LinkageError(e.getMessage(), e);
}
}
}

0 comments on commit c1ea933

Please sign in to comment.