Skip to content
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
Expand Up @@ -91,19 +91,18 @@ public boolean isFromPackage(String pkg) {

@Override
public boolean isSupertypeOrEquals(Reference other) {
if (generics().size() != other.generics().size()) {
return false;
}

// Don't check generics for cases like "List<String> foo = new ArrayList<>();
if (!isAssignableFrom(other)) {
return false;
}

for (int i = 0; i < generics().size(); i++) {
Reference thisGeneric = generics().get(i);
Reference otherGeneric = other.generics().get(i);
if (!thisGeneric.isSupertypeOrEquals(otherGeneric)) {
return false;
if (generics().size() == other.generics().size()) {
for (int i = 0; i < generics().size(); i++) {
Reference thisGeneric = generics().get(i);
Reference otherGeneric = other.generics().get(i);
if (!thisGeneric.isSupertypeOrEquals(otherGeneric)) {
return false;
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,12 @@
import com.google.api.generator.engine.ast.InstanceofExpr;
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.ScopeNode;
import com.google.api.generator.engine.ast.Statement;
import com.google.api.generator.engine.ast.ThisObjectValue;
import com.google.api.generator.engine.ast.TypeNode;
import com.google.api.generator.engine.ast.ValueExpr;
import com.google.api.generator.engine.ast.VaporReference;
import com.google.api.generator.engine.ast.Variable;
import com.google.api.generator.engine.ast.VariableExpr;
Expand Down Expand Up @@ -134,17 +137,17 @@ private static List<MethodDefinition> createClassMethods(
types.get(String.format(MOCK_SERVICE_IMPL_NAME_PATTERN, service.name()))));
javaMethods.add(createGetRequestsMethod());
javaMethods.add(createAddResponseMethod());
javaMethods.add(createSetResponsesMethod());
javaMethods.add(createSetResponsesMethod(service));
javaMethods.add(createAddExceptionMethod());
javaMethods.add(createResetMethod());
javaMethods.addAll(createProtoMethodOverrides(service));
return javaMethods;
}

private static MethodDefinition createConstructor(TypeNode classType) {
// TODO(miraleung): Instantiate fields here.
return MethodDefinition.constructorBuilder()
.setScope(ScopeNode.PUBLIC)
.setBody(createRequestResponseAssignStatements())
.setReturnType(classType)
.build();
}
Expand Down Expand Up @@ -182,30 +185,46 @@ private static MethodDefinition createAddResponseMethod() {
.build();
}

private static MethodDefinition createSetResponsesMethod() {
private static MethodDefinition createSetResponsesMethod(Service service) {
// TODO(miraleung): Re-instantiate the fields here.
VariableExpr responsesArgVarExpr =
VariableExpr.withVariable(
Variable.builder()
.setName("responses")
.setType(
TypeNode.withReference(
ConcreteReference.builder()
.setClazz(List.class)
.setGenerics(
Arrays.asList(staticTypes.get("AbstractMessage").reference()))
.build()))
.build());
Expr responseAssignExpr =
AssignmentExpr.builder()
.setVariableExpr(
responsesVarExpr.toBuilder()
.setExprReferenceExpr(
ValueExpr.withValue(ThisObjectValue.withType(getThisClassType(service))))
.build())
.setValueExpr(
NewObjectExpr.builder()
.setType(
TypeNode.withReference(
ConcreteReference.builder()
.setClazz(LinkedList.class)
.setGenerics(
Arrays.asList(ConcreteReference.withClazz(Object.class)))
.build()))
.setArguments(Arrays.asList(responsesArgVarExpr))
.build())
.build();
return MethodDefinition.builder()
.setIsOverride(true)
.setScope(ScopeNode.PUBLIC)
.setReturnType(TypeNode.VOID)
.setName("setResponses")
.setArguments(
Arrays.asList(
VariableExpr.builder()
.setVariable(
Variable.builder()
.setName("responses")
.setType(
TypeNode.withReference(
ConcreteReference.builder()
.setClazz(List.class)
.setGenerics(
Arrays.asList(
staticTypes.get("AbstractMessage").reference()))
.build()))
.build())
.setIsDecl(true)
.build()))
.setArguments(Arrays.asList(responsesArgVarExpr.toBuilder().setIsDecl(true).build()))
.setBody(Arrays.asList(ExprStatement.withExpr(responseAssignExpr)))
.build();
}

Expand Down Expand Up @@ -233,12 +252,12 @@ private static MethodDefinition createAddExceptionMethod() {
}

private static MethodDefinition createResetMethod() {
// TODO(miraleung): Re-instantiate the fields here.
return MethodDefinition.builder()
.setIsOverride(true)
.setScope(ScopeNode.PUBLIC)
.setReturnType(TypeNode.VOID)
.setName("reset")
.setBody(createRequestResponseAssignStatements())
.build();
}

Expand Down Expand Up @@ -550,4 +569,36 @@ private static Map<String, TypeNode> createDynamicTypes(Service service) {
.build()));
return types;
}

private static TypeNode getThisClassType(Service service) {
return TypeNode.withReference(
VaporReference.builder()
.setName(String.format(MOCK_SERVICE_IMPL_NAME_PATTERN, service.name()))
.setPakkage(service.pakkage())
.build());
}

private static List<Statement> createRequestResponseAssignStatements() {
Expr assignRequestVarExpr =
AssignmentExpr.builder()
.setVariableExpr(requestsVarExpr)
.setValueExpr(
NewObjectExpr.builder()
.setType(TypeNode.withReference(ConcreteReference.withClazz(ArrayList.class)))
.setIsGeneric(true)
.build())
.build();
Expr assignResponsesVarExpr =
AssignmentExpr.builder()
.setVariableExpr(responsesVarExpr)
.setValueExpr(
NewObjectExpr.builder()
.setType(TypeNode.withReference(ConcreteReference.withClazz(LinkedList.class)))
.setIsGeneric(true)
.build())
.build();
return Arrays.asList(assignRequestVarExpr, assignResponsesVarExpr).stream()
.map(e -> ExprStatement.withExpr(e))
.collect(Collectors.toList());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ public void generateServiceClasses() {
+ "import com.google.protobuf.AbstractMessage;\n"
+ "import com.google.showcase.v1beta1.EchoGrpc.EchoImplBase;\n"
+ "import io.grpc.stub.StreamObserver;\n"
+ "import java.util.ArrayList;\n"
+ "import java.util.LinkedList;\n"
+ "import java.util.List;\n"
+ "import java.util.Queue;\n"
+ "import javax.annotation.Generated;\n"
Expand All @@ -79,7 +81,10 @@ public void generateServiceClasses() {
+ " private List<AbstractMessage> requests;\n"
+ " private Queue<Object> responses;\n"
+ "\n"
+ " public MockEchoImpl() {}\n"
+ " public MockEchoImpl() {\n"
+ " requests = new ArrayList<>();\n"
+ " responses = new LinkedList<>();\n"
+ " }\n"
+ "\n"
+ " @Override\n"
+ " public List<AbstractMessage> getRequests() {\n"
Expand All @@ -92,15 +97,20 @@ public void generateServiceClasses() {
+ " }\n"
+ "\n"
+ " @Override\n"
+ " public void setResponses(List<AbstractMessage> responses) {}\n"
+ " public void setResponses(List<AbstractMessage> responses) {\n"
+ " this.responses = new LinkedList<Object>(responses);\n"
+ " }\n"
+ "\n"
+ " @Override\n"
+ " public void addException(Exception exception) {\n"
+ " responses.add(exception);\n"
+ " }\n"
+ "\n"
+ " @Override\n"
+ " public void reset() {}\n"
+ " public void reset() {\n"
+ " requests = new ArrayList<>();\n"
+ " responses = new LinkedList<>();\n"
+ " }\n"
+ "\n"
+ " @Override\n"
+ " public void echo(EchoRequest request, StreamObserver<EchoResponse>"
Expand Down