\subsection{\refactoring{Extract Constant}}
This refactoring extracts a constant expression into a field. Implemented in \sourcelink{ExtractTemp/ExtractConstant.jrag}; see Algorithm~\ref{alg:ExtractConstant}.

\begin{algorithm}[p]
\caption{$\refactoring{Extract Constant}(e : \type{Expr}, n : \type{Name})$}\label{alg:ExtractConstant}
\begin{algorithmic}[1]
\REQUIRE Java
\ENSURE Java $\cup$ locked names, locked dataflow
\medskip
\STATE \assert $e$ is extractible
\STATE $A \leftarrow \text{enclosing type of $e$}$
\STATE $t \leftarrow \text{effective type of $e$}$
\STATE $f \leftarrow$ new \code{private} (\code{public} if $A$ is an interface) \code{static final} field of type $t$ and name $n$
\STATE $\refactoring{Insert Field}(A, f)$
\STATE lock names, flow, and synchronisation of $e$
\STATE set initialiser of $f$ to $e$
\STATE replace $e$ with locked access to $f$
\end{algorithmic}
\end{algorithm}

An expression is extractible if its type is not \code{void}, it is not a reference to a type or package, and it is not the keyword \code{super}; furthermore, it cannot be on the right-hand side of a dot.

The \emph{effective type} of an expression $e$ is the same as the type of $e$, except when the type of $e$ is an anonymous class, in which case the effective type is its superclass, or when the type of $e$ is a captured type variable, in which case the effective type is its upper bound.
