@@ -30,9 +30,6 @@ const String _codecName = 'PigeonCodec';
3030
3131const String _overflowClassName = '${classNamePrefix }CodecOverflow' ;
3232
33- // Used to create classes with type Int rather than long.
34- const String _forceInt = '${varNamePrefix }forceInt' ;
35-
3633/// Options that control how Java code will be generated.
3734class JavaOptions {
3835 /// Creates a [JavaOptions] object
@@ -253,18 +250,11 @@ class JavaGenerator extends StructuredGenerator<JavaOptions> {
253250 }
254251
255252 void _writeClassField (
256- JavaOptions generatorOptions,
257- Indent indent,
258- NamedType field, {
259- bool isPrimitive = false ,
260- }) {
253+ JavaOptions generatorOptions, Indent indent, NamedType field) {
261254 final HostDatatype hostDatatype = getFieldHostDatatype (
262255 field, (TypeDeclaration x) => _javaTypeForBuiltinDartType (x));
263- final String nullability = isPrimitive
264- ? ''
265- : field.type.isNullable
266- ? '@Nullable '
267- : '@NonNull ' ;
256+ final String nullability =
257+ field.type.isNullable ? '@Nullable ' : '@NonNull ' ;
268258 addDocumentationComments (
269259 indent, field.documentationComments, _docCommentSpec);
270260
@@ -280,7 +270,7 @@ class JavaGenerator extends StructuredGenerator<JavaOptions> {
280270 indent.writeScoped (
281271 'public void ${_makeSetter (field )}($nullability ${hostDatatype .datatype } setterArg) {' ,
282272 '}' , () {
283- if (! field.type.isNullable && ! isPrimitive ) {
273+ if (! field.type.isNullable) {
284274 indent.writeScoped ('if (setterArg == null) {' , '}' , () {
285275 indent.writeln (
286276 'throw new IllegalStateException("Nonnull field \\ "${field .name }\\ " is null.");' );
@@ -306,7 +296,6 @@ class JavaGenerator extends StructuredGenerator<JavaOptions> {
306296 generatorOptions,
307297 indent,
308298 field,
309- isPrimitive: field.type.baseName == _forceInt,
310299 );
311300 indent.newln ();
312301 }
@@ -490,7 +479,7 @@ class JavaGenerator extends StructuredGenerator<JavaOptions> {
490479 indent
491480 .writeln ('$_overflowClassName wrap = new $_overflowClassName ();' );
492481 indent.writeln (
493- 'wrap.setType(${customType .enumeration - maximumCodecFieldKey });' );
482+ 'wrap.setType(${customType .enumeration - maximumCodecFieldKey }L );' );
494483 indent.writeln (
495484 'wrap.setWrapped($nullCheck ((${customType .name }) value).$encodeString );' );
496485 }
@@ -580,7 +569,7 @@ class JavaGenerator extends StructuredGenerator<JavaOptions> {
580569 }) {
581570 final NamedType overflowInteration = NamedType (
582571 name: 'type' ,
583- type: const TypeDeclaration (baseName: _forceInt , isNullable: false ));
572+ type: const TypeDeclaration (baseName: 'int' , isNullable: false ));
584573 final NamedType overflowObject = NamedType (
585574 name: 'wrapped' ,
586575 type: const TypeDeclaration (baseName: 'Object' , isNullable: true ));
@@ -607,7 +596,7 @@ class JavaGenerator extends StructuredGenerator<JavaOptions> {
607596 indent.format ('''
608597static @Nullable Object fromList(@NonNull ArrayList<Object> ${varNamePrefix }list) {
609598 $_overflowClassName wrapper = new $_overflowClassName ();
610- wrapper.setType((int ) ${varNamePrefix }list.get(0));
599+ wrapper.setType((Long ) ${varNamePrefix }list.get(0));
611600 wrapper.setWrapped(${varNamePrefix }list.get(1));
612601 return wrapper.unwrap();
613602}
@@ -619,7 +608,7 @@ if (wrapped == null) {
619608 return null;
620609}
621610 ''' );
622- indent.writeScoped ('switch (type) {' , '}' , () {
611+ indent.writeScoped ('switch (type.intValue() ) {' , '}' , () {
623612 for (int i = totalCustomCodecKeysAllowed; i < types.length; i++ ) {
624613 indent.writeln ('case ${i - totalCustomCodecKeysAllowed }:' );
625614 indent.nest (1 , () {
@@ -628,7 +617,7 @@ if (wrapped == null) {
628617 'return ${types [i ].name }.fromList((ArrayList<Object>) wrapped);' );
629618 } else if (types[i].type == CustomTypes .customEnum) {
630619 indent.writeln (
631- 'return ${types [i ].name }.values()[(int) wrapped] ;' );
620+ 'return ${_intToEnum ( 'wrapped' , types [i ].name , false )} ;' );
632621 }
633622 });
634623 }
@@ -766,13 +755,8 @@ if (wrapped == null) {
766755 const String output = 'output' ;
767756 final String outputExpression;
768757 indent.writeln ('@SuppressWarnings("ConstantConditions")' );
769- if (func.returnType.baseName == 'int' ) {
770- outputExpression =
771- 'listReply.get(0) == null ? null : ((Number) listReply.get(0)).longValue();' ;
772- } else {
773- outputExpression =
774- '${_cast ('listReply.get(0)' , javaType : returnType )};' ;
775- }
758+ outputExpression =
759+ '${_cast ('listReply.get(0)' , javaType : returnType )};' ;
776760 indent.writeln ('$returnType $output = $outputExpression ' );
777761 indent.writeln ('result.success($output );' );
778762 }
@@ -951,16 +935,9 @@ if (wrapped == null) {
951935 indent.writeln (
952936 'ArrayList<Object> args = (ArrayList<Object>) message;' );
953937 enumerate (method.parameters, (int index, NamedType arg) {
954- // The StandardMessageCodec can give us [Integer, Long] for
955- // a Dart 'int'. To keep things simple we just use 64bit
956- // longs in Pigeon with Java.
957- final bool isInt = arg.type.baseName == 'int' ;
958- final String argType =
959- isInt ? 'Number' : _javaTypeForDartType (arg.type);
938+ final String argType = _javaTypeForDartType (arg.type);
960939 final String argName = _getSafeArgumentName (index, arg);
961- final String argExpression = isInt
962- ? '($argName == null) ? null : $argName .longValue()'
963- : argName;
940+ final String argExpression = argName;
964941 String accessor = 'args.get($index )' ;
965942 if (argType != 'Object' ) {
966943 accessor = _cast (accessor, javaType: argType);
@@ -1176,9 +1153,10 @@ protected static ArrayList<Object> wrapError(@NonNull Throwable exception) {
11761153
11771154/// Converts an expression that evaluates to an nullable int to an expression
11781155/// that evaluates to a nullable enum.
1179- String _intToEnum (String expression, String enumName, bool nullable) => nullable
1180- ? '$expression == null ? null : $enumName .values()[(int) $expression ]'
1181- : '$enumName .values()[(int) $expression ]' ;
1156+ String _intToEnum (String expression, String enumName, bool nullable) {
1157+ final String toEnum = '$enumName .values()[((Long) $expression ).intValue()]' ;
1158+ return nullable ? '$expression == null ? null : $toEnum ' : toEnum;
1159+ }
11821160
11831161String _getArgumentName (int count, NamedType argument) =>
11841162 argument.name.isEmpty ? 'arg$count ' : argument.name;
@@ -1231,8 +1209,6 @@ String? _javaTypeForBuiltinDartType(TypeDeclaration type) {
12311209 'Int64List' : 'long[]' ,
12321210 'Float64List' : 'double[]' ,
12331211 'Object' : 'Object' ,
1234- // This is used to allow the creation of true `int`s for the codec overflow class.
1235- _forceInt: 'int' ,
12361212 };
12371213 if (javaTypeForDartTypeMap.containsKey (type.baseName)) {
12381214 return javaTypeForDartTypeMap[type.baseName];
@@ -1271,11 +1247,7 @@ String _cast(String variable, {required String javaType}) {
12711247String _castObject (NamedType field, String varName) {
12721248 final HostDatatype hostDatatype = getFieldHostDatatype (
12731249 field, (TypeDeclaration x) => _javaTypeForBuiltinDartType (x));
1274- if (field.type.baseName == 'int' ) {
1275- return '($varName == null) ? null : (($varName instanceof Integer) ? (Integer) $varName : (${hostDatatype .datatype }) $varName )' ;
1276- } else {
1277- return _cast (varName, javaType: hostDatatype.datatype);
1278- }
1250+ return _cast (varName, javaType: hostDatatype.datatype);
12791251}
12801252
12811253/// Returns string of Result class type for method based on [TypeDeclaration] .
0 commit comments