All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.codemodder.codemods.DeclareVariableOnSeparateLine Maven / Gradle / Ivy

There is a newer version: 0.97.3
Show newest version
package io.codemodder.codemods;

import com.github.javaparser.ast.Node;
import com.github.javaparser.ast.NodeList;
import com.github.javaparser.ast.body.VariableDeclarator;
import com.github.javaparser.ast.nodeTypes.NodeWithVariables;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

abstract class DeclareVariableOnSeparateLine {

  protected final NodeWithVariables parentNode;

  protected DeclareVariableOnSeparateLine(final NodeWithVariables parentNode) {
    this.parentNode = Objects.requireNonNull(parentNode);
  }

  /**
   * Splits multiple inline variables within a parent node into separate variable declaration
   * statements.
   */
  protected boolean splitVariablesIntoTheirOwnStatements() {

    final List inlineVariables = parentNode.getVariables().stream().toList();

    final List remainingVariables =
        inlineVariables.subList(1, inlineVariables.size());

    final List newVariableNodes = createVariableNodesToAdd(remainingVariables);

    if (!addNewNodesToParentNode(newVariableNodes)) {
      return false;
    }
    // Replace parent's node that has all inline variables with only the first variable
    parentNode.setVariables(new NodeList<>(inlineVariables.get(0)));

    return true;
  }

  /** Returns a list of nodes created from the list of inline variables. */
  protected abstract List createVariableNodesToAdd(List inlineVariables);

  /** Adds new variable nodes to the parent node. */
  protected abstract boolean addNewNodesToParentNode(List nodesToAdd);

  /**
   * Inserts a list of nodes after a specified reference node within an original list of nodes. The
   * result of this operation is a new list containing the original nodes with the additional nodes
   * inserted after the specified reference node while maintaining the original order of elements.
   */
  static  List insertNodesAfterReference(
      final List originalNodes, final Node referenceNode, final List nodesToAdd) {

    // Find the index of the reference node in the original list
    final int referenceIndex = originalNodes.indexOf(referenceNode);

    // Split the original list into elements before and after the reference node
    final List elementsBefore = originalNodes.subList(0, referenceIndex + 1);
    final List elementsAfter = originalNodes.subList(referenceIndex + 1, originalNodes.size());

    // Create a new list with nodes inserted after the reference node
    final List newElements = new ArrayList<>(elementsBefore);
    nodesToAdd.forEach(node -> newElements.add((T) node));
    newElements.addAll(elementsAfter);

    return newElements;
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy