\subsection{\refactoring{Extract Class}}
This refactoring extracts some fields of a class into a newly created member class. Implemented in \sourcelink{ExtractClass/ExtractClass.jrag}; 
see Algorithms~\ref{alg:ExtractClass},~\ref{alg:MoveInitsTogether},~\ref{alg:PassInitsToConstructor}.

We can pass initializers to a constructor if they do not depend on values of previous initializers.

\begin{algorithm}[p]
\caption{$\refactoring{Extract Class}(C : \type{Class}, fs : \listtp{\type{Field}}, n : \type{Name}, fn : \type{Name})$}\label{alg:ExtractClass}
\begin{algorithmic}[1]
\REQUIRE Java
\ENSURE Java $\cup$ locked names, locked dataflow, first-class array init
\medskip
\STATE $v \leftarrow \text{maximum visibility of any of the $fs$}$
\STATE $W \leftarrow \text{new \code{static} class of name $n$ with visibility $v$}$
\STATE $\refactoring{Insert Type}(C, W)$
\STATE $w \leftarrow \text{new field of type $W$ and name $fn$, initialised to a new instance of $W$}$
\STATE $\refactoring{Insert Field}(C, w)$
\FORALL{$f\in fs$}
  \STATE \assert $f$ is not static
  \FORALL{uses $v$ of $f$}
    \STATE qualify $v$ with a locked access to $w$
  \ENDFOR
  \IF{$f$ has initialiser}
    \STATE split field declaration and initializer, leaving initializer in initializer block after
  \ENDIF
  \STATE remove $f$
  \STATE $\refactoring{Insert Field}(W, f)$
\ENDFOR
\STATE $inits \leftarrow \{\text{initializers of }fs\}$
\STATE $\refactoring{Move Inits Together}(inits, w)$ \orelse{} $\refactoring{Id}()$
\STATE merge consecutive $inits$ to common initializer blocks
\STATE $\refactoring{PassInitsToConstructor}(inits, w)$ \orelse{} $\refactoring{Id}()$
\end{algorithmic}
\end{algorithm}


\begin{algorithm}[p]
\caption{$\refactoring{Move Inits Together}(inits : \listtp{InitializerBlock}, after : \type{Field})$}\label{alg:MoveInitsTogether}
\begin{algorithmic}[1]
\REQUIRE Java
\ENSURE Java
\medskip
\FORALL{$init\in inits$}
  \STATE lock names and dataflow in $init$
  \STATE remove $init$ and insert it after already moved initializers (possibly $after$)
  \STATE unlock names and dataflow in $init$
\ENDFOR
\end{algorithmic}
\end{algorithm}

\begin{algorithm}[p]
\caption{$\refactoring{PassInitsToConstructor}(inits : \listtp{InitializerBlock}, w : Field)$}\label{alg:PassInitsToConstructor}
\begin{algorithmic}[1]
\STATE \assert all $inits$ are in one initializer block
\STATE $index\leftarrow$ possition of $w$
\STATE $vars\leftarrow$ [ ]
\FORALL{$init$ in $inits$}
  \STATE $v\leftarrow\refactoringNoExt{Extract Temp}(\text{left side of $init$}, \text{fresh name}, index)$ 
  \STATE $vars\leftarrow v : vars$
  \STATE $index\leftarrow index + 1$
\ENDFOR
\FORALL{$var$ in $vars$}
  \STATE $\refactoring{Inline Temp}(var)$
\ENDFOR
\STATE in $W$ create a constructor for initializing all fields
\STATE change the constructor call for $w$ to initialize the fields and remove $inits$
\end{algorithmic}
\end{algorithm}

This is only a bare-bones specification. The implementation additionally allows to encapsulate the extracted fields, and to move the wrapper class $W$ to the toplevel.
