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);
}
}
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());
}
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;
}
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;
}
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());
}
Aggregations