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());
}
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());
}
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());
}
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;
}
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;
}
Aggregations