Class ASTTransforms

java.lang.Object
io.codemodder.ast.ASTTransforms

public final class ASTTransforms extends Object
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static void
    addImportIfMissing(com.github.javaparser.ast.CompilationUnit cu, Class<?> clazz)
    Add an import in alphabetical order.
    static void
    addImportIfMissing(com.github.javaparser.ast.CompilationUnit cu, String className)
    Add an import in alphabetical order.
    static void
    addStatementAfterStatement(com.github.javaparser.ast.stmt.Statement existingStatement, com.github.javaparser.ast.stmt.Statement newStatement)
    Adds an Statement after another Statement.
    static void
    addStatementBeforeStatement(com.github.javaparser.ast.stmt.Statement existingStatement, com.github.javaparser.ast.stmt.Statement newStatement)
    Adds an Statement before another Statement.
    static void
    addStaticImportIfMissing(com.github.javaparser.ast.CompilationUnit cu, String method)
     
    static com.github.javaparser.ast.stmt.TryStmt
    combineResources(com.github.javaparser.ast.stmt.TryStmt innerTry)
    Given a TryStmt without any finally and catch clauses, and that is the first statement of a try with resources block, merge the two try statements into one.
    static void
    mergeConcatenatedLiterals(com.github.javaparser.ast.expr.Expression e)
    Given a string expression, merge any literals that are directly concatenated.
    static com.github.javaparser.ast.expr.Expression
    removeEmptyStringConcatenation(com.github.javaparser.ast.expr.BinaryExpr binexp)
    Removes concatenation with empty strings.
    static void
    removeEmptyStringConcatenation(com.github.javaparser.ast.Node subtree)
    Removes all concatenations with empty strings in the given subtree.
    static void
    removeImportIfUnused(com.github.javaparser.ast.CompilationUnit cu, String className)
    Remove an import if we can't find references to it in the code.
    static void
    removeUnusedLocalVariables(com.github.javaparser.ast.Node subtree)
    Removes unused variables.
    static com.github.javaparser.ast.stmt.TryStmt
    splitResources(com.github.javaparser.ast.stmt.TryStmt stmt, int index)
    Given a TryStmt split its resources into two nested TryStmts.
    static com.github.javaparser.ast.stmt.TryStmt
    wrapIntoResource(com.github.javaparser.ast.stmt.ExpressionStmt stmt, com.github.javaparser.ast.expr.VariableDeclarationExpr vdecl, LocalScope scope)
    Given a local variable declaration stmt, where vdecl is a single initialized declaration of a variable v with scope scope, v is never assigned in its scope, then wrap the declaration into as a resource of a try stmt.

    Methods inherited from class java.lang.Object

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

    • ASTTransforms

      public ASTTransforms()
  • Method Details

    • addImportIfMissing

      public static void addImportIfMissing(com.github.javaparser.ast.CompilationUnit cu, String className)
      Add an import in alphabetical order.
    • addImportIfMissing

      public static void addImportIfMissing(com.github.javaparser.ast.CompilationUnit cu, Class<?> clazz)
      Add an import in alphabetical order.
    • addStaticImportIfMissing

      public static void addStaticImportIfMissing(com.github.javaparser.ast.CompilationUnit cu, String method)
    • addStatementBeforeStatement

      public static void addStatementBeforeStatement(com.github.javaparser.ast.stmt.Statement existingStatement, com.github.javaparser.ast.stmt.Statement newStatement)
      Adds an Statement before another Statement. Single Statements in the body of For/Do/While/If/Labeled Statements are replaced with a BlockStmt containing both statements.
    • addStatementAfterStatement

      public static void addStatementAfterStatement(com.github.javaparser.ast.stmt.Statement existingStatement, com.github.javaparser.ast.stmt.Statement newStatement)
      Adds an Statement after another Statement. Single Statements in the body of For/Do/While/If/Labeled Statements are replaced with a BlockStmt containing both statements.
    • wrapIntoResource

      public static com.github.javaparser.ast.stmt.TryStmt wrapIntoResource(com.github.javaparser.ast.stmt.ExpressionStmt stmt, com.github.javaparser.ast.expr.VariableDeclarationExpr vdecl, LocalScope scope)
      Given a local variable declaration stmt, where vdecl is a single initialized declaration of a variable v with scope scope, v is never assigned in its scope, then wrap the declaration into as a resource of a try stmt.
    • splitResources

      public static com.github.javaparser.ast.stmt.TryStmt splitResources(com.github.javaparser.ast.stmt.TryStmt stmt, int index)
      Given a TryStmt split its resources into two nested TryStmts.
    • combineResources

      public static com.github.javaparser.ast.stmt.TryStmt combineResources(com.github.javaparser.ast.stmt.TryStmt innerTry)
      Given a TryStmt without any finally and catch clauses, and that is the first statement of a try with resources block, merge the two try statements into one.
    • removeImportIfUnused

      public static void removeImportIfUnused(com.github.javaparser.ast.CompilationUnit cu, String className)
      Remove an import if we can't find references to it in the code.
    • removeEmptyStringConcatenation

      public static com.github.javaparser.ast.expr.Expression removeEmptyStringConcatenation(com.github.javaparser.ast.expr.BinaryExpr binexp)
      Removes concatenation with empty strings. For example, in : ``` String a = "some string"; String b = ""; a + "" + b; ``` The expression `a + "" + b` would be reduced to `a`. Returns the expression without the empty concatenations.
    • removeEmptyStringConcatenation

      public static void removeEmptyStringConcatenation(com.github.javaparser.ast.Node subtree)
      Removes all concatenations with empty strings in the given subtree.
    • removeUnusedLocalVariables

      public static void removeUnusedLocalVariables(com.github.javaparser.ast.Node subtree)
      Removes unused variables.
    • mergeConcatenatedLiterals

      public static void mergeConcatenatedLiterals(com.github.javaparser.ast.expr.Expression e)
      Given a string expression, merge any literals that are directly concatenated. This transform will recurse over any Names referenced.