use of com.google.api.generator.engine.ast.ThrowExpr in project gapic-generator-java by googleapis.
the class JavaWriterVisitorTest method writeThrowExpr_messageExpr.
@Test
public void writeThrowExpr_messageExpr() {
TypeNode npeType = TypeNode.withExceptionClazz(NullPointerException.class);
Expr messageExpr = MethodInvocationExpr.builder().setMethodName("foobar").setReturnType(TypeNode.STRING).build();
ThrowExpr throwExpr = ThrowExpr.builder().setType(npeType).setMessageExpr(messageExpr).build();
throwExpr.accept(writerVisitor);
assertEquals("throw new NullPointerException(foobar())", writerVisitor.write());
}
use of com.google.api.generator.engine.ast.ThrowExpr in project gapic-generator-java by googleapis.
the class JavaWriterVisitorTest method writeThrowExpr_basicThrowExpr.
@Test
public void writeThrowExpr_basicThrowExpr() {
Expr exprToThrow = MethodInvocationExpr.builder().setStaticReferenceType(TypeNode.withReference(ConcreteReference.withClazz(Statement.class))).setMethodName("createException").setReturnType(TypeNode.withExceptionClazz(Exception.class)).build();
ThrowExpr throwExpr = ThrowExpr.builder().setThrowExpr(exprToThrow).build();
throwExpr.accept(writerVisitor);
assertEquals("throw Statement.createException()", writerVisitor.write());
}
use of com.google.api.generator.engine.ast.ThrowExpr in project gapic-generator-java by googleapis.
the class ImportWriterVisitorTest method writeThrowExprImports_basic.
@Test
public void writeThrowExprImports_basic() {
TypeNode exceptionTypes = TypeNode.withReference(ConcreteReference.withClazz(IOException.class));
String message = "Some message asdf";
ThrowExpr throwExpr = ThrowExpr.builder().setType(exceptionTypes).setMessageExpr(message).build();
throwExpr.accept(writerVisitor);
assertEquals("import java.io.IOException;\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_messageAndCauseExpr.
@Test
public void writeThrowExprImports_messageAndCauseExpr() {
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).setCauseExpr(NewObjectExpr.builder().setType(TypeNode.withExceptionClazz(FileNotFoundException.class)).build()).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", "import java.io.FileNotFoundException;\n\n"), writerVisitor.write());
}
use of com.google.api.generator.engine.ast.ThrowExpr in project gapic-generator-java by googleapis.
the class JavaWriterVisitorTest method writeThrowExpr_basicWithCause.
@Test
public void writeThrowExpr_basicWithCause() {
TypeNode npeType = TypeNode.withReference(ConcreteReference.withClazz(NullPointerException.class));
ThrowExpr throwExpr = ThrowExpr.builder().setType(npeType).setCauseExpr(NewObjectExpr.builder().setType(TypeNode.withReference(ConcreteReference.withClazz(Throwable.class))).build()).build();
throwExpr.accept(writerVisitor);
assertEquals("throw new NullPointerException(new Throwable())", writerVisitor.write());
}
Aggregations