use of com.google.api.generator.engine.ast.CastExpr in project gapic-generator-java by googleapis.
the class ResourceNameHelperClassComposer method createEqualsMethod.
private static MethodDefinition createEqualsMethod(ResourceName resourceName, List<List<String>> tokenHierarchies, TypeStore typeStore) {
// Create method definition variables.
Variable oVariable = Variable.builder().setType(TypeNode.withReference(javaObjectReference)).setName("o").build();
VariableExpr argVarExpr = VariableExpr.builder().setIsDecl(false).setVariable(oVariable).build();
TypeNode thisClassType = typeStore.get(getThisClassName(resourceName));
ValueExpr thisValueExpr = ValueExpr.withValue(ThisObjectValue.withType(thisClassType));
ValueExpr trueValueExpr = ValueExpr.withValue(PrimitiveValue.builder().setType(TypeNode.BOOLEAN).setValue("true").build());
// Create first if statement's return expression
ReturnExpr returnTrueExpr = ReturnExpr.withExpr(trueValueExpr);
// Create second if statement's condition expression
RelationalOperationExpr oEqualsThisExpr = RelationalOperationExpr.equalToWithExprs(argVarExpr, thisValueExpr);
RelationalOperationExpr oNotEqualsNullExpr = RelationalOperationExpr.notEqualToWithExprs(argVarExpr, ValueExpr.createNullExpr());
MethodInvocationExpr getClassMethodInvocationExpr = MethodInvocationExpr.builder().setMethodName("getClass").build();
RelationalOperationExpr getClassEqualsExpr = RelationalOperationExpr.equalToWithExprs(getClassMethodInvocationExpr, getClassMethodInvocationExpr.toBuilder().setExprReferenceExpr(argVarExpr).build());
LogicalOperationExpr orLogicalExpr = LogicalOperationExpr.logicalOrWithExprs(oNotEqualsNullExpr, getClassEqualsExpr);
// Create second if statement's body assignment expression.
Variable thatVariable = Variable.builder().setName("that").setType(thisClassType).build();
VariableExpr thatVariableExpr = VariableExpr.builder().setIsDecl(false).setVariable(thatVariable).build();
CastExpr oCastExpr = CastExpr.builder().setExpr(argVarExpr).setType(thisClassType).build();
AssignmentExpr thatAssignmentExpr = AssignmentExpr.builder().setVariableExpr(thatVariableExpr.toBuilder().setIsDecl(true).build()).setValueExpr(oCastExpr).build();
// PubSub special-case handling - exclude _deleted-topic_.
List<List<String>> processedTokenHierarchies = tokenHierarchies.stream().filter(ts -> !ts.contains(ResourceNameConstants.DELETED_TOPIC_LITERAL)).collect(Collectors.toList());
// Create return expression in the second if statement's body.
Set<String> tokenSet = getTokenSet(processedTokenHierarchies);
Iterator<String> itToken = tokenSet.iterator();
Expr curTokenExpr = createObjectsEqualsForTokenMethodExpr(thisValueExpr, thatVariableExpr, Variable.builder().setType(TypeNode.STRING).setName(JavaStyle.toLowerCamelCase(itToken.next())).build());
while (itToken.hasNext()) {
Expr nextTokenExpr = createObjectsEqualsForTokenMethodExpr(thisValueExpr, thatVariableExpr, Variable.builder().setType(TypeNode.STRING).setName(JavaStyle.toLowerCamelCase(itToken.next())).build());
curTokenExpr = LogicalOperationExpr.logicalAndWithExprs(curTokenExpr, nextTokenExpr);
}
ReturnExpr secondIfReturnExpr = ReturnExpr.withExpr(curTokenExpr);
// Code: if (o == this) { return true;}
IfStatement firstIfStatement = IfStatement.builder().setConditionExpr(oEqualsThisExpr).setBody(Arrays.asList(ExprStatement.withExpr(returnTrueExpr))).build();
// Code: if (o != null || getClass() == o.getClass()) { FoobarName that = ((FoobarName) o);
// return ..}
IfStatement secondIfStatement = IfStatement.builder().setConditionExpr(orLogicalExpr).setBody(Arrays.asList(ExprStatement.withExpr(thatAssignmentExpr), ExprStatement.withExpr(secondIfReturnExpr))).build();
// Create method's return expression.
ValueExpr falseValueExpr = ValueExpr.withValue(PrimitiveValue.builder().setType(TypeNode.BOOLEAN).setValue("false").build());
return MethodDefinition.builder().setIsOverride(true).setScope(ScopeNode.PUBLIC).setArguments(argVarExpr.toBuilder().setIsDecl(true).build()).setReturnType(TypeNode.BOOLEAN).setName("equals").setReturnExpr(falseValueExpr).setBody(Arrays.asList(firstIfStatement, secondIfStatement)).build();
}
use of com.google.api.generator.engine.ast.CastExpr in project gapic-generator-java by googleapis.
the class JavaWriterVisitorTest method writeCastExpr_nested.
@Test
public void writeCastExpr_nested() {
Variable variable = Variable.builder().setType(TypeNode.STRING).setName("str").build();
VariableExpr varExpr = VariableExpr.builder().setVariable(variable).build();
CastExpr castExpr = CastExpr.builder().setType(TypeNode.withReference(ConcreteReference.withClazz(Object.class))).setExpr(varExpr).build();
castExpr = CastExpr.builder().setType(TypeNode.STRING).setExpr(castExpr).build();
castExpr.accept(writerVisitor);
assertEquals("((String) ((Object) str))", writerVisitor.write());
}
use of com.google.api.generator.engine.ast.CastExpr in project gapic-generator-java by googleapis.
the class JavaWriterVisitorTest method writeCastExpr_methodInvocation.
@Test
public void writeCastExpr_methodInvocation() {
TypeNode someType = TypeNode.withReference(VaporReference.builder().setName("SomeClass").setPakkage("com.google.api.some.pakkage").build());
MethodInvocationExpr methodExpr = MethodInvocationExpr.builder().setMethodName("foobar").setStaticReferenceType(someType).setReturnType(TypeNode.STRING).build();
CastExpr castExpr = CastExpr.builder().setType(TypeNode.withReference(ConcreteReference.withClazz(Object.class))).setExpr(methodExpr).build();
castExpr.accept(writerVisitor);
assertEquals("((Object) SomeClass.foobar())", writerVisitor.write());
}
use of com.google.api.generator.engine.ast.CastExpr in project gapic-generator-java by googleapis.
the class JavaCodeGeneratorTest method createAddShelfMethod.
private MethodDefinition createAddShelfMethod() {
Variable nameVar = createVarFromType(TypeNode.STRING, "name");
Variable seriesDoubleNumVar = createVarFromType(TypeNode.DOUBLE, "seriesDoubleNum");
CastExpr seriesNumDoubleToIntExpr = CastExpr.builder().setExpr(VariableExpr.withVariable(seriesDoubleNumVar)).setType(TypeNode.INT).build();
AssignmentExpr castSeriesNumExpr = createAssignmentExpr(createVarDeclExpr(seriesNumVar), seriesNumDoubleToIntExpr);
ReturnExpr maxValueReturnExpr = createReturnExpr("Series number equals to max int value.");
ReturnExpr duplicateKeyReturnExpr = createReturnExpr("Shelf is already existing in the map.");
// TODO: update the condition from `condition` to `seriesNum == MAX_VALUE`
IfStatement maxValueCheck = IfStatement.builder().setConditionExpr(VariableExpr.withVariable(createVarFromType(TypeNode.BOOLEAN, "condition"))).setBody(Arrays.asList(ExprStatement.withExpr(maxValueReturnExpr))).build();
NewObjectExpr newShelfExpr = createNewObjectExprWithArg(nameVar);
MethodInvocationExpr addShelfToListExpr = methodExprWithRefAndArg(shelfListVar, "add", Arrays.asList(newShelfExpr));
MethodInvocationExpr putShelfToMapExpr = methodExprWithRefAndArg(shelfMapVar, "put", Arrays.asList(VariableExpr.withVariable(nameVar), newShelfExpr));
MethodInvocationExpr mapContainsKey = methodExprWithRefArgAndReturn(shelfMapVar, Arrays.asList(VariableExpr.withVariable(nameVar)));
IfStatement mapContainsKeyCheck = IfStatement.builder().setConditionExpr(mapContainsKey).setBody(Arrays.asList(ExprStatement.withExpr(duplicateKeyReturnExpr))).build();
return MethodDefinition.builder().setAnnotations(Arrays.asList(AnnotationNode.OVERRIDE)).setName("addShelf").setReturnType(TypeNode.STRING).setReturnExpr(ValueExpr.withValue(StringObjectValue.withValue("Shelf added."))).setScope(ScopeNode.PUBLIC).setBody(Arrays.asList(ExprStatement.withExpr(castSeriesNumExpr), maxValueCheck, ExprStatement.withExpr(addShelfToListExpr), mapContainsKeyCheck, ExprStatement.withExpr(putShelfToMapExpr))).setArguments(Arrays.asList(createVarDeclExpr(nameVar), createVarDeclExpr(seriesDoubleNumVar))).build();
}
use of com.google.api.generator.engine.ast.CastExpr in project gapic-generator-java by googleapis.
the class JavaWriterVisitorTest method writeCastExpr_basic.
@Test
public void writeCastExpr_basic() {
Variable variable = Variable.builder().setType(TypeNode.STRING).setName("str").build();
VariableExpr varExpr = VariableExpr.builder().setVariable(variable).build();
CastExpr castExpr = CastExpr.builder().setType(TypeNode.withReference(ConcreteReference.withClazz(Object.class))).setExpr(varExpr).build();
castExpr.accept(writerVisitor);
assertEquals("((Object) str)", writerVisitor.write());
}
Aggregations