Visitar URL original
integrate return into method definition (#369) · googleapis/sdk-platform-java@2cd9cf4 · GitHub
Skip to content
This repository was archived by the owner on Oct 29, 2026. It is now read-only.

Commit 2cd9cf4

Browse files
integrate return into method definition (#369)
1 parent 92703d8 commit 2cd9cf4

3 files changed

Lines changed: 25 additions & 14 deletions

File tree

‎src/main/java/com/google/api/generator/engine/ast/MethodDefinition.java‎

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public abstract class MethodDefinition implements AstNode {
6666
abstract ImmutableList<String> returnTemplateNames();
6767

6868
@Nullable
69-
public abstract Expr returnExpr();
69+
public abstract ReturnExpr returnExpr();
7070

7171
abstract boolean isOverride();
7272

@@ -147,7 +147,11 @@ public Builder setArguments(VariableExpr... arguments) {
147147

148148
public abstract Builder setBody(List<Statement> body);
149149

150-
public abstract Builder setReturnExpr(Expr returnExpr);
150+
public Builder setReturnExpr(Expr expr) {
151+
return setReturnExpr(ReturnExpr.withExpr(expr));
152+
}
153+
154+
public abstract Builder setReturnExpr(ReturnExpr returnExpr);
151155

152156
public abstract Builder setIsOverride(boolean isOverride);
153157

@@ -292,18 +296,14 @@ public MethodDefinition build() {
292296
}
293297

294298
if (method.returnExpr() != null && !isLastStatementThrowExpr) {
295-
// TODO(miraleung): Refactor this to use ReturnExpr under the covers instead.
296-
Preconditions.checkState(
297-
!(method.returnExpr() instanceof ReturnExpr),
298-
"A method's return expression can only consist of non-ReturnExpr expressions");
299299
if (method.returnType().isPrimitiveType()
300-
|| method.returnExpr().type().isPrimitiveType()) {
300+
|| method.returnExpr().expr().type().isPrimitiveType()) {
301301
Preconditions.checkState(
302-
method.returnType().equals((method.returnExpr().type())),
302+
method.returnType().equals((method.returnExpr().expr().type())),
303303
"Method return type does not match the return expression type");
304304
} else {
305305
Preconditions.checkState(
306-
method.returnType().isSupertypeOrEquals(method.returnExpr().type()),
306+
method.returnType().isSupertypeOrEquals(method.returnExpr().expr().type()),
307307
"Method reference return type is not a supertype of the return expression type");
308308
}
309309
}

‎src/main/java/com/google/api/generator/engine/writer/JavaWriterVisitor.java‎

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -767,11 +767,7 @@ public void visit(MethodDefinition methodDefinition) {
767767
newline();
768768
statements(methodDefinition.body());
769769
if (methodDefinition.returnExpr() != null) {
770-
buffer.append(RETURN);
771-
space();
772-
methodDefinition.returnExpr().accept(this);
773-
semicolon();
774-
newline();
770+
ExprStatement.withExpr(methodDefinition.returnExpr()).accept(this);
775771
}
776772

777773
rightBrace();

‎src/test/java/com/google/api/generator/engine/ast/MethodDefinitionTest.java‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,21 @@ public void validMethodDefinition_boxedReturnType() {
279279
.build();
280280
}
281281

282+
@Test
283+
public void validMethodDefinition_setReturnExprUsingReturnExpr() {
284+
ReturnExpr returnExpr =
285+
ReturnExpr.withExpr(
286+
ValueExpr.withValue(
287+
PrimitiveValue.builder().setType(TypeNode.INT).setValue("3").build()));
288+
MethodDefinition.builder()
289+
.setName("foobar")
290+
.setScope(ScopeNode.PUBLIC)
291+
.setReturnType(TypeNode.INT_OBJECT)
292+
.setBody(Arrays.asList(ExprStatement.withExpr(createAssignmentExpr())))
293+
.setReturnExpr(returnExpr)
294+
.build();
295+
}
296+
282297
@Test
283298
public void invalidMethodDefinition_badTemplateName() {
284299
assertThrows(

0 commit comments

Comments
 (0)