use of com.google.api.generator.engine.ast.MethodDefinition in project gapic-generator-java by googleapis.
the class ImportWriterVisitor method visit.
@Override
public void visit(ClassDefinition classDefinition) {
annotations(classDefinition.annotations());
types(classDefinition.implementsTypes());
if (classDefinition.extendsType() != null) {
classDefinition.extendsType().accept(this);
}
statements(classDefinition.statements());
for (MethodDefinition method : classDefinition.methods()) {
method.accept(this);
}
for (ClassDefinition nestedClass : classDefinition.nestedClasses()) {
nestedClass.accept(this);
}
}
use of com.google.api.generator.engine.ast.MethodDefinition in project gapic-generator-java by googleapis.
the class ServiceClientTestClassComposer method createSetUpMethod.
@Override
protected MethodDefinition createSetUpMethod(Service service, Map<String, VariableExpr> classMemberVarExprs, TypeStore typeStore) {
VariableExpr clientVarExpr = classMemberVarExprs.get(CLIENT_VAR_NAME);
VariableExpr serviceHelperVarExpr = classMemberVarExprs.get(SERVICE_HELPER_VAR_NAME);
VariableExpr channelProviderVarExpr = classMemberVarExprs.get(CHANNEL_PROVIDER_VAR_NAME);
Expr resetServiceHelperExpr = MethodInvocationExpr.builder().setExprReferenceExpr(serviceHelperVarExpr).setMethodName("reset").build();
Expr channelProviderInitExpr = AssignmentExpr.builder().setVariableExpr(channelProviderVarExpr).setValueExpr(MethodInvocationExpr.builder().setExprReferenceExpr(serviceHelperVarExpr).setMethodName("createChannelProvider").setReturnType(channelProviderVarExpr.type()).build()).build();
TypeNode settingsType = typeStore.get(ClassNames.getServiceSettingsClassName(service));
VariableExpr localSettingsVarExpr = VariableExpr.withVariable(Variable.builder().setName("settings").setType(settingsType).build());
Expr settingsBuilderExpr = MethodInvocationExpr.builder().setStaticReferenceType(settingsType).setMethodName("newBuilder").build();
Function<Expr, BiFunction<String, Expr, MethodInvocationExpr>> methodBuilderFn = methodExpr -> (mName, argExpr) -> MethodInvocationExpr.builder().setExprReferenceExpr(methodExpr).setMethodName(mName).setArguments(Arrays.asList(argExpr)).build();
settingsBuilderExpr = methodBuilderFn.apply(settingsBuilderExpr).apply("setTransportChannelProvider", classMemberVarExprs.get(CHANNEL_PROVIDER_VAR_NAME));
settingsBuilderExpr = methodBuilderFn.apply(settingsBuilderExpr).apply("setCredentialsProvider", MethodInvocationExpr.builder().setStaticReferenceType(FIXED_TYPESTORE.get("NoCredentialsProvider")).setMethodName("create").build());
settingsBuilderExpr = MethodInvocationExpr.builder().setExprReferenceExpr(settingsBuilderExpr).setMethodName("build").setReturnType(localSettingsVarExpr.type()).build();
Expr initLocalSettingsExpr = AssignmentExpr.builder().setVariableExpr(localSettingsVarExpr.toBuilder().setIsDecl(true).build()).setValueExpr(settingsBuilderExpr).build();
Expr initClientExpr = AssignmentExpr.builder().setVariableExpr(clientVarExpr).setValueExpr(MethodInvocationExpr.builder().setStaticReferenceType(typeStore.get(ClassNames.getServiceClientClassName(service))).setMethodName("create").setArguments(Arrays.asList(localSettingsVarExpr)).setReturnType(clientVarExpr.type()).build()).build();
return MethodDefinition.builder().setAnnotations(Arrays.asList(AnnotationNode.withType(FIXED_TYPESTORE.get("Before")))).setScope(ScopeNode.PUBLIC).setReturnType(TypeNode.VOID).setName("setUp").setThrowsExceptions(Arrays.asList(FIXED_TYPESTORE.get("IOException"))).setBody(Arrays.asList(resetServiceHelperExpr, channelProviderInitExpr, initLocalSettingsExpr, initClientExpr).stream().map(e -> ExprStatement.withExpr(e)).collect(Collectors.toList())).build();
}
use of com.google.api.generator.engine.ast.MethodDefinition in project gapic-generator-java by googleapis.
the class ResourceNameHelperClassComposer method createGetFieldValuesMapMethod.
private static MethodDefinition createGetFieldValuesMapMethod(ResourceName resourceName, TypeNode thisClassType, Map<String, VariableExpr> patternTokenVarExprs, List<List<String>> tokenHierarchies) {
Reference strRef = TypeNode.STRING.reference();
TypeNode mapBuilderType = TypeNode.withReference(ConcreteReference.builder().setClazz(ImmutableMap.Builder.class).setGenerics(Arrays.asList(strRef, strRef)).build());
VariableExpr fieldMapBuilderVarExpr = VariableExpr.withVariable(Variable.builder().setName("fieldMapBuilder").setType(mapBuilderType).build());
AssignmentExpr builderAssignExpr = AssignmentExpr.builder().setVariableExpr(fieldMapBuilderVarExpr.toBuilder().setIsDecl(true).build()).setValueExpr(MethodInvocationExpr.builder().setStaticReferenceType(FIXED_TYPESTORE.get("ImmutableMap")).setMethodName("builder").setReturnType(mapBuilderType).build()).build();
// Special-cased PubSub handling.
List<List<String>> processedTokenHierarchies = tokenHierarchies.stream().filter(tokens -> !tokens.contains(ResourceNameConstants.DELETED_TOPIC_LITERAL)).collect(Collectors.toList());
// Innermost if-blocks.
List<Statement> tokenIfStatements = new ArrayList<>();
for (String token : getTokenSet(processedTokenHierarchies)) {
VariableExpr tokenVarExpr = patternTokenVarExprs.get(token);
Preconditions.checkNotNull(tokenVarExpr, String.format("No variable found for %s among %s", token, patternTokenVarExprs.keySet()));
StringObjectValue tokenStrVal = StringObjectValue.withValue(token);
MethodInvocationExpr putExpr = MethodInvocationExpr.builder().setExprReferenceExpr(fieldMapBuilderVarExpr).setMethodName("put").setArguments(ValueExpr.withValue(tokenStrVal), tokenVarExpr).build();
Expr notNullCheckExpr = RelationalOperationExpr.notEqualToWithExprs(tokenVarExpr, ValueExpr.createNullExpr());
tokenIfStatements.add(IfStatement.builder().setConditionExpr(notNullCheckExpr).setBody(Arrays.asList(ExprStatement.withExpr(putExpr))).build());
}
// Put the innermost if-statements and assignment expressions together.
VariableExpr fieldValuesMapVarExpr = FIXED_CLASS_VARS.get("fieldValuesMap");
AssignmentExpr fieldValuesMapAssignExpr = AssignmentExpr.builder().setVariableExpr(fieldValuesMapVarExpr).setValueExpr(MethodInvocationExpr.builder().setExprReferenceExpr(fieldMapBuilderVarExpr).setMethodName("build").setReturnType(fieldValuesMapVarExpr.type()).build()).build();
List<Statement> middleIfBlockStatements = new ArrayList<>();
middleIfBlockStatements.add(ExprStatement.withExpr(builderAssignExpr));
middleIfBlockStatements.addAll(tokenIfStatements);
middleIfBlockStatements.add(ExprStatement.withExpr(fieldValuesMapAssignExpr));
// Middle if-block, i.e. `if (fieldValuesMap == null)`.
Expr fieldValuesMapNullCheckExpr = RelationalOperationExpr.equalToWithExprs(fieldValuesMapVarExpr, ValueExpr.createNullExpr());
IfStatement fieldValuesMapIfStatement = IfStatement.builder().setConditionExpr(fieldValuesMapNullCheckExpr).setBody(middleIfBlockStatements).build();
// Outer if-block.
IfStatement outerIfStatement = IfStatement.builder().setConditionExpr(fieldValuesMapNullCheckExpr).setBody(Arrays.asList(SynchronizedStatement.builder().setLock(ThisObjectValue.withType(thisClassType)).setBody(Arrays.asList(fieldValuesMapIfStatement)).build())).build();
// Put the method together.
TypeNode mapStringType = fieldValuesMapVarExpr.type();
return MethodDefinition.builder().setIsOverride(true).setScope(ScopeNode.PUBLIC).setReturnType(mapStringType).setName("getFieldValuesMap").setBody(Arrays.asList(outerIfStatement)).setReturnExpr(fieldValuesMapVarExpr).build();
}
use of com.google.api.generator.engine.ast.MethodDefinition in project gapic-generator-java by googleapis.
the class ResourceNameHelperClassComposer method createEqualsMethod.
private static MethodDefinition createEqualsMethod(ResourceName resourceName, List<List<String>> tokenHierarchies, TypeStore typeStore) {
// Create method definition variables.
Variable oVariable = Variable.builder().setType(TypeNode.withReference(javaObjectReference)).setName("o").build();
VariableExpr argVarExpr = VariableExpr.builder().setIsDecl(false).setVariable(oVariable).build();
TypeNode thisClassType = typeStore.get(getThisClassName(resourceName));
ValueExpr thisValueExpr = ValueExpr.withValue(ThisObjectValue.withType(thisClassType));
ValueExpr trueValueExpr = ValueExpr.withValue(PrimitiveValue.builder().setType(TypeNode.BOOLEAN).setValue("true").build());
// Create first if statement's return expression
ReturnExpr returnTrueExpr = ReturnExpr.withExpr(trueValueExpr);
// Create second if statement's condition expression
RelationalOperationExpr oEqualsThisExpr = RelationalOperationExpr.equalToWithExprs(argVarExpr, thisValueExpr);
RelationalOperationExpr oNotEqualsNullExpr = RelationalOperationExpr.notEqualToWithExprs(argVarExpr, ValueExpr.createNullExpr());
MethodInvocationExpr getClassMethodInvocationExpr = MethodInvocationExpr.builder().setMethodName("getClass").build();
RelationalOperationExpr getClassEqualsExpr = RelationalOperationExpr.equalToWithExprs(getClassMethodInvocationExpr, getClassMethodInvocationExpr.toBuilder().setExprReferenceExpr(argVarExpr).build());
LogicalOperationExpr orLogicalExpr = LogicalOperationExpr.logicalOrWithExprs(oNotEqualsNullExpr, getClassEqualsExpr);
// Create second if statement's body assignment expression.
Variable thatVariable = Variable.builder().setName("that").setType(thisClassType).build();
VariableExpr thatVariableExpr = VariableExpr.builder().setIsDecl(false).setVariable(thatVariable).build();
CastExpr oCastExpr = CastExpr.builder().setExpr(argVarExpr).setType(thisClassType).build();
AssignmentExpr thatAssignmentExpr = AssignmentExpr.builder().setVariableExpr(thatVariableExpr.toBuilder().setIsDecl(true).build()).setValueExpr(oCastExpr).build();
// PubSub special-case handling - exclude _deleted-topic_.
List<List<String>> processedTokenHierarchies = tokenHierarchies.stream().filter(ts -> !ts.contains(ResourceNameConstants.DELETED_TOPIC_LITERAL)).collect(Collectors.toList());
// Create return expression in the second if statement's body.
Set<String> tokenSet = getTokenSet(processedTokenHierarchies);
Iterator<String> itToken = tokenSet.iterator();
Expr curTokenExpr = createObjectsEqualsForTokenMethodExpr(thisValueExpr, thatVariableExpr, Variable.builder().setType(TypeNode.STRING).setName(JavaStyle.toLowerCamelCase(itToken.next())).build());
while (itToken.hasNext()) {
Expr nextTokenExpr = createObjectsEqualsForTokenMethodExpr(thisValueExpr, thatVariableExpr, Variable.builder().setType(TypeNode.STRING).setName(JavaStyle.toLowerCamelCase(itToken.next())).build());
curTokenExpr = LogicalOperationExpr.logicalAndWithExprs(curTokenExpr, nextTokenExpr);
}
ReturnExpr secondIfReturnExpr = ReturnExpr.withExpr(curTokenExpr);
// Code: if (o == this) { return true;}
IfStatement firstIfStatement = IfStatement.builder().setConditionExpr(oEqualsThisExpr).setBody(Arrays.asList(ExprStatement.withExpr(returnTrueExpr))).build();
// Code: if (o != null || getClass() == o.getClass()) { FoobarName that = ((FoobarName) o);
// return ..}
IfStatement secondIfStatement = IfStatement.builder().setConditionExpr(orLogicalExpr).setBody(Arrays.asList(ExprStatement.withExpr(thatAssignmentExpr), ExprStatement.withExpr(secondIfReturnExpr))).build();
// Create method's return expression.
ValueExpr falseValueExpr = ValueExpr.withValue(PrimitiveValue.builder().setType(TypeNode.BOOLEAN).setValue("false").build());
return MethodDefinition.builder().setIsOverride(true).setScope(ScopeNode.PUBLIC).setArguments(argVarExpr.toBuilder().setIsDecl(true).build()).setReturnType(TypeNode.BOOLEAN).setName("equals").setReturnExpr(falseValueExpr).setBody(Arrays.asList(firstIfStatement, secondIfStatement)).build();
}
use of com.google.api.generator.engine.ast.MethodDefinition in project gapic-generator-java by googleapis.
the class ResourceNameHelperClassComposer method createBuilderCreatorMethods.
private static List<MethodDefinition> createBuilderCreatorMethods(ResourceName resourceName, List<List<String>> tokenHierarchies, TypeStore typeStore) {
List<MethodDefinition> javaMethods = new ArrayList<>();
String newMethodNameFormat = "new%s";
AnnotationNode betaAnnotation = AnnotationNode.builder().setType(FIXED_TYPESTORE.get("BetaApi")).setDescription("The per-pattern Builders are not stable yet and may be changed in the future.").build();
List<AnnotationNode> annotations = Arrays.asList(betaAnnotation);
// Variation example: newProjectLocationAutoscalingPolicyBuilder().
for (int i = 0; i < tokenHierarchies.size(); i++) {
// PubSub special-case handling.
if (tokenHierarchies.get(i).contains(ResourceNameConstants.DELETED_TOPIC_LITERAL)) {
continue;
}
final TypeNode returnType = getBuilderType(typeStore, tokenHierarchies, i);
final Expr returnExpr = NewObjectExpr.withType(returnType);
Function<String, MethodDefinition.Builder> methodDefStarterFn = methodName -> MethodDefinition.builder().setScope(ScopeNode.PUBLIC).setIsStatic(true).setReturnType(returnType).setName(methodName).setReturnExpr(returnExpr);
String variantName = getBuilderTypeName(tokenHierarchies, i);
javaMethods.add(methodDefStarterFn.apply(String.format(newMethodNameFormat, variantName)).setAnnotations(i == 0 ? Collections.emptyList() : annotations).build());
if (i == 0 && tokenHierarchies.size() > 1) {
// Create another builder creator method, but with the per-variant name.
javaMethods.add(methodDefStarterFn.apply(String.format(newMethodNameFormat, getBuilderTypeName(tokenHierarchies.get(i)))).setAnnotations(annotations).build());
}
}
// TODO(miraleung, v2): It seems weird that we currently generate a toBuilder method only for
// the default class, and none for the Builder variants.
TypeNode toBuilderReturnType = getBuilderType(typeStore, tokenHierarchies, 0);
TypeNode thisClassType = typeStore.get(getThisClassName(resourceName));
javaMethods.add(MethodDefinition.builder().setScope(ScopeNode.PUBLIC).setReturnType(toBuilderReturnType).setName("toBuilder").setReturnExpr(NewObjectExpr.builder().setType(toBuilderReturnType).setArguments(Arrays.asList(ValueExpr.withValue(ThisObjectValue.withType(thisClassType)))).build()).build());
return javaMethods;
}
Aggregations