@@ -119,6 +119,7 @@ object JavaCode {
119119 * A trait representing a block of java code.
120120 */
121121trait Block extends JavaCode {
122+ import Block ._
122123
123124 // The expressions to be evaluated inside this block.
124125 def exprValues : Set [ExprValue ]
@@ -148,14 +149,17 @@ trait Block extends JavaCode {
148149 }
149150
150151 // Concatenates this block with other block.
151- def + (other : Block ): Block
152+ def + (other : Block ): Block = other match {
153+ case EmptyBlock => this
154+ case _ => code " $this\n $other"
155+ }
152156}
153157
154158object Block {
155159
156160 val CODE_BLOCK_BUFFER_LENGTH : Int = 512
157161
158- implicit def blocksToBlock (blocks : Seq [Block ]): Block = Blocks ( blocks)
162+ implicit def blocksToBlock (blocks : Seq [Block ]): Block = blocks.reduceLeft(_ + _ )
159163
160164 implicit class BlockHelper (val sc : StringContext ) extends AnyVal {
161165 def code (args : Any * ): Block = {
@@ -190,26 +194,29 @@ object Block {
190194 while (strings.hasNext) {
191195 val input = inputs.next
192196 input match {
193- case _ : ExprValue | _ : Block =>
197+ case _ : ExprValue | _ : CodeBlock =>
194198 codeParts += buf.toString
195199 buf.clear
196200 blockInputs += input.asInstanceOf [JavaCode ]
201+ case EmptyBlock =>
197202 case _ =>
198203 buf.append(input)
199204 }
200205 buf.append(strings.next)
201206 }
202- if (buf.nonEmpty) {
203- codeParts += buf.toString
204- }
207+ codeParts += buf.toString
205208
206209 (codeParts.toSeq, blockInputs.toSeq)
207210 }
208211}
209212
210213/**
211214 * A block of java code. Including a sequence of code parts and some inputs to this block.
212- * The actual java code is generated by embedding the inputs into the code parts.
215+ * The actual java code is generated by embedding the inputs into the code parts. Here we keep
216+ * inputs of `JavaCode` instead of simply folding them as a string of code, because we need to
217+ * track expressions (`ExprValue`) in this code block. We need to be able to manipulate the
218+ * expressions later without changing the behavior of this code block in some applications, e.g.,
219+ * method splitting.
213220 */
214221case class CodeBlock (codeParts : Seq [String ], blockInputs : Seq [JavaCode ]) extends Block {
215222 override lazy val exprValues : Set [ExprValue ] = {
@@ -230,30 +237,11 @@ case class CodeBlock(codeParts: Seq[String], blockInputs: Seq[JavaCode]) extends
230237 }
231238 buf.toString
232239 }
233-
234- override def + (other : Block ): Block = other match {
235- case c : CodeBlock => Blocks (Seq (this , c))
236- case b : Blocks => Blocks (Seq (this ) ++ b.blocks)
237- case EmptyBlock => this
238- }
239- }
240-
241- case class Blocks (blocks : Seq [Block ]) extends Block {
242- override lazy val exprValues : Set [ExprValue ] = blocks.flatMap(_.exprValues).toSet
243- override lazy val code : String = blocks.map(_.toString).mkString(" \n " )
244-
245- override def + (other : Block ): Block = other match {
246- case c : CodeBlock => Blocks (blocks :+ c)
247- case b : Blocks => Blocks (blocks ++ b.blocks)
248- case EmptyBlock => this
249- }
250240}
251241
252242object EmptyBlock extends Block with Serializable {
253243 override val code : String = " "
254244 override val exprValues : Set [ExprValue ] = Set .empty
255-
256- override def + (other : Block ): Block = other
257245}
258246
259247/**
0 commit comments