Skip to content
This repository has been archived by the owner on Oct 10, 2024. It is now read-only.

Fix #848: nest codeBlock problem for issue #884

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions src/main/java/com/squareup/javapoet/CodeBlock.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import java.io.IOException;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
Expand Down Expand Up @@ -126,6 +127,26 @@ public static CodeBlock join(Iterable<CodeBlock> codeBlocks, String separator) {
CodeBlockJoiner::merge,
CodeBlockJoiner::join);
}
/**
* Joins {@code codeBlocks} into a single {@link CodeBlock}, each separated by {@code separators}.
* For example, joining {@code String s}, {@code Object o} and {@code int i} using {@code "[, ,\] "}
* would produce {@code String s, Object o\ int i}.
*/
public static CodeBlock join(Iterable<CodeBlock> codeBlocks, String[] separators) {
List<CodeBlock> list = new ArrayList<>();
Iterator<CodeBlock> iter = codeBlocks.iterator();
list.add((CodeBlock) iter.next());

for (String separator : separators) {
list.add((CodeBlock) iter.next());
list.add(join(list, separator));
list.remove(0);
list.remove(0);
}
return list.get(0);
}



/**
* A {@link Collector} implementation that joins {@link CodeBlock} instances together into one
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/com/squareup/javapoet/CodeWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,11 @@ private void emitLiteral(Object o) throws IOException {
annotationSpec.emit(this, true);
} else if (o instanceof CodeBlock) {
CodeBlock codeBlock = (CodeBlock) o;
CodeBlock.Builder builder = CodeBlock.builder();
builder.formatParts.addAll(codeBlock.formatParts);
builder.args.addAll(codeBlock.args);
builder.formatParts.remove("$[");
builder.formatParts.remove("$]");
emit(codeBlock);
} else {
emitAndIndent(String.valueOf(o));
Expand Down
30 changes: 30 additions & 0 deletions src/test/java/com/squareup/javapoet/CodeBlockTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,36 @@ public final class CodeBlockTest {
assertThat(joined.toString()).isEqualTo("start {\"hello\" || world.World || need tacos} end");
}

@Test public void NestCodeBlock(){
CodeBlock inner = CodeBlock.builder()
.beginControlFlow("() ->")
.addStatement("return 42")
.endControlFlow()
.build();

CodeBlock outer =
CodeBlock.builder()
.add(
"$T lambda = $L;",
ParameterizedTypeName.get(
ClassName.get(ArrayList.class), ClassName.get(Integer.class)),
inner)
.build();
assertThat(outer.toString()).isEqualTo("java.util.ArrayList<java.lang.Integer> lambda = () -> {\n" +
" return 42;\n" +
"}\n" +
";");

}
@Test public void joiningWithDifferentSeparator() {
List<CodeBlock> codeBlocks = new ArrayList<>();
codeBlocks.add(CodeBlock.of("$S", "hello"));
codeBlocks.add(CodeBlock.of("$T", ClassName.get("world", "World")));
codeBlocks.add(CodeBlock.of("need tacos"));
CodeBlock info = CodeBlock.join(codeBlocks, new String[]{" || "," | "});

assertThat(info.toString()).isEqualTo("\"hello\" || world.World | need tacos");
}
@Test public void clear() {
CodeBlock block = CodeBlock.builder()
.addStatement("$S", "Test string")
Expand Down