Skip to content

Commit

Permalink
Add args to control flow methods.
Browse files Browse the repository at this point in the history
Since control flow statements often involve a field or local, a format string and variable(s) are likely in the calling code.
  • Loading branch information
JakeWharton committed Jan 26, 2014
1 parent f6a479c commit c268516
Showing 1 changed file with 6 additions and 6 deletions.
12 changes: 6 additions & 6 deletions src/main/java/com/squareup/javawriter/JavaWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -667,10 +667,10 @@ public JavaWriter emitStatement(String pattern, Object... args) throws IOExcepti
* @param controlFlow the control flow construct and its code, such as "if (foo == 5)". Shouldn't
* contain braces or newline characters.
*/
public JavaWriter beginControlFlow(String controlFlow) throws IOException {
public JavaWriter beginControlFlow(String controlFlow, Object... args) throws IOException {
checkInMethod();
indent();
out.write(controlFlow);
out.write(String.format(controlFlow, args));
out.write(" {\n");
scopes.push(Scope.CONTROL_FLOW);
return this;
Expand All @@ -680,12 +680,12 @@ public JavaWriter beginControlFlow(String controlFlow) throws IOException {
* @param controlFlow the control flow construct and its code, such as "else if (foo == 10)".
* Shouldn't contain braces or newline characters.
*/
public JavaWriter nextControlFlow(String controlFlow) throws IOException {
public JavaWriter nextControlFlow(String controlFlow, Object... args) throws IOException {
popScope(Scope.CONTROL_FLOW);
indent();
scopes.push(Scope.CONTROL_FLOW);
out.write("} ");
out.write(controlFlow);
out.write(String.format(controlFlow, args));
out.write(" {\n");
return this;
}
Expand All @@ -698,12 +698,12 @@ public JavaWriter endControlFlow() throws IOException {
* @param controlFlow the optional control flow construct and its code, such as
* "while(foo == 20)". Only used for "do/while" control flows.
*/
public JavaWriter endControlFlow(String controlFlow) throws IOException {
public JavaWriter endControlFlow(String controlFlow, Object... args) throws IOException {
popScope(Scope.CONTROL_FLOW);
indent();
if (controlFlow != null) {
out.write("} ");
out.write(controlFlow);
out.write(String.format(controlFlow, args));
out.write(";\n");
} else {
out.write("}\n");
Expand Down

2 comments on commit c268516

@solcott
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change breaks backward compatibility. Clients that were compiled against older versions of JavaWriter and that use these methods will get a NoSuchMethodError.

This for example means that I can't upgrade to the newest version of Dagger until other my other dependencies that depend on an old version of JavaWriter have updated to compile against JavaWriter 2.5.

@JakeWharton
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, crap. We can add overload without varargs that just pass a new Object[0].

Please sign in to comment.