Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ggj][engx][codegen] fix: use gapic-remapped names in package-info.java #600

Merged
merged 12 commits into from
Dec 16, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// Copyright 2020 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package com.google.api.generator.gapic.composer;

import com.google.api.generator.gapic.model.Service;

/** Provides Gapic class names. */
public class ClassNames {
// Using constants since many of these class names are used often.
private static final String MOCK_SERVICE_CLASS_NAME_PATTERN = "Mock%s";
private static final String SERVICE_CLIENT_CLASS_NAME_PATTERN = "%sClient";
private static final String SERVICE_SETTINGS_CLASS_NAME_PATTERN = "%sSettings";
private static final String SERVICE_STUB_SETTINGS_CLASS_NAME_PATTERN = "%sStubSettings";
private static final String SERVICE_STUB_CLASS_NAME_PATTERN = "%sStub";
private static final String GRPC_SERVICE_STUB_CLASS_NAME_PATTERN = "Grpc%sStub";
private static final String GRPC_SERVICE_CALLABLE_FACTORY_CLASS_NAME_PATTERN =
"Grpc%sCallableFactory";
private static final String MOCK_SERVICE_IMPL_CLASS_NAME_PATTERN = "Mock%sImpl";

protected static String getServiceClientClassName(Service service) {
return String.format(SERVICE_CLIENT_CLASS_NAME_PATTERN, service.overriddenName());
}

protected static String getServiceSettingsClassName(Service service) {
return String.format(SERVICE_SETTINGS_CLASS_NAME_PATTERN, service.overriddenName());
}

protected static String getMockServiceClassName(Service service) {
return String.format(MOCK_SERVICE_CLASS_NAME_PATTERN, service.name());
}

protected static String getMockServiceImplClassName(Service service) {
return String.format(MOCK_SERVICE_IMPL_CLASS_NAME_PATTERN, service.name());
}

protected static String getServiceStubSettingsClassName(Service service) {
return String.format(SERVICE_STUB_SETTINGS_CLASS_NAME_PATTERN, service.name());
}

protected static String getServiceStubClassName(Service service) {
return String.format(SERVICE_STUB_CLASS_NAME_PATTERN, service.name());
}

protected static String getGrpcServiceCallableFactoryClassName(Service service) {
return String.format(GRPC_SERVICE_CALLABLE_FACTORY_CLASS_NAME_PATTERN, service.name());
}

protected static String getGrpcServiceStubClassName(Service service) {
return String.format(GRPC_SERVICE_STUB_CLASS_NAME_PATTERN, service.name());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ public class ClientLibraryPackageInfoComposer {
private static final String PACKAGE_INFO_DESCRIPTION =
"The interfaces provided are listed below, along with usage samples.";

private static final String CLIENT_PATTERN = "%sClient";
private static final String PACKAGE_INFO_TITLE_PATTERN = "A client to %s";
private static final String SAMPLE_CODE_HEADER_PATTERN = "Sample for %s:";
private static final String SERVICE_DESCRIPTION_HEADER_PATTERN = "Service Description: %s";
Expand Down Expand Up @@ -71,7 +70,7 @@ private static CommentStatement createPackageInfoJavadoc(GapicContext context) {
javaDocCommentBuilder = javaDocCommentBuilder.addParagraph(PACKAGE_INFO_DESCRIPTION);

for (Service service : context.services()) {
String javaClientName = String.format(CLIENT_PATTERN, service.name());
String javaClientName = ClassNames.getServiceClientClassName(service);
javaDocCommentBuilder =
javaDocCommentBuilder.addParagraph(
String.format("%s %s %s", DIVIDER, javaClientName, DIVIDER));
Expand Down Expand Up @@ -111,7 +110,6 @@ private static CommentStatement createPackageInfoJavadoc(GapicContext context) {
javaDocCommentBuilder =
javaDocCommentBuilder.addParagraph(
String.format(SAMPLE_CODE_HEADER_PATTERN, javaClientName));

// TODO(summerji): Add package-info.java sample code here.
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public static GrpcServiceCallableFactoryClassComposer instance() {
@Override
public GapicClass generate(Service service, Map<String, Message> ignore) {
Map<String, TypeNode> types = createTypes(service);
String className = String.format("Grpc%sCallableFactory", service.name());
String className = ClassNames.getGrpcServiceCallableFactoryClassName(service);
GapicClass.Kind kind = Kind.STUB;
String pakkage = String.format("%s.stub", service.pakkage());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,6 @@
public class GrpcServiceStubClassComposer implements ClassComposer {
private static final Statement EMPTY_LINE_STATEMENT = EmptyLineStatement.create();

private static final String CLASS_NAME_PATTERN = "Grpc%sStub";
private static final String GRPC_SERVICE_CALLABLE_FACTORY_PATTERN = "Grpc%sCallableFactory";
private static final String METHOD_DESCRIPTOR_NAME_PATTERN = "%sMethodDescriptor";
private static final String PAGED_RESPONSE_TYPE_NAME_PATTERN = "%sPagedResponse";
Expand Down Expand Up @@ -117,7 +116,7 @@ public static GrpcServiceStubClassComposer instance() {
public GapicClass generate(Service service, Map<String, Message> ignore) {
String pakkage = service.pakkage() + ".stub";
Map<String, TypeNode> types = createDynamicTypes(service, pakkage);
String className = getThisClassName(service.name());
String className = ClassNames.getGrpcServiceStubClassName(service);
GapicClass.Kind kind = Kind.STUB;

Map<String, VariableExpr> protoMethodNameToDescriptorVarExprs =
Expand Down Expand Up @@ -400,7 +399,7 @@ private static List<MethodDefinition> createClassMethods(
Map<String, VariableExpr> callableClassMemberVarExprs,
Map<String, VariableExpr> protoMethodNameToDescriptorVarExprs) {
List<MethodDefinition> javaMethods = new ArrayList<>();
javaMethods.addAll(createStaticCreatorMethods(service.name(), types));
javaMethods.addAll(createStaticCreatorMethods(service, types));
javaMethods.addAll(
createConstructorMethods(
service,
Expand All @@ -417,8 +416,8 @@ private static List<MethodDefinition> createClassMethods(
}

private static List<MethodDefinition> createStaticCreatorMethods(
String serviceName, Map<String, TypeNode> types) {
TypeNode creatorMethodReturnType = types.get(getThisClassName(serviceName));
Service service, Map<String, TypeNode> types) {
TypeNode creatorMethodReturnType = types.get(ClassNames.getGrpcServiceStubClassName(service));
Function<List<VariableExpr>, MethodDefinition.Builder> creatorMethodStarterFn =
argList ->
MethodDefinition.builder()
Expand All @@ -439,7 +438,7 @@ private static List<MethodDefinition> createStaticCreatorMethods(
argList ->
NewObjectExpr.builder().setType(creatorMethodReturnType).setArguments(argList).build();

TypeNode stubSettingsType = types.get(String.format(STUB_SETTINGS_PATTERN, serviceName));
TypeNode stubSettingsType = types.get(ClassNames.getServiceStubSettingsClassName(service));
VariableExpr settingsVarExpr =
VariableExpr.withVariable(
Variable.builder().setName("settings").setType(stubSettingsType).build());
Expand Down Expand Up @@ -518,7 +517,7 @@ private static List<MethodDefinition> createConstructorMethods(
.setType(STATIC_TYPES.get("GrpcStubCallableFactory"))
.build());

TypeNode thisClassType = types.get(getThisClassName(service.name()));
TypeNode thisClassType = types.get(ClassNames.getGrpcServiceStubClassName(service));
TypeNode ioExceptionType =
TypeNode.withReference(ConcreteReference.withClazz(IOException.class));

Expand All @@ -527,8 +526,7 @@ private static List<MethodDefinition> createConstructorMethods(
MethodDefinition.constructorBuilder()
.setScope(ScopeNode.PROTECTED)
.setReturnType(thisClassType)
.setHeaderCommentStatements(
Arrays.asList(createProtectedCtorComment(service.name())))
.setHeaderCommentStatements(Arrays.asList(createProtectedCtorComment(service)))
.setArguments(
args.stream()
.map(v -> v.toBuilder().setIsDecl(true).build())
Expand Down Expand Up @@ -557,7 +555,8 @@ private static List<MethodDefinition> createConstructorMethods(
.build())));

Expr thisExpr =
ValueExpr.withValue(ThisObjectValue.withType(types.get(getThisClassName(service.name()))));
ValueExpr.withValue(
ThisObjectValue.withType(types.get(ClassNames.getGrpcServiceStubClassName(service))));
// Body of the second constructor method.
List<Statement> secondCtorStatements = new ArrayList<>();
List<Expr> secondCtorExprs = new ArrayList<>();
Expand Down Expand Up @@ -1049,20 +1048,17 @@ private static Map<String, TypeNode> createDynamicTypes(Service service, String
Map<String, TypeNode> types = new HashMap<>();
types.putAll(
Arrays.asList(
CLASS_NAME_PATTERN,
STUB_SETTINGS_PATTERN,
STUB_PATTERN,
GRPC_SERVICE_CALLABLE_FACTORY_PATTERN)
ClassNames.getGrpcServiceStubClassName(service),
ClassNames.getServiceStubSettingsClassName(service),
ClassNames.getServiceStubClassName(service),
ClassNames.getGrpcServiceCallableFactoryClassName(service))
.stream()
.collect(
Collectors.toMap(
p -> String.format(p, service.name()),
p ->
n -> n,
n ->
TypeNode.withReference(
VaporReference.builder()
.setName(String.format(p, service.name()))
.setPakkage(stubPakkage)
.build()))));
VaporReference.builder().setName(n).setPakkage(stubPakkage).build()))));
// Pagination types.
types.putAll(
service.methods().stream()
Expand All @@ -1076,7 +1072,7 @@ private static Map<String, TypeNode> createDynamicTypes(Service service, String
.setName(String.format(PAGED_RESPONSE_TYPE_NAME_PATTERN, m.name()))
.setPakkage(service.pakkage())
.setEnclosingClassNames(
String.format("%sClient", service.overriddenName()))
ClassNames.getServiceClientClassName(service))
.setIsStaticImport(true)
.build()))));
return types;
Expand Down Expand Up @@ -1144,17 +1140,13 @@ private static String getProtoRpcFullMethodName(Service protoService, Method pro
return String.format("google.iam.v1.IAMPolicy/%s", protoMethod.name());
}

private static String getThisClassName(String serviceName) {
return String.format(CLASS_NAME_PATTERN, serviceName);
}

private static CommentStatement createProtectedCtorComment(String serviceName) {
private static CommentStatement createProtectedCtorComment(Service service) {
return CommentStatement.withComment(
JavaDocComment.withComment(
String.format(
"Constructs an instance of %s, using the given settings. This is protected so that"
+ " it is easy to make a subclass, but otherwise, the static factory methods"
+ " should be preferred.",
getThisClassName(serviceName))));
ClassNames.getGrpcServiceStubClassName(service))));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,6 @@

public class MockServiceClassComposer implements ClassComposer {
private static final MockServiceClassComposer INSTANCE = new MockServiceClassComposer();
private static final String MOCK_SERVICE_NAME_PATTERN = "Mock%s";
private static final String MOCK_IMPL_NAME_PATTERN = "Mock%sImpl";
private static final String SERVICE_IMPL_VAR_NAME = "serviceImpl";

private MockServiceClassComposer() {}
Expand All @@ -57,7 +55,7 @@ public static MockServiceClassComposer instance() {
@Override
public GapicClass generate(Service service, Map<String, Message> ignore) {
Map<String, TypeNode> types = createTypes(service);
String className = String.format(MOCK_SERVICE_NAME_PATTERN, service.name());
String className = ClassNames.getMockServiceClassName(service);
GapicClass.Kind kind = Kind.TEST;
String pakkage = service.pakkage();

Expand All @@ -68,18 +66,18 @@ public GapicClass generate(Service service, Map<String, Message> ignore) {
.setScope(ScopeNode.PUBLIC)
.setName(className)
.setImplementsTypes(createClassImplements(types))
.setStatements(createFieldDeclarations(service.name(), types))
.setStatements(createFieldDeclarations(service, types))
.setMethods(createClassMethods(service, types))
.build();
return GapicClass.create(kind, classDef);
}

private static List<Statement> createFieldDeclarations(
String serviceName, Map<String, TypeNode> types) {
Service service, Map<String, TypeNode> types) {
return Arrays.asList(
ExprStatement.withExpr(
VariableExpr.builder()
.setVariable(getServiceImplVariable(serviceName, types))
.setVariable(getServiceImplVariable(service, types))
.setScope(ScopeNode.PRIVATE)
.setIsFinal(true)
.setIsDecl(true)
Expand All @@ -102,9 +100,9 @@ private static List<TypeNode> createClassImplements(Map<String, TypeNode> types)
private static List<MethodDefinition> createClassMethods(
Service service, Map<String, TypeNode> types) {
VariableExpr serviceImplVarExpr =
VariableExpr.withVariable(getServiceImplVariable(service.name(), types));
VariableExpr.withVariable(getServiceImplVariable(service, types));
return Arrays.asList(
createConstructor(service.name(), serviceImplVarExpr, types),
createConstructor(service, serviceImplVarExpr, types),
createGetRequestsMethod(serviceImplVarExpr, types),
createAddResponseMethod(serviceImplVarExpr, types),
createAddExceptionMethod(serviceImplVarExpr),
Expand All @@ -113,8 +111,8 @@ private static List<MethodDefinition> createClassMethods(
}

private static MethodDefinition createConstructor(
String serviceName, VariableExpr serviceImplVarExpr, Map<String, TypeNode> types) {
String mockImplName = String.format(MOCK_IMPL_NAME_PATTERN, serviceName);
Service service, VariableExpr serviceImplVarExpr, Map<String, TypeNode> types) {
String mockImplName = ClassNames.getMockServiceImplClassName(service);
Expr serviceImplAssignExpr =
AssignmentExpr.builder()
.setVariableExpr(serviceImplVarExpr)
Expand All @@ -124,7 +122,7 @@ private static MethodDefinition createConstructor(
return MethodDefinition.constructorBuilder()
.setScope(ScopeNode.PUBLIC)
.setBody(Arrays.asList(ExprStatement.withExpr(serviceImplAssignExpr)))
.setReturnType(types.get(String.format(MOCK_SERVICE_NAME_PATTERN, serviceName)))
.setReturnType(types.get(ClassNames.getMockServiceClassName(service)))
.build();
}

Expand Down Expand Up @@ -255,24 +253,24 @@ private static Map<String, TypeNode> createTypes(Service service) {
.setPakkage("com.google.api.gax.grpc.testing")
.build()));
// Vapor same-package deps.
String mockImplName = String.format(MOCK_IMPL_NAME_PATTERN, service.name());
String mockImplName = ClassNames.getMockServiceImplClassName(service);
types.put(
mockImplName,
TypeNode.withReference(
VaporReference.builder().setName(mockImplName).setPakkage(service.pakkage()).build()));

String className = String.format(MOCK_SERVICE_NAME_PATTERN, service.name());
String className = ClassNames.getMockServiceClassName(service);
types.put(
className,
TypeNode.withReference(
VaporReference.builder().setName(className).setPakkage(service.pakkage()).build()));
return types;
}

private static Variable getServiceImplVariable(String serviceName, Map<String, TypeNode> types) {
private static Variable getServiceImplVariable(Service service, Map<String, TypeNode> types) {
return Variable.builder()
.setName(SERVICE_IMPL_VAR_NAME)
.setType(types.get(String.format(MOCK_IMPL_NAME_PATTERN, serviceName)))
.setType(types.get(ClassNames.getMockServiceImplClassName(service)))
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@

public class MockServiceImplClassComposer implements ClassComposer {
private static final MockServiceImplClassComposer INSTANCE = new MockServiceImplClassComposer();
private static final String MOCK_SERVICE_IMPL_NAME_PATTERN = "Mock%sImpl";
private static final String IMPL_BASE_PATTERN = "%sImplBase";
private static final Map<String, TypeNode> staticTypes = createStaticTypes();
private static final VariableExpr requestsVarExpr =
Expand Down Expand Up @@ -96,7 +95,7 @@ public static MockServiceImplClassComposer instance() {
@Override
public GapicClass generate(Service service, Map<String, Message> ignore) {
Map<String, TypeNode> types = createDynamicTypes(service);
String className = String.format(MOCK_SERVICE_IMPL_NAME_PATTERN, service.name());
String className = ClassNames.getMockServiceImplClassName(service);
GapicClass.Kind kind = Kind.TEST;
String pakkage = service.pakkage();

Expand Down Expand Up @@ -133,9 +132,7 @@ private static List<AnnotationNode> createClassAnnotations() {
private static List<MethodDefinition> createClassMethods(
Service service, Map<String, TypeNode> types) {
List<MethodDefinition> javaMethods = new ArrayList<>();
javaMethods.add(
createConstructor(
types.get(String.format(MOCK_SERVICE_IMPL_NAME_PATTERN, service.name()))));
javaMethods.add(createConstructor(types.get(ClassNames.getMockServiceImplClassName(service))));
javaMethods.add(createGetRequestsMethod());
javaMethods.add(createAddResponseMethod());
javaMethods.add(createSetResponsesMethod(service));
Expand Down Expand Up @@ -562,7 +559,7 @@ private static Map<String, TypeNode> createDynamicTypes(Service service) {
.setPakkage(
String.format("%s.%sGrpc", service.originalJavaPackage(), service.name()))
.build()));
String className = String.format(MOCK_SERVICE_IMPL_NAME_PATTERN, service.name());
String className = ClassNames.getMockServiceImplClassName(service);
types.put(
className,
TypeNode.withReference(
Expand All @@ -577,7 +574,7 @@ private static Map<String, TypeNode> createDynamicTypes(Service service) {
private static TypeNode getThisClassType(Service service) {
return TypeNode.withReference(
VaporReference.builder()
.setName(String.format(MOCK_SERVICE_IMPL_NAME_PATTERN, service.name()))
.setName(ClassNames.getMockServiceImplClassName(service))
.setPakkage(service.pakkage())
.build());
}
Expand Down
Loading