Class ResourceLeakFixer
java.lang.Object
io.codemodder.remediation.resourceleak.ResourceLeakFixer
A library that contains methods for automatically fixing resource leaks detected by CodeQL's
rules "java/database-resource-leak", java/input-resource-leak, and java/output-resource-leak
whenever possible.
-
Method Summary
Modifier and TypeMethodDescriptionstatic Optional<com.github.javaparser.ast.stmt.TryStmt>
checkAndFix
(com.github.javaparser.ast.expr.Expression expr) Detects if anExpression
that creates a resource type is fixable and tries to fix it.static Optional<com.github.javaparser.ast.expr.Expression>
findResourceInit
(com.github.javaparser.ast.expr.NameExpr name) static com.github.javaparser.ast.expr.ObjectCreationExpr
findRootExpression
(com.github.javaparser.ast.expr.ObjectCreationExpr creationExpr) static List<Either<LocalDeclaration,
com.github.javaparser.ast.Node>> static boolean
isFixable
(com.github.javaparser.ast.expr.Expression expr) Detects if aExpression
detected by CodeQL that creates a leaking resource is fixable.static Optional<com.github.javaparser.ast.stmt.TryStmt>
tryToFix
(com.github.javaparser.ast.expr.MethodCallExpr mce) Tries to fix the leak ofexpr
and returns line if it does.static Optional<com.github.javaparser.ast.stmt.TryStmt>
tryToFix
(com.github.javaparser.ast.expr.ObjectCreationExpr creationExpr)
-
Method Details
-
checkAndFix
public static Optional<com.github.javaparser.ast.stmt.TryStmt> checkAndFix(com.github.javaparser.ast.expr.Expression expr) Detects if anExpression
that creates a resource type is fixable and tries to fix it. CombinesisFixable
andtryToFix
. -
isFixable
public static boolean isFixable(com.github.javaparser.ast.expr.Expression expr) Detects if aExpression
detected by CodeQL that creates a leaking resource is fixable.A resource R is dependent of another resource S if closing S will also close R. The following suffices to check if a resource R can be closed. (*) A resource R can be closed if no dependent resource S exists s.t.: (i) S is not closed within R's scope, and (ii) S escapes R's scope Currently, we cannot test (*), as it requires some dataflow analysis, instead we test for (+): S is assigned to a variable that escapes.
-
findRootExpression
public static com.github.javaparser.ast.expr.ObjectCreationExpr findRootExpression(com.github.javaparser.ast.expr.ObjectCreationExpr creationExpr) -
tryToFix
public static Optional<com.github.javaparser.ast.stmt.TryStmt> tryToFix(com.github.javaparser.ast.expr.ObjectCreationExpr creationExpr) -
tryToFix
public static Optional<com.github.javaparser.ast.stmt.TryStmt> tryToFix(com.github.javaparser.ast.expr.MethodCallExpr mce) Tries to fix the leak ofexpr
and returns line if it does. -
flowsInto
public static List<Either<LocalDeclaration,com.github.javaparser.ast.Node>> flowsInto(LocalDeclaration ld) -
findResourceInit
public static Optional<com.github.javaparser.ast.expr.Expression> findResourceInit(com.github.javaparser.ast.expr.NameExpr name)
-