use of com.google.api.generator.engine.ast.AssignmentOperationExpr in project gapic-generator-java by googleapis.
the class JavaWriterVisitorTest method writeAssignmentOperationExpr_multiplyAssignment.
@Test
public void writeAssignmentOperationExpr_multiplyAssignment() {
VariableExpr lhsExpr = createVariableExpr("h", TypeNode.INT);
ValueExpr rhsExpr = ValueExpr.withValue(PrimitiveValue.builder().setType(TypeNode.INT).setValue("1000003").build());
AssignmentOperationExpr assignmentOperationExpr = AssignmentOperationExpr.multiplyAssignmentWithExprs(lhsExpr, rhsExpr);
assignmentOperationExpr.accept(writerVisitor);
assertThat(writerVisitor.write()).isEqualTo("h *= 1000003");
}
use of com.google.api.generator.engine.ast.AssignmentOperationExpr in project gapic-generator-java by googleapis.
the class JavaWriterVisitorTest method writeAssignmentOperationExpr_xorAssignment.
@Test
public void writeAssignmentOperationExpr_xorAssignment() {
VariableExpr lhsExpr = createVariableExpr("h", TypeNode.INT);
TypeNode objectType = TypeNode.withReference(VaporReference.builder().setName("Objects").setPakkage("java.lang.Object").build());
MethodInvocationExpr rhsExpr = MethodInvocationExpr.builder().setReturnType(TypeNode.INT).setMethodName("hashCode").setStaticReferenceType(objectType).setArguments(Arrays.asList(VariableExpr.withVariable(createVariable("fixedValue", TypeNode.OBJECT)))).build();
AssignmentOperationExpr assignmentOperationExpr = AssignmentOperationExpr.xorAssignmentWithExprs(lhsExpr, rhsExpr);
assignmentOperationExpr.accept(writerVisitor);
assertThat(writerVisitor.write()).isEqualTo("h ^= Objects.hashCode(fixedValue)");
}
use of com.google.api.generator.engine.ast.AssignmentOperationExpr in project gapic-generator-java by googleapis.
the class ResourceNameHelperClassComposer method createHashCodeMethod.
private static MethodDefinition createHashCodeMethod(List<List<String>> tokenHierarchies) {
List<Statement> assignmentBody = new ArrayList<>();
// code: int h = 1;
Variable hVar = Variable.builder().setType(TypeNode.INT).setName("h").build();
VariableExpr hVarExpr = VariableExpr.builder().setVariable(hVar).build();
ValueExpr hValueExpr = ValueExpr.withValue(PrimitiveValue.builder().setType(TypeNode.INT).setValue("1").build());
AssignmentExpr hAssignmentExpr = AssignmentExpr.builder().setVariableExpr(hVarExpr.toBuilder().setIsDecl(true).build()).setValueExpr(hValueExpr).build();
assignmentBody.add(ExprStatement.withExpr(hAssignmentExpr));
// code: h *= 1000003;
// code: h ^= Objects.hashCode(...);
ValueExpr numValueExpr = ValueExpr.withValue(PrimitiveValue.builder().setType(TypeNode.INT).setValue("1000003").build());
AssignmentOperationExpr multiplyAssignmentOpExpr = AssignmentOperationExpr.multiplyAssignmentWithExprs(hVarExpr, numValueExpr);
// PubSub special-case handling - exclude _deleted-topic_.
List<List<String>> processedTokenHierarchies = tokenHierarchies.stream().filter(ts -> !ts.contains(ResourceNameConstants.DELETED_TOPIC_LITERAL)).collect(Collectors.toList());
// If it has variants, add the multiply and xor assignment operation exprs for fixedValue.
boolean hasVariants = processedTokenHierarchies.size() > 1;
if (hasVariants) {
VariableExpr fixedValueVarExpr = FIXED_CLASS_VARS.get("fixedValue");
assignmentBody.add(ExprStatement.withExpr(multiplyAssignmentOpExpr));
assignmentBody.add(ExprStatement.withExpr(AssignmentOperationExpr.xorAssignmentWithExprs(hVarExpr, createObjectsHashCodeForVarMethod(fixedValueVarExpr))));
}
// Add the multiply and xor assignment operation exprs for tokens.
Set<String> tokenSet = getTokenSet(processedTokenHierarchies);
tokenSet.stream().forEach(token -> {
VariableExpr tokenVarExpr = VariableExpr.withVariable(Variable.builder().setName(JavaStyle.toLowerCamelCase(token)).setType(TypeNode.STRING).build());
assignmentBody.add(ExprStatement.withExpr(multiplyAssignmentOpExpr));
assignmentBody.add(ExprStatement.withExpr(AssignmentOperationExpr.xorAssignmentWithExprs(hVarExpr, createObjectsHashCodeForVarMethod(tokenVarExpr))));
});
return MethodDefinition.builder().setIsOverride(true).setScope(ScopeNode.PUBLIC).setReturnType(TypeNode.INT).setName("hashCode").setBody(assignmentBody).setReturnExpr(hVarExpr).build();
}
Aggregations