Search in sources :

Example 21 with Reference

use of com.google.api.generator.engine.ast.Reference in project gapic-generator-java by googleapis.

the class JavaWriterVisitor method visit.

@Override
public void visit(ConcreteReference reference) {
    if (reference.isWildcard()) {
        buffer.append(QUESTION_MARK);
        if (reference.wildcardUpperBound() != null) {
            // Handle the upper bound.
            buffer.append(SPACE);
            buffer.append(EXTENDS);
            buffer.append(SPACE);
            reference.wildcardUpperBound().accept(this);
        }
        return;
    }
    String pakkage = reference.pakkage();
    String shortName = reference.name();
    if (reference.useFullName() || importWriterVisitor.collidesWithImport(pakkage, shortName)) {
        buffer.append(pakkage);
        buffer.append(DOT);
    }
    if (reference.hasEnclosingClass() && !reference.isStaticImport()) {
        buffer.append(String.join(DOT, reference.enclosingClassNames()));
        buffer.append(DOT);
    }
    buffer.append(reference.simpleName());
    if (!reference.generics().isEmpty()) {
        buffer.append(LEFT_ANGLE);
        for (int i = 0; i < reference.generics().size(); i++) {
            Reference r = reference.generics().get(i);
            r.accept(this);
            if (i < reference.generics().size() - 1) {
                buffer.append(COMMA);
                buffer.append(SPACE);
            }
        }
        buffer.append(RIGHT_ANGLE);
    }
}
Also used : Reference(com.google.api.generator.engine.ast.Reference) ConcreteReference(com.google.api.generator.engine.ast.ConcreteReference) VaporReference(com.google.api.generator.engine.ast.VaporReference)

Example 22 with Reference

use of com.google.api.generator.engine.ast.Reference in project gapic-generator-java by googleapis.

the class ImportWriterVisitorTest method writeLambdaExprImports.

@Test
public void writeLambdaExprImports() {
    // Similar to method defnitions.
    Reference mapRef = ConcreteReference.withClazz(Map.class);
    List<VariableExpr> arguments = Arrays.asList(VariableExpr.builder().setVariable(createVariable("x", TypeNode.withReference(mapRef))).setIsDecl(true).setTemplateObjects(Arrays.asList("K", TypeNode.withReference(ConcreteReference.withClazz(AssignmentExpr.class)))).build(), VariableExpr.builder().setVariable(createVariable("y", TypeNode.withReference(mapRef))).setIsDecl(true).setTemplateObjects(Arrays.asList("T", "V")).build());
    Statement bodyStatement = ExprStatement.withExpr(MethodInvocationExpr.builder().setMethodName("doStuff").setReturnType(TypeNode.withReference(ConcreteReference.withClazz(Arrays.class))).build());
    TypeNode returnType = TypeNode.withReference(ConcreteReference.withClazz(Expr.class));
    LambdaExpr lambdaExpr = LambdaExpr.builder().setArguments(arguments).setBody(Arrays.asList(bodyStatement)).setReturnExpr(MethodInvocationExpr.builder().setMethodName("foobar").setReturnType(returnType).build()).build();
    lambdaExpr.accept(writerVisitor);
    assertEquals(LineFormatter.lines("import com.google.api.generator.engine.ast.AssignmentExpr;\n", "import com.google.api.generator.engine.ast.Expr;\n", "import java.util.Arrays;\n", "import java.util.Map;\n\n"), writerVisitor.write());
}
Also used : ReferenceConstructorExpr(com.google.api.generator.engine.ast.ReferenceConstructorExpr) LogicalOperationExpr(com.google.api.generator.engine.ast.LogicalOperationExpr) ValueExpr(com.google.api.generator.engine.ast.ValueExpr) EnumRefExpr(com.google.api.generator.engine.ast.EnumRefExpr) ReturnExpr(com.google.api.generator.engine.ast.ReturnExpr) 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) UnaryOperationExpr(com.google.api.generator.engine.ast.UnaryOperationExpr) AssignmentExpr(com.google.api.generator.engine.ast.AssignmentExpr) VariableExpr(com.google.api.generator.engine.ast.VariableExpr) InstanceofExpr(com.google.api.generator.engine.ast.InstanceofExpr) ArithmeticOperationExpr(com.google.api.generator.engine.ast.ArithmeticOperationExpr) AnonymousClassExpr(com.google.api.generator.engine.ast.AnonymousClassExpr) LambdaExpr(com.google.api.generator.engine.ast.LambdaExpr) MethodInvocationExpr(com.google.api.generator.engine.ast.MethodInvocationExpr) ThrowExpr(com.google.api.generator.engine.ast.ThrowExpr) Reference(com.google.api.generator.engine.ast.Reference) ConcreteReference(com.google.api.generator.engine.ast.ConcreteReference) VaporReference(com.google.api.generator.engine.ast.VaporReference) EmptyLineStatement(com.google.api.generator.engine.ast.EmptyLineStatement) 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) LambdaExpr(com.google.api.generator.engine.ast.LambdaExpr) VariableExpr(com.google.api.generator.engine.ast.VariableExpr) AssignmentExpr(com.google.api.generator.engine.ast.AssignmentExpr) TypeNode(com.google.api.generator.engine.ast.TypeNode) Test(org.junit.Test)

Example 23 with Reference

use of com.google.api.generator.engine.ast.Reference in project gapic-generator-java by googleapis.

the class TypeParser method parseEnumReference.

@VisibleForTesting
static Reference parseEnumReference(@Nonnull EnumDescriptor enumDescriptor) {
    // This is similar to parseMessageReference, but we make it a separate method because
    // EnumDescriptor and Descriptor are sibling types.
    FileOptions fileOptions = enumDescriptor.getFile().getOptions();
    String javaOuterClassname = fileOptions.hasJavaOuterClassname() ? fileOptions.getJavaOuterClassname() : null;
    // classname option set either.
    if (!fileOptions.getJavaMultipleFiles() && !fileOptions.hasJavaOuterClassname()) {
        String fullFilePath = JavaStyle.toUpperCamelCase(enumDescriptor.getFile().getName());
        javaOuterClassname = JavaStyle.toUpperCamelCase(fullFilePath.substring(fullFilePath.lastIndexOf("/") + 1, fullFilePath.lastIndexOf(".")));
    }
    boolean hasJavaOuterClass = !Strings.isNullOrEmpty(javaOuterClassname) && !fileOptions.getJavaMultipleFiles();
    List<String> outerNestedTypeNames = new ArrayList<>();
    if (hasJavaOuterClass) {
        outerNestedTypeNames.add(javaOuterClassname);
    }
    Descriptor containingType = enumDescriptor.getContainingType();
    // Handles nesting.
    while (containingType != null) {
        // Outermost type in the nested type hierarchy lies at index 0.
        // If the Java outer proto class has already been set, apapend after it at index 1.
        outerNestedTypeNames.add(hasJavaOuterClass ? 1 : 0, containingType.getName());
        containingType = containingType.getContainingType();
    }
    String pakkage = getPackage(enumDescriptor.getFile());
    Reference enumReference = VaporReference.builder().setName(enumDescriptor.getName()).setPakkage(pakkage).setEnclosingClassNames(outerNestedTypeNames).build();
    String protoPackage = enumDescriptor.getFile().getPackage();
    String enumFullName = enumDescriptor.getFullName();
    if (hasJavaOuterClass) {
        int packageIndex = protoPackage.length();
        enumFullName = String.format("%s.%s.%s", enumFullName.substring(0, packageIndex), javaOuterClassname, enumFullName.substring(packageIndex + 1));
    }
    Preconditions.checkState(enumReference.fullName().replace(pakkage, protoPackage).equals(enumFullName), String.format("Parsed enum name %s does not match actual name %s", enumReference.fullName().replace(pakkage, ""), enumDescriptor.getFullName().replace(protoPackage, "")));
    return enumReference;
}
Also used : ConcreteReference(com.google.api.generator.engine.ast.ConcreteReference) VaporReference(com.google.api.generator.engine.ast.VaporReference) Reference(com.google.api.generator.engine.ast.Reference) ArrayList(java.util.ArrayList) Descriptor(com.google.protobuf.Descriptors.Descriptor) FieldDescriptor(com.google.protobuf.Descriptors.FieldDescriptor) EnumDescriptor(com.google.protobuf.Descriptors.EnumDescriptor) FileDescriptor(com.google.protobuf.Descriptors.FileDescriptor) ByteString(com.google.protobuf.ByteString) FileOptions(com.google.protobuf.DescriptorProtos.FileOptions) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 24 with Reference

use of com.google.api.generator.engine.ast.Reference in project gapic-generator-java by googleapis.

the class TypeParser method parseMessageReference.

@VisibleForTesting
static Reference parseMessageReference(@Nonnull Descriptor messageDescriptor) {
    List<String> outerNestedTypeNames = new ArrayList<>();
    FileOptions fileOptions = messageDescriptor.getFile().getOptions();
    String javaOuterClassname = fileOptions.hasJavaOuterClassname() ? fileOptions.getJavaOuterClassname() : null;
    // classname option set either.
    if (!fileOptions.getJavaMultipleFiles() && !fileOptions.hasJavaOuterClassname()) {
        String fullFilePath = JavaStyle.toUpperCamelCase(messageDescriptor.getFile().getName());
        javaOuterClassname = JavaStyle.toUpperCamelCase(fullFilePath.substring(fullFilePath.lastIndexOf("/") + 1, fullFilePath.lastIndexOf(".")));
    }
    boolean hasJavaOuterClass = !Strings.isNullOrEmpty(javaOuterClassname) && !fileOptions.getJavaMultipleFiles();
    if (hasJavaOuterClass) {
        outerNestedTypeNames.add(javaOuterClassname);
    }
    Descriptor containingType = messageDescriptor.getContainingType();
    // Handles nesting.
    while (containingType != null) {
        // Outermost type in the nested type hierarchy lies at index 0.
        // If the Java outer proto class has already been set, apapend after it at index 1.
        outerNestedTypeNames.add(hasJavaOuterClass ? 1 : 0, containingType.getName());
        containingType = containingType.getContainingType();
    }
    String pakkage = getPackage(messageDescriptor.getFile());
    Reference messageReference = VaporReference.builder().setName(messageDescriptor.getName()).setPakkage(pakkage).setEnclosingClassNames(outerNestedTypeNames).build();
    String protoPackage = messageDescriptor.getFile().getPackage();
    String messageFullName = messageDescriptor.getFullName();
    if (hasJavaOuterClass) {
        int packageIndex = protoPackage.length();
        messageFullName = String.format("%s.%s.%s", messageFullName.substring(0, packageIndex), javaOuterClassname, messageFullName.substring(packageIndex + 1));
    }
    Preconditions.checkState(messageReference.fullName().replace(pakkage, protoPackage).equals(messageFullName), String.format("Parsed message name %s does not match actual name %s", messageReference.fullName().replace(pakkage, ""), messageFullName.replace(protoPackage, "")));
    return messageReference;
}
Also used : ConcreteReference(com.google.api.generator.engine.ast.ConcreteReference) VaporReference(com.google.api.generator.engine.ast.VaporReference) Reference(com.google.api.generator.engine.ast.Reference) ArrayList(java.util.ArrayList) Descriptor(com.google.protobuf.Descriptors.Descriptor) FieldDescriptor(com.google.protobuf.Descriptors.FieldDescriptor) EnumDescriptor(com.google.protobuf.Descriptors.EnumDescriptor) FileDescriptor(com.google.protobuf.Descriptors.FileDescriptor) ByteString(com.google.protobuf.ByteString) FileOptions(com.google.protobuf.DescriptorProtos.FileOptions) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 25 with Reference

use of com.google.api.generator.engine.ast.Reference in project gapic-generator-java by googleapis.

the class JavaWriterVisitorTest method writeTryCatchStatement_simple.

@Test
public void writeTryCatchStatement_simple() {
    Reference exceptionReference = ConcreteReference.withClazz(IllegalArgumentException.class);
    TypeNode type = TypeNode.withReference(exceptionReference);
    VariableExpr variableExpr = VariableExpr.builder().setVariable(createVariable("e", type)).setIsDecl(true).build();
    TryCatchStatement tryCatch = TryCatchStatement.builder().setTryBody(Arrays.asList(ExprStatement.withExpr(createAssignmentExpr("x", "3", TypeNode.INT)))).addCatch(variableExpr, Collections.emptyList()).build();
    tryCatch.accept(writerVisitor);
    assertEquals(String.format("%s%s%s%s", "try {\n", "int x = 3;\n", "} catch (IllegalArgumentException e) {\n", "}\n"), writerVisitor.write());
}
Also used : Reference(com.google.api.generator.engine.ast.Reference) ConcreteReference(com.google.api.generator.engine.ast.ConcreteReference) VaporReference(com.google.api.generator.engine.ast.VaporReference) TryCatchStatement(com.google.api.generator.engine.ast.TryCatchStatement) VariableExpr(com.google.api.generator.engine.ast.VariableExpr) TypeNode(com.google.api.generator.engine.ast.TypeNode) Test(org.junit.Test)

Aggregations

Reference (com.google.api.generator.engine.ast.Reference)29 ConcreteReference (com.google.api.generator.engine.ast.ConcreteReference)27 TypeNode (com.google.api.generator.engine.ast.TypeNode)21 VaporReference (com.google.api.generator.engine.ast.VaporReference)21 VariableExpr (com.google.api.generator.engine.ast.VariableExpr)17 AssignmentExpr (com.google.api.generator.engine.ast.AssignmentExpr)13 ArrayList (java.util.ArrayList)13 Test (org.junit.Test)13 MethodDefinition (com.google.api.generator.engine.ast.MethodDefinition)12 MethodInvocationExpr (com.google.api.generator.engine.ast.MethodInvocationExpr)12 CommentStatement (com.google.api.generator.engine.ast.CommentStatement)11 Expr (com.google.api.generator.engine.ast.Expr)11 ExprStatement (com.google.api.generator.engine.ast.ExprStatement)11 Statement (com.google.api.generator.engine.ast.Statement)11 ValueExpr (com.google.api.generator.engine.ast.ValueExpr)11 AnnotationNode (com.google.api.generator.engine.ast.AnnotationNode)10 ClassDefinition (com.google.api.generator.engine.ast.ClassDefinition)10 NewObjectExpr (com.google.api.generator.engine.ast.NewObjectExpr)10 RelationalOperationExpr (com.google.api.generator.engine.ast.RelationalOperationExpr)10 TernaryExpr (com.google.api.generator.engine.ast.TernaryExpr)10