private void fixVariableDeclarations ( SwitchStatement node ) { List < Statement > statements = node . getStatements ( ) ; Block block = new Block ( ) ; List < Statement > blockStmts = block . getStatements ( ) ; for ( int i = 0 ; i < statements . size ( ) ; i ++ ) { Statement stmt = statements . get ( i ) ; if ( stmt instanceof VariableDeclarationStatement ) { VariableDeclarationStatement declStmt = ( VariableDeclarationStatement ) stmt ; statements . remove ( i -- ) ; List < VariableDeclarationFragment > fragments = declStmt . getFragments ( ) ; for ( VariableDeclarationFragment decl : fragments ) { Expression initializer = decl . getInitializer ( ) ; if ( initializer != null ) { Assignment assignment = new Assignment ( new SimpleName ( decl . getVariableElement ( ) ) , TreeUtil . remove ( initializer ) ) ; statements . add ( ++ i , new ExpressionStatement ( assignment ) ) ; } } blockStmts . add ( declStmt ) ; } } if ( blockStmts . size ( ) > 0 ) { // There is at least one variable declaration, so copy this switch // statement into the new block and replace it in the parent list. node . replaceWith ( block ) ; blockStmts . add ( node ) ; } }
Moves all variable declarations above the first case statement .