Class SideEffectAnalysis
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 TypeMethodDescriptionstatic boolean
hasSideEffect
(com.sun.source.tree.ExpressionTree expression) Tries to establish whetherexpression
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
-
Method Details
-
hasSideEffect
public static boolean hasSideEffect(com.sun.source.tree.ExpressionTree expression) Tries to establish whetherexpression
is side-effect free. The heuristics here are very conservative. -
visitCompoundAssignment
-
visitMethodInvocation
-
visitNewClass
-
visitUnary
-