use of com.google.api.generator.engine.ast.Statement in project gapic-generator-java by googleapis.
the class AbstractServiceStubSettingsClassComposer method createClassConstructor.
private static MethodDefinition createClassConstructor(Service service, Map<String, VariableExpr> methodSettingsMemberVarExprs, TypeStore typeStore) {
TypeNode thisType = typeStore.get(ClassNames.getServiceStubSettingsClassName(service));
final VariableExpr settingsBuilderVarExpr = VariableExpr.withVariable(Variable.builder().setType(typeStore.get(NESTED_BUILDER_CLASS_NAME)).setName("settingsBuilder").build());
Expr superCtorExpr = ReferenceConstructorExpr.superBuilder().setType(FIXED_TYPESTORE.get("StubSettings")).setArguments(settingsBuilderVarExpr).build();
List<Statement> bodyStatements = new ArrayList<>();
bodyStatements.add(ExprStatement.withExpr(superCtorExpr));
bodyStatements.add(EMPTY_LINE_STATEMENT);
Function<Map.Entry<String, VariableExpr>, AssignmentExpr> varInitExprFn = e -> AssignmentExpr.builder().setVariableExpr(e.getValue()).setValueExpr(MethodInvocationExpr.builder().setExprReferenceExpr(MethodInvocationExpr.builder().setExprReferenceExpr(settingsBuilderVarExpr).setMethodName(e.getKey()).build()).setMethodName("build").setReturnType(e.getValue().type()).build()).build();
bodyStatements.addAll(methodSettingsMemberVarExprs.entrySet().stream().map(e -> ExprStatement.withExpr(varInitExprFn.apply(e))).collect(Collectors.toList()));
return MethodDefinition.constructorBuilder().setScope(ScopeNode.PROTECTED).setReturnType(thisType).setArguments(settingsBuilderVarExpr.toBuilder().setIsDecl(true).build()).setThrowsExceptions(Arrays.asList(TypeNode.withExceptionClazz(IOException.class))).setBody(bodyStatements).build();
}
use of com.google.api.generator.engine.ast.Statement in project gapic-generator-java by googleapis.
the class AbstractServiceStubSettingsClassComposer method createClassStatements.
private static List<Statement> createClassStatements(Service service, GapicServiceConfig serviceConfig, Map<String, VariableExpr> methodSettingsMemberVarExprs, Map<String, Message> messageTypes, TypeStore typeStore) {
Function<Expr, Statement> exprToStatementFn = e -> ExprStatement.withExpr(e);
List<Statement> statements = new ArrayList<>();
// Assign DEFAULT_SERVICE_SCOPES.
statements.add(SettingsCommentComposer.DEFAULT_SCOPES_COMMENT);
VariableExpr defaultServiceScopesDeclVarExpr = DEFAULT_SERVICE_SCOPES_VAR_EXPR.toBuilder().setIsDecl(true).setScope(ScopeNode.PRIVATE).setIsStatic(true).setIsFinal(true).build();
MethodInvocationExpr listBuilderExpr = MethodInvocationExpr.builder().setStaticReferenceType(FIXED_TYPESTORE.get("ImmutableList")).setGenerics(Arrays.asList(ConcreteReference.withClazz(String.class))).setMethodName("builder").build();
for (String serviceScope : service.oauthScopes()) {
listBuilderExpr = MethodInvocationExpr.builder().setExprReferenceExpr(listBuilderExpr).setMethodName("add").setArguments(ValueExpr.withValue(StringObjectValue.withValue(serviceScope))).build();
}
listBuilderExpr = MethodInvocationExpr.builder().setExprReferenceExpr(listBuilderExpr).setMethodName("build").setReturnType(DEFAULT_SERVICE_SCOPES_VAR_EXPR.type()).build();
statements.add(exprToStatementFn.apply(AssignmentExpr.builder().setVariableExpr(defaultServiceScopesDeclVarExpr).setValueExpr(listBuilderExpr).build()));
// Declare settings members.
statements.addAll(methodSettingsMemberVarExprs.values().stream().map(v -> exprToStatementFn.apply(v.toBuilder().setIsDecl(true).setScope(ScopeNode.PRIVATE).setIsFinal(true).build())).collect(Collectors.toList()));
statements.add(EMPTY_LINE_STATEMENT);
for (Expr pagingAssignExpr : createPagingStaticAssignExprs(service, serviceConfig, messageTypes, typeStore)) {
statements.add(exprToStatementFn.apply(pagingAssignExpr));
statements.add(EMPTY_LINE_STATEMENT);
}
for (Method method : service.methods()) {
Optional<GapicBatchingSettings> batchingSettingOpt = Objects.isNull(serviceConfig) ? Optional.empty() : serviceConfig.getBatchingSetting(service, method);
if (batchingSettingOpt.isPresent()) {
statements.add(exprToStatementFn.apply(BatchingDescriptorComposer.createBatchingDescriptorFieldDeclExpr(method, batchingSettingOpt.get(), messageTypes)));
}
statements.add(EMPTY_LINE_STATEMENT);
}
return statements;
}
use of com.google.api.generator.engine.ast.Statement in project gapic-generator-java by googleapis.
the class AbstractServiceStubSettingsClassComposer method createNestedClassCreateDefaultMethod.
protected MethodDefinition createNestedClassCreateDefaultMethod(TypeStore typeStore, String methodName, String defaultTransportChannelProvider, String defaultTransportChannelProviderBuilder, String defaultApiClientHeaderProviderBuilder) {
List<Statement> bodyStatements = new ArrayList<>();
// Initialize the builder: Builder builder = new Builder((ClientContext) null);
TypeNode builderType = typeStore.get(NESTED_BUILDER_CLASS_NAME);
VariableExpr builderVarExpr = VariableExpr.withVariable(Variable.builder().setType(builderType).setName("builder").build());
bodyStatements.add(ExprStatement.withExpr(AssignmentExpr.builder().setVariableExpr(builderVarExpr.toBuilder().setIsDecl(true).build()).setValueExpr(NewObjectExpr.builder().setType(builderType).setArguments(CastExpr.builder().setType(FIXED_TYPESTORE.get("ClientContext")).setExpr(ValueExpr.createNullExpr()).build()).build()).build()));
bodyStatements.add(EMPTY_LINE_STATEMENT);
List<Expr> bodyExprs = new ArrayList<>();
if (defaultTransportChannelProvider != null) {
bodyExprs.add(MethodInvocationExpr.builder().setExprReferenceExpr(builderVarExpr).setMethodName("setTransportChannelProvider").setArguments(MethodInvocationExpr.builder().setMethodName(defaultTransportChannelProvider).build()).build());
} else {
bodyExprs.add(MethodInvocationExpr.builder().setExprReferenceExpr(builderVarExpr).setMethodName("setTransportChannelProvider").setArguments(MethodInvocationExpr.builder().setExprReferenceExpr(MethodInvocationExpr.builder().setMethodName(defaultTransportChannelProviderBuilder).build()).setMethodName("build").build()).build());
}
bodyExprs.add(MethodInvocationExpr.builder().setExprReferenceExpr(builderVarExpr).setMethodName("setCredentialsProvider").setArguments(MethodInvocationExpr.builder().setExprReferenceExpr(MethodInvocationExpr.builder().setMethodName("defaultCredentialsProviderBuilder").build()).setMethodName("build").build()).build());
bodyExprs.add(MethodInvocationExpr.builder().setExprReferenceExpr(builderVarExpr).setMethodName("setInternalHeaderProvider").setArguments(MethodInvocationExpr.builder().setExprReferenceExpr(MethodInvocationExpr.builder().setMethodName(defaultApiClientHeaderProviderBuilder).build()).setMethodName("build").build()).build());
bodyExprs.add(MethodInvocationExpr.builder().setExprReferenceExpr(builderVarExpr).setMethodName("setEndpoint").setArguments(MethodInvocationExpr.builder().setMethodName("getDefaultEndpoint").build()).build());
bodyExprs.add(MethodInvocationExpr.builder().setExprReferenceExpr(builderVarExpr).setMethodName("setMtlsEndpoint").setArguments(MethodInvocationExpr.builder().setMethodName("getDefaultMtlsEndpoint").build()).build());
bodyExprs.add(MethodInvocationExpr.builder().setExprReferenceExpr(builderVarExpr).setMethodName("setSwitchToMtlsEndpointAllowed").setArguments(ValueExpr.withValue(PrimitiveValue.builder().setType(TypeNode.BOOLEAN).setValue("true").build())).build());
bodyStatements.addAll(bodyExprs.stream().map(e -> ExprStatement.withExpr(e)).collect(Collectors.toList()));
bodyStatements.add(EMPTY_LINE_STATEMENT);
Expr returnExpr = MethodInvocationExpr.builder().setMethodName("initDefaults").setArguments(builderVarExpr).setReturnType(builderType).build();
return MethodDefinition.builder().setScope(ScopeNode.PRIVATE).setIsStatic(true).setReturnType(builderType).setName(methodName).setBody(bodyStatements).setReturnExpr(returnExpr).build();
}
use of com.google.api.generator.engine.ast.Statement in project gapic-generator-java by googleapis.
the class JavaWriterVisitor method visit.
@Override
public void visit(IfStatement ifStatement) {
buffer.append(IF);
space();
leftParen();
ifStatement.conditionExpr().accept(this);
rightParen();
space();
leftBrace();
newline();
statements(ifStatement.body());
buffer.append(RIGHT_BRACE);
if (!ifStatement.elseIfs().isEmpty()) {
for (Map.Entry<Expr, List<Statement>> elseIfEntry : ifStatement.elseIfs().entrySet()) {
Expr elseIfConditionExpr = elseIfEntry.getKey();
List<Statement> elseIfBody = elseIfEntry.getValue();
space();
buffer.append(ELSE);
space();
buffer.append(IF);
space();
leftParen();
elseIfConditionExpr.accept(this);
rightParen();
space();
leftBrace();
newline();
statements(elseIfBody);
rightBrace();
}
}
if (!ifStatement.elseBody().isEmpty()) {
space();
buffer.append(ELSE);
space();
leftBrace();
newline();
statements(ifStatement.elseBody());
rightBrace();
}
newline();
}
use of com.google.api.generator.engine.ast.Statement in project gapic-generator-java by googleapis.
the class ResourceNameHelperClassComposer method createParseMethod.
private static MethodDefinition createParseMethod(TypeNode thisClassType, List<VariableExpr> templateFinalVarExprs, List<List<String>> tokenHierarchies, TypeStore typeStore) {
String formattedStringArgName = "formattedString";
VariableExpr formattedStringArgExpr = VariableExpr.withVariable(Variable.builder().setName(formattedStringArgName).setType(TypeNode.STRING).build());
String exceptionMessageString = String.format("%s.parse: %s not in valid format", thisClassType.reference().name(), formattedStringArgName);
ValueExpr exceptionMessageExpr = ValueExpr.withValue(StringObjectValue.withValue(exceptionMessageString));
TypeNode mapStringType = TypeNode.withReference(ConcreteReference.builder().setClazz(Map.class).setGenerics(Arrays.asList(ConcreteReference.withClazz(String.class), ConcreteReference.withClazz(String.class))).build());
VariableExpr matchMapVarExpr = VariableExpr.withVariable(Variable.builder().setName("matchMap").setType(mapStringType).build());
List<Statement> body = new ArrayList<>();
body.add(IfStatement.builder().setConditionExpr(MethodInvocationExpr.builder().setExprReferenceExpr(formattedStringArgExpr).setMethodName("isEmpty").setReturnType(TypeNode.BOOLEAN).build()).setBody(Arrays.asList(ExprStatement.withExpr(ReturnExpr.withExpr(ValueExpr.createNullExpr())))).build());
List<Expr> formattedStringArgList = Arrays.asList(formattedStringArgExpr);
List<VariableExpr> formattedStringArgDeclList = Arrays.asList(formattedStringArgExpr.toBuilder().setIsDecl(true).build());
boolean hasVariants = tokenHierarchies.size() > 1;
if (!hasVariants) {
List<Expr> methodArgs = Arrays.asList(formattedStringArgExpr, exceptionMessageExpr);
MethodInvocationExpr validatedMatchExpr = MethodInvocationExpr.builder().setExprReferenceExpr(templateFinalVarExprs.get(0)).setMethodName("validatedMatch").setArguments(methodArgs).setReturnType(mapStringType).build();
AssignmentExpr matchMapAssignExpr = AssignmentExpr.builder().setVariableExpr(matchMapVarExpr.toBuilder().setIsDecl(true).build()).setValueExpr(validatedMatchExpr).build();
body.add(ExprStatement.withExpr(matchMapAssignExpr));
List<Expr> ofMethodArgExprs = tokenHierarchies.get(0).stream().map(t -> MethodInvocationExpr.builder().setExprReferenceExpr(matchMapVarExpr).setMethodName("get").setArguments(Arrays.asList(ValueExpr.withValue(StringObjectValue.withValue(t)))).build()).collect(Collectors.toList());
MethodInvocationExpr ofMethodExpr = MethodInvocationExpr.builder().setMethodName("of").setArguments(ofMethodArgExprs).setReturnType(thisClassType).build();
return MethodDefinition.builder().setScope(ScopeNode.PUBLIC).setIsStatic(true).setReturnType(thisClassType).setName("parse").setArguments(formattedStringArgDeclList).setBody(body).setReturnExpr(ofMethodExpr).build();
}
IfStatement.Builder ifStatementBuilder = IfStatement.builder();
String ofMethodNamePattern = "of%sName";
for (int i = 0; i < tokenHierarchies.size(); i++) {
// PubSub special-case handling for the "_deleted-topic_" pattern.
boolean isDeletedTopicPattern = tokenHierarchies.get(i).contains(ResourceNameConstants.DELETED_TOPIC_LITERAL);
VariableExpr templateVarExpr = templateFinalVarExprs.get(i);
MethodInvocationExpr conditionExpr = MethodInvocationExpr.builder().setExprReferenceExpr(templateVarExpr).setMethodName(isDeletedTopicPattern ? "equals" : "matches").setArguments(formattedStringArgList).setReturnType(TypeNode.BOOLEAN).build();
MethodInvocationExpr matchValueExpr = MethodInvocationExpr.builder().setExprReferenceExpr(templateVarExpr).setMethodName("match").setArguments(formattedStringArgList).setReturnType(mapStringType).build();
AssignmentExpr matchMapAssignExpr = AssignmentExpr.builder().setVariableExpr(matchMapVarExpr.toBuilder().setIsDecl(true).build()).setValueExpr(matchValueExpr).build();
List<String> tokens = tokenHierarchies.get(i);
MethodInvocationExpr ofMethodExpr = MethodInvocationExpr.builder().setMethodName(String.format(ofMethodNamePattern, concatToUpperCamelCaseName(tokens))).setArguments(tokens.stream().map(t -> MethodInvocationExpr.builder().setExprReferenceExpr(matchMapVarExpr).setMethodName("get").setArguments(Arrays.asList(ValueExpr.withValue(StringObjectValue.withValue(t)))).build()).collect(Collectors.toList())).setReturnType(thisClassType).build();
ReturnExpr subReturnExpr = ReturnExpr.withExpr(ofMethodExpr);
List<Statement> ifStatements = Arrays.asList(matchMapAssignExpr, subReturnExpr).stream().map(e -> ExprStatement.withExpr(e)).collect(Collectors.toList());
if (i == 0) {
ifStatementBuilder = ifStatementBuilder.setConditionExpr(conditionExpr).setBody(ifStatements);
} else {
// _deleted-topic_.
if (isDeletedTopicPattern) {
ifStatements.clear();
ifStatements.add(ExprStatement.withExpr(ReturnExpr.withExpr(NewObjectExpr.builder().setType(thisClassType).setArguments(ValueExpr.withValue(StringObjectValue.withValue(ResourceNameConstants.DELETED_TOPIC_LITERAL))).build())));
}
ifStatementBuilder = ifStatementBuilder.addElseIf(conditionExpr, ifStatements);
}
}
body.add(ifStatementBuilder.build());
body.add(ExprStatement.withExpr(ThrowExpr.builder().setType(FIXED_TYPESTORE.get("ValidationException")).setMessageExpr(exceptionMessageString).build()));
return MethodDefinition.builder().setScope(ScopeNode.PUBLIC).setIsStatic(true).setReturnType(thisClassType).setName("parse").setArguments(formattedStringArgDeclList).setBody(body).build();
}
Aggregations