aboutsummaryrefslogtreecommitdiff
path: root/libjava/classpath/gnu/CORBA/gnuAny.java
blob: 7e5ef33514929d07f05591081f7c3922d28376f1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
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
94
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
120
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
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
220
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
352
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
430
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
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
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
719
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
748
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
790
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
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
/* gnuAny.java --
   Copyright (C) 2005 Free Software Foundation, Inc.

This file is part of GNU Classpath.

GNU Classpath is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.

GNU Classpath is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
General Public License for more details.

You should have received a copy of the GNU General Public License
along with GNU Classpath; see the file COPYING.  If not, write to the
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA.

Linking this library statically or dynamically with other modules is
making a combined work based on this library.  Thus, the terms and
conditions of the GNU General Public License cover the whole
combination.

As a special exception, the copyright holders of this library give you
permission to link this library with independent modules to produce an
executable, regardless of the license terms of these independent
modules, and to copy and distribute the resulting executable under
terms of your choice, provided that you also meet, for each linked
independent module, the terms and conditions of the license of that
module.  An independent module is a module which is not derived from
or based on this library.  If you modify this library, you may extend
this exception to your version of the library, but you are not
obligated to do so.  If you do not wish to do so, delete this
exception statement from your version. */


package gnu.CORBA;

import gnu.CORBA.CDR.Vio;
import gnu.CORBA.CDR.cdrBufInput;
import gnu.CORBA.CDR.cdrBufOutput;

import org.omg.CORBA.Any;
import org.omg.CORBA.AnyHolder;
import org.omg.CORBA.BAD_OPERATION;
import org.omg.CORBA.BooleanHolder;
import org.omg.CORBA.CharHolder;
import org.omg.CORBA.DoubleHolder;
import org.omg.CORBA.FixedHolder;
import org.omg.CORBA.FloatHolder;
import org.omg.CORBA.IntHolder;
import org.omg.CORBA.LongHolder;
import org.omg.CORBA.MARSHAL;
import org.omg.CORBA.ORB;
import org.omg.CORBA.ObjectHolder;
import org.omg.CORBA.Principal;
import org.omg.CORBA.PrincipalHolder;
import org.omg.CORBA.ShortHolder;
import org.omg.CORBA.StringHolder;
import org.omg.CORBA.TCKind;
import org.omg.CORBA.TypeCode;
import org.omg.CORBA.TypeCodeHolder;
import org.omg.CORBA.TypeCodePackage.BadKind;
import org.omg.CORBA.ValueBaseHolder;
import org.omg.CORBA.portable.BoxedValueHelper;
import org.omg.CORBA.portable.Streamable;

import java.io.IOException;
import java.io.Serializable;

import java.lang.reflect.Field;

import java.math.BigDecimal;

import java.util.Arrays;

/**
 * The implementation of {@link Any}.
 *
 * For performance reasonse, the inserted values are not cloned.
 * If the value object allows modifications (like {@link Streamable}),
 * these subsequent alterations are reflected by the instance of
 * this gnuAny, and the gnuAny alterations are reflected by the
 * returned value. If it is required to have the uncoupled value,
 * it must be requested from the copy of the current instance.
 * The {@link gnuAny} can be simply cloned by the provided
 * {@link Clone()} method.
 *
 * @author Audrius Meskauskas (AudriusA@Bioinformatics.org)
 */
public class gnuAny
  extends Any
{
  /**
   * The value, returned by {@link #type()} if the value has been
   * not intialized.
   */
  protected static final TypeCode nullType =
    new primitiveTypeCode(TCKind.tk_null);

  /**
   * The Streamable, representing the value, held by this gnuAny.
   */
  protected Streamable has;

  /**
   * The complete typecode of the Streamable, if explicitly set.
   */
  protected TypeCode typecode;

  /**
   * The typecode kind of the Streamable, if explicitly set.
   */
  protected int xKind = -1;

  /**
   * The associated ORB.
   */
  private ORB orb;

  /**
   * Set the associated orb.
   */
  public void setOrb(ORB an_orb)
  {
    orb = an_orb;
  }

  /**
   * Creates a deep copy of this gnuAny, writing to and subsequently
   * reading from from the byte buffer.
   *
   * @return the uncoupled gnuAny with all fields set to identical
   * values.
   */
  public gnuAny Clone()
  {
    cdrBufOutput out = new cdrBufOutput();
    out.setOrb(orb);
    out.write_any(this);

    cdrBufInput in = new cdrBufInput(out.buffer.toByteArray());
    in.setOrb(orb);
    return (gnuAny) in.read_any();
  }

  /**
   * Create the buffered CDR input stream, containing the
   * value, stored inside of this {@link Any}.
   */
  public org.omg.CORBA.portable.InputStream create_input_stream()
  {
    if (has instanceof universalHolder)
      {
        universalHolder u = (universalHolder) has;
        return u.getInputStream();
      }
    else
      {
        cdrBufOutput out = new cdrBufOutput();
        out.setOrb(orb);
        write_value(out);

        cdrBufInput in = new cdrBufInput(out.buffer.toByteArray());
        in.setOrb(orb);
        return in;
      }
  }

  /**
   * Create the buffered CDR output stream (empty).
   */
  public org.omg.CORBA.portable.OutputStream create_output_stream()
  {
    cdrBufOutput stream = new cdrBufOutput();
    stream.setOrb(orb);
    return stream;
  }

  /**
   * Compare two Any's for equality.
   * @param other the other Any to compare.
   */
  public boolean equal(Any other)
  {
    if (other == this)
      return true;
    if (type().kind() != other.type().kind())
      return false;

    if (has != null && other instanceof gnuAny)
      if (has.equals(((gnuAny) other).has))
        return true;

    cdrBufOutput a = new cdrBufOutput();
    a.setOrb(orb);
    write_value(a);

    cdrBufOutput b = new cdrBufOutput();
    b.setOrb(orb);
    other.write_value(b);

    byte[] ba = a.buffer.toByteArray();
    byte[] bb = b.buffer.toByteArray();

    return Arrays.equals(ba, bb);
  }

  /**
   * Delegates functionality to {@link #equal(Any)}.
   */
  public boolean equals(java.lang.Object other)
  {
    if (other == this)
      return true;
    if (!(other instanceof Any))
      return false;

    return equal((Any) other);
  }

  /**
   * Extract the previously stored object.
   */
  public org.omg.CORBA.Object extract_Object()
  {
    try
      {
        return ((ObjectHolder) has).value;
      }
    catch (ClassCastException ex)
      {
        throw new BAD_OPERATION();
      }
  }

  /**
   * Extract the previously inserted CORBA <code>Principal</code>/
   * @return the previously inserted value.
   *
   * @throws org.omg.CORBA.BAD_OPERATION if the holder contains something
   * else than Principal.
   *
   * @deprecated by CORBA 2.2.
   */
  public Principal extract_Principal()
  {
    check(TCKind._tk_Principal);
    return ((PrincipalHolder) has).value;
  }

  /**
   * Return the value, encapsulated in a suitable holder.
   * This implementation returns the direct reference,
   * so the alterations on the returned streamable are
   * directly reflected to the content of this {@link Any}.
   */
  public Streamable extract_Streamable()
  {
    return has;
  }

  public TypeCode extract_TypeCode()
                            throws BAD_OPERATION
  {
    check(TCKind._tk_TypeCode);
    return ((TypeCodeHolder) has).value;
  }

  /**
   * Extract the stored value type.
   *
   * @return the previously stored value type.
   *
   * @throws BAD_OPERATION if the Any contains something different.
   *
   * @see org.omg.CORBA.portable.ValueBase
   */
  public Serializable extract_Value()
                             throws BAD_OPERATION
  {
    try
      {
        if (has instanceof ValueBaseHolder)
          return ((ValueBaseHolder) has).value;
        else
          {
            // Normally, ValueBase holder must be an instance of the
            // ValueBaseHolder. However some IDL compilers probably
            // have a bug, do not deriving this way. The the only
            // way to access the wrapped value is via reflection.
            Field f = has.getClass().getField("value");
            return (Serializable) f.get(has);
          }
      }
    catch (Exception ex)
      {
        return new BAD_OPERATION("Value type expected");
      }
  }

  /** {@inheritDoc} */
  public Any extract_any()
                  throws BAD_OPERATION
  {
    check(TCKind._tk_any);
    return ((AnyHolder) has).value;
  }

  /** {@inheritDoc} */
  public boolean extract_boolean()
                          throws BAD_OPERATION
  {
    check(TCKind._tk_boolean);
    return ((BooleanHolder) has).value;
  }

  /** {@inheritDoc} */
  public char extract_char()
                    throws BAD_OPERATION
  {
    check(TCKind._tk_char);
    return ((CharHolder) has).value;
  }

  /** {@inheritDoc} */
  public double extract_double()
                        throws BAD_OPERATION
  {
    check(TCKind._tk_double);
    return ((DoubleHolder) has).value;
  }

  /**
   * Extract the previously inserted CORBA <code>fixed</code>/
   * @return the previously inserted value.
   *
   * @throws org.omg.CORBA.BAD_OPERATION if the holder contains something
   * else than BigDecimal.
   */
  public BigDecimal extract_fixed()
                           throws org.omg.CORBA.BAD_OPERATION
  {
    check(TCKind._tk_fixed);
    return ((FixedHolder) has).value;
  }

  /** {@inheritDoc} */
  public float extract_float()
                      throws BAD_OPERATION
  {
    check(TCKind._tk_float);
    return ((FloatHolder) has).value;
  }

  /** {@inheritDoc} */
  public int extract_long()
                   throws BAD_OPERATION
  {
    // CORBA long = java int.
    check(TCKind._tk_long);
    return ((IntHolder) has).value;
  }

  /** {@inheritDoc} */
  public long extract_longlong()
                        throws BAD_OPERATION
  {
    check(TCKind._tk_longlong);
    return ((LongHolder) has).value;
  }

  /** {@inheritDoc} */
  public byte extract_octet()
                     throws BAD_OPERATION
  {
    // ShortHolder holds also octets.
    check(TCKind._tk_octet);
    return (byte) ((OctetHolder) has).value;
  }

  /** {@inheritDoc} */
  public short extract_short()
                      throws BAD_OPERATION
  {
    check(TCKind._tk_short);
    return ((ShortHolder) has).value;
  }

  /** {@inheritDoc} */
  public String extract_string()
                        throws BAD_OPERATION
  {
    check(TCKind._tk_string);
    return ((StringHolder) has).value;
  }

  /** {@inheritDoc} */
  public int extract_ulong()
                    throws BAD_OPERATION
  {
    // IntHolder also holds ulongs.
    check(TCKind._tk_ulong);
    return ((IntHolder) has).value;
  }

  /** {@inheritDoc} */
  public long extract_ulonglong()
                         throws BAD_OPERATION
  {
    // LongHolder also holds ulonglong
    check(TCKind._tk_ulonglong);
    return ((LongHolder) has).value;
  }

  /** {@inheritDoc} */
  public short extract_ushort()
                       throws BAD_OPERATION
  {
    // ShortHolder also holds ushorts.
    check(TCKind._tk_ushort);
    return ((ShortHolder) has).value;
  }

  /** {@inheritDoc} */
  public char extract_wchar()
                     throws BAD_OPERATION
  {
    check(TCKind._tk_wchar);
    return ((WCharHolder) has).value;
  }

  /** {@inheritDoc} */
  public String extract_wstring()
                         throws BAD_OPERATION
  {
    // StringHolder also holds wstrings.
    check(TCKind._tk_wstring);
    return ((WStringHolder) has).value;
  }

  /**
   * Inserts the CORBA object and sets the typecode to the given type.
   */
  public void insert_Object(org.omg.CORBA.Object x, TypeCode typecode)
  {
    has = new ObjectHolder(x);
    type(typecode);
  }

  /**
   * Inserts the CORBA object.
   */
  public void insert_Object(org.omg.CORBA.Object x)
  {
    has = new ObjectHolder(x);
  }

  /**
   * Insert the CORBA Principal.
   * This implementation uses direct assignment, so the later
   * alterations of that BigDecimal are reflected on the
   * content of this {@link Any}.
   *
   * @deprecated by CORBA 2.2.
   */
  public void insert_Principal(Principal x)
  {
    resetTypes();
    if (has instanceof PrincipalHolder)
      ((PrincipalHolder) has).value = x;
    else
      has = new PrincipalHolder(x);
  }

  /**
   * Sets the value to the value, encapsulated in this holder.
   * This implementation uses direct assignment, so the later
   * alterations of that streamable are reflected on the
   * content of this {@link Any}.
   */
  public void insert_Streamable(Streamable x)
  {
    resetTypes();
    has = x;
  }

  /**
   * Insert the typecode into this Any
   * @param typecode the typecode to insert.
   */
  public void insert_TypeCode(TypeCode typecode)
  {
    resetTypes();
    if (has instanceof TypeCodeHolder)
      ((TypeCodeHolder) has).value = typecode;
    else
      has = new TypeCodeHolder(typecode);
  }

  /** {@inheritDoc} */
  public void insert_Value(Serializable x, TypeCode c_typecode)
  {
    if (typecode != null && typecode.kind() == TCKind.tk_value_box)
      {
        has = new gnuValueHolder(x, typecode);
      }
    else
      {
        type(typecode);
        insert_Value(x);
      }
  }

  /** {@inheritDoc} */
  public void insert_Value(Serializable x)
  {
    if (typecode != null && typecode.kind() == TCKind.tk_value_box)
      {
        has = new gnuValueHolder(x, typecode);
      }
    else
      {
        if (has instanceof ValueBaseHolder)
          ((ValueBaseHolder) has).value = x;
        else
          has = new ValueBaseHolder(x);
      }
  }

  /**
  * Insert another {@link Any} into this {@link Any}.
  * This implementation uses direct assignment, so the later
  * alterations of that {@link Any} are reflected on the
  * content of this {@link Any}.
  */
  public void insert_any(Any an_any)
  {
    resetTypes();
    if (has instanceof AnyHolder)
      ((AnyHolder) has).value = an_any;
    else
      has = new AnyHolder(an_any);
  }

  /** {@inheritDoc} */
  public void insert_boolean(boolean x)
  {
    resetTypes();
    if (has instanceof BooleanHolder)
      ((BooleanHolder) has).value = x;
    else
      has = new BooleanHolder(x);
  }

  /** {@inheritDoc} */
  public void insert_char(char x)
  {
    resetTypes();
    if (has instanceof CharHolder)
      ((CharHolder) has).value = x;
    else
      has = new CharHolder(x);
  }

  /** {@inheritDoc} */
  public void insert_double(double x)
  {
    resetTypes();
    if (has instanceof DoubleHolder)
      ((DoubleHolder) has).value = x;
    else
      has = new DoubleHolder(x);
  }

  /**
   * Inserts the CORBA <code>fixed</code>, setting the typecode
   * explicitly.
   * This implementation uses direct assignment, so the later
   * alterations of that BigDecimal are reflected on the
   * content of this {@link Any}.
   */
  public void insert_fixed(BigDecimal x, TypeCode x_typecode)
  {
    resetTypes();
    insert_fixed(x);
    typecode = x_typecode;
  }

  /**
   * Inserts the CORBA <code>fixed</code>, setting the typecode
   * by example of the currently passed value.
   * This implementation uses direct assignment, so the later
   * alterations of that BigDecimal are reflected on the
   * content of this {@link Any}, including the typecode.
   */
  public void insert_fixed(BigDecimal x)
  {
    resetTypes();
    if (has instanceof FixedHolder)
      ((FixedHolder) has).value = x;
    else
      has = new FixedHolder(x);
  }

  /** {@inheritDoc} */
  public void insert_float(float x)
  {
    resetTypes();
    if (has instanceof FloatHolder)
      ((FloatHolder) has).value = x;
    else
      has = new FloatHolder(x);
  }

  /** {@inheritDoc} */
  public void insert_long(int x)
  {
    resetTypes();
    if (has instanceof IntHolder)
      ((IntHolder) has).value = x;
    else
      has = new IntHolder(x);
  }

  /** {@inheritDoc} */
  public void insert_longlong(long x)
  {
    resetTypes();
    if (has instanceof LongHolder)
      ((LongHolder) has).value = x;
    else
      has = new LongHolder(x);
  }

  /** {@inheritDoc} */
  public void insert_octet(byte x)
  {
    resetTypes();
    if (has instanceof OctetHolder)
      ((OctetHolder) has).value = x;
    else
      has = new OctetHolder(x);
  }

  /** {@inheritDoc} */
  public void insert_short(short x)
  {
    resetTypes();
    if (has instanceof ShortHolder)
      ((ShortHolder) has).value = x;
    else
      has = new ShortHolder(x);
  }

  /** {@inheritDoc} */
  public void insert_string(String x)
  {
    resetTypes();
    if (has instanceof StringHolder)
      ((StringHolder) has).value = x;
    else
      has = new StringHolder(x);

    typecode = new stringTypeCode(TCKind.tk_string);
  }

  /** {@inheritDoc} */
  public void insert_ulong(int x)
  {
    resetTypes();
    if (has instanceof IntHolder)
      ((IntHolder) has).value = x;
    else
      has = new IntHolder(x);
    xKind = TCKind._tk_ulong;
  }

  /** {@inheritDoc} */
  public void insert_ulonglong(long x)
  {
    resetTypes();
    if (has instanceof LongHolder)
      ((LongHolder) has).value = x;
    else
      has = new LongHolder(x);
    xKind = TCKind._tk_ulonglong;
  }

  /** {@inheritDoc} */
  public void insert_ushort(short x)
  {
    resetTypes();
    if (has instanceof ShortHolder)
      ((ShortHolder) has).value = x;
    else
      has = new ShortHolder(x);
    xKind = TCKind._tk_ushort;
  }

  /** {@inheritDoc} */
  public void insert_wchar(char x)
  {
    resetTypes();
    if (has instanceof WCharHolder)
      ((WCharHolder) has).value = x;
    else
      has = new WCharHolder(x);
  }

  /** {@inheritDoc} */
  public void insert_wstring(String x)
  {
    resetTypes();
    if (has instanceof WStringHolder)
      ((WStringHolder) has).value = x;
    else
      has = new WStringHolder(x);
  }

  /**
   * Return the associated orb.
   */
  public ORB orb()
  {
    return orb;
  }

  /**
   * Read the value of the given type from the given stream.
   *
   * @param input a stream to read from.
   * @param a_type a typecode of the value to read.
   */
  public void read_value(org.omg.CORBA.portable.InputStream input,
                         TypeCode a_type
                        )
                  throws MARSHAL
  {
    try
      {
        int kind = a_type.kind().value();

        // Fixed needs special handling.
        if (kind == TCKind._tk_fixed)
          {
            BigDecimal dec = BigDecimalHelper.read(input, a_type.fixed_scale());
            has = new FixedHolder(dec);
          }
        else
          {
            has = holderFactory.createHolder(a_type);
            if (has == null)
              {
                // Use the Universal Holder that reads till the end of stream.
                // This works with the extract/insert pair of the typical
                // Helper.
                cdrBufOutput buffer = new cdrBufOutput();
                buffer.setOrb(orb);
                has = new universalHolder(buffer);
              }
          }
        type(a_type);

        if (!(has instanceof universalHolder) &&
            (kind == TCKind._tk_value_box))
          {
            // The streamable only contains operations for
            // reading the value, not the value header.
            Field vField = has.getClass().getField("value");

            BoxedValueHelper helper;

            try
              {
                Class helperClass =
                  Class.forName(ObjectCreator.toHelperName(a_type.id()));
                helper = (BoxedValueHelper) helperClass.newInstance();
              }
            catch (Exception ex)
              {
                helper = null;
              }

            Object content = Vio.read(input, helper);
            vField.set(has, content);
          }
        else
          has._read(input);
      }
    catch (Exception ex)
      {
        MARSHAL m = new MARSHAL();
        m.initCause(ex);
        throw m;
      }
  }

  /** {@inheritDoc} */
  public TypeCode type()
  {
    if (typecode != null)
      return typecode;
    else if (xKind >= 0)
      {
        typecode = new primitiveTypeCode(TCKind.from_int(xKind));
        return typecode;
      }
    else
      return has != null ? has._type() : nullType;
  }

  /**
   * Explicitly set the typecode of the value to the given type.
   *
   * @param valueTypeCode the typecode of the value.
   */
  public void type(TypeCode valueTypeCode)
  {
    xKind = valueTypeCode.kind().value();
    typecode = valueTypeCode;
  }

  /** {@inheritDoc} */
  public void write_value(org.omg.CORBA.portable.OutputStream output)
  {
    if (has != null)
      has._write(output);
    else
    // These kinds support null.
    if (xKind == TCKind._tk_null || xKind == TCKind._tk_objref ||
        xKind == TCKind._tk_value || xKind == TCKind._tk_value_box
       )
      output.write_long(0);
  }

  /**
   * Check if the current value if the value of the given kind.
   * @param kind a kind to check.
   * @throws BAD_OPERATION if the value is not set of is different kind.
   */
  protected void check(int kind)
                throws BAD_OPERATION
  {
    if (has == null)
      throw new BAD_OPERATION("value not set");

    if (xKind >= 0)
      {
        if (xKind != kind)
          if (!(
                xKind == TCKind._tk_alias &&
                has._type().kind().value() == kind
              )
             )
            throw new BAD_OPERATION("Extracting " + typeNamer.nameIt(kind) +
                                    " when stored " + typeNamer.nameIt(xKind)
                                   );
      }
    else
      {
        if (type().kind().value() != kind)
          if (!(
                type().kind().value() == TCKind._tk_alias &&
                has._type().kind().value() == kind
              )
             )
            throw new BAD_OPERATION("Extracting " + typeNamer.nameIt(kind) +
                                    " stored " + typeNamer.nameIt(type())
                                   );
      }
  }

  /**
   * Clear the additional type information before reusing this instance.
   */
  private final void resetTypes()
  {
    typecode = null;
    xKind = -1;
  }
}