Class ResourceLeakFixer

java.lang.Object
io.codemodder.remediation.resourceleak.ResourceLeakFixer

public final class ResourceLeakFixer extends Object
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 Type
    Method
    Description
    static Optional<com.github.javaparser.ast.stmt.TryStmt>
    checkAndFix(com.github.javaparser.ast.expr.Expression expr)
    Detects if an Expression 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 a Expression 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 of expr and returns line if it does.
    static Optional<com.github.javaparser.ast.stmt.TryStmt>
    tryToFix(com.github.javaparser.ast.expr.ObjectCreationExpr creationExpr)
     

    Methods inherited from class java.lang.Object

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

    • checkAndFix

      public static Optional<com.github.javaparser.ast.stmt.TryStmt> checkAndFix(com.github.javaparser.ast.expr.Expression expr)
      Detects if an Expression that creates a resource type is fixable and tries to fix it. Combines isFixable and tryToFix.
    • isFixable

      public static boolean isFixable(com.github.javaparser.ast.expr.Expression expr)
      Detects if a Expression 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 of expr 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)