aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorChristian Stein <sormuras@gmail.com>2015-12-09 20:37:21 +0100
committerChristian Stein <sormuras@gmail.com>2015-12-09 22:38:40 +0100
commit4ce8e427b533ec2a598ae9c967487713ce731662 (patch)
tree57e0087a269b387adc11191e7af4f59da1421fbe /src
parent4522c42783ac897f26b98232ca39c76a803b07b1 (diff)
downloadjavapoet-4ce8e427b533ec2a598ae9c967487713ce731662.tar.gz
Too many indexed arguments does fail now.
Diffstat (limited to 'src')
-rw-r--r--src/main/java/com/squareup/javapoet/CodeBlock.java24
-rw-r--r--src/test/java/com/squareup/javapoet/TypeSpecTest.java14
2 files changed, 32 insertions, 6 deletions
diff --git a/src/main/java/com/squareup/javapoet/CodeBlock.java b/src/main/java/com/squareup/javapoet/CodeBlock.java
index 5fff86f..28eebe5 100644
--- a/src/main/java/com/squareup/javapoet/CodeBlock.java
+++ b/src/main/java/com/squareup/javapoet/CodeBlock.java
@@ -111,7 +111,8 @@ public final class CodeBlock {
public Builder add(String format, Object... args) {
boolean hasRelative = false;
boolean hasIndexed = false;
- int parameterCount = 0;
+ int relativeParameterCount = 0;
+ int[] indexedParameterCount = new int[args.length];
for (int p = 0; p < format.length(); ) {
if (format.charAt(p) != '$') {
@@ -145,11 +146,12 @@ public final class CodeBlock {
if (indexStart < indexEnd) {
index = Integer.parseInt(format.substring(indexStart, indexEnd)) - 1;
hasIndexed = true;
+ indexedParameterCount[index % args.length]++; // modulo is needed, checked below anyway
} else {
- index = parameterCount;
+ index = relativeParameterCount;
hasRelative = true;
+ relativeParameterCount++;
}
- parameterCount++;
checkArgument(index >= 0 && index < args.length,
"index %d for '%s' not in range (received %s arguments)",
@@ -177,8 +179,20 @@ public final class CodeBlock {
formatParts.add("$" + c);
}
- checkArgument(parameterCount >= args.length,
- "unused arguments: expected %s, received %s", parameterCount, args.length);
+ if (hasRelative) {
+ checkArgument(relativeParameterCount >= args.length,
+ "unused arguments: expected %s, received %s", relativeParameterCount, args.length);
+ }
+ if (hasIndexed) {
+ List<String> unused = new ArrayList<>();
+ for (int i = 0; i < args.length; i++) {
+ if (indexedParameterCount[i] == 0) {
+ unused.add("$" + (i + 1));
+ }
+ }
+ String s = unused.size() == 1 ? "" : "s";
+ checkArgument(unused.isEmpty(), "unused argument%s: %s", s, Util.join(", ", unused));
+ }
return this;
}
diff --git a/src/test/java/com/squareup/javapoet/TypeSpecTest.java b/src/test/java/com/squareup/javapoet/TypeSpecTest.java
index ee86006..78abbe5 100644
--- a/src/test/java/com/squareup/javapoet/TypeSpecTest.java
+++ b/src/test/java/com/squareup/javapoet/TypeSpecTest.java
@@ -1914,7 +1914,19 @@ public final class TypeSpecTest {
CodeBlock.builder().add("$1L $2L", "a", "b", "c");
fail();
} catch (IllegalArgumentException expected) {
- assertThat(expected).hasMessage("unused arguments: expected 2, received 3");
+ assertThat(expected).hasMessage("unused argument: $3");
+ }
+ try {
+ CodeBlock.builder().add("$1L $1L $1L", "a", "b", "c");
+ fail();
+ } catch (IllegalArgumentException expected) {
+ assertThat(expected).hasMessage("unused arguments: $2, $3");
+ }
+ try {
+ CodeBlock.builder().add("$3L $1L $3L $1L $3L", "a", "b", "c", "d");
+ fail();
+ } catch (IllegalArgumentException expected) {
+ assertThat(expected).hasMessage("unused arguments: $2, $4");
}
}