Search in sources :

Example 81 with Expr

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

the class JavaWriterVisitorTest method writeAssignmentExpr_varToVar.

@Test
public void writeAssignmentExpr_varToVar() {
    Variable variable = Variable.builder().setName("foobar").setType(TypeNode.INT).build();
    VariableExpr variableExpr = VariableExpr.builder().setVariable(variable).setIsStatic(true).setIsFinal(true).setScope(ScopeNode.PRIVATE).setIsDecl(true).build();
    Variable anotherVariable = Variable.builder().setName("y").setType(TypeNode.INT).build();
    Expr valueExpr = VariableExpr.builder().setVariable(anotherVariable).build();
    AssignmentExpr assignExpr = AssignmentExpr.builder().setVariableExpr(variableExpr).setValueExpr(valueExpr).build();
    assignExpr.accept(writerVisitor);
    assertEquals("private static final int foobar = y", writerVisitor.write());
}
Also used : Variable(com.google.api.generator.engine.ast.Variable) ReferenceConstructorExpr(com.google.api.generator.engine.ast.ReferenceConstructorExpr) LogicalOperationExpr(com.google.api.generator.engine.ast.LogicalOperationExpr) ValueExpr(com.google.api.generator.engine.ast.ValueExpr) EnumRefExpr(com.google.api.generator.engine.ast.EnumRefExpr) ReturnExpr(com.google.api.generator.engine.ast.ReturnExpr) NewObjectExpr(com.google.api.generator.engine.ast.NewObjectExpr) Expr(com.google.api.generator.engine.ast.Expr) TernaryExpr(com.google.api.generator.engine.ast.TernaryExpr) RelationalOperationExpr(com.google.api.generator.engine.ast.RelationalOperationExpr) UnaryOperationExpr(com.google.api.generator.engine.ast.UnaryOperationExpr) AssignmentExpr(com.google.api.generator.engine.ast.AssignmentExpr) VariableExpr(com.google.api.generator.engine.ast.VariableExpr) InstanceofExpr(com.google.api.generator.engine.ast.InstanceofExpr) CastExpr(com.google.api.generator.engine.ast.CastExpr) ArithmeticOperationExpr(com.google.api.generator.engine.ast.ArithmeticOperationExpr) AnonymousClassExpr(com.google.api.generator.engine.ast.AnonymousClassExpr) LambdaExpr(com.google.api.generator.engine.ast.LambdaExpr) AssignmentOperationExpr(com.google.api.generator.engine.ast.AssignmentOperationExpr) MethodInvocationExpr(com.google.api.generator.engine.ast.MethodInvocationExpr) ThrowExpr(com.google.api.generator.engine.ast.ThrowExpr) VariableExpr(com.google.api.generator.engine.ast.VariableExpr) AssignmentExpr(com.google.api.generator.engine.ast.AssignmentExpr) Test(org.junit.Test)

Example 82 with Expr

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

the class JavaWriterVisitorTest method writeAssignmentExpr_nullObjectValueReferenceType.

@Test
public void writeAssignmentExpr_nullObjectValueReferenceType() {
    Variable variable = Variable.builder().setName("x").setType(TypeNode.STRING).build();
    VariableExpr variableExpr = VariableExpr.builder().setVariable(variable).setIsDecl(true).build();
    Value value = NullObjectValue.create();
    Expr valueExpr = ValueExpr.builder().setValue(value).build();
    AssignmentExpr assignExpr = AssignmentExpr.builder().setVariableExpr(variableExpr).setValueExpr(valueExpr).build();
    assignExpr.accept(writerVisitor);
    assertEquals("String x = null", writerVisitor.write());
}
Also used : Variable(com.google.api.generator.engine.ast.Variable) ReferenceConstructorExpr(com.google.api.generator.engine.ast.ReferenceConstructorExpr) LogicalOperationExpr(com.google.api.generator.engine.ast.LogicalOperationExpr) ValueExpr(com.google.api.generator.engine.ast.ValueExpr) EnumRefExpr(com.google.api.generator.engine.ast.EnumRefExpr) ReturnExpr(com.google.api.generator.engine.ast.ReturnExpr) NewObjectExpr(com.google.api.generator.engine.ast.NewObjectExpr) Expr(com.google.api.generator.engine.ast.Expr) TernaryExpr(com.google.api.generator.engine.ast.TernaryExpr) RelationalOperationExpr(com.google.api.generator.engine.ast.RelationalOperationExpr) UnaryOperationExpr(com.google.api.generator.engine.ast.UnaryOperationExpr) AssignmentExpr(com.google.api.generator.engine.ast.AssignmentExpr) VariableExpr(com.google.api.generator.engine.ast.VariableExpr) InstanceofExpr(com.google.api.generator.engine.ast.InstanceofExpr) CastExpr(com.google.api.generator.engine.ast.CastExpr) ArithmeticOperationExpr(com.google.api.generator.engine.ast.ArithmeticOperationExpr) AnonymousClassExpr(com.google.api.generator.engine.ast.AnonymousClassExpr) LambdaExpr(com.google.api.generator.engine.ast.LambdaExpr) AssignmentOperationExpr(com.google.api.generator.engine.ast.AssignmentOperationExpr) MethodInvocationExpr(com.google.api.generator.engine.ast.MethodInvocationExpr) ThrowExpr(com.google.api.generator.engine.ast.ThrowExpr) ThisObjectValue(com.google.api.generator.engine.ast.ThisObjectValue) StringObjectValue(com.google.api.generator.engine.ast.StringObjectValue) PrimitiveValue(com.google.api.generator.engine.ast.PrimitiveValue) NullObjectValue(com.google.api.generator.engine.ast.NullObjectValue) Value(com.google.api.generator.engine.ast.Value) SuperObjectValue(com.google.api.generator.engine.ast.SuperObjectValue) VariableExpr(com.google.api.generator.engine.ast.VariableExpr) AssignmentExpr(com.google.api.generator.engine.ast.AssignmentExpr) Test(org.junit.Test)

Example 83 with Expr

use of com.google.api.generator.engine.ast.Expr 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());
}
Also used : ReferenceConstructorExpr(com.google.api.generator.engine.ast.ReferenceConstructorExpr) LogicalOperationExpr(com.google.api.generator.engine.ast.LogicalOperationExpr) ValueExpr(com.google.api.generator.engine.ast.ValueExpr) EnumRefExpr(com.google.api.generator.engine.ast.EnumRefExpr) ReturnExpr(com.google.api.generator.engine.ast.ReturnExpr) NewObjectExpr(com.google.api.generator.engine.ast.NewObjectExpr) Expr(com.google.api.generator.engine.ast.Expr) TernaryExpr(com.google.api.generator.engine.ast.TernaryExpr) RelationalOperationExpr(com.google.api.generator.engine.ast.RelationalOperationExpr) UnaryOperationExpr(com.google.api.generator.engine.ast.UnaryOperationExpr) AssignmentExpr(com.google.api.generator.engine.ast.AssignmentExpr) VariableExpr(com.google.api.generator.engine.ast.VariableExpr) InstanceofExpr(com.google.api.generator.engine.ast.InstanceofExpr) CastExpr(com.google.api.generator.engine.ast.CastExpr) ArithmeticOperationExpr(com.google.api.generator.engine.ast.ArithmeticOperationExpr) AnonymousClassExpr(com.google.api.generator.engine.ast.AnonymousClassExpr) LambdaExpr(com.google.api.generator.engine.ast.LambdaExpr) AssignmentOperationExpr(com.google.api.generator.engine.ast.AssignmentOperationExpr) MethodInvocationExpr(com.google.api.generator.engine.ast.MethodInvocationExpr) ThrowExpr(com.google.api.generator.engine.ast.ThrowExpr) ThrowExpr(com.google.api.generator.engine.ast.ThrowExpr) TypeNode(com.google.api.generator.engine.ast.TypeNode) Test(org.junit.Test)

Example 84 with Expr

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

the class AbstractServiceClientClassComposer method createOperationsClientAssignExprs.

private List<AssignmentExpr> createOperationsClientAssignExprs(Expr thisExpr, VariableExpr stubVarExpr) {
    List<AssignmentExpr> operationsClientAssignExprs = new ArrayList<>();
    Iterator<TypeNode> opClientTypesIt = getTransportContext().operationsClientTypes().iterator();
    Iterator<String> opClientNamesIt = getTransportContext().operationsClientNames().iterator();
    Iterator<String> opStubNamesIt = getTransportContext().transportOperationsStubNames().iterator();
    while (opClientTypesIt.hasNext() && opClientNamesIt.hasNext() && opStubNamesIt.hasNext()) {
        TypeNode operationsClientType = opClientTypesIt.next();
        String opClientName = opClientNamesIt.next();
        String opStubName = opStubNamesIt.next();
        VariableExpr operationsClientVarExpr = VariableExpr.withVariable(Variable.builder().setType(operationsClientType).setName(opClientName).build());
        String operationsStubGetterName = String.format("get%s", JavaStyle.toUpperCamelCase(opStubName));
        Expr clientArgExpr = MethodInvocationExpr.builder().setExprReferenceExpr(stubVarExpr.toBuilder().setExprReferenceExpr(thisExpr).build()).setMethodName(operationsStubGetterName).build();
        AssignmentExpr operationsClientAssignExpr = AssignmentExpr.builder().setVariableExpr(operationsClientVarExpr.toBuilder().setExprReferenceExpr(thisExpr).build()).setValueExpr(MethodInvocationExpr.builder().setStaticReferenceType(operationsClientType).setMethodName("create").setArguments(clientArgExpr).setReturnType(operationsClientVarExpr.type()).build()).build();
        operationsClientAssignExprs.add(operationsClientAssignExpr);
    }
    return operationsClientAssignExprs;
}
Also used : ReferenceConstructorExpr(com.google.api.generator.engine.ast.ReferenceConstructorExpr) ValueExpr(com.google.api.generator.engine.ast.ValueExpr) NewObjectExpr(com.google.api.generator.engine.ast.NewObjectExpr) Expr(com.google.api.generator.engine.ast.Expr) TernaryExpr(com.google.api.generator.engine.ast.TernaryExpr) RelationalOperationExpr(com.google.api.generator.engine.ast.RelationalOperationExpr) AssignmentExpr(com.google.api.generator.engine.ast.AssignmentExpr) VariableExpr(com.google.api.generator.engine.ast.VariableExpr) CastExpr(com.google.api.generator.engine.ast.CastExpr) LambdaExpr(com.google.api.generator.engine.ast.LambdaExpr) MethodInvocationExpr(com.google.api.generator.engine.ast.MethodInvocationExpr) ArrayList(java.util.ArrayList) VariableExpr(com.google.api.generator.engine.ast.VariableExpr) AssignmentExpr(com.google.api.generator.engine.ast.AssignmentExpr) TypeNode(com.google.api.generator.engine.ast.TypeNode)

Example 85 with Expr

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

the class AbstractServiceClientClassComposer method createConstructorMethods.

private List<MethodDefinition> createConstructorMethods(Service service, TypeStore typeStore, boolean hasLroClient) {
    List<MethodDefinition> methods = new ArrayList<>();
    String thisClientName = ClassNames.getServiceClientClassName(service);
    String settingsName = ClassNames.getServiceSettingsClassName(service);
    TypeNode thisClassType = typeStore.get(thisClientName);
    TypeNode stubSettingsType = typeStore.get(ClassNames.getServiceStubSettingsClassName(service));
    TypeNode exceptionType = typeStore.get("IOException");
    VariableExpr settingsVarExpr = VariableExpr.withVariable(Variable.builder().setName("settings").setType(typeStore.get(settingsName)).build());
    VariableExpr stubVarExpr = VariableExpr.withVariable(Variable.builder().setType(typeStore.get(ClassNames.getServiceStubClassName(service))).setName("stub").build());
    // Create the ServiceClient(ServiceSettings settings) ctor.
    List<Expr> ctorAssignmentExprs = new ArrayList<>();
    Expr thisExpr = ValueExpr.withValue(ThisObjectValue.withType(thisClassType));
    ctorAssignmentExprs.add(AssignmentExpr.builder().setVariableExpr(settingsVarExpr.toBuilder().setExprReferenceExpr(thisExpr).build()).setValueExpr(settingsVarExpr).build());
    ctorAssignmentExprs.add(AssignmentExpr.builder().setVariableExpr(stubVarExpr.toBuilder().setExprReferenceExpr(thisExpr).build()).setValueExpr(MethodInvocationExpr.builder().setExprReferenceExpr(CastExpr.builder().setType(stubSettingsType).setExpr(MethodInvocationExpr.builder().setExprReferenceExpr(settingsVarExpr).setMethodName("getStubSettings").setReturnType(stubSettingsType).build()).build()).setMethodName("createStub").setReturnType(stubVarExpr.type()).build()).build());
    List<AssignmentExpr> operationsClientAssignExprs = createOperationsClientAssignExprs(thisExpr, stubVarExpr);
    if (hasLroClient) {
        ctorAssignmentExprs.addAll(operationsClientAssignExprs);
    }
    methods.add(MethodDefinition.constructorBuilder().setHeaderCommentStatements(ServiceClientCommentComposer.createProtectedCtorSettingsArgComment(ClassNames.getServiceClientClassName(service))).setScope(ScopeNode.PROTECTED).setReturnType(thisClassType).setArguments(settingsVarExpr.toBuilder().setIsDecl(true).build()).setThrowsExceptions(Arrays.asList(exceptionType)).setBody(ctorAssignmentExprs.stream().map(e -> ExprStatement.withExpr(e)).collect(Collectors.toList())).build());
    // Create the ServiceClient(ServiceStub stub) ctor.
    ctorAssignmentExprs.clear();
    ctorAssignmentExprs.add(AssignmentExpr.builder().setVariableExpr(settingsVarExpr.toBuilder().setExprReferenceExpr(thisExpr).build()).setValueExpr(ValueExpr.createNullExpr()).build());
    ctorAssignmentExprs.add(AssignmentExpr.builder().setVariableExpr(stubVarExpr.toBuilder().setExprReferenceExpr(thisExpr).build()).setValueExpr(stubVarExpr).build());
    if (hasLroClient) {
        ctorAssignmentExprs.addAll(operationsClientAssignExprs);
    }
    AnnotationNode betaAnnotation = AnnotationNode.builder().setType(typeStore.get("BetaApi")).setDescription("A restructuring of stub classes is planned, so this may break in the future").build();
    methods.add(MethodDefinition.constructorBuilder().setAnnotations(Arrays.asList(betaAnnotation)).setScope(ScopeNode.PROTECTED).setReturnType(thisClassType).setArguments(stubVarExpr.toBuilder().setIsDecl(true).build()).setBody(ctorAssignmentExprs.stream().map(e -> ExprStatement.withExpr(e)).collect(Collectors.toList())).build());
    return methods;
}
Also used : ReferenceConstructorExpr(com.google.api.generator.engine.ast.ReferenceConstructorExpr) ValueExpr(com.google.api.generator.engine.ast.ValueExpr) NewObjectExpr(com.google.api.generator.engine.ast.NewObjectExpr) Expr(com.google.api.generator.engine.ast.Expr) TernaryExpr(com.google.api.generator.engine.ast.TernaryExpr) RelationalOperationExpr(com.google.api.generator.engine.ast.RelationalOperationExpr) AssignmentExpr(com.google.api.generator.engine.ast.AssignmentExpr) VariableExpr(com.google.api.generator.engine.ast.VariableExpr) CastExpr(com.google.api.generator.engine.ast.CastExpr) LambdaExpr(com.google.api.generator.engine.ast.LambdaExpr) MethodInvocationExpr(com.google.api.generator.engine.ast.MethodInvocationExpr) AnnotationNode(com.google.api.generator.engine.ast.AnnotationNode) MethodDefinition(com.google.api.generator.engine.ast.MethodDefinition) ArrayList(java.util.ArrayList) VariableExpr(com.google.api.generator.engine.ast.VariableExpr) TypeNode(com.google.api.generator.engine.ast.TypeNode) AssignmentExpr(com.google.api.generator.engine.ast.AssignmentExpr)

Aggregations

Expr (com.google.api.generator.engine.ast.Expr)151 VariableExpr (com.google.api.generator.engine.ast.VariableExpr)130 MethodInvocationExpr (com.google.api.generator.engine.ast.MethodInvocationExpr)126 AssignmentExpr (com.google.api.generator.engine.ast.AssignmentExpr)124 ValueExpr (com.google.api.generator.engine.ast.ValueExpr)114 NewObjectExpr (com.google.api.generator.engine.ast.NewObjectExpr)96 TypeNode (com.google.api.generator.engine.ast.TypeNode)80 ExprStatement (com.google.api.generator.engine.ast.ExprStatement)72 ArrayList (java.util.ArrayList)72 RelationalOperationExpr (com.google.api.generator.engine.ast.RelationalOperationExpr)67 Statement (com.google.api.generator.engine.ast.Statement)64 CastExpr (com.google.api.generator.engine.ast.CastExpr)59 TernaryExpr (com.google.api.generator.engine.ast.TernaryExpr)59 AnonymousClassExpr (com.google.api.generator.engine.ast.AnonymousClassExpr)58 List (java.util.List)58 EnumRefExpr (com.google.api.generator.engine.ast.EnumRefExpr)55 Variable (com.google.api.generator.engine.ast.Variable)54 ThrowExpr (com.google.api.generator.engine.ast.ThrowExpr)51 ReferenceConstructorExpr (com.google.api.generator.engine.ast.ReferenceConstructorExpr)49 ReturnExpr (com.google.api.generator.engine.ast.ReturnExpr)47