Skip to content

Commit

Permalink
Abstract out fields to avoid re-computation
Browse files Browse the repository at this point in the history
  • Loading branch information
mernst authored Oct 26, 2020
1 parent 32f5d3b commit eac5a54
Showing 1 changed file with 55 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,29 @@ public class CFGTranslationPhaseOne extends TreePathScanner<Node, Void> {
*/
final List<LambdaExpressionTree> declaredLambdas;

/** The ArithmeticException type. */
final TypeMirror arithmeticExceptionType;
/** The AssertionError type. */
final TypeMirror assertionErrorType;

/** The ArrayIndexOutOfBoundsException type */
final TypeMirror arrayIndexOutOfBoundsExceptionType;

/** The ClassCastException type . */
final TypeMirror classCastExceptionType;

/** The (erased) Iterable type . */
final TypeMirror iterableType;

/** The NullPointerException type . */
final TypeMirror nullPointerExceptionType;

/** The String type. */
final TypeMirror stringType;

/** The Throwable type. */
final TypeMirror throwableType;

/**
* @param treeBuilder builder for new AST nodes
* @param annotationProvider extracts annotations from AST nodes
Expand Down Expand Up @@ -337,6 +360,15 @@ public CFGTranslationPhaseOne(
returnNodes = new ArrayList<>();
declaredClasses = new ArrayList<>();
declaredLambdas = new ArrayList<>();

arithmeticExceptionType = getTypeMirror(ArithmeticException.class);
arrayIndexOutOfBoundsExceptionType = getTypeMirror(ArrayIndexOutOfBoundsException.class);
assertionErrorType = getTypeMirror(AssertionError.class);
classCastExceptionType = getTypeMirror(ClassCastException.class);
iterableType = types.erasure(getTypeMirror(Iterable.class));
nullPointerExceptionType = getTypeMirror(NullPointerException.class);
stringType = getTypeMirror(String.class);
throwableType = getTypeMirror(Throwable.class);
}

/**
Expand Down Expand Up @@ -667,10 +699,9 @@ protected Node box(Node node) {
getCurrentPath());
boxed.setInSource(false);
// Add Throwable to account for unchecked exceptions
TypeElement throwableElement = getTypeElement(Throwable.class);
addToConvertedLookupMap(node.getTree(), boxed);
insertNodeWithExceptionsAfter(
boxed, Collections.singleton(throwableElement.asType()), valueOfAccess);
boxed, Collections.singleton(throwableType), valueOfAccess);
return boxed;
} else {
return node;
Expand All @@ -693,9 +724,8 @@ protected Node unbox(Node node) {
MethodAccessNode primValueAccess = new MethodAccessNode(primValueSelect, node);
primValueAccess.setInSource(false);
// Method access may throw NullPointerException
TypeElement npeElement = getTypeElement(NullPointerException.class);
insertNodeWithExceptionsAfter(
primValueAccess, Collections.singleton(npeElement.asType()), node);
primValueAccess, Collections.singleton(nullPointerExceptionType), node);

MethodInvocationTree primValueCall = treeBuilder.buildMethodInvocation(primValueSelect);
handleArtificialTree(primValueCall);
Expand All @@ -708,10 +738,9 @@ protected Node unbox(Node node) {
unboxed.setInSource(false);

// Add Throwable to account for unchecked exceptions
TypeElement throwableElement = getTypeElement(Throwable.class);
addToConvertedLookupMap(node.getTree(), unboxed);
insertNodeWithExceptionsAfter(
unboxed, Collections.singleton(throwableElement.asType()), primValueAccess);
unboxed, Collections.singleton(throwableType), primValueAccess);
return unboxed;
} else {
return node;
Expand Down Expand Up @@ -766,9 +795,8 @@ private Node unboxAsNeeded(Node node, boolean boxed) {
*/
protected Node stringConversion(Node node) {
// For string conversion, see JLS 5.1.11
TypeElement stringElement = getTypeElement(String.class);
if (!TypesUtils.isString(node.getType())) {
Node converted = new StringConversionNode(node.getTree(), node, stringElement.asType());
Node converted = new StringConversionNode(node.getTree(), node, stringType);
addToConvertedLookupMap(converted);
insertNodeAfter(converted, node);
return converted;
Expand Down Expand Up @@ -1248,8 +1276,7 @@ public MethodInvocationNode visitMethodInvocation(MethodInvocationTree tree, Voi
// No NullPointerException can be thrown, use normal node
extendWithNode(target);
} else {
TypeElement npeElement = getTypeElement(NullPointerException.class);
extendWithNodeWithException(target, npeElement.asType());
extendWithNodeWithException(target, nullPointerExceptionType);
}

List<Node> arguments = new ArrayList<>();
Expand All @@ -1275,8 +1302,7 @@ public MethodInvocationNode visitMethodInvocation(MethodInvocationTree tree, Voi
List<? extends TypeMirror> thrownTypes = element.getThrownTypes();
thrownSet.addAll(thrownTypes);
// Add Throwable to account for unchecked exceptions
TypeElement throwableElement = getTypeElement(Throwable.class);
thrownSet.add(throwableElement.asType());
thrownSet.add(throwableType);

ExtendedNode extendedNode = extendWithNodeWithExceptions(node, thrownSet);

Expand Down Expand Up @@ -1393,14 +1419,12 @@ protected void translateAssertWithAssertionsEnabled(AssertTree tree) {
if (tree.getDetail() != null) {
detail = scan(tree.getDetail(), null);
}
TypeElement assertException = getTypeElement(AssertionError.class);
AssertionErrorNode assertNode =
new AssertionErrorNode(tree, condition, detail, assertException.asType());
new AssertionErrorNode(tree, condition, detail, assertionErrorType);
extendWithNode(assertNode);
NodeWithExceptionsHolder exNode =
extendWithNodeWithException(
new ThrowNode(null, assertNode, env.getTypeUtils()),
assertException.asType());
new ThrowNode(null, assertNode, env.getTypeUtils()), assertionErrorType);
exNode.setTerminatesExecution(true);

// then branch (nothing happens)
Expand Down Expand Up @@ -1434,8 +1458,7 @@ public Node visitAssignment(AssignmentTree tree, Void p) {
// No NullPointerException can be thrown, use normal node
extendWithNode(target);
} else {
TypeElement npeElement = getTypeElement(NullPointerException.class);
extendWithNodeWithException(target, npeElement.asType());
extendWithNodeWithException(target, nullPointerExceptionType);
}

// add assignment node
Expand Down Expand Up @@ -1572,9 +1595,7 @@ public Node visitCompoundAssignment(CompoundAssignmentTree tree, Void p) {
if (TypesUtils.isIntegralPrimitive(exprType)) {
operNode = new IntegerDivisionNode(operTree, targetRHS, value);

TypeElement throwableElement =
getTypeElement(ArithmeticException.class);
extendWithNodeWithException(operNode, throwableElement.asType());
extendWithNodeWithException(operNode, arithmeticExceptionType);
} else {
operNode = new FloatingDivisionNode(operTree, targetRHS, value);
}
Expand All @@ -1583,9 +1604,7 @@ public Node visitCompoundAssignment(CompoundAssignmentTree tree, Void p) {
if (TypesUtils.isIntegralPrimitive(exprType)) {
operNode = new IntegerRemainderNode(operTree, targetRHS, value);

TypeElement throwableElement =
getTypeElement(ArithmeticException.class);
extendWithNodeWithException(operNode, throwableElement.asType());
extendWithNodeWithException(operNode, arithmeticExceptionType);
} else {
operNode = new FloatingRemainderNode(operTree, targetRHS, value);
}
Expand Down Expand Up @@ -1787,9 +1806,7 @@ public Node visitBinary(BinaryTree tree, Void p) {
if (TypesUtils.isIntegralPrimitive(exprType)) {
r = new IntegerDivisionNode(tree, left, right);

TypeElement throwableElement =
getTypeElement(ArithmeticException.class);
extendWithNodeWithException(r, throwableElement.asType());
extendWithNodeWithException(r, arithmeticExceptionType);
} else {
r = new FloatingDivisionNode(tree, left, right);
}
Expand All @@ -1798,9 +1815,7 @@ public Node visitBinary(BinaryTree tree, Void p) {
if (TypesUtils.isIntegralPrimitive(exprType)) {
r = new IntegerRemainderNode(tree, left, right);

TypeElement throwableElement =
getTypeElement(ArithmeticException.class);
extendWithNodeWithException(r, throwableElement.asType());
extendWithNodeWithException(r, arithmeticExceptionType);
} else {
r = new FloatingRemainderNode(tree, left, right);
}
Expand Down Expand Up @@ -2307,9 +2322,6 @@ public Node visitEnhancedForLoop(EnhancedForLoopTree tree, Void p) {

// Distinguish loops over Iterables from loops over arrays.

TypeElement iterableElement = getTypeElement(Iterable.class);
TypeMirror iterableType = types.erasure(iterableElement.asType());

VariableTree variable = tree.getVariable();
VariableElement variableElement = TreeUtils.elementFromDeclaration(variable);
ExpressionTree expression = tree.getExpression();
Expand Down Expand Up @@ -2513,8 +2525,7 @@ public Node visitEnhancedForLoop(EnhancedForLoopTree tree, Void p) {
arrayAccessNode.setInSource(false);
extendWithNode(arrayAccessNode);
translateAssignment(variable, new LocalVariableNode(variable), arrayAccessNode);
Element npeElement = getTypeElement(NullPointerException.class);
extendWithNodeWithException(arrayAccessNode, npeElement.asType());
extendWithNodeWithException(arrayAccessNode, nullPointerExceptionType);

assert statement != null;
scan(statement, p);
Expand Down Expand Up @@ -2727,10 +2738,8 @@ public Node visitArrayAccess(ArrayAccessTree tree, Void p) {
Node index = unaryNumericPromotion(scan(tree.getIndex(), p));
Node arrayAccess = new ArrayAccessNode(tree, array, index);
extendWithNode(arrayAccess);
Element aioobeElement = getTypeElement(ArrayIndexOutOfBoundsException.class);
extendWithNodeWithException(arrayAccess, aioobeElement.asType());
Element npeElement = getTypeElement(NullPointerException.class);
extendWithNodeWithException(arrayAccess, npeElement.asType());
extendWithNodeWithException(arrayAccess, arrayIndexOutOfBoundsExceptionType);
extendWithNodeWithException(arrayAccess, nullPointerExceptionType);
return arrayAccess;
}

Expand Down Expand Up @@ -2863,8 +2872,7 @@ public Node visitNewClass(NewClassTree tree, Void p) {
List<? extends TypeMirror> thrownTypes = constructor.getThrownTypes();
thrownSet.addAll(thrownTypes);
// Add Throwable to account for unchecked exceptions
TypeElement throwableElement = getTypeElement(Throwable.class);
thrownSet.add(throwableElement.asType());
thrownSet.add(throwableType);

extendWithNodeWithExceptions(node, thrownSet);

Expand Down Expand Up @@ -2955,8 +2963,7 @@ public Node visitMemberSelect(MemberSelectTree tree, Void p) {
// No NullPointerException can be thrown, use normal node
extendWithNode(node);
} else {
TypeElement npeElement = getTypeElement(NullPointerException.class);
extendWithNodeWithException(node, npeElement.asType());
extendWithNodeWithException(node, nullPointerExceptionType);
}

return node;
Expand Down Expand Up @@ -3132,7 +3139,6 @@ public Node visitTry(TryTree tree, Void p) {

scan(finallyBlock, p);

TypeMirror throwableType = getTypeElement(Throwable.class).asType();
NodeWithExceptionsHolder throwing =
extendWithNodeWithException(
new MarkerNode(
Expand Down Expand Up @@ -3319,9 +3325,8 @@ public Node visitTypeCast(TypeCastTree tree, Void p) {
final Node operand = scan(tree.getExpression(), p);
final TypeMirror type = TreeUtils.typeOf(tree.getType());
final Node node = new TypeCastNode(tree, operand, type, types);
final TypeElement cceElement = getTypeElement(ClassCastException.class);

extendWithNodeWithException(node, cceElement.asType());
extendWithNodeWithException(node, classCastExceptionType);
return node;
}

Expand Down Expand Up @@ -3645,12 +3650,12 @@ public Node visitOther(Tree tree, Void p) {
}

/**
* Returns the TypeElement for the given class.
* Returns the TypeMirror for the given class.
*
* @param clazz a class
* @return the TypeElement for the class
* @return the TypeMirror for the class
*/
private TypeElement getTypeElement(Class<?> clazz) {
return elements.getTypeElement(clazz.getCanonicalName());
private TypeMirror getTypeMirror(Class<?> clazz) {
return TypesUtils.typeFromClass(clazz, types, elements);
}
}

0 comments on commit eac5a54

Please sign in to comment.