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

fix(mocks): Use java.lang.Object if there are protos named 'Object' [ggj] #760

Merged
merged 8 commits into from
Jun 10, 2021
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import com.google.api.generator.engine.ast.MethodDefinition;
import com.google.api.generator.engine.ast.MethodInvocationExpr;
import com.google.api.generator.engine.ast.NewObjectExpr;
import com.google.api.generator.engine.ast.Reference;
import com.google.api.generator.engine.ast.RelationalOperationExpr;
import com.google.api.generator.engine.ast.ScopeNode;
import com.google.api.generator.engine.ast.Statement;
Expand Down Expand Up @@ -78,17 +79,9 @@ public class MockServiceImplClassComposer implements ClassComposer {
Arrays.asList(FIXED_TYPESTORE.get("AbstractMessage").reference()))
.build()))
.build());
private static final VariableExpr responsesVarExpr =
VariableExpr.withVariable(
Variable.builder()
.setName("responses")
.setType(
TypeNode.withReference(
ConcreteReference.builder()
.setClazz(Queue.class)
.setGenerics(Arrays.asList(ConcreteReference.withClazz(Object.class)))
.build()))
.build());

private static Reference javaObjectReference = ConcreteReference.withClazz(Object.class);
private static VariableExpr responsesVarExpr;

private MockServiceImplClassComposer() {}

Expand All @@ -97,12 +90,34 @@ public static MockServiceImplClassComposer instance() {
}

@Override
public GapicClass generate(GapicContext ignored, Service service) {
public GapicClass generate(GapicContext context, Service service) {
TypeStore typeStore = createDynamicTypes(service);
String className = ClassNames.getMockServiceImplClassName(service);
GapicClass.Kind kind = Kind.TEST;
String pakkage = service.pakkage();

// Use the full name java.lang.Object if there is a proto message that is also named "Object".
// Affects GCS.
if (context.messages().keySet().stream()
.map(s -> s.substring(Math.max(0, s.lastIndexOf(".") + 1), s.length()))
miraleung marked this conversation as resolved.
Show resolved Hide resolved
.collect(Collectors.toSet())
miraleung marked this conversation as resolved.
Show resolved Hide resolved
.contains("Object")) {
javaObjectReference =
ConcreteReference.builder().setClazz(Object.class).setUseFullName(true).build();
}

responsesVarExpr =
VariableExpr.withVariable(
Variable.builder()
.setName("responses")
.setType(
TypeNode.withReference(
ConcreteReference.builder()
.setClazz(Queue.class)
.setGenerics(Arrays.asList(javaObjectReference))
.build()))
.build());

ClassDefinition classDef =
ClassDefinition.builder()
.setPackageString(pakkage)
Expand Down Expand Up @@ -201,8 +216,7 @@ private static MethodDefinition createSetResponsesMethod(Service service) {
Expr responseAssignExpr =
AssignmentExpr.builder()
.setVariableExpr(
responsesVarExpr
.toBuilder()
responsesVarExpr.toBuilder()
.setExprReferenceExpr(
ValueExpr.withValue(ThisObjectValue.withType(getThisClassType(service))))
.build())
Expand All @@ -212,8 +226,7 @@ private static MethodDefinition createSetResponsesMethod(Service service) {
TypeNode.withReference(
ConcreteReference.builder()
.setClazz(LinkedList.class)
.setGenerics(
Arrays.asList(ConcreteReference.withClazz(Object.class)))
.setGenerics(Arrays.asList(javaObjectReference))
.build()))
.setArguments(Arrays.asList(responsesArgVarExpr))
.build())
Expand Down Expand Up @@ -267,7 +280,7 @@ private static List<MethodDefinition> createProtoMethodOverrides(Service service

private static MethodDefinition createGenericProtoMethodOverride(Method protoMethod) {
ConcreteReference streamObserverRef = ConcreteReference.withClazz(StreamObserver.class);
TypeNode objectType = TypeNode.withReference(ConcreteReference.withClazz(Object.class));
TypeNode objectType = TypeNode.withReference(javaObjectReference);
VariableExpr localResponseVarExpr =
VariableExpr.withVariable(
Variable.builder().setName("response").setType(objectType).build());
Expand Down Expand Up @@ -372,7 +385,7 @@ private static MethodDefinition createOnNextJavaMethod(
VariableExpr valueVarExpr =
VariableExpr.withVariable(
Variable.builder().setName("value").setType(protoMethod.inputType()).build());
TypeNode objectType = TypeNode.withReference(ConcreteReference.withClazz(Object.class));
TypeNode objectType = TypeNode.withReference(javaObjectReference);

Statement addValueToRequestsStatement =
ExprStatement.withExpr(
Expand Down