Class: Xdrgen::Generators::Java

Inherits:
Base
  • Object
show all
Defined in:
lib/xdrgen/generators/java.rb

Instance Method Summary collapse

Methods inherited from Base

#initialize

Constructor Details

This class inherits a constructor from Xdrgen::Generators::Base

Instance Method Details

#add_imports_for_definition(defn, imports) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/xdrgen/generators/java.rb', line 36

def add_imports_for_definition(defn, imports)
  case defn
  when AST::Definitions::Struct ;
    defn.members.each do |m|
      if is_decl_array(m.declaration)
        imports.add('java.util.Arrays')
      else
        imports.add('com.google.common.base.Objects')
      end
    end
    # if we have more than one member field then the
    # hash code will be computed by
    # Objects.hashCode(field1, field2, ..., fieldN)
    # therefore, we should always import com.google.common.base.Objects
    if defn.members.length > 1
      imports.add("com.google.common.base.Objects")
    end
  when AST::Definitions::Enum ;
    # no imports required for enums
  when AST::Definitions::Union ;
    nonVoidArms = defn.arms.select { |arm| !arm.void? }
    # add 1 because of the discriminant
    totalFields = nonVoidArms.length + 1

    if is_type_array(defn.discriminant.type)
      imports.add('java.util.Arrays')
    else
      imports.add('com.google.common.base.Objects')
    end

    nonVoidArms.each do |a|
      if is_decl_array(a.declaration)
        imports.add('java.util.Arrays')
      else
        imports.add('com.google.common.base.Objects')
      end
    end

    # if we have more than one field then the
    # hash code will be computed by
    # Objects.hashCode(field1, field2, ..., fieldN)
    # therefore, we should always import com.google.common.base.Objects
    # if we have more than one field
    if totalFields > 1
      imports.add("com.google.common.base.Objects")
    end
  when AST::Definitions::Typedef ;
    if is_decl_array(defn.declaration)
      imports.add('java.util.Arrays')
    else
      imports.add('com.google.common.base.Objects')
    end
  end

  if defn.respond_to? :nested_definitions
    defn.nested_definitions.each{ |child_defn| add_imports_for_definition(child_defn, imports) }
  end
end

#decl_string(decl) ⇒ Object



820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
# File 'lib/xdrgen/generators/java.rb', line 820

def decl_string(decl)
  case decl
  when AST::Declarations::Opaque ;
    "byte[]"
  when AST::Declarations::String ;
    "XdrString"
  when AST::Declarations::Array ;
    "#{type_string decl.type}[]"
  when AST::Declarations::Optional ;
    "#{type_string(decl.type)}"
  when AST::Declarations::Simple ;
    type_string(decl.type)
  else
    raise "Unknown declaration type: #{decl.class.name}"
  end
end

#decode_member(value, member, out) ⇒ Object



749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
# File 'lib/xdrgen/generators/java.rb', line 749

def decode_member(value, member, out)
  case member.declaration
  when AST::Declarations::Void ;
    return
  end
  if member.type.sub_type == :optional
    out.puts <<-EOS.strip_heredoc
      int #{member.name}Present = stream.readInt();
      if (#{member.name}Present != 0) {
    EOS
  end
  case member.declaration
  when AST::Declarations::Opaque ;
    if (member.declaration.fixed?)
      out.puts "int #{member.name}size = #{member.declaration.size};"
    else
      out.puts "int #{member.name}size = stream.readInt();"
    end
    out.puts <<-EOS.strip_heredoc
      #{value}.#{member.name} = new byte[#{member.name}size];
      stream.read(#{value}.#{member.name}, 0, #{member.name}size);
    EOS
  when AST::Declarations::Array ;
    if (member.declaration.fixed?)
      out.puts "int #{member.name}size = #{member.declaration.size};"
    else
      out.puts "int #{member.name}size = stream.readInt();"
    end
    out.puts <<-EOS.strip_heredoc
      #{value}.#{member.name} = new #{type_string member.type}[#{member.name}size];
      for (int i = 0; i < #{member.name}size; i++) {
        #{value}.#{member.name}[i] = #{decode_type member.declaration};
      }
    EOS
  else
    out.puts "#{value}.#{member.name} = #{decode_type member.declaration};"
  end
  if member.type.sub_type == :optional
    out.puts "}"
  end
end

#decode_type(decl) ⇒ Object



791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
# File 'lib/xdrgen/generators/java.rb', line 791

def decode_type(decl)
  case decl.type
  when AST::Typespecs::Int ;
    "stream.readInt()"
  when AST::Typespecs::UnsignedInt ;
    "stream.readInt()"
  when AST::Typespecs::Hyper ;
    "stream.readLong()"
  when AST::Typespecs::UnsignedHyper ;
    "stream.readLong()"
  when AST::Typespecs::Float ;
    "stream.readFloat()"
  when AST::Typespecs::Double ;
    "stream.readDouble()"
  when AST::Typespecs::Quadruple ;
    raise "cannot render quadruple in golang"
  when AST::Typespecs::Bool ;
    "stream.readInt() == 1 ? true : false"
  when AST::Typespecs::String ;
    "XdrString.decode(stream, #{decl.size})"
  when AST::Typespecs::Simple ;
    "#{name decl.type.resolved_type}.decode(stream)"
  when AST::Concerns::NestedDefinition ;
    "#{name decl.type}.decode(stream)"
  else
    raise "Unknown typespec: #{decl.type.class.name}"
  end
end

#encode_member(value, member, out) ⇒ Object



681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
# File 'lib/xdrgen/generators/java.rb', line 681

def encode_member(value, member, out)
  case member.declaration
    when AST::Declarations::Void
      return
  end

  if member.type.sub_type == :optional
    out.puts "if (#{value}.#{member.name} != null) {"
    out.puts "stream.writeInt(1);"
  end
  case member.declaration
  when AST::Declarations::Opaque ;
    out.puts "int #{member.name}size = #{value}.#{member.name}.length;"
    unless member.declaration.fixed?
      out.puts "stream.writeInt(#{member.name}size);"
    end
    out.puts <<-EOS.strip_heredoc
      stream.write(#{value}.get#{member.name.slice(0,1).capitalize+member.name.slice(1..-1)}(), 0, #{member.name}size);
    EOS
  when AST::Declarations::Array ;
    out.puts "int #{member.name}size = #{value}.get#{member.name.slice(0,1).capitalize+member.name.slice(1..-1)}().length;"
    unless member.declaration.fixed?
      out.puts "stream.writeInt(#{member.name}size);"
    end
    out.puts <<-EOS.strip_heredoc
      for (int i = 0; i < #{member.name}size; i++) {
        #{encode_type member.declaration.type, "#{value}.#{member.name}[i]"};
      }
    EOS
  else
    out.puts "#{encode_type member.declaration.type, "#{value}.#{member.name}"};"
  end
  if member.type.sub_type == :optional
    out.puts "} else {"
    out.puts "stream.writeInt(0);"
    out.puts "}"
  end
end

#encode_type(type, value) ⇒ Object



720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
# File 'lib/xdrgen/generators/java.rb', line 720

def encode_type(type, value)
  case type
  when AST::Typespecs::Int ;
    "stream.writeInt(#{value})"
  when AST::Typespecs::UnsignedInt ;
    "stream.writeInt(#{value})"
  when AST::Typespecs::Hyper ;
    "stream.writeLong(#{value})"
  when AST::Typespecs::UnsignedHyper ;
    "stream.writeLong(#{value})"
  when AST::Typespecs::Float ;
    "stream.writeFloat(#{value})"
  when AST::Typespecs::Double ;
    "stream.writeDouble(#{value})"
  when AST::Typespecs::Quadruple ;
    raise "cannot render quadruple in golang"
  when AST::Typespecs::Bool ;
    "stream.writeInt(#{value} ? 1 : 0)"
  when AST::Typespecs::String ;
    "#{value}.encode(stream)"
  when AST::Typespecs::Simple ;
    "#{name type.resolved_type}.encode(stream, #{value})"
  when AST::Concerns::NestedDefinition ;
    "#{name type}.encode(stream, #{value})"
  else
    raise "Unknown typespec: #{type.class.name}"
  end
end

#generateObject



8
9
10
11
# File 'lib/xdrgen/generators/java.rb', line 8

def generate
  render_lib
  render_definitions(@top)
end

#is_decl_array(decl) ⇒ Object



837
838
839
840
841
842
843
844
845
846
847
848
849
850
# File 'lib/xdrgen/generators/java.rb', line 837

def is_decl_array(decl)
  case decl
  when AST::Declarations::Opaque ;
    true
  when AST::Declarations::Array ;
    true
  when AST::Declarations::Optional ;
    is_type_array(decl.type)
  when AST::Declarations::Simple ;
  is_type_array(decl.type)
  else
    false
  end
end

#is_type_array(type) ⇒ Object



852
853
854
855
856
857
858
859
# File 'lib/xdrgen/generators/java.rb', line 852

def is_type_array(type)
  case type
  when AST::Typespecs::Opaque ;
    true
  else
    false
  end
end

#name(named) ⇒ Object



892
893
894
895
896
897
# File 'lib/xdrgen/generators/java.rb', line 892

def name(named)
  parent = name named.parent_defn if named.is_a?(AST::Concerns::NestedDefinition)
  result = named.name.camelize

  "#{parent}#{result}"
end

#name_string(name) ⇒ Object



899
900
901
# File 'lib/xdrgen/generators/java.rb', line 899

def name_string(name)
  name.camelize
end

#render_definition(defn) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/xdrgen/generators/java.rb', line 95

def render_definition(defn)
  imports = Set[]
  add_imports_for_definition(defn, imports)

  case defn
  when AST::Definitions::Struct ;
    render_element "public class", imports, defn do |out|
      render_struct defn, out
      render_nested_definitions defn, out
    end
  when AST::Definitions::Enum ;
    render_element "public enum", imports, defn do |out|
      render_enum defn, out
    end
  when AST::Definitions::Union ;
    render_element "public class", imports, defn do |out|
      render_union defn, out
      render_nested_definitions defn, out
    end
  when AST::Definitions::Typedef ;
    render_element "public class", imports, defn do |out|
      render_typedef defn, out
    end
  end
end

#render_definitions(node) ⇒ Object



31
32
33
34
# File 'lib/xdrgen/generators/java.rb', line 31

def render_definitions(node)
  node.namespaces.each{|n| render_definitions n }
  node.definitions.each(&method(:render_definition))
end

#render_element(type, imports, element, post_name = "implements XdrElement") ⇒ Object



159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/xdrgen/generators/java.rb', line 159

def render_element(type, imports, element, post_name="implements XdrElement")
  path = element.name.camelize + ".java"
  name = name_string element.name
  out  = @output.open(path)
  render_top_matter out
  imports.each do |import|
    out.puts "import #{import};"
  end
  out.puts "\n"
  render_source_comment out, element
  out.puts "#{type} #{name} #{post_name} {"
  out.indent do
    yield out
    out.unbreak
  end
  out.puts "}"
end

#render_enum(enum, out) ⇒ Object



177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
# File 'lib/xdrgen/generators/java.rb', line 177

def render_enum(enum, out)
  out.balance_after /,[\s]*/ do
    enum.members.each do |em|
      out.puts "#{em.name}(#{em.value}),"
    end
  end
  out.puts ";\n"
  out.puts <<-EOS.strip_heredoc
  private int mValue;

  #{name_string enum.name}(int value) {
      mValue = value;
  }

  public int getValue() {
      return mValue;
  }

  public static #{name_string enum.name} decode(XdrDataInputStream stream) throws IOException {
    int value = stream.readInt();
    switch (value) {
  EOS
  out.indent 2 do
    enum.members.each do |em|
      out.puts "case #{em.value}: return #{em.name};"
    end
  end
  out.puts <<-EOS.strip_heredoc
      default:
        throw new RuntimeException("Unknown enum value: " + value);
    }
  }

  public static void encode(XdrDataOutputStream stream, #{name_string enum.name} value) throws IOException {
    stream.writeInt(value.getValue());
  }

  public void encode(XdrDataOutputStream stream) throws IOException {
    encode(stream, this);
  }
  EOS
  out.break
end

#render_libObject



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/xdrgen/generators/java.rb', line 13

def render_lib
  template = IO.read(__dir__ + "/java/XdrDataInputStream.erb")
  result = ERB.new(template).result binding
  @output.write  "XdrDataInputStream.java", result

  template = IO.read(__dir__ + "/java/XdrDataOutputStream.erb")
  result = ERB.new(template).result binding
  @output.write  "XdrDataOutputStream.java", result

  template = IO.read(__dir__ + "/java/XdrElement.erb")
  result = ERB.new(template).result binding
  @output.write  "XdrElement.java", result

  template = IO.read(__dir__ + "/java/XdrString.erb")
  result = ERB.new(template).result binding
  @output.write  "XdrString.java", result
end

#render_nested_definitions(defn, out) ⇒ Object



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/xdrgen/generators/java.rb', line 121

def render_nested_definitions(defn, out)
  return unless defn.respond_to? :nested_definitions
  defn.nested_definitions.each{|ndefn|
    case ndefn
    when AST::Definitions::Struct ;
      name = name ndefn
      out.puts "public static class #{name} {"
      out.indent do
        render_struct ndefn, out
        render_nested_definitions ndefn , out
      end
      out.puts "}"
    when AST::Definitions::Enum ;
      name = name ndefn
      out.puts "public static enum #{name} {"
      out.indent do
        render_enum ndefn, out
      end
      out.puts "}"
    when AST::Definitions::Union ;
      name = name ndefn
      out.puts "public static class #{name} {"
      out.indent do
        render_union ndefn, out
        render_nested_definitions ndefn, out
      end
      out.puts "}"
    when AST::Definitions::Typedef ;
      name = name ndefn
      out.puts "public static class #{name} {"
      out.indent do
        render_typedef ndefn, out
      end
      out.puts "}"
    end
  }
end

#render_source_comment(out, defn) ⇒ Object



665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
# File 'lib/xdrgen/generators/java.rb', line 665

def render_source_comment(out, defn)
  return if defn.is_a?(AST::Definitions::Namespace)

  out.puts <<-EOS.strip_heredoc
  // === xdr source ============================================================

  EOS

  out.puts "//  " + defn.text_value.split("\n").join("\n//  ")

  out.puts <<-EOS.strip_heredoc

  //  ===========================================================================
  EOS
end

#render_struct(struct, out) ⇒ Object



221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
# File 'lib/xdrgen/generators/java.rb', line 221

def render_struct(struct, out)
  out.puts "public #{name struct} () {}"
  struct.members.each do |m|
    out.puts <<-EOS.strip_heredoc
      private #{decl_string(m.declaration)} #{m.name};
      public #{decl_string(m.declaration)} get#{m.name.slice(0,1).capitalize+m.name.slice(1..-1)}() {
        return this.#{m.name};
      }
      public void set#{m.name.slice(0,1).capitalize+m.name.slice(1..-1)}(#{decl_string m.declaration} value) {
        this.#{m.name} = value;
      }
    EOS
  end


  out.puts "public static void encode(XdrDataOutputStream stream, #{name struct} encoded#{name struct}) throws IOException{"
  struct.members.each do |m|
    out.indent do
      encode_member "encoded#{name struct}", m, out
    end
  end
  out.puts "}"

  out.puts <<-EOS.strip_heredoc
    public void encode(XdrDataOutputStream stream) throws IOException {
      encode(stream, this);
    }
  EOS

  out.puts <<-EOS.strip_heredoc
    public static #{name struct} decode(XdrDataInputStream stream) throws IOException {
      #{name struct} decoded#{name struct} = new #{name struct}();
  EOS
  struct.members.each do |m|
    out.indent do
      decode_member "decoded#{name struct}", m, out
    end
  end
  out.indent do
    out.puts "return decoded#{name struct};"
  end
  out.puts "}"

  hashCodeExpression = case struct.members.length
    when 0
      "0"
    when 1
      if is_decl_array(struct.members[0].declaration)
        "Arrays.hashCode(this.#{struct.members[0].name})"
      else
        "Objects.hashCode(this.#{struct.members[0].name})"
      end
    else
      "Objects.hashCode(#{
        (struct.members.map { |m|
          if is_decl_array(m.declaration)
            "Arrays.hashCode(this.#{m.name})"
          else
            "this.#{m.name}"
          end
        }).join(", ")
      })"
  end
  out.puts <<-EOS.strip_heredoc
    @Override
    public int hashCode() {
      return #{hashCodeExpression};
    }
  EOS

  equalParts = struct.members.map { |m|
    if is_decl_array(m.declaration)
      "Arrays.equals(this.#{m.name}, other.#{m.name})"
    else
      "Objects.equal(this.#{m.name}, other.#{m.name})"
    end
  }
  equalExpression = case equalParts.length
    when 0
      "true"
    else
      equalParts.join(" && ")
  end
  type = name struct
  out.puts <<-EOS.strip_heredoc
    @Override
    public boolean equals(Object object) {
      if (!(object instanceof #{type})) {
        return false;
      }

      #{type} other = (#{type}) object;
      return #{equalExpression};
    }

  EOS

  out.puts "public static final class Builder {"
  out.indent do
    struct.members.map { |m|
      out.puts "private #{decl_string(m.declaration)} #{m.name};"
    }

    struct.members.map { |m|
      out.puts <<-EOS.strip_heredoc

        public Builder #{m.name}(#{decl_string(m.declaration)} #{m.name}) {
          this.#{m.name} = #{m.name};
          return this;
        }
      EOS
    }

  end


  out.indent do
    out.break
    out.puts "public #{name struct} build() {"
    out.indent do
      out.puts "#{name struct} val = new #{name struct}();"
      struct.members.map { |m|
        out.puts "val.set#{m.name.slice(0,1).capitalize+m.name.slice(1..-1)}(#{m.name});"
      }
      out.puts "return val;"
    end
    out.puts "}"
  end
  out.puts "}"
  out.break
end

#render_top_matter(out) ⇒ Object



652
653
654
655
656
657
658
659
660
661
662
663
# File 'lib/xdrgen/generators/java.rb', line 652

def render_top_matter(out)
  out.puts <<-EOS.strip_heredoc
    // Automatically generated by xdrgen
    // DO NOT EDIT or your changes may be overwritten

    package #{@namespace};


    import java.io.IOException;
  EOS
  out.break
end

#render_typedef(typedef, out) ⇒ Object



353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
# File 'lib/xdrgen/generators/java.rb', line 353

def render_typedef(typedef, out)
  out.puts <<-EOS.strip_heredoc
    private #{decl_string typedef.declaration} #{typedef.name};

    public #{typedef.name.camelize}() {}

    public #{typedef.name.camelize}(#{decl_string typedef.declaration} #{typedef.name}) {
      this.#{typedef.name} = #{typedef.name};
    }

    public #{decl_string typedef.declaration} get#{typedef.name.slice(0,1).capitalize+typedef.name.slice(1..-1)}() {
      return this.#{typedef.name};
    }

    public void set#{typedef.name.slice(0,1).capitalize+typedef.name.slice(1..-1)}(#{decl_string typedef.declaration} value) {
      this.#{typedef.name} = value;
    }

  EOS

  out.puts "public static void encode(XdrDataOutputStream stream, #{name typedef}  encoded#{name typedef}) throws IOException {"
  out.indent do
    encode_member "encoded#{name typedef}", typedef, out
  end
  out.puts "}"
  out.break

  out.puts <<-EOS.strip_heredoc
    public void encode(XdrDataOutputStream stream) throws IOException {
      encode(stream, this);
    }
  EOS

  out.puts <<-EOS.strip_heredoc
    public static #{name typedef} decode(XdrDataInputStream stream) throws IOException {
      #{name typedef} decoded#{name typedef} = new #{name typedef}();
  EOS
  out.indent do
    decode_member "decoded#{name typedef}", typedef, out
    out.puts "return decoded#{name typedef};"
  end
  out.puts "}"
  out.break

  hash_coder_for_decl =
    if is_decl_array(typedef.declaration)
      "Arrays.hashCode"
    else
      "Objects.hashCode"
    end
  out.puts <<-EOS.strip_heredoc
    @Override
    public int hashCode() {
      return #{hash_coder_for_decl}(this.#{typedef.name});
    }

  EOS

  equals_for_decl =
    if is_decl_array(typedef.declaration)
      "Arrays.equals"
    else
      "Objects.equal"
    end
  type = name_string typedef.name
  out.puts <<-EOS.strip_heredoc
    @Override
    public boolean equals(Object object) {
      if (!(object instanceof #{type})) {
        return false;
      }

      #{type} other = (#{type}) object;
      return #{equals_for_decl}(this.#{typedef.name}, other.#{typedef.name});
    }
  EOS
end

#render_union(union, out) ⇒ Object



431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
# File 'lib/xdrgen/generators/java.rb', line 431

def render_union(union, out)
  out.puts "public #{name union} () {}"
  out.puts <<-EOS.strip_heredoc
    #{type_string union.discriminant.type} #{union.discriminant.name};
    public #{type_string union.discriminant.type} getDiscriminant() {
      return this.#{union.discriminant.name};
    }
    public void setDiscriminant(#{type_string union.discriminant.type} value) {
      this.#{union.discriminant.name} = value;
    }
  EOS
  union.arms.each do |arm|
    next if arm.void?
    out.puts <<-EOS.strip_heredoc
      private #{decl_string(arm.declaration)} #{arm.name};
      public #{decl_string(arm.declaration)} get#{arm.name.slice(0,1).capitalize+arm.name.slice(1..-1)}() {
        return this.#{arm.name};
      }
      public void set#{arm.name.slice(0,1).capitalize+arm.name.slice(1..-1)}(#{decl_string arm.declaration} value) {
        this.#{arm.name} = value;
      }
    EOS
  end
  out.break

  out.puts "public static final class Builder {"
  out.indent do
    out.puts "private #{type_string union.discriminant.type} discriminant;"
    union.arms.each do |arm|
      next if arm.void?
      out.puts "private #{decl_string(arm.declaration)} #{arm.name};"
    end
    out.break

    out.puts <<-EOS.strip_heredoc
      public Builder discriminant(#{type_string union.discriminant.type} discriminant) {
        this.discriminant = discriminant;
        return this;
      }
    EOS

    union.arms.each do |arm|
      next if arm.void?
      out.puts <<-EOS.strip_heredoc

        public Builder #{arm.name}(#{decl_string(arm.declaration)} #{arm.name}) {
          this.#{arm.name} = #{arm.name};
          return this;
        }
      EOS
    end
  end

  out.indent do
    out.break
    out.puts "public #{name union} build() {"
    out.indent do
      out.puts "#{name union} val = new #{name union}();"
      out.puts "val.setDiscriminant(discriminant);"
      union.arms.each do |arm|
        next if arm.void?
        out.puts "val.set#{arm.name.slice(0,1).capitalize+arm.name.slice(1..-1)}(#{arm.name});"
      end
      out.puts "return val;"
    end
    out.puts "}"
  end
  out.puts "}"
  out.break


  out.puts "public static void encode(XdrDataOutputStream stream, #{name union} encoded#{name union}) throws IOException {"
  out.puts('//' + union.discriminant.type.class.to_s)
  out.puts("//" + type_string(union.discriminant.type))
  if union.discriminant.type.is_a?(AST::Typespecs::Int)
    out.puts "stream.writeInt(encoded#{name union}.getDiscriminant().intValue());"
  elsif type_string(union.discriminant.type) == "Uint32"
    # ugly workaround for compile error after generating source for AuthenticatedMessage in stellar-core
    out.puts "stream.writeInt(encoded#{name union}.getDiscriminant().getUint32());"
  else
    out.puts "stream.writeInt(encoded#{name union}.getDiscriminant().getValue());"
  end
  if type_string(union.discriminant.type) == "Uint32"
    # ugly workaround for compile error after generating source for AuthenticatedMessage in stellar-core
    out.puts "switch (encoded#{name union}.getDiscriminant().getUint32()) {"
  else
    out.puts "switch (encoded#{name union}.getDiscriminant()) {"
  end
  union.arms.each do |arm|
    case arm
      when AST::Definitions::UnionDefaultArm ;
        out.puts "default:"
      else
        arm.cases.each do |kase|
          if kase.value.is_a?(AST::Identifier)
            if type_string(union.discriminant.type) == "Integer"
              member = union.resolved_case(kase)
              out.puts "case #{member.value}:"
            else
              out.puts "case #{kase.value.name}:"
            end
          else
            out.puts "case #{kase.value.value}:"
          end
        end
    end
    encode_member "encoded#{name union}", arm, out
    out.puts "break;"
  end
  out.puts "}\n}"
  out.puts <<-EOS.strip_heredoc
    public void encode(XdrDataOutputStream stream) throws IOException {
      encode(stream, this);
    }
  EOS

  out.puts "public static #{name union} decode(XdrDataInputStream stream) throws IOException {"
  out.puts "#{name union} decoded#{name union} = new #{name union}();"
  if union.discriminant.type.is_a?(AST::Typespecs::Int)
    out.puts "Integer discriminant = stream.readInt();"
  else
    out.puts "#{name union.discriminant.type} discriminant = #{name union.discriminant.type}.decode(stream);"
  end
  out.puts "decoded#{name union}.setDiscriminant(discriminant);"

  if type_string(union.discriminant.type) == "Uint32"
    # ugly workaround for compile error after generating source for AuthenticatedMessage in stellar-core
    out.puts "switch (decoded#{name union}.getDiscriminant().getUint32()) {"
  else
    out.puts "switch (decoded#{name union}.getDiscriminant()) {"
  end

  union.arms.each do |arm|
    case arm
      when AST::Definitions::UnionDefaultArm ;
        out.puts "default:"
      else
        arm.cases.each do |kase|
          if kase.value.is_a?(AST::Identifier)
            if type_string(union.discriminant.type) == "Integer"
              member = union.resolved_case(kase)
              out.puts "case #{member.value}:"
            else
              out.puts "case #{kase.value.name}:"
            end
          else
            out.puts "case #{kase.value.value}:"
          end
        end
    end
    decode_member "decoded#{name union}", arm, out
    out.puts "break;"
  end
  out.puts "}\n"
  out.indent do
    out.puts "return decoded#{name union};"
  end
  out.puts "}"

  nonVoidArms = union.arms.select { |arm| !arm.void? }

  discriminantPart = if is_type_array(union.discriminant.type)
    "Arrays.hashCode(this.#{union.discriminant.name})"
  else
    "this.#{union.discriminant.name}"
  end

  parts = nonVoidArms.map { |a|
    if is_decl_array(a.declaration)
      "Arrays.hashCode(this.#{a.name})"
    else
      "this.#{a.name}"
    end
  }
  parts.append(discriminantPart)

  hashCodeExpression = "Objects.hashCode(#{parts.join(", ")})"
  out.puts <<-EOS.strip_heredoc
    @Override
    public int hashCode() {
      return #{hashCodeExpression};
    }
  EOS

  equalParts = nonVoidArms.map { |a|
    if is_decl_array(a.declaration)
      "Arrays.equals(this.#{a.name}, other.#{a.name})"
    else
      "Objects.equal(this.#{a.name}, other.#{a.name})"
    end
  }
  equalParts.append(
    if is_type_array(union.discriminant.type)
      "Arrays.equals(this.#{union.discriminant.name}, other.#{union.discriminant.name})"
    else
      "Objects.equal(this.#{union.discriminant.name}, other.#{union.discriminant.name})"
    end
  )

  equalExpression = case equalParts.length
    when 0
      "true"
    else
      equalParts.join(" && ")
  end
  type = name union
  out.puts <<-EOS.strip_heredoc
    @Override
    public boolean equals(Object object) {
      if (!(object instanceof #{type})) {
        return false;
      }

      #{type} other = (#{type}) object;
      return #{equalExpression};
    }
  EOS

  out.break
end

#type_string(type) ⇒ Object



861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
# File 'lib/xdrgen/generators/java.rb', line 861

def type_string(type)
  case type
  when AST::Typespecs::Int ;
    "Integer"
  when AST::Typespecs::UnsignedInt ;
    "Integer"
  when AST::Typespecs::Hyper ;
    "Long"
  when AST::Typespecs::UnsignedHyper ;
    "Long"
  when AST::Typespecs::Float ;
    "Float"
  when AST::Typespecs::Double ;
    "Double"
  when AST::Typespecs::Quadruple ;
    raise "cannot render quadruple in golang"
  when AST::Typespecs::Bool ;
    "Boolean"
  when AST::Typespecs::Opaque ;
    "Byte[#{type.size}]"
  when AST::Typespecs::String ;
    "XdrString"
  when AST::Typespecs::Simple ;
    name type.resolved_type
  when AST::Concerns::NestedDefinition ;
    name type
  else
    raise "Unknown typespec: #{type.class.name}"
  end
end