Skip to content

Commit

Permalink
[pigeon] java non null void (flutter#5786)
Browse files Browse the repository at this point in the history
Fixes java requiring `Null` to be `NonNull`.

fixes: flutter/flutter#139194

apparently also fixes an extra space that I removed a month ago and never merged.
  • Loading branch information
tarrinneal authored and arc-yong committed Jun 14, 2024
1 parent 38eaebd commit 3d371e9
Show file tree
Hide file tree
Showing 9 changed files with 101 additions and 57 deletions.
5 changes: 5 additions & 0 deletions packages/pigeon/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## 16.0.0

* [java] Adds `VoidResult` type for `Void` returns.
* **Breaking Change** [java] Updates all `Void` return types to use new `VoidResult`.

## 15.0.3

* Fixes new lint warnings.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,14 @@ public interface NullableResult<T> {
/** Failure case callback method for handling errors. */
void error(@NonNull Throwable error);
}
/** Asynchronous error handling return type for void API method returns. */
public interface VoidResult {
/** Success case callback method for handling returns. */
void success();

/** Failure case callback method for handling errors. */
void error(@NonNull Throwable error);
}

private static class ExampleHostApiCodec extends StandardMessageCodec {
public static final ExampleHostApiCodec INSTANCE = new ExampleHostApiCodec();
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.3';
const String pigeonVersion = '16.0.0';

/// Read all the content from [stdin] to a String.
String readStdin() {
Expand Down
58 changes: 40 additions & 18 deletions packages/pigeon/lib/java_generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -448,17 +448,16 @@ class JavaGenerator extends StructuredGenerator<JavaOptions> {
});

for (final Method func in api.methods) {
final String resultType =
func.returnType.isNullable ? 'NullableResult' : 'Result';
final String resultType = _getResultType(func.returnType);
final String returnType = func.returnType.isVoid
? 'Void'
: _javaTypeForDartType(func.returnType);
String sendArgument;
addDocumentationComments(
indent, func.documentationComments, _docCommentSpec);
if (func.parameters.isEmpty) {
indent.write(
'public void ${func.name}(@NonNull $resultType<$returnType> result) ');
indent
.write('public void ${func.name}(@NonNull $resultType result) ');
sendArgument = 'null';
} else {
final Iterable<String> argTypes = func.parameters
Expand All @@ -478,7 +477,7 @@ class JavaGenerator extends StructuredGenerator<JavaOptions> {
map2(argTypes, argNames, (String x, String y) => '$x $y')
.join(', ');
indent.write(
'public void ${func.name}($argsSignature, @NonNull $resultType<$returnType> result) ');
'public void ${func.name}($argsSignature, @NonNull $resultType result) ');
}
indent.addScoped('{', '}', () {
const String channel = 'channel';
Expand Down Expand Up @@ -513,7 +512,7 @@ class JavaGenerator extends StructuredGenerator<JavaOptions> {
}
indent.addScoped('else {', '}', () {
if (func.returnType.isVoid) {
indent.writeln('result.success(null);');
indent.writeln('result.success();');
} else {
const String output = 'output';
final String outputExpression;
Expand Down Expand Up @@ -633,8 +632,7 @@ class JavaGenerator extends StructuredGenerator<JavaOptions> {
/// int add(int x, int y);
void _writeInterfaceMethod(JavaOptions generatorOptions, Root root,
Indent indent, Api api, final Method method) {
final String resultType =
method.returnType.isNullable ? 'NullableResult' : 'Result';
final String resultType = _getResultType(method.returnType);
final String nullableType = method.isAsynchronous
? ''
: _nullabilityAnnotationFromType(method.returnType);
Expand All @@ -653,10 +651,7 @@ class JavaGenerator extends StructuredGenerator<JavaOptions> {
}));
}
if (method.isAsynchronous) {
final String returnType = method.returnType.isVoid
? 'Void'
: _javaTypeForDartType(method.returnType);
argSignature.add('@NonNull $resultType<$returnType> result');
argSignature.add('@NonNull $resultType result');
}
if (method.documentationComments.isNotEmpty) {
addDocumentationComments(
Expand Down Expand Up @@ -748,14 +743,17 @@ class JavaGenerator extends StructuredGenerator<JavaOptions> {
? ' == null ? null : $resultValue.index'
: '.index';
}
final String resultType =
method.returnType.isNullable ? 'NullableResult' : 'Result';
final String resultType = _getResultType(method.returnType);
final String resultParam =
method.returnType.isVoid ? '' : '$returnType result';
final String addResultArg =
method.returnType.isVoid ? 'null' : '$resultValue$enumTag';
const String resultName = 'resultCallback';
indent.format('''
$resultType<$returnType> $resultName =
\t\tnew $resultType<$returnType>() {
\t\t\tpublic void success($returnType result) {
\t\t\t\twrapped.add(0, $resultValue$enumTag);
$resultType $resultName =
\t\tnew $resultType() {
\t\t\tpublic void success($resultParam) {
\t\t\t\twrapped.add(0, $addResultArg);
\t\t\t\treply.reply(wrapped);
\t\t\t}
Expand Down Expand Up @@ -895,6 +893,19 @@ $resultType<$returnType> $resultName =
.writeln('/** Failure case callback method for handling errors. */');
indent.writeln('void error(@NonNull Throwable error);');
});

indent.writeln(
'/** Asynchronous error handling return type for void API method returns. */');
indent.write('public interface VoidResult ');
indent.addScoped('{', '}', () {
indent
.writeln('/** Success case callback method for handling returns. */');
indent.writeln('void success();');
indent.newln();
indent
.writeln('/** Failure case callback method for handling errors. */');
indent.writeln('void error(@NonNull Throwable error);');
});
}

void _writeErrorClass(Indent indent) {
Expand Down Expand Up @@ -1102,3 +1113,14 @@ String _castObject(NamedType field, String varName) {
return _cast(varName, javaType: hostDatatype.datatype);
}
}

/// Returns string of Result class type for method based on [TypeDeclaration].
String _getResultType(TypeDeclaration type) {
if (type.isVoid) {
return 'VoidResult';
}
if (type.isNullable) {
return 'NullableResult<${_javaTypeForDartType(type)}>';
}
return 'Result<${_javaTypeForDartType(type)}>';
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import com.example.alternate_language_test_plugin.CoreTests.HostIntegrationCoreApi;
import com.example.alternate_language_test_plugin.CoreTests.NullableResult;
import com.example.alternate_language_test_plugin.CoreTests.Result;
import com.example.alternate_language_test_plugin.CoreTests.VoidResult;
import io.flutter.embedding.engine.plugins.FlutterPlugin;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -208,8 +209,8 @@ public void throwErrorFromVoid() {
}

@Override
public void noopAsync(@NonNull Result<Void> result) {
result.success(null);
public void noopAsync(@NonNull VoidResult result) {
result.success();
}

@Override
Expand All @@ -218,7 +219,7 @@ public void throwAsyncError(@NonNull NullableResult<Object> result) {
}

@Override
public void throwAsyncErrorFromVoid(@NonNull Result<Void> result) {
public void throwAsyncErrorFromVoid(@NonNull VoidResult result) {
result.error(new RuntimeException("An error"));
}

Expand Down Expand Up @@ -338,7 +339,7 @@ public void echoAsyncNullableEnum(
}

@Override
public void callFlutterNoop(@NonNull Result<Void> result) {
public void callFlutterNoop(@NonNull VoidResult result) {
flutterApi.noop(result);
}

Expand All @@ -348,7 +349,7 @@ public void callFlutterThrowError(@NonNull NullableResult<Object> result) {
}

@Override
public void callFlutterThrowErrorFromVoid(@NonNull Result<Void> result) {
public void callFlutterThrowErrorFromVoid(@NonNull VoidResult result) {
flutterApi.throwErrorFromVoid(result);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -980,6 +980,14 @@ public interface NullableResult<T> {
/** Failure case callback method for handling errors. */
void error(@NonNull Throwable error);
}
/** Asynchronous error handling return type for void API method returns. */
public interface VoidResult {
/** Success case callback method for handling returns. */
void success();

/** Failure case callback method for handling errors. */
void error(@NonNull Throwable error);
}

private static class HostIntegrationCoreApiCodec extends StandardMessageCodec {
public static final HostIntegrationCoreApiCodec INSTANCE = new HostIntegrationCoreApiCodec();
Expand Down Expand Up @@ -1139,7 +1147,7 @@ AllNullableTypes sendMultipleNullableTypes(
* A no-op function taking no arguments and returning no value, to sanity test basic
* asynchronous calling.
*/
void noopAsync(@NonNull Result<Void> result);
void noopAsync(@NonNull VoidResult result);
/** Returns passed in int asynchronously. */
void echoAsyncInt(@NonNull Long anInt, @NonNull Result<Long> result);
/** Returns passed in double asynchronously. */
Expand All @@ -1162,7 +1170,7 @@ void echoAsyncMap(
/** Responds with an error from an async function returning a value. */
void throwAsyncError(@NonNull NullableResult<Object> result);
/** Responds with an error from an async void function. */
void throwAsyncErrorFromVoid(@NonNull Result<Void> result);
void throwAsyncErrorFromVoid(@NonNull VoidResult result);
/** Responds with a Flutter error from an async function returning a value. */
void throwAsyncFlutterError(@NonNull NullableResult<Object> result);
/** Returns the passed object, to test async serialization and deserialization. */
Expand Down Expand Up @@ -1192,11 +1200,11 @@ void echoAsyncNullableMap(
/** Returns the passed enum, to test asynchronous serialization and deserialization. */
void echoAsyncNullableEnum(@Nullable AnEnum anEnum, @NonNull NullableResult<AnEnum> result);

void callFlutterNoop(@NonNull Result<Void> result);
void callFlutterNoop(@NonNull VoidResult result);

void callFlutterThrowError(@NonNull NullableResult<Object> result);

void callFlutterThrowErrorFromVoid(@NonNull Result<Void> result);
void callFlutterThrowErrorFromVoid(@NonNull VoidResult result);

void callFlutterEchoAllTypes(@NonNull AllTypes everything, @NonNull Result<AllTypes> result);

Expand Down Expand Up @@ -2097,9 +2105,9 @@ static void setUp(
channel.setMessageHandler(
(message, reply) -> {
ArrayList<Object> wrapped = new ArrayList<Object>();
Result<Void> resultCallback =
new Result<Void>() {
public void success(Void result) {
VoidResult resultCallback =
new VoidResult() {
public void success() {
wrapped.add(0, null);
reply.reply(wrapped);
}
Expand Down Expand Up @@ -2434,9 +2442,9 @@ public void error(Throwable error) {
channel.setMessageHandler(
(message, reply) -> {
ArrayList<Object> wrapped = new ArrayList<Object>();
Result<Void> resultCallback =
new Result<Void>() {
public void success(Void result) {
VoidResult resultCallback =
new VoidResult() {
public void success() {
wrapped.add(0, null);
reply.reply(wrapped);
}
Expand Down Expand Up @@ -2834,9 +2842,9 @@ public void error(Throwable error) {
channel.setMessageHandler(
(message, reply) -> {
ArrayList<Object> wrapped = new ArrayList<Object>();
Result<Void> resultCallback =
new Result<Void>() {
public void success(Void result) {
VoidResult resultCallback =
new VoidResult() {
public void success() {
wrapped.add(0, null);
reply.reply(wrapped);
}
Expand Down Expand Up @@ -2892,9 +2900,9 @@ public void error(Throwable error) {
channel.setMessageHandler(
(message, reply) -> {
ArrayList<Object> wrapped = new ArrayList<Object>();
Result<Void> resultCallback =
new Result<Void>() {
public void success(Void result) {
VoidResult resultCallback =
new VoidResult() {
public void success() {
wrapped.add(0, null);
reply.reply(wrapped);
}
Expand Down Expand Up @@ -3574,7 +3582,7 @@ public FlutterIntegrationCoreApi(@NonNull BinaryMessenger argBinaryMessenger) {
/**
* A no-op function taking no arguments and returning no value, to sanity test basic calling.
*/
public void noop(@NonNull Result<Void> result) {
public void noop(@NonNull VoidResult result) {
final String channelName =
"dev.flutter.pigeon.pigeon_integration_tests.FlutterIntegrationCoreApi.noop";
BasicMessageChannel<Object> channel =
Expand All @@ -3591,7 +3599,7 @@ public void noop(@NonNull Result<Void> result) {
(String) listReply.get(1),
(String) listReply.get(2)));
} else {
result.success(null);
result.success();
}
} else {
result.error(createConnectionError(channelName));
Expand Down Expand Up @@ -3626,7 +3634,7 @@ public void throwError(@NonNull NullableResult<Object> result) {
});
}
/** Responds with an error from an async void function. */
public void throwErrorFromVoid(@NonNull Result<Void> result) {
public void throwErrorFromVoid(@NonNull VoidResult result) {
final String channelName =
"dev.flutter.pigeon.pigeon_integration_tests.FlutterIntegrationCoreApi.throwErrorFromVoid";
BasicMessageChannel<Object> channel =
Expand All @@ -3643,7 +3651,7 @@ public void throwErrorFromVoid(@NonNull Result<Void> result) {
(String) listReply.get(1),
(String) listReply.get(2)));
} else {
result.success(null);
result.success();
}
} else {
result.error(createConnectionError(channelName));
Expand Down Expand Up @@ -4251,7 +4259,7 @@ public void echoNullableEnum(
* A no-op function taking no arguments and returning no value, to sanity test basic
* asynchronous calling.
*/
public void noopAsync(@NonNull Result<Void> result) {
public void noopAsync(@NonNull VoidResult result) {
final String channelName =
"dev.flutter.pigeon.pigeon_integration_tests.FlutterIntegrationCoreApi.noopAsync";
BasicMessageChannel<Object> channel =
Expand All @@ -4268,7 +4276,7 @@ public void noopAsync(@NonNull Result<Void> result) {
(String) listReply.get(1),
(String) listReply.get(2)));
} else {
result.success(null);
result.success();
}
} else {
result.error(createConnectionError(channelName));
Expand Down Expand Up @@ -4358,7 +4366,7 @@ public interface HostSmallApi {

void echo(@NonNull String aString, @NonNull Result<String> result);

void voidVoid(@NonNull Result<Void> result);
void voidVoid(@NonNull VoidResult result);

/** The codec used by HostSmallApi. */
static @NonNull MessageCodec<Object> getCodec() {
Expand Down Expand Up @@ -4407,9 +4415,9 @@ public void error(Throwable error) {
channel.setMessageHandler(
(message, reply) -> {
ArrayList<Object> wrapped = new ArrayList<Object>();
Result<Void> resultCallback =
new Result<Void>() {
public void success(Void result) {
VoidResult resultCallback =
new VoidResult() {
public void success() {
wrapped.add(0, null);
reply.reply(wrapped);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ public void echo(@NonNull String value, Result<String> result) {
}

@Override
public void voidVoid(Result<Void> result) {
result.success(null);
public void voidVoid(VoidResult result) {
result.success();
}
}

Expand All @@ -36,7 +36,7 @@ public void echo(@NonNull String value, Result<String> result) {
}

@Override
public void voidVoid(Result<Void> result) {
public void voidVoid(VoidResult result) {
result.error(new Exception("error"));
}
}
Expand Down
Loading

0 comments on commit 3d371e9

Please sign in to comment.