Class SideEffectAnalysis

java.lang.Object
com.sun.source.util.TreeScanner<Void,Void>
com.google.errorprone.util.SideEffectAnalysis
All Implemented Interfaces:
com.sun.source.tree.TreeVisitor<Void,Void>

public final class SideEffectAnalysis extends com.sun.source.util.TreeScanner<Void,Void>
This class is responsible for analyzing an expression and telling if the expression can have side effects. This class is used by calling the static hasSideEffect(ExpressionTree) method.

A side-effect is what that would change the state of the program. Here are examples of expressions that could possibly change the state of a program.

  • Any function call: toString(), hashCode(), setX(value), etc.
  • Unary expression: i += 1, i++, --j
  • New expression: new SomeClass()

Everything besides the above examples is considered side-effect free.

The analysis in this class initially assumes that the expression is side-effect free and then tries to prove it wrong.

  • Method Summary

    Modifier and Type
    Method
    Description
    static boolean
    hasSideEffect(com.sun.source.tree.ExpressionTree expression)
    Tries to establish whether expression is side-effect free.
    visitCompoundAssignment(com.sun.source.tree.CompoundAssignmentTree tree, Void unused)
     
    visitMethodInvocation(com.sun.source.tree.MethodInvocationTree tree, Void unused)
     
    visitNewClass(com.sun.source.tree.NewClassTree tree, Void unused)
     
    visitUnary(com.sun.source.tree.UnaryTree tree, Void unused)
     

    Methods inherited from class com.sun.source.util.TreeScanner

    reduce, scan, scan, visitAnnotatedType, visitAnnotation, visitAnyPattern, visitArrayAccess, visitArrayType, visitAssert, visitAssignment, visitBinary, visitBindingPattern, visitBlock, visitBreak, visitCase, visitCatch, visitClass, visitCompilationUnit, visitConditionalExpression, visitConstantCaseLabel, visitContinue, visitDeconstructionPattern, visitDefaultCaseLabel, visitDoWhileLoop, visitEmptyStatement, visitEnhancedForLoop, visitErroneous, visitExports, visitExpressionStatement, visitForLoop, visitIdentifier, visitIf, visitImport, visitInstanceOf, visitIntersectionType, visitLabeledStatement, visitLambdaExpression, visitLiteral, visitMemberReference, visitMemberSelect, visitMethod, visitModifiers, visitModule, visitNewArray, visitOpens, visitOther, visitPackage, visitParameterizedType, visitParenthesized, visitPatternCaseLabel, visitPrimitiveType, visitProvides, visitRequires, visitReturn, visitSwitch, visitSwitchExpression, visitSynchronized, visitThrow, visitTry, visitTypeCast, visitTypeParameter, visitUnionType, visitUses, visitVariable, visitWhileLoop, visitWildcard, visitYield

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • hasSideEffect

      public static boolean hasSideEffect(com.sun.source.tree.ExpressionTree expression)
      Tries to establish whether expression is side-effect free. The heuristics here are very conservative.
    • visitCompoundAssignment

      public Void visitCompoundAssignment(com.sun.source.tree.CompoundAssignmentTree tree, Void unused)
      Specified by:
      visitCompoundAssignment in interface com.sun.source.tree.TreeVisitor<Void,Void>
      Overrides:
      visitCompoundAssignment in class com.sun.source.util.TreeScanner<Void,Void>
    • visitMethodInvocation

      public Void visitMethodInvocation(com.sun.source.tree.MethodInvocationTree tree, Void unused)
      Specified by:
      visitMethodInvocation in interface com.sun.source.tree.TreeVisitor<Void,Void>
      Overrides:
      visitMethodInvocation in class com.sun.source.util.TreeScanner<Void,Void>
    • visitNewClass

      public Void visitNewClass(com.sun.source.tree.NewClassTree tree, Void unused)
      Specified by:
      visitNewClass in interface com.sun.source.tree.TreeVisitor<Void,Void>
      Overrides:
      visitNewClass in class com.sun.source.util.TreeScanner<Void,Void>
    • visitUnary

      public Void visitUnary(com.sun.source.tree.UnaryTree tree, Void unused)
      Specified by:
      visitUnary in interface com.sun.source.tree.TreeVisitor<Void,Void>
      Overrides:
      visitUnary in class com.sun.source.util.TreeScanner<Void,Void>