Visitar URL original
[Pigeon] Added java target for host functions. by gaaclarke · Pull Request #96 · flutter/packages · GitHub
Skip to content
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
responded to xster feedback
  • Loading branch information
gaaclarke committed Feb 20, 2020
commit 3dc9bd742147dd9dc4746f90e4ecb068592d0150
68 changes: 37 additions & 31 deletions packages/pigeon/lib/java_generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import 'ast.dart';
import 'generator_tools.dart';

/// Options that control how Objective-C code will be generated.
/// Options that control how Java code will be generated.
class JavaOptions {
/// The name of the class that will house all the generated classes.
String className;
Expand All @@ -16,42 +16,47 @@ class JavaOptions {

void _writeHostApi(Indent indent, Api api) {
assert(api.location == ApiLocation.host);
indent.writeln('/** Generated interface from pigeon. */');

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Be consistent on capitalization.
It's min case here and uppercase below.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done.

indent.write('public interface ${api.name} ');

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add code comment above, i.e. this class was generated from source IDL

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

indent.scoped('{', '}', () {
for (Method func in api.methods) {
indent.writeln('${func.returnType} ${func.name}(${func.argType} arg);');
for (Method method in api.methods) {
indent.writeln(
'${method.returnType} ${method.name}(${method.argType} arg);');
}
});
indent.write(
'public static void ${api.name}Setup(BinaryMessenger binaryMessenger, ${api.name} api) ');
indent.scoped('{', '}', () {
for (Method func in api.methods) {
final String channelName = makeChannelName(api, func);
indent.write('');
indent.scoped('{', '}', () {
indent.writeln('BasicMessageChannel<Object> channel =');
indent.inc();
indent.inc();
indent.writeln(
'new BasicMessageChannel<Object>(binaryMessenger, "$channelName", new StandardMessageCodec());');
indent.dec();
indent.dec();
indent.write(
'channel.setMessageHandler(new BasicMessageChannel.MessageHandler<Object>() ');
indent.scoped('{', '});', () {
indent.addln('');
indent.writeln(
'/** Sets up an instance of `${api.name}` to handle messages through the `binaryMessenger` */');

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit, since the Java class api.name is something you made, it'll still not be clear what an instance of it does specifically. Mention that the instance is meant to hold a message handler and that it's needed for the java side to receive signals from flutter or some such.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Expanded on the docstrings for the interfaces and generated classes.

indent.write(
'public static void setup(BinaryMessenger binaryMessenger, ${api.name} api) ');
indent.scoped('{', '}', () {
for (Method method in api.methods) {
final String channelName = makeChannelName(api, method);
indent.write('');
indent.scoped('{', '}', () {
indent.writeln('BasicMessageChannel<Object> channel =');
indent.inc();
indent.inc();
indent.writeln(
'new BasicMessageChannel<Object>(binaryMessenger, "$channelName", new StandardMessageCodec());');
indent.dec();
indent.dec();
indent.write(
'public void onMessage(Object message, BasicMessageChannel.Reply<Object> reply) ');
indent.scoped('{', '}', () {
final String argType = func.argType;
final String returnType = func.returnType;
indent.writeln(
'$argType input = $argType.fromMap((HashMap)message);');
indent.writeln('$returnType output = api.${func.name}(input);');
indent.writeln('reply.reply(output.toMap());');
'channel.setMessageHandler(new BasicMessageChannel.MessageHandler<Object>() ');
indent.scoped('{', '});', () {
indent.write(
'public void onMessage(Object message, BasicMessageChannel.Reply<Object> reply) ');
indent.scoped('{', '}', () {
final String argType = method.argType;
final String returnType = method.returnType;
indent.writeln(
'$argType input = $argType.fromMap((HashMap)message);');
indent.writeln('$returnType output = api.${method.name}(input);');
indent.writeln('reply.reply(output.toMap());');
});
});
});
});
}
}
});
});
}

Expand All @@ -76,6 +81,7 @@ String _makeSetter(Field field) {
void generateJava(JavaOptions options, Root root, StringSink sink) {
final Indent indent = Indent(sink);
indent.writeln('// Autogenerated from Pigeon, do not edit directly.');

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the java and source idl files map one to one right? If so, maybe just print out the name of the source as well.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea, i'll add it to the backlog.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe add a link too. If we go the simple way, the person looking at this code might not be the same person who ran pigeon and therefore might have no idea what pigeon is. This is a viral marketing opportunity :)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

indent.writeln('// See also: https://pub.dev/packages/pigeon');
indent.addln('');
if (options.package != null) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder what this branch means in reality, from the tool's CLI API perspective, since it's not valid java anymore.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You don't need a package statement in java.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

are unnamed packages practically useful in the wild? seems like a footgun

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"You’re a rotten driver,” I protested. “Either you ought to be more careful, or you oughtn’t to drive at all.”

“I am careful.”

“No, you’re not.”

“Well, other people are,” she said lightly.

“What’s that got to do with it?”

“They’ll keep out of my way,” she insisted. “It takes two to make an accident.”

-- great gatsby

indent.writeln('package ${options.package};');
Expand Down