Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -35,39 +35,40 @@
return new AutoValue_CastExpr.Builder();
}

@AutoValue.Builder
public abstract static class Builder {
public abstract Builder setType(TypeNode type);

public abstract Builder setExpr(Expr expr);

abstract CastExpr autoBuild();

public CastExpr build() {
CastExpr castExpr = autoBuild();
TypeNode castType = castExpr.type();
TypeNode exprType = castExpr.expr().type();
Preconditions.checkState(
!castType.equals(TypeNode.VOID) && !exprType.equals(TypeNode.VOID),
"Casting cannot be done on or to void types");

// Only numeric types can be casted around.
if (castType.isPrimitiveType() && exprType.isPrimitiveType()) {
// Check that the types are either both booleans or both not booleans.
Preconditions.checkState(
!(castType.equals(TypeNode.BOOLEAN) ^ exprType.equals(TypeNode.BOOLEAN)),
"Numeric and boolean types are not inter-castable");
} else {
boolean isExprTypeReferenceOrNull =
exprType.equals(TypeNode.NULL) || TypeNode.isReferenceType(exprType);
boolean isValidReferenceTypeCast =
TypeNode.isReferenceType(castType) && isExprTypeReferenceOrNull;
boolean isEqualType = castType.equals(exprType);
Preconditions.checkState(
isEqualType || isValidReferenceTypeCast,
"Boxed type and primitive type are inter-castable, otherwise reference types can only be casted to reference types or null type.");
"Boxed type and primitive type are inter-castable, otherwise reference types can only"
+ " be casted to reference types or null type.");
}
return castExpr;
}
}

Check warning on line 73 in sdk-platform-java/gapic-generator-java/src/main/java/com/google/api/generator/engine/ast/CastExpr.java

View check run for this annotation

SonarQubeCloud / [gapic-generator-java-root] SonarCloud Code Analysis

Remove redundant annotation @NullMarked at class level as inside scope annotation @NullMarked at class level.

See more on https://sonarcloud.io/project/issues?id=googleapis_google-cloud-java_generator&issues=AaIypa-jz9GJYIAwQmeV&open=AaIypa-jz9GJYIAwQmeV&pullRequest=15325
}
Original file line number Diff line number Diff line change
Expand Up @@ -69,15 +69,17 @@ public class CommentComposer {
Arrays.asList(
CommentStatement.withComment(
LineComment.withComment(
"This snippet has been automatically generated and should be regarded as a code template only.")),
"This snippet has been automatically generated and should be regarded as a code"
+ " template only.")),
CommentStatement.withComment(
LineComment.withComment("It will require modifications to work:")),
CommentStatement.withComment(
LineComment.withComment(
"- It may require correct/in-range values for request initialization.")),
CommentStatement.withComment(
LineComment.withComment(
"- It may require specifying regional endpoints when creating the service client as shown in")),
"- It may require specifying regional endpoints when creating the service client"
+ " as shown in")),
CommentStatement.withComment(
LineComment.withComment(
"https://cloud.google.com/java/docs/setup#configure_endpoints_for_the_client_library")));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,8 @@ void createJavaDocComment_throwsAndDeprecatedAndInternalAndReturn() {
.build();
String expected =
LineFormatter.lines(
"<p> <b>Warning: </b>This method is for internal use only. Please do not use it directly.\n",
"<p> <b>Warning: </b>This method is for internal use only. Please do not use it"
+ " directly.\n",
"@throws java.lang.RuntimeException if the remote call fails.\n",
"@deprecated Use the {@link ShelfBookName} class instead.\n",
"@return This is the correct method return text.");
Expand Down Expand Up @@ -276,7 +277,8 @@ void createJavaDocComment_allComponents() {
"<li> A request object method.\n",
"<li> A callable method.\n",
"</ol>\n",
"<p> <b>Warning: </b>This method is for internal use only. Please do not use it directly.\n",
"<p> <b>Warning: </b>This method is for internal use only. Please do not use it"
+ " directly.\n",
"@param shelfName The name of the shelf where books are published to.\n",
"@param shelf The shelf to create.\n",
"@throws com.google.api.gax.rpc.ApiException if the remote call fails.\n",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,8 @@ void writeAnnotation_withMultipleNamedDescriptions() {
.build();
annotation.accept(writerVisitor);
assertEquals(
"@ConditionalOnProperty(value = \"spring.cloud.gcp.language.enabled\", matchIfMissing = false)\n",
"@ConditionalOnProperty(value = \"spring.cloud.gcp.language.enabled\", matchIfMissing ="
+ " false)\n",
writerVisitor.write());
}

Expand Down Expand Up @@ -966,7 +967,8 @@ void writeFailingComment_specialChar() {
JavaDocComment javaDocComment =
JavaDocComment.builder()
.addUnescapedComment(
"This resource reference needs to be CommentFormatted or it will fail: `bookShelves/*/`")
"This resource reference needs to be CommentFormatted or it will fail:"
+ " `bookShelves/*/`")
.build();

FormatException exceptionForIncorrectlyFormattedComment =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -333,8 +333,7 @@ void lroBuilderExpr() {
builderExpr.accept(writerVisitor);
String expected =
LineFormatter.lines(
"builder.waitOperationSettings()"
+ ".setInitialCallSettings(UnaryCallSettings.<WaitRequest,"
"builder.waitOperationSettings().setInitialCallSettings(UnaryCallSettings.<WaitRequest,"
+ " OperationSnapshot>newUnaryCallSettingsBuilder()"
+ ".setRetryableCodes(RETRYABLE_CODE_DEFINITIONS.get(\"no_retry_0_codes\"))"
+ ".setRetrySettings(RETRY_PARAM_DEFINITIONS.get(\"no_retry_0_params\")).build())"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -441,7 +441,8 @@ Blurb Resource contains four patterns (in order of):
expr.accept(writerVisitor);
// Matches with the default pattern in the HttpBindings and uses the variables in the pattern
assertEquals(
"Blurb.newBuilder().setName(BlurbName.ofRoomBlurbName(\"[ROOM]\", \"[BLURB]\").toString()).build()",
"Blurb.newBuilder().setName(BlurbName.ofRoomBlurbName(\"[ROOM]\","
+ " \"[BLURB]\").toString()).build()",
writerVisitor.write());
}

Expand Down Expand Up @@ -476,7 +477,8 @@ Blurb Resource contains four patterns (in order of):
// Because the default pattern does not match, it will attempt to match with patterns in the
// additional bindings and use variables for any pattern that matches
assertEquals(
"Blurb.newBuilder().setName(BlurbName.ofUserBlurbName(\"[USER]\", \"[BLURB]\").toString()).build()",
"Blurb.newBuilder().setName(BlurbName.ofUserBlurbName(\"[USER]\","
+ " \"[BLURB]\").toString()).build()",
writerVisitor.write());
}

Expand Down Expand Up @@ -511,7 +513,8 @@ Blurb Resource contains four patterns (in order of):
// If no pattern matches (default and additional bindings), it will simply pick the first
// resource pattern in the resource definition.
assertEquals(
"Blurb.newBuilder().setName(BlurbName.ofUserLegacyUserBlurbName(\"[USER]\", \"[LEGACY_USER]\", \"[BLURB]\").toString()).build()",
"Blurb.newBuilder().setName(BlurbName.ofUserLegacyUserBlurbName(\"[USER]\","
+ " \"[LEGACY_USER]\", \"[BLURB]\").toString()).build()",
writerVisitor.write());
}

Expand All @@ -530,7 +533,8 @@ void defaultValue_resourceNameMultiplePatterns_noHttpBinding() {
// If no pattern matches (default and additional bindings), it will simply pick the first
// resource pattern in the resource definition.
assertEquals(
"Blurb.newBuilder().setName(BlurbName.ofUserLegacyUserBlurbName(\"[USER]\", \"[LEGACY_USER]\", \"[BLURB]\").toString()).build()",
"Blurb.newBuilder().setName(BlurbName.ofUserLegacyUserBlurbName(\"[USER]\","
+ " \"[LEGACY_USER]\", \"[BLURB]\").toString()).build()",
writerVisitor.write());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,12 @@ void writeInlineSample() {
String result = SampleCodeWriter.writeInlineSample(testingSampleStatements);
String expected =
LineFormatter.lines(
"// This snippet has been automatically generated and should be regarded as a code template only.\n",
"// This snippet has been automatically generated and should be regarded as a code"
+ " template only.\n",
"// It will require modifications to work:\n",
"// - It may require correct/in-range values for request initialization.\n",
"// - It may require specifying regional endpoints when creating the service client as shown in\n",
"// - It may require specifying regional endpoints when creating the service client as"
+ " shown in\n",
"// https://cloud.google.com/java/docs/setup#configure_endpoints_for_the_client_library\n",
"ClientSettings clientSettings = ClientSettings.newBuilder().build();\n",
"try (boolean aBool = false) {\n",
Expand Down Expand Up @@ -142,11 +144,14 @@ void writeExecutableSample() {
" }\n",
"\n",
" public static void syncWriteExecutableSample() throws Exception {\n",
" // This snippet has been automatically generated and should be regarded as a code template only.\n",
" // This snippet has been automatically generated and should be regarded as a code"
+ " template only.\n",
" // It will require modifications to work:\n",
" // - It may require correct/in-range values for request initialization.\n",
" // - It may require specifying regional endpoints when creating the service client as shown in\n",
" // https://cloud.google.com/java/docs/setup#configure_endpoints_for_the_client_library\n",
" // - It may require specifying regional endpoints when creating the service client"
+ " as shown in\n",
" //"
+ " https://cloud.google.com/java/docs/setup#configure_endpoints_for_the_client_library\n",
" ClientSettings clientSettings = ClientSettings.newBuilder().build();\n",
" try (boolean aBool = false) {\n",
" int x = 3;\n",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,12 @@ void createInlineSample() {
String sampleResult = writeSample(SampleComposer.composeInlineSample(sampleBody));
String expected =
LineFormatter.lines(
"// This snippet has been automatically generated and should be regarded as a code template only.\n",
"// This snippet has been automatically generated and should be regarded as a code"
+ " template only.\n",
"// It will require modifications to work:\n",
"// - It may require correct/in-range values for request initialization.\n",
"// - It may require specifying regional endpoints when creating the service client as shown in\n",
"// - It may require specifying regional endpoints when creating the service client as"
+ " shown in\n",
"// https://cloud.google.com/java/docs/setup#configure_endpoints_for_the_client_library\n",
"System.out.println(\"testing\");");

Expand Down Expand Up @@ -82,12 +84,16 @@ void createExecutableSampleEmptyStatementSample() {
" syncCreateExecutableSampleEmptyStatementSample();\n",
" }\n",
"\n",
" public static void syncCreateExecutableSampleEmptyStatementSample() throws Exception {\n",
" // This snippet has been automatically generated and should be regarded as a code template only.\n",
" public static void syncCreateExecutableSampleEmptyStatementSample() throws Exception"
+ " {\n",
" // This snippet has been automatically generated and should be regarded as a code"
+ " template only.\n",
" // It will require modifications to work:\n",
" // - It may require correct/in-range values for request initialization.\n",
" // - It may require specifying regional endpoints when creating the service client as shown in\n",
" // https://cloud.google.com/java/docs/setup#configure_endpoints_for_the_client_library\n",
" // - It may require specifying regional endpoints when creating the service client"
+ " as shown in\n",
" //"
+ " https://cloud.google.com/java/docs/setup#configure_endpoints_for_the_client_library\n",
" }\n",
"}\n",
"// [END apiname_generated_Echo_CreateExecutableSample_EmptyStatementSample_sync]\n");
Expand Down Expand Up @@ -122,11 +128,14 @@ void createExecutableSampleMethodArgsNoVar() {
" }\n",
"\n",
" public static void syncCreateExecutableSampleMethodArgsNoVar() throws Exception {\n",
" // This snippet has been automatically generated and should be regarded as a code template only.\n",
" // This snippet has been automatically generated and should be regarded as a code"
+ " template only.\n",
" // It will require modifications to work:\n",
" // - It may require correct/in-range values for request initialization.\n",
" // - It may require specifying regional endpoints when creating the service client as shown in\n",
" // https://cloud.google.com/java/docs/setup#configure_endpoints_for_the_client_library\n",
" // - It may require specifying regional endpoints when creating the service client"
+ " as shown in\n",
" //"
+ " https://cloud.google.com/java/docs/setup#configure_endpoints_for_the_client_library\n",
" System.out.println(\"Testing CreateExecutableSampleMethodArgsNoVar\");\n",
" }\n",
"}\n",
Expand Down Expand Up @@ -171,11 +180,14 @@ void createExecutableSampleMethod() {
" }\n",
"\n",
" public static void syncCreateExecutableSample(String content) throws Exception {\n",
" // This snippet has been automatically generated and should be regarded as a code template only.\n",
" // This snippet has been automatically generated and should be regarded as a code"
+ " template only.\n",
" // It will require modifications to work:\n",
" // - It may require correct/in-range values for request initialization.\n",
" // - It may require specifying regional endpoints when creating the service client as shown in\n",
" // https://cloud.google.com/java/docs/setup#configure_endpoints_for_the_client_library\n",
" // - It may require specifying regional endpoints when creating the service client"
+ " as shown in\n",
" //"
+ " https://cloud.google.com/java/docs/setup#configure_endpoints_for_the_client_library\n",
" System.out.println(content);\n",
" }\n",
"}\n",
Expand Down Expand Up @@ -248,7 +260,8 @@ void createExecutableSampleMethodMultipleStatements() {
LineFormatter.lines(
"package com.google.example;\n",
"\n",
"// [START apiname_generated_Echo_CreateExecutableSample_MethodMultipleStatements_sync]\n",
"// [START"
+ " apiname_generated_Echo_CreateExecutableSample_MethodMultipleStatements_sync]\n",
"public class SyncCreateExecutableSampleMethodMultipleStatements {\n",
"\n",
" public static void main(String[] args) throws Exception {\n",
Expand All @@ -260,17 +273,21 @@ void createExecutableSampleMethodMultipleStatements() {
"\n",
" public static void syncCreateExecutableSampleMethodMultipleStatements(\n",
" String content, int num, Object thing) throws Exception {\n",
" // This snippet has been automatically generated and should be regarded as a code template only.\n",
" // This snippet has been automatically generated and should be regarded as a code"
+ " template only.\n",
" // It will require modifications to work:\n",
" // - It may require correct/in-range values for request initialization.\n",
" // - It may require specifying regional endpoints when creating the service client as shown in\n",
" // https://cloud.google.com/java/docs/setup#configure_endpoints_for_the_client_library\n",
" // - It may require specifying regional endpoints when creating the service client"
+ " as shown in\n",
" //"
+ " https://cloud.google.com/java/docs/setup#configure_endpoints_for_the_client_library\n",
" System.out.println(content);\n",
" System.out.println(num);\n",
" System.out.println(thing.response());\n",
" }\n",
"}\n",
"// [END apiname_generated_Echo_CreateExecutableSample_MethodMultipleStatements_sync]\n");
"// [END"
+ " apiname_generated_Echo_CreateExecutableSample_MethodMultipleStatements_sync]\n");
assertEquals(expected, sampleResult);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -462,7 +462,8 @@ void valid_composeStreamCallableMethod_serverStream() {
LineFormatter.lines(
"try (EchoClient echoClient = EchoClient.create()) {\n",
" ExpandRequest request =\n",
" ExpandRequest.newBuilder().setContent(\"content951530617\").setInfo(\"info3237038\").build();\n",
" "
+ " ExpandRequest.newBuilder().setContent(\"content951530617\").setInfo(\"info3237038\").build();\n",
" ServerStream<EchoResponse> stream = echoClient.expandCallable().call(request);\n",
" for (EchoResponse response : stream) {\n",
" // Do something when a response is received.\n",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,8 @@ void composeClassHeaderSample_firstMethodIsStream() {
LineFormatter.lines(
"try (EchoClient echoClient = EchoClient.create()) {\n",
" ExpandRequest request =\n",
" ExpandRequest.newBuilder().setContent(\"content951530617\").setInfo(\"info3237038\").build();\n",
" "
+ " ExpandRequest.newBuilder().setContent(\"content951530617\").setInfo(\"info3237038\").build();\n",
" ServerStream<EchoResponse> stream = echoClient.expandCallable().call(request);\n",
" for (EchoResponse response : stream) {\n",
" // Do something when a response is received.\n",
Expand Down Expand Up @@ -355,10 +356,12 @@ void composeClassHeaderSample_firstMethodIsInternal() {
+ " EchoRequest.newBuilder()\n"
+ " .setName(FoobarName.of(\"[PROJECT]\", \"[FOOBAR]\").toString())\n"
+ " .setParent(\n"
+ " FoobarbazName.ofProjectFoobarbazName(\"[PROJECT]\", \"[FOOBARBAZ]\").toString())\n"
+ " FoobarbazName.ofProjectFoobarbazName(\"[PROJECT]\","
+ " \"[FOOBARBAZ]\").toString())\n"
+ " .setFoobar(Foobar.newBuilder().build())\n"
+ " .build();\n"
+ " EchoResponse response = echoServiceSelectiveApiClient.chatShouldGenerateAsUsual(request);\n"
+ " EchoResponse response ="
+ " echoServiceSelectiveApiClient.chatShouldGenerateAsUsual(request);\n"
+ "}");
Assert.assertEquals(results, expected);
}
Expand Down
Loading