Skip to content

Commit

Permalink
PUBLIC: Look up some Method variables just once. `Class.getMethod()…
Browse files Browse the repository at this point in the history
…` can be quite slow so it's best to avoid doing it repeatedly.

PiperOrigin-RevId: 387621256
  • Loading branch information
eamonnmcmanus authored and google-java-format Team committed Jul 29, 2021
1 parent dfb7a23 commit 753930c
Showing 1 changed file with 51 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
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;
Expand All @@ -48,34 +49,43 @@
* Java 14.
*/
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");

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

@Override
protected void handleModule(boolean first, CompilationUnitTree node) {
try {
ModuleTree module =
(ModuleTree) CompilationUnitTree.class.getMethod("getModule").invoke(node);
if (module != null) {
if (!first) {
builder.blankLineWanted(BlankLineWanted.YES);
}
markForPartialFormat();
visitModule(module, null);
builder.forcedBreak();
}
} catch (ReflectiveOperationException e) {
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);
if (module != null) {
if (!first) {
builder.blankLineWanted(BlankLineWanted.YES);
}
markForPartialFormat();
visitModule(module, null);
builder.forcedBreak();
}
}

@Override
protected List<? extends Tree> getPermitsClause(ClassTree node) {
try {
return (List<? extends Tree>) ClassTree.class.getMethod("getPermitsClause").invoke(node);
} catch (ReflectiveOperationException e) {
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);
}
Expand All @@ -84,20 +94,18 @@ protected List<? extends Tree> getPermitsClause(ClassTree node) {
@Override
public Void visitBindingPattern(BindingPatternTree node, Void unused) {
sync(node);
try {
VariableTree variableTree =
(VariableTree) BindingPatternTree.class.getMethod("getVariable").invoke(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());
} catch (ReflectiveOperationException e1) {
try {
Tree type = (Tree) BindingPatternTree.class.getMethod("getType").invoke(node);
Name name = (Name) BindingPatternTree.class.getMethod("getBinding").invoke(node);
visitBindingPattern(/* modifiers= */ null, type, name);
} catch (ReflectiveOperationException e2) {
e2.addSuppressed(e1);
throw new LinkageError(e2.getMessage(), e2);
}
} 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");
}
return null;
}
Expand Down Expand Up @@ -288,4 +296,20 @@ 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 753930c

Please sign in to comment.