Search in sources :

Example 1 with AssignmentOperationExpr

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");
}
Also used : ValueExpr(com.google.api.generator.engine.ast.ValueExpr) AssignmentOperationExpr(com.google.api.generator.engine.ast.AssignmentOperationExpr) VariableExpr(com.google.api.generator.engine.ast.VariableExpr) Test(org.junit.Test)

Example 2 with AssignmentOperationExpr

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)");
}
Also used : MethodInvocationExpr(com.google.api.generator.engine.ast.MethodInvocationExpr) AssignmentOperationExpr(com.google.api.generator.engine.ast.AssignmentOperationExpr) VariableExpr(com.google.api.generator.engine.ast.VariableExpr) TypeNode(com.google.api.generator.engine.ast.TypeNode) Test(org.junit.Test)

Example 3 with AssignmentOperationExpr

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();
}
Also used : Arrays(java.util.Arrays) Reference(com.google.api.generator.engine.ast.Reference) PathTemplate(com.google.api.pathtemplate.PathTemplate) ThisObjectValue(com.google.api.generator.engine.ast.ThisObjectValue) Variable(com.google.api.generator.engine.ast.Variable) StringObjectValue(com.google.api.generator.engine.ast.StringObjectValue) LogicalOperationExpr(com.google.api.generator.engine.ast.LogicalOperationExpr) Generated(javax.annotation.Generated) MethodDefinition(com.google.api.generator.engine.ast.MethodDefinition) TypeStore(com.google.api.generator.gapic.composer.store.TypeStore) PrimitiveValue(com.google.api.generator.engine.ast.PrimitiveValue) CommentComposer(com.google.api.generator.gapic.composer.comment.CommentComposer) Map(java.util.Map) ConcreteReference(com.google.api.generator.engine.ast.ConcreteReference) ImmutableMap(com.google.common.collect.ImmutableMap) ForStatement(com.google.api.generator.engine.ast.ForStatement) Set(java.util.Set) ScopeNode(com.google.api.generator.engine.ast.ScopeNode) Collectors(java.util.stream.Collectors) Objects(java.util.Objects) ResourceNameConstants(com.google.api.generator.gapic.utils.ResourceNameConstants) List(java.util.List) GapicContext(com.google.api.generator.gapic.model.GapicContext) ValueExpr(com.google.api.generator.engine.ast.ValueExpr) IfStatement(com.google.api.generator.engine.ast.IfStatement) BetaApi(com.google.api.core.BetaApi) TypeNode(com.google.api.generator.engine.ast.TypeNode) ClassDefinition(com.google.api.generator.engine.ast.ClassDefinition) ReturnExpr(com.google.api.generator.engine.ast.ReturnExpr) HashMap(java.util.HashMap) JavaDocComment(com.google.api.generator.engine.ast.JavaDocComment) Function(java.util.function.Function) ArrayList(java.util.ArrayList) NewObjectExpr(com.google.api.generator.engine.ast.NewObjectExpr) Expr(com.google.api.generator.engine.ast.Expr) TernaryExpr(com.google.api.generator.engine.ast.TernaryExpr) RelationalOperationExpr(com.google.api.generator.engine.ast.RelationalOperationExpr) SynchronizedStatement(com.google.api.generator.engine.ast.SynchronizedStatement) ValidationException(com.google.api.pathtemplate.ValidationException) LinkedHashSet(java.util.LinkedHashSet) AssignmentExpr(com.google.api.generator.engine.ast.AssignmentExpr) VariableExpr(com.google.api.generator.engine.ast.VariableExpr) Iterator(java.util.Iterator) CastExpr(com.google.api.generator.engine.ast.CastExpr) CommentStatement(com.google.api.generator.engine.ast.CommentStatement) ExprStatement(com.google.api.generator.engine.ast.ExprStatement) AnnotationNode(com.google.api.generator.engine.ast.AnnotationNode) GapicClass(com.google.api.generator.gapic.model.GapicClass) ResourceName(com.google.api.generator.gapic.model.ResourceName) Statement(com.google.api.generator.engine.ast.Statement) JavaStyle(com.google.api.generator.gapic.utils.JavaStyle) AssignmentOperationExpr(com.google.api.generator.engine.ast.AssignmentOperationExpr) MethodInvocationExpr(com.google.api.generator.engine.ast.MethodInvocationExpr) ThrowExpr(com.google.api.generator.engine.ast.ThrowExpr) Preconditions(com.google.common.base.Preconditions) VisibleForTesting(com.google.common.annotations.VisibleForTesting) Collections(java.util.Collections) ValueExpr(com.google.api.generator.engine.ast.ValueExpr) Variable(com.google.api.generator.engine.ast.Variable) ForStatement(com.google.api.generator.engine.ast.ForStatement) IfStatement(com.google.api.generator.engine.ast.IfStatement) SynchronizedStatement(com.google.api.generator.engine.ast.SynchronizedStatement) CommentStatement(com.google.api.generator.engine.ast.CommentStatement) ExprStatement(com.google.api.generator.engine.ast.ExprStatement) Statement(com.google.api.generator.engine.ast.Statement) ArrayList(java.util.ArrayList) AssignmentExpr(com.google.api.generator.engine.ast.AssignmentExpr) AssignmentOperationExpr(com.google.api.generator.engine.ast.AssignmentOperationExpr) VariableExpr(com.google.api.generator.engine.ast.VariableExpr) List(java.util.List) ArrayList(java.util.ArrayList)

Aggregations

AssignmentOperationExpr (com.google.api.generator.engine.ast.AssignmentOperationExpr)3 VariableExpr (com.google.api.generator.engine.ast.VariableExpr)3 MethodInvocationExpr (com.google.api.generator.engine.ast.MethodInvocationExpr)2 TypeNode (com.google.api.generator.engine.ast.TypeNode)2 ValueExpr (com.google.api.generator.engine.ast.ValueExpr)2 Test (org.junit.Test)2 BetaApi (com.google.api.core.BetaApi)1 AnnotationNode (com.google.api.generator.engine.ast.AnnotationNode)1 AssignmentExpr (com.google.api.generator.engine.ast.AssignmentExpr)1 CastExpr (com.google.api.generator.engine.ast.CastExpr)1 ClassDefinition (com.google.api.generator.engine.ast.ClassDefinition)1 CommentStatement (com.google.api.generator.engine.ast.CommentStatement)1 ConcreteReference (com.google.api.generator.engine.ast.ConcreteReference)1 Expr (com.google.api.generator.engine.ast.Expr)1 ExprStatement (com.google.api.generator.engine.ast.ExprStatement)1 ForStatement (com.google.api.generator.engine.ast.ForStatement)1 IfStatement (com.google.api.generator.engine.ast.IfStatement)1 JavaDocComment (com.google.api.generator.engine.ast.JavaDocComment)1 LogicalOperationExpr (com.google.api.generator.engine.ast.LogicalOperationExpr)1 MethodDefinition (com.google.api.generator.engine.ast.MethodDefinition)1