Skip to content

Commit

Permalink
[pigeon] Adds @CanIgnoreReturnValue annotation (flutter#5601)
Browse files Browse the repository at this point in the history
adds @CanIgnoreReturnValue annotation to java class builder method
  • Loading branch information
tarrinneal authored and HugoOlthof committed Dec 13, 2023
1 parent d2c5af0 commit 1c64208
Show file tree
Hide file tree
Showing 7 changed files with 86 additions and 2 deletions.
4 changes: 4 additions & 0 deletions packages/pigeon/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 15.0.1

* [java] Adds @CanIgnoreReturnValue annotation to class builder.

## 15.0.0

* **Breaking Change** [kotlin] Updates Flutter API to use new errorClassName.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
// Autogenerated from Pigeon, do not edit directly.
// See also: https://pub.dev/packages/pigeon

import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.RetentionPolicy.CLASS;

import android.util.Log;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
Expand All @@ -12,6 +15,8 @@
import io.flutter.plugin.common.MessageCodec;
import io.flutter.plugin.common.StandardMessageCodec;
import java.io.ByteArrayOutputStream;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.Collections;
Expand Down Expand Up @@ -61,6 +66,10 @@ protected static FlutterError createConnectionError(@NonNull String channelName)
"channel-error", "Unable to establish connection on channel: " + channelName + ".", "");
}

@Target(METHOD)
@Retention(CLASS)
@interface CanIgnoreReturnValue {}

public enum Code {
ONE(0),
TWO(1);
Expand Down Expand Up @@ -127,27 +136,31 @@ public static final class Builder {

private @Nullable String name;

@CanIgnoreReturnValue
public @NonNull Builder setName(@Nullable String setterArg) {
this.name = setterArg;
return this;
}

private @Nullable String description;

@CanIgnoreReturnValue
public @NonNull Builder setDescription(@Nullable String setterArg) {
this.description = setterArg;
return this;
}

private @Nullable Code code;

@CanIgnoreReturnValue
public @NonNull Builder setCode(@NonNull Code setterArg) {
this.code = setterArg;
return this;
}

private @Nullable Map<String, String> data;

@CanIgnoreReturnValue
public @NonNull Builder setData(@NonNull Map<String, String> setterArg) {
this.data = setterArg;
return this;
Expand Down
2 changes: 1 addition & 1 deletion packages/pigeon/lib/generator_tools.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import 'ast.dart';
/// The current version of pigeon.
///
/// This must match the version in pubspec.yaml.
const String pigeonVersion = '15.0.0';
const String pigeonVersion = '15.0.1';

/// Read all the content from [stdin] to a String.
String readStdin() {
Expand Down
25 changes: 25 additions & 0 deletions packages/pigeon/lib/java_generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,12 @@ class JavaGenerator extends StructuredGenerator<JavaOptions> {
indent.writeln('package ${generatorOptions.package};');
indent.newln();
}
if (root.classes.isNotEmpty) {
indent.writeln('import static java.lang.annotation.ElementType.METHOD;');
indent
.writeln('import static java.lang.annotation.RetentionPolicy.CLASS;');
indent.newln();
}
indent.writeln('import android.util.Log;');
indent.writeln('import androidx.annotation.NonNull;');
indent.writeln('import androidx.annotation.Nullable;');
Expand All @@ -124,6 +130,10 @@ class JavaGenerator extends StructuredGenerator<JavaOptions> {
indent.writeln('import io.flutter.plugin.common.MessageCodec;');
indent.writeln('import io.flutter.plugin.common.StandardMessageCodec;');
indent.writeln('import java.io.ByteArrayOutputStream;');
if (root.classes.isNotEmpty) {
indent.writeln('import java.lang.annotation.Retention;');
indent.writeln('import java.lang.annotation.Target;');
}
indent.writeln('import java.nio.ByteBuffer;');
indent.writeln('import java.util.ArrayList;');
indent.writeln('import java.util.Arrays;');
Expand Down Expand Up @@ -290,6 +300,7 @@ class JavaGenerator extends StructuredGenerator<JavaOptions> {
indent.writeln(
'private @Nullable ${hostDatatype.datatype} ${field.name};');
indent.newln();
indent.writeln('@CanIgnoreReturnValue');
indent.writeScoped(
'public @NonNull Builder ${_makeSetter(field)}($nullability ${hostDatatype.datatype} setterArg) {',
'}', () {
Expand Down Expand Up @@ -939,6 +950,17 @@ protected static ArrayList<Object> wrapError(@NonNull Throwable exception) {
});
}

// We are emitting our own definition of [@CanIgnoreReturnValue] to support
// clients who use CheckReturnValue, without having to force Pigeon clients
// to take a new dependency on error_prone_annotations.
void _writeCanIgnoreReturnValueAnnotation(
JavaOptions opt, Root root, Indent indent) {
indent.newln();
indent.writeln('@Target(METHOD)');
indent.writeln('@Retention(CLASS)');
indent.writeln('@interface CanIgnoreReturnValue {}');
}

@override
void writeGeneralUtilities(
JavaOptions generatorOptions,
Expand All @@ -961,6 +983,9 @@ protected static ArrayList<Object> wrapError(@NonNull Throwable exception) {
indent.newln();
_writeCreateConnectionError(indent);
}
if (root.classes.isNotEmpty) {
_writeCanIgnoreReturnValueAnnotation(generatorOptions, root, indent);
}
}

@override
Expand Down
Loading

0 comments on commit 1c64208

Please sign in to comment.