Search in sources :

Example 11 with AnonymousClassExpr

use of com.google.api.generator.engine.ast.AnonymousClassExpr in project gapic-generator-java by googleapis.

the class JavaWriterVisitorTest method writeAnonymousClassExpr_withStatementsMethods.

@Test
public void writeAnonymousClassExpr_withStatementsMethods() {
    ConcreteReference ref = ConcreteReference.withClazz(Runnable.class);
    TypeNode type = TypeNode.withReference(ref);
    // [Constructing] private static final String s = "foo";
    Variable variable = createVariable("s", TypeNode.STRING);
    VariableExpr variableExpr = VariableExpr.builder().setScope(ScopeNode.PRIVATE).setIsDecl(true).setIsFinal(true).setIsStatic(true).setVariable(variable).build();
    ValueExpr valueExpr = ValueExpr.builder().setValue(StringObjectValue.withValue("foo")).build();
    AssignmentExpr assignmentExpr = AssignmentExpr.builder().setVariableExpr(variableExpr).setValueExpr(valueExpr).build();
    ExprStatement exprStatement = ExprStatement.withExpr(assignmentExpr);
    MethodDefinition methodDefinition = MethodDefinition.builder().setName("run").setIsOverride(true).setScope(ScopeNode.PUBLIC).setReturnType(TypeNode.VOID).setBody(Arrays.asList(ExprStatement.withExpr(createAssignmentExpr("x", "3", TypeNode.INT)))).build();
    AnonymousClassExpr anonymousClassExpr = AnonymousClassExpr.builder().setType(type).setStatements(Arrays.asList(exprStatement)).setMethods(Arrays.asList(methodDefinition)).build();
    anonymousClassExpr.accept(writerVisitor);
    String expected = LineFormatter.lines("new Runnable() {\n", "private static final String s = \"foo\";\n", "@Override\n", "public void run() {\n", "int x = 3;\n}\n\n}");
    assertEquals(expected, writerVisitor.write());
}
Also used : ValueExpr(com.google.api.generator.engine.ast.ValueExpr) Variable(com.google.api.generator.engine.ast.Variable) MethodDefinition(com.google.api.generator.engine.ast.MethodDefinition) ExprStatement(com.google.api.generator.engine.ast.ExprStatement) VariableExpr(com.google.api.generator.engine.ast.VariableExpr) TypeNode(com.google.api.generator.engine.ast.TypeNode) AssignmentExpr(com.google.api.generator.engine.ast.AssignmentExpr) ConcreteReference(com.google.api.generator.engine.ast.ConcreteReference) AnonymousClassExpr(com.google.api.generator.engine.ast.AnonymousClassExpr) Test(org.junit.Test)

Aggregations

AnonymousClassExpr (com.google.api.generator.engine.ast.AnonymousClassExpr)11 MethodDefinition (com.google.api.generator.engine.ast.MethodDefinition)10 TypeNode (com.google.api.generator.engine.ast.TypeNode)10 AssignmentExpr (com.google.api.generator.engine.ast.AssignmentExpr)8 VariableExpr (com.google.api.generator.engine.ast.VariableExpr)8 ConcreteReference (com.google.api.generator.engine.ast.ConcreteReference)6 ValueExpr (com.google.api.generator.engine.ast.ValueExpr)6 Expr (com.google.api.generator.engine.ast.Expr)5 ExprStatement (com.google.api.generator.engine.ast.ExprStatement)5 MethodInvocationExpr (com.google.api.generator.engine.ast.MethodInvocationExpr)5 NewObjectExpr (com.google.api.generator.engine.ast.NewObjectExpr)4 IOException (java.io.IOException)4 Test (org.junit.Test)4 Variable (com.google.api.generator.engine.ast.Variable)3 ArrayList (java.util.ArrayList)3 MonitoredResourceDescriptor (com.google.api.MonitoredResourceDescriptor)2 ApiFunction (com.google.api.core.ApiFunction)2 ApiFuture (com.google.api.core.ApiFuture)2 BetaApi (com.google.api.core.BetaApi)2 BatchingSettings (com.google.api.gax.batching.BatchingSettings)2