use of com.google.api.generator.engine.ast.MethodDefinition in project gapic-generator-java by googleapis.
the class JavaWriterVisitorTest method writeMethodDefinition_withCommentsAnnotationsAndThrows.
@Test
public void writeMethodDefinition_withCommentsAnnotationsAndThrows() {
LineComment lineComment = LineComment.withComment("AUTO-GENERATED DOCUMENTATION AND METHOD");
JavaDocComment javaDocComment = JavaDocComment.builder().addComment("This is an override method called `close()`").addParam("valOne", "string type").addParam("valTwo", "boolean type").addComment("The return value is int 3.").build();
ValueExpr returnExpr = ValueExpr.builder().setValue(PrimitiveValue.builder().setType(TypeNode.INT).setValue("3").build()).build();
List<VariableExpr> arguments = Arrays.asList(VariableExpr.builder().setVariable(createVariable("valOne", TypeNode.STRING)).setIsDecl(true).build(), VariableExpr.builder().setVariable(createVariable("valTwo", TypeNode.BOOLEAN)).setIsDecl(true).build());
MethodDefinition methodDefinition = MethodDefinition.builder().setName("close").setIsOverride(true).setIsFinal(true).setIsStatic(true).setScope(ScopeNode.PROTECTED).setReturnType(TypeNode.INT).setThrowsExceptions(Arrays.asList(TypeNode.withExceptionClazz(IOException.class), TypeNode.withExceptionClazz(TimeoutException.class), TypeNode.withExceptionClazz(InterruptedException.class))).setArguments(arguments).setReturnExpr(returnExpr).setHeaderCommentStatements(Arrays.asList(CommentStatement.withComment(lineComment), CommentStatement.withComment(javaDocComment))).setAnnotations(Arrays.asList(AnnotationNode.withSuppressWarnings("all"), AnnotationNode.DEPRECATED)).setBody(Arrays.asList(createForStatement(), ExprStatement.withExpr(createAssignmentExpr("foobar", "false", TypeNode.BOOLEAN)))).build();
methodDefinition.accept(writerVisitor);
String expected = LineFormatter.lines("// AUTO-GENERATED DOCUMENTATION AND METHOD\n", "/**\n", "* This is an override method called `close()`\n", "* The return value is int 3.\n", "* @param valOne string type\n", "* @param valTwo boolean type\n", "*/\n", "@SuppressWarnings(\"all\")\n", "@Deprecated\n", "@Override\n", "protected static final int close(String valOne, boolean valTwo) throws" + " IOException, TimeoutException, InterruptedException {\n", "for (String str : getSomeStrings()) {\n", "boolean aBool = false;\n", "}\n", "boolean foobar = false;\n", "return 3;\n", "}\n\n");
assertEquals(expected, writerVisitor.write());
}
use of com.google.api.generator.engine.ast.MethodDefinition in project gapic-generator-java by googleapis.
the class JavaWriterVisitorTest method writeAnonymousClassExpr_basic.
@Test
public void writeAnonymousClassExpr_basic() {
ConcreteReference ref = ConcreteReference.withClazz(Runnable.class);
TypeNode type = TypeNode.withReference(ref);
AssignmentExpr assignmentExpr = createAssignmentExpr("foobar", "false", TypeNode.BOOLEAN);
ExprStatement statement = ExprStatement.withExpr(assignmentExpr);
MethodDefinition method = MethodDefinition.builder().setScope(ScopeNode.PUBLIC).setReturnType(TypeNode.VOID).setName("run").setIsOverride(true).setBody(Arrays.asList(statement)).build();
AnonymousClassExpr anonymousClassExpr = AnonymousClassExpr.builder().setType(type).setMethods(Arrays.asList(method)).build();
anonymousClassExpr.accept(writerVisitor);
assertEquals(LineFormatter.lines("new Runnable() {\n", "@Override\n", "public void run() {\n", "boolean foobar = false;\n}\n\n}"), writerVisitor.write());
}
use of com.google.api.generator.engine.ast.MethodDefinition in project gapic-generator-java by googleapis.
the class JavaWriterVisitorTest method writeMethodDefinition_basicEmptyBody.
@Test
public void writeMethodDefinition_basicEmptyBody() {
MethodDefinition methodDefinition = MethodDefinition.builder().setName("close").setScope(ScopeNode.PUBLIC).setReturnType(TypeNode.VOID).build();
methodDefinition.accept(writerVisitor);
assertEquals("public void close() {\n}\n\n", writerVisitor.write());
}
use of com.google.api.generator.engine.ast.MethodDefinition in project gapic-generator-java by googleapis.
the class JavaWriterVisitorTest method writeClassDefinition_commentsStatementsAndMethods.
@Test
public void writeClassDefinition_commentsStatementsAndMethods() {
LineComment lineComment = LineComment.withComment("AUTO-GENERATED DOCUMENTATION AND CLASS");
JavaDocComment javaDocComment = JavaDocComment.builder().addComment("Class to configure an instance of {@link LibraryServiceStub}.").addParagraph("The default instance has everything set to sensible defaults:").addUnorderedList(Arrays.asList("The default service address (library-example.googleapis.com) and default port" + " (1234) are used.", "Credentials are acquired automatically through Application Default" + " Credentials.", "Retries are configured for idempotent methods but not for non-idempotent" + " methods.")).build();
List<Reference> subGenerics = Arrays.asList(ConcreteReference.withClazz(String.class), ConcreteReference.withClazz(MethodDefinition.class));
Reference mapEntryReference = ConcreteReference.builder().setClazz(Map.Entry.class).setGenerics(subGenerics).build();
List<Reference> generics = Arrays.asList(ConcreteReference.withClazz(ClassDefinition.class), mapEntryReference);
Reference mapReference = ConcreteReference.builder().setClazz(Map.class).setGenerics(generics).build();
List<Statement> statements = Arrays.asList(ExprStatement.withExpr(VariableExpr.builder().setVariable(createVariable("x", TypeNode.withReference(ConcreteReference.withClazz(AssignmentExpr.class)))).setIsDecl(true).setScope(ScopeNode.PRIVATE).build()), ExprStatement.withExpr(VariableExpr.builder().setVariable(createVariable("y", TypeNode.withReference(mapReference))).setIsDecl(true).setScope(ScopeNode.PROTECTED).build()));
MethodDefinition methodOne = MethodDefinition.builder().setName("open").setScope(ScopeNode.PUBLIC).setReturnType(TypeNode.BOOLEAN).setReturnExpr(ValueExpr.builder().setValue(PrimitiveValue.builder().setType(TypeNode.BOOLEAN).setValue("true").build()).build()).build();
MethodDefinition methodTwo = MethodDefinition.builder().setName("close").setScope(ScopeNode.PUBLIC).setReturnType(TypeNode.VOID).setBody(Arrays.asList(ExprStatement.withExpr(createAssignmentExpr("foobar", "false", TypeNode.BOOLEAN)))).build();
List<MethodDefinition> methods = Arrays.asList(methodOne, methodTwo);
ClassDefinition nestedClassDef = ClassDefinition.builder().setName("IAmANestedClass").setIsNested(true).setScope(ScopeNode.PRIVATE).setIsStatic(true).setMethods(Arrays.asList(methodOne)).build();
ClassDefinition classDef = ClassDefinition.builder().setPackageString("com.google.example.library.v1.stub").setHeaderCommentStatements(Arrays.asList(CommentStatement.withComment(lineComment), CommentStatement.withComment(javaDocComment))).setName("LibraryServiceStub").setScope(ScopeNode.PUBLIC).setStatements(statements).setMethods(methods).setNestedClasses(Arrays.asList(nestedClassDef)).build();
classDef.accept(writerVisitor);
String expected = LineFormatter.lines("package com.google.example.library.v1.stub;\n", "\n", "import com.google.api.generator.engine.ast.AssignmentExpr;\n", "import com.google.api.generator.engine.ast.ClassDefinition;\n", "import com.google.api.generator.engine.ast.MethodDefinition;\n", "import java.util.Map;\n", "\n", "// AUTO-GENERATED DOCUMENTATION AND CLASS\n", "/**\n", " * Class to configure an instance of {{@literal @}link LibraryServiceStub}.\n", " *\n", " * <p>The default instance has everything set to sensible defaults:\n", " *\n", " * <ul>\n", " * <li>The default service address (library-example.googleapis.com) and default" + " port (1234) are\n", " * used.\n", " * <li>Credentials are acquired automatically through Application Default" + " Credentials.\n", " * <li>Retries are configured for idempotent methods but not for non-idempotent" + " methods.\n", " * </ul>\n", " */\n", "public class LibraryServiceStub {\n", " private AssignmentExpr x;\n", " protected Map<ClassDefinition, Map.Entry<String, MethodDefinition>> y;\n\n", " public boolean open() {\n", " return true;\n", " }\n\n", " public void close() {\n", " boolean foobar = false;\n", " }\n", "\n", " private static class IAmANestedClass {\n\n", " public boolean open() {\n", " return true;\n", " }\n", " }\n", "}\n");
assertEquals(expected, writerVisitor.write());
}
use of com.google.api.generator.engine.ast.MethodDefinition in project gapic-generator-java by googleapis.
the class AbstractServiceClientClassComposer method createStaticCreatorMethods.
private static List<MethodDefinition> createStaticCreatorMethods(Service service, TypeStore typeStore) {
List<MethodDefinition> methods = new ArrayList<>();
String thisClientName = ClassNames.getServiceClientClassName(service);
String settingsName = ClassNames.getServiceSettingsClassName(service);
TypeNode thisClassType = typeStore.get(thisClientName);
TypeNode exceptionType = typeStore.get("IOException");
TypeNode settingsType = typeStore.get(settingsName);
Preconditions.checkNotNull(settingsType, String.format("Type %s not found", settingsName));
MethodInvocationExpr newBuilderExpr = MethodInvocationExpr.builder().setMethodName("newBuilder").setStaticReferenceType(settingsType).build();
MethodInvocationExpr buildExpr = MethodInvocationExpr.builder().setMethodName("build").setExprReferenceExpr(newBuilderExpr).build();
MethodInvocationExpr createMethodInvocationExpr = MethodInvocationExpr.builder().setMethodName("create").setArguments(Arrays.asList(buildExpr)).setReturnType(typeStore.get(thisClientName)).build();
MethodDefinition createMethodOne = MethodDefinition.builder().setHeaderCommentStatements(ServiceClientCommentComposer.createMethodNoArgComment(ClassNames.getServiceClientClassName(service))).setScope(ScopeNode.PUBLIC).setIsStatic(true).setIsFinal(true).setReturnType(thisClassType).setName("create").setThrowsExceptions(Arrays.asList(exceptionType)).setReturnExpr(createMethodInvocationExpr).build();
methods.add(createMethodOne);
// Second create(ServiceSettings settings) method.
VariableExpr settingsVarExpr = VariableExpr.withVariable(Variable.builder().setName("settings").setType(typeStore.get(settingsName)).build());
methods.add(MethodDefinition.builder().setHeaderCommentStatements(ServiceClientCommentComposer.createMethodSettingsArgComment(ClassNames.getServiceClientClassName(service))).setScope(ScopeNode.PUBLIC).setIsStatic(true).setIsFinal(true).setReturnType(thisClassType).setName("create").setThrowsExceptions(Arrays.asList(exceptionType)).setArguments(settingsVarExpr.toBuilder().setIsDecl(true).build()).setReturnExpr(NewObjectExpr.builder().setType(thisClassType).setArguments(settingsVarExpr).build()).build());
// Third create(ServiceStub stub) method.
VariableExpr stubVarExpr = VariableExpr.withVariable(Variable.builder().setType(typeStore.get(ClassNames.getServiceStubClassName(service))).setName("stub").build());
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.builder().setHeaderCommentStatements(ServiceClientCommentComposer.createCreateMethodStubArgComment(ClassNames.getServiceClientClassName(service), settingsVarExpr.type())).setAnnotations(Arrays.asList(betaAnnotation)).setScope(ScopeNode.PUBLIC).setIsStatic(true).setIsFinal(true).setReturnType(thisClassType).setName("create").setArguments(stubVarExpr.toBuilder().setIsDecl(true).build()).setReturnExpr(NewObjectExpr.builder().setType(thisClassType).setArguments(stubVarExpr).build()).build());
return methods;
}
Aggregations