Class BlockStatementSplitter


  • @Internal
    public class BlockStatementSplitter
    extends Object
    Extract statements from IFs, ELSEs and WHILEs blocks making them smaller.

    BlockStatementSplitter does not recognize if statement operates on local of class member variable. Because of that, code must be preprocessed by DeclarationRewriter which converts all local variables extracted as to member variables.

    Before

    
     while (counter > 0) {
         int localA = a + 1000;
         System.out.println(localA);
         if (a > 0) {
             b = a * 2;
             c = b * 2;
             System.out.println(b);
         } else {
             b = a * 3;
             System.out.println(b);
         }
         counter--;
     }
    
     

    After

    
     while (counter > 0) {
         myFun_0_1(a, b, c);
         if (a > 0) {
             myFun_0_1_2(a, b, c);
         } else {
             myFun_0_1_3(a, b, c);
         }
         counter--;
     }
     

    Where bodies of extracted "methods" are:

    
     myFun_0_1(int a, int b, int c) ->
         int localA = a + 1000;
         System.out.println(localA);
     
    
     myFun_0_1_3(int a, int b, int c) ->
         b = a * 2;
         c = b * 2;
         System.out.println(b);
     
    
     myFun_whileBody0_0_ifBody1(int a) ->
         b = a * 3;
         System.out.println(b);
     
    • Constructor Detail

      • BlockStatementSplitter

        public BlockStatementSplitter​(String code,
                                      String parameters)
        Initialize new BlockStatementSplitter.
        Parameters:
        code - a code block that should be rewritten.
        parameters - parameters definition that should be used for extracted methods.
    • Method Detail

      • rewriteBlock

        public String rewriteBlock​(String context)
        Rewrite code block that was used for this object initialization.
        Parameters:
        context - prefix for extracted blocks.
        Returns:
        a map which key represent rewritten block name and value represents rewritten code block, including calls to extracted methods
      • extractBlocks

        public Map<String,​List<String>> extractBlocks()
        This method extracts statements from IFs, ELSE's and WHILE blocks from block code used during initialization of this object. Every entry of returned map can be seen as new method name (map key) and method's body (map value). The block names will be prefixed with provided context.
        Returns:
        a map of block name to block statements mappings. The key can be interpreted as name of extracted block/method and corresponding List represents individual statements (block' lines) for this block.