use of com.google.api.generator.engine.ast.ThrowExpr in project gapic-generator-java by googleapis.
the class JavaWriterVisitorTest method writeThrowExpr_basic.
@Test
public void writeThrowExpr_basic() {
TypeNode npeType = TypeNode.withReference(ConcreteReference.withClazz(NullPointerException.class));
ThrowExpr throwExpr = ThrowExpr.builder().setType(npeType).build();
throwExpr.accept(writerVisitor);
assertEquals("throw new NullPointerException()", writerVisitor.write());
}
use of com.google.api.generator.engine.ast.ThrowExpr in project gapic-generator-java by googleapis.
the class JavaWriterVisitorTest method writeThrowExpr_messageAndCauseExpr.
@Test
public void writeThrowExpr_messageAndCauseExpr() {
TypeNode npeType = TypeNode.withExceptionClazz(NullPointerException.class);
Expr messageExpr = MethodInvocationExpr.builder().setMethodName("foobar").setReturnType(TypeNode.STRING).build();
ThrowExpr throwExpr = ThrowExpr.builder().setType(npeType).setMessageExpr(messageExpr).setCauseExpr(NewObjectExpr.builder().setType(TypeNode.withReference(ConcreteReference.withClazz(Throwable.class))).build()).build();
throwExpr.accept(writerVisitor);
assertEquals("throw new NullPointerException(foobar(), new Throwable())", writerVisitor.write());
}
use of com.google.api.generator.engine.ast.ThrowExpr in project gapic-generator-java by googleapis.
the class JavaWriterVisitorTest method writeThrowExpr_basicWithMessage.
@Test
public void writeThrowExpr_basicWithMessage() {
TypeNode npeType = TypeNode.withReference(ConcreteReference.withClazz(NullPointerException.class));
String message = "Some message asdf";
ThrowExpr throwExpr = ThrowExpr.builder().setType(npeType).setMessageExpr(message).build();
throwExpr.accept(writerVisitor);
assertEquals("throw new NullPointerException(\"Some message asdf\")", writerVisitor.write());
}
use of com.google.api.generator.engine.ast.ThrowExpr in project gapic-generator-java by googleapis.
the class ImportWriterVisitorTest method writeThrowExprImports_messageExpr.
@Test
public void writeThrowExprImports_messageExpr() {
TypeNode npeType = TypeNode.withExceptionClazz(NullPointerException.class);
Expr messageExpr = MethodInvocationExpr.builder().setStaticReferenceType(TypeNode.withReference(ConcreteReference.withClazz(IfStatement.class))).setMethodName("conditionExpr").setReturnType(TypeNode.withReference(ConcreteReference.withClazz(Expr.class))).build();
messageExpr = MethodInvocationExpr.builder().setExprReferenceExpr(messageExpr).setMethodName("foobar").setReturnType(TypeNode.STRING).build();
ThrowExpr throwExpr = ThrowExpr.builder().setType(npeType).setMessageExpr(messageExpr).build();
throwExpr.accept(writerVisitor);
assertEquals(LineFormatter.lines("import com.google.api.generator.engine.ast.Expr;\n", "import com.google.api.generator.engine.ast.IfStatement;\n\n"), writerVisitor.write());
}
use of com.google.api.generator.engine.ast.ThrowExpr in project gapic-generator-java by googleapis.
the class ImportWriterVisitorTest method writeThrowExprImports_throwExpr.
@Test
public void writeThrowExprImports_throwExpr() {
Expr exprToThrow = MethodInvocationExpr.builder().setStaticReferenceType(TypeNode.withReference(ConcreteReference.withClazz(Statement.class))).setMethodName("createException").setReturnType(TypeNode.withExceptionClazz(Exception.class)).build();
TypeNode ignoredExceptionType = TypeNode.withReference(ConcreteReference.withClazz(IOException.class));
ThrowExpr throwExpr = ThrowExpr.builder().setType(ignoredExceptionType).setThrowExpr(exprToThrow).build();
throwExpr.accept(writerVisitor);
assertEquals(LineFormatter.lines("import com.google.api.generator.engine.ast.Statement;\n\n"), writerVisitor.write());
}
Aggregations