use of com.google.api.generator.engine.ast.AnnotationNode in project gapic-generator-java by googleapis.
the class AbstractServiceStubSettingsClassComposer method createApiClientHeaderProviderBuilderMethod.
protected MethodDefinition createApiClientHeaderProviderBuilderMethod(Service service, TypeStore typeStore, String methodName, TypeNode gaxPropertiesType, String getTokenMethodName, String getVersionMethodName) {
TypeNode returnType = TypeNode.withReference(ConcreteReference.withClazz(ApiClientHeaderProvider.Builder.class));
MethodInvocationExpr returnExpr = MethodInvocationExpr.builder().setStaticReferenceType(FIXED_TYPESTORE.get("ApiClientHeaderProvider")).setMethodName("newBuilder").build();
MethodInvocationExpr versionArgExpr = MethodInvocationExpr.builder().setStaticReferenceType(FIXED_TYPESTORE.get("GaxProperties")).setMethodName("getLibraryVersion").setArguments(VariableExpr.builder().setVariable(Variable.builder().setType(TypeNode.CLASS_OBJECT).setName("class").build()).setStaticReferenceType(typeStore.get(ClassNames.getServiceStubSettingsClassName(service))).build()).build();
returnExpr = MethodInvocationExpr.builder().setExprReferenceExpr(returnExpr).setMethodName("setGeneratedLibToken").setArguments(ValueExpr.withValue(StringObjectValue.withValue("gapic")), versionArgExpr).build();
returnExpr = MethodInvocationExpr.builder().setExprReferenceExpr(returnExpr).setMethodName("setTransportToken").setArguments(MethodInvocationExpr.builder().setStaticReferenceType(gaxPropertiesType).setMethodName(getTokenMethodName).build(), MethodInvocationExpr.builder().setStaticReferenceType(gaxPropertiesType).setMethodName(getVersionMethodName).build()).setReturnType(returnType).build();
AnnotationNode annotation = AnnotationNode.builder().setType(FIXED_TYPESTORE.get("BetaApi")).setDescription("The surface for customizing headers is not stable yet and may change in the" + " future.").build();
return MethodDefinition.builder().setAnnotations(Arrays.asList(annotation)).setScope(ScopeNode.PUBLIC).setIsStatic(true).setReturnType(returnType).setName(methodName).setReturnExpr(returnExpr).build();
}
use of com.google.api.generator.engine.ast.AnnotationNode in project gapic-generator-java by googleapis.
the class AbstractServiceStubSettingsClassComposer method createCreateStubMethod.
private MethodDefinition createCreateStubMethod(Service service, TypeStore typeStore) {
// Set up the if-statement.
List<Statement> bodyStatements = new ArrayList<>();
Expr getTransportNameExpr = MethodInvocationExpr.builder().setMethodName("getTransportChannelProvider").build();
getTransportNameExpr = MethodInvocationExpr.builder().setExprReferenceExpr(getTransportNameExpr).setMethodName("getTransportName").build();
Iterator<TypeNode> channelTypesIt = getTransportContext().transportChannelTypes().iterator();
Iterator<String> getterNameIt = getTransportContext().transportGetterNames().iterator();
Iterator<String> serivceStubClassNameIt = getTransportContext().classNames().getTransportServiceStubClassNames(service).iterator();
while (channelTypesIt.hasNext() && getterNameIt.hasNext()) {
TypeNode channelType = channelTypesIt.next();
String getterName = getterNameIt.next();
String serivceStubClassName = serivceStubClassNameIt.next();
Expr tRansportNameExpr = MethodInvocationExpr.builder().setStaticReferenceType(channelType).setMethodName(getterName).build();
Expr ifConditionExpr = MethodInvocationExpr.builder().setExprReferenceExpr(getTransportNameExpr).setMethodName("equals").setArguments(tRansportNameExpr).setReturnType(TypeNode.BOOLEAN).build();
Expr createExpr = MethodInvocationExpr.builder().setStaticReferenceType(typeStore.get(serivceStubClassName)).setMethodName("create").setArguments(ValueExpr.withValue(ThisObjectValue.withType(typeStore.get(ClassNames.getServiceStubSettingsClassName(service))))).build();
IfStatement ifStatement = IfStatement.builder().setConditionExpr(ifConditionExpr).setBody(Arrays.asList(ExprStatement.withExpr(ReturnExpr.withExpr(createExpr)))).build();
bodyStatements.add(ifStatement);
}
// Set up exception throwing.
Expr errorMessageExpr = MethodInvocationExpr.builder().setStaticReferenceType(TypeNode.STRING).setMethodName("format").setArguments(ValueExpr.withValue(StringObjectValue.withValue("Transport not supported: %s")), getTransportNameExpr).setReturnType(TypeNode.STRING).build();
TypeNode exceptionType = TypeNode.withExceptionClazz(UnsupportedOperationException.class);
Statement throwStatement = ExprStatement.withExpr(ThrowExpr.builder().setType(exceptionType).setMessageExpr(errorMessageExpr).build());
bodyStatements.add(throwStatement);
// Put the method together.
TypeNode returnType = typeStore.get(ClassNames.getServiceStubClassName(service));
AnnotationNode annotation = AnnotationNode.builder().setType(FIXED_TYPESTORE.get("BetaApi")).setDescription("A restructuring of stub classes is planned, so this may break in the future").build();
return MethodDefinition.builder().setAnnotations(Arrays.asList(annotation)).setScope(ScopeNode.PUBLIC).setReturnType(returnType).setName("createStub").setThrowsExceptions(Arrays.asList(TypeNode.withExceptionClazz(IOException.class))).setBody(bodyStatements).build();
}
use of com.google.api.generator.engine.ast.AnnotationNode in project gapic-generator-java by googleapis.
the class AbstractServiceClientClassComposer method createGetterMethods.
private List<MethodDefinition> createGetterMethods(Service service, TypeStore typeStore, boolean hasLroClient) {
Map<String, TypeNode> methodNameToTypes = new LinkedHashMap<>();
methodNameToTypes.put("getSettings", typeStore.get(ClassNames.getServiceSettingsClassName(service)));
methodNameToTypes.put("getStub", typeStore.get(ClassNames.getServiceStubClassName(service)));
Set<String> getOperationsClientMethodNames = new HashSet<>();
if (hasLroClient) {
Iterator<String> opClientNamesIt = getTransportContext().operationsClientNames().iterator();
Iterator<TypeNode> opClientTypesIt = getTransportContext().operationsClientTypes().iterator();
while (opClientNamesIt.hasNext() && opClientTypesIt.hasNext()) {
String opClientMethodName = String.format("get%s", JavaStyle.toUpperCamelCase(opClientNamesIt.next()));
getOperationsClientMethodNames.add(opClientMethodName);
methodNameToTypes.put(opClientMethodName, opClientTypesIt.next());
}
}
AnnotationNode betaStubAnnotation = AnnotationNode.builder().setType(typeStore.get("BetaApi")).setDescription("A restructuring of stub classes is planned, so this may break in the future").build();
return methodNameToTypes.entrySet().stream().map(e -> {
String methodName = e.getKey();
TypeNode methodReturnType = e.getValue();
String returnVariableName = JavaStyle.toLowerCamelCase(methodName.substring(3));
MethodDefinition.Builder methodBuilder = MethodDefinition.builder();
if (getOperationsClientMethodNames.contains(methodName)) {
methodBuilder = methodBuilder.setHeaderCommentStatements(ServiceClientCommentComposer.GET_OPERATIONS_CLIENT_METHOD_COMMENT);
}
return methodBuilder.setAnnotations(methodName.equals("getStub") ? Arrays.asList(betaStubAnnotation) : Collections.emptyList()).setScope(ScopeNode.PUBLIC).setName(methodName).setIsFinal(!methodName.equals("getStub")).setReturnType(methodReturnType).setReturnExpr(VariableExpr.builder().setVariable(Variable.builder().setName(returnVariableName).setType(methodReturnType).build()).build()).build();
}).collect(Collectors.toList());
}
use of com.google.api.generator.engine.ast.AnnotationNode in project gapic-generator-java by googleapis.
the class AbstractServiceStubClassComposer method createCallableGetterHelper.
private MethodDefinition createCallableGetterHelper(Method method, TypeStore typeStore, boolean isLroCallable, boolean isPaged) {
TypeNode returnType;
switch(method.stream()) {
case CLIENT:
returnType = typeStore.get("ClientStreamingCallable");
break;
case SERVER:
returnType = typeStore.get("ServerStreamingCallable");
break;
case BIDI:
returnType = typeStore.get("BidiStreamingCallable");
break;
case NONE:
// Fall through.
default:
returnType = typeStore.get(isLroCallable ? "OperationCallable" : "UnaryCallable");
}
String methodName = String.format("%s%sCallable", JavaStyle.toLowerCamelCase(method.name()), (isLroCallable ? "Operation" : isPaged ? "Paged" : ""));
List<Reference> genericRefs = new ArrayList<>();
genericRefs.add(method.inputType().reference());
if (method.hasLro() && isLroCallable) {
genericRefs.add(method.lro().responseType().reference());
genericRefs.add(method.lro().metadataType().reference());
} else if (isPaged) {
genericRefs.add(typeStore.get(String.format(PAGED_RESPONSE_TYPE_NAME_PATTERN, method.name())).reference());
} else {
genericRefs.add(method.outputType().reference());
}
List<AnnotationNode> annotations = method.isDeprecated() ? Arrays.asList(AnnotationNode.withType(TypeNode.DEPRECATED)) : Collections.emptyList();
returnType = TypeNode.withReference(returnType.reference().copyAndSetGenerics(genericRefs));
return createCallableGetterMethodDefinition(returnType, methodName, annotations, typeStore);
}
use of com.google.api.generator.engine.ast.AnnotationNode 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