gnuDynAny.java 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946
  1. /* gnuDynAny.java --
  2. Copyright (C) 2005 Free Software Foundation, Inc.
  3. This file is part of GNU Classpath.
  4. GNU Classpath is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8. GNU Classpath is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNU Classpath; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  15. 02110-1301 USA.
  16. Linking this library statically or dynamically with other modules is
  17. making a combined work based on this library. Thus, the terms and
  18. conditions of the GNU General Public License cover the whole
  19. combination.
  20. As a special exception, the copyright holders of this library give you
  21. permission to link this library with independent modules to produce an
  22. executable, regardless of the license terms of these independent
  23. modules, and to copy and distribute the resulting executable under
  24. terms of your choice, provided that you also meet, for each linked
  25. independent module, the terms and conditions of the license of that
  26. module. An independent module is a module which is not derived from
  27. or based on this library. If you modify this library, you may extend
  28. this exception to your version of the library, but you are not
  29. obligated to do so. If you do not wish to do so, delete this
  30. exception statement from your version. */
  31. package gnu.CORBA.DynAn;
  32. import gnu.CORBA.CDR.BufferedCdrOutput;
  33. import gnu.CORBA.OctetHolder;
  34. import gnu.CORBA.Unexpected;
  35. import gnu.CORBA.WCharHolder;
  36. import gnu.CORBA.WStringHolder;
  37. import gnu.CORBA.HolderLocator;
  38. import gnu.CORBA.TypeKindNamer;
  39. import gnu.CORBA.GeneralHolder;
  40. import org.omg.CORBA.Any;
  41. import org.omg.CORBA.AnyHolder;
  42. import org.omg.CORBA.BooleanHolder;
  43. import org.omg.CORBA.CharHolder;
  44. import org.omg.CORBA.DoubleHolder;
  45. import org.omg.CORBA.FloatHolder;
  46. import org.omg.CORBA.IntHolder;
  47. import org.omg.CORBA.LongHolder;
  48. import org.omg.CORBA.MARSHAL;
  49. import org.omg.CORBA.ORB;
  50. import org.omg.CORBA.Object;
  51. import org.omg.CORBA.ObjectHolder;
  52. import org.omg.CORBA.ShortHolder;
  53. import org.omg.CORBA.StringHolder;
  54. import org.omg.CORBA.TCKind;
  55. import org.omg.CORBA.TypeCode;
  56. import org.omg.CORBA.TypeCodeHolder;
  57. import org.omg.CORBA.TypeCodePackage.BadKind;
  58. import org.omg.CORBA.ValueBaseHolder;
  59. import org.omg.CORBA.portable.InputStream;
  60. import org.omg.CORBA.portable.OutputStream;
  61. import org.omg.CORBA.portable.Streamable;
  62. import org.omg.DynamicAny.DynAny;
  63. import org.omg.DynamicAny.DynAnyPackage.InvalidValue;
  64. import org.omg.DynamicAny.DynAnyPackage.TypeMismatch;
  65. import java.io.IOException;
  66. import java.io.Serializable;
  67. import java.util.Arrays;
  68. /**
  69. * The primitive dynamic Any holds the value basic final_type that cannot be
  70. * traversed.
  71. *
  72. * @author Audrius Meskauskas, Lithuania (AudriusA@Bioinformatics.org)
  73. */
  74. public class gnuDynAny extends AbstractAny implements DynAny, Serializable
  75. {
  76. /**
  77. * Use serialVersionUID for interoperability.
  78. */
  79. private static final long serialVersionUID = 1;
  80. /**
  81. * The enclosed Streamable, holding the actual value.
  82. */
  83. protected Streamable holder;
  84. /**
  85. * Create DynAny providing the holder.
  86. *
  87. * @param a_holder
  88. */
  89. public gnuDynAny(Streamable aHolder, TypeCode oType, TypeCode aType,
  90. gnuDynAnyFactory aFactory, ORB anOrb
  91. )
  92. {
  93. super(oType, aType, aFactory, anOrb);
  94. holder = aHolder;
  95. }
  96. /**
  97. * Assign the contents of the given {@link DynAny} to this DynAny.
  98. *
  99. * @param from the source to assign from.
  100. */
  101. public void assign(DynAny from) throws TypeMismatch
  102. {
  103. checkType(official_type, from.type());
  104. if (from instanceof gnuDynAny)
  105. holder = ((gnuDynAny) from).holder;
  106. else
  107. holder = from.to_any().extract_Streamable();
  108. valueChanged();
  109. }
  110. /**
  111. * Create a copy of this {@link DynAny} via buffer read/write.
  112. */
  113. public DynAny copy()
  114. {
  115. if (holder != null)
  116. {
  117. BufferedCdrOutput buffer = new BufferedCdrOutput();
  118. holder._write(buffer);
  119. gnuDynAny other;
  120. try
  121. {
  122. other =
  123. new gnuDynAny((Streamable) (holder.getClass().newInstance()),
  124. official_type, final_type, factory, orb
  125. );
  126. }
  127. catch (Exception e)
  128. {
  129. // Holder must have parameterless constructor.
  130. throw new Unexpected(e);
  131. }
  132. other.holder._read(buffer.create_input_stream());
  133. return other;
  134. }
  135. else
  136. {
  137. return new gnuDynAny(null, official_type, final_type, factory, orb);
  138. }
  139. }
  140. /**
  141. * Always returns <code>null</code>.
  142. *
  143. * @return <code>null</code>, always.
  144. */
  145. public DynAny current_component() throws TypeMismatch
  146. {
  147. throw new TypeMismatch("Not applicable for " +
  148. TypeKindNamer.nameIt(final_type)
  149. );
  150. }
  151. /**
  152. * Returns without action, leaving all work to the garbage collector.
  153. */
  154. public void destroy()
  155. {
  156. }
  157. /**
  158. * Takes the passed parameter as the enclosed {@link Any} reference.
  159. *
  160. * @param an_any the {@link Any} that will be used as an enclosed reference.
  161. *
  162. * @throws TypeMismatch if the final_type of the passed Any is not the same as
  163. * the final_type, currently stored in this Any.
  164. */
  165. public void from_any(Any an_any) throws TypeMismatch, InvalidValue
  166. {
  167. checkType(official_type, an_any.type());
  168. Streamable a_holder = an_any.extract_Streamable();
  169. if (a_holder == null)
  170. {
  171. throw new InvalidValue(ISNULL);
  172. }
  173. else if (a_holder instanceof GeneralHolder)
  174. {
  175. holder = HolderLocator.createHolder(official_type);
  176. if (holder == null)
  177. holder = HolderLocator.createHolder(final_type);
  178. if (holder == null)
  179. holder = ((GeneralHolder) a_holder).Clone();
  180. else
  181. {
  182. InputStream in = an_any.create_input_stream();
  183. holder._read(in);
  184. try
  185. {
  186. in.close();
  187. }
  188. catch (IOException ex)
  189. {
  190. throw new Unexpected(ex);
  191. }
  192. }
  193. }
  194. else
  195. {
  196. try
  197. {
  198. InputStream in = an_any.create_input_stream();
  199. holder = (Streamable) a_holder.getClass().newInstance();
  200. holder._read(in);
  201. in.close();
  202. }
  203. catch (Exception ex)
  204. {
  205. TypeMismatch t = new TypeMismatch();
  206. t.initCause(ex);
  207. throw t;
  208. }
  209. }
  210. valueChanged();
  211. }
  212. /**
  213. * Return the second (enclosed any) that is stored in the wrapped Any.
  214. */
  215. public Any get_any() throws TypeMismatch
  216. {
  217. try
  218. {
  219. return ((AnyHolder) holder).value;
  220. }
  221. catch (ClassCastException cex)
  222. {
  223. TypeMismatch m = new TypeMismatch();
  224. m.initCause(cex);
  225. throw m;
  226. }
  227. }
  228. /** {@inheritDoc} */
  229. public boolean get_boolean() throws TypeMismatch
  230. {
  231. try
  232. {
  233. return ((BooleanHolder) holder).value;
  234. }
  235. catch (ClassCastException cex)
  236. {
  237. TypeMismatch m = new TypeMismatch();
  238. m.initCause(cex);
  239. throw m;
  240. }
  241. }
  242. /** {@inheritDoc} */
  243. public char get_char() throws TypeMismatch
  244. {
  245. try
  246. {
  247. return ((CharHolder) holder).value;
  248. }
  249. catch (ClassCastException cex)
  250. {
  251. TypeMismatch m = new TypeMismatch();
  252. m.initCause(cex);
  253. throw m;
  254. }
  255. }
  256. /** {@inheritDoc} */
  257. public double get_double() throws TypeMismatch
  258. {
  259. try
  260. {
  261. return ((DoubleHolder) holder).value;
  262. }
  263. catch (ClassCastException cex)
  264. {
  265. TypeMismatch m = new TypeMismatch();
  266. m.initCause(cex);
  267. throw m;
  268. }
  269. }
  270. /** {@inheritDoc} */
  271. public float get_float() throws TypeMismatch
  272. {
  273. try
  274. {
  275. return ((FloatHolder) holder).value;
  276. }
  277. catch (ClassCastException cex)
  278. {
  279. TypeMismatch m = new TypeMismatch();
  280. m.initCause(cex);
  281. throw m;
  282. }
  283. }
  284. /** {@inheritDoc} */
  285. public int get_long() throws TypeMismatch
  286. {
  287. try
  288. {
  289. return ((IntHolder) holder).value;
  290. }
  291. catch (ClassCastException cex)
  292. {
  293. TypeMismatch m = new TypeMismatch();
  294. m.initCause(cex);
  295. throw m;
  296. }
  297. }
  298. /** {@inheritDoc} */
  299. public long get_longlong() throws TypeMismatch
  300. {
  301. try
  302. {
  303. return ((LongHolder) holder).value;
  304. }
  305. catch (ClassCastException cex)
  306. {
  307. TypeMismatch m = new TypeMismatch();
  308. m.initCause(cex);
  309. throw m;
  310. }
  311. }
  312. /** {@inheritDoc} */
  313. public byte get_octet() throws TypeMismatch
  314. {
  315. try
  316. {
  317. return ((OctetHolder) holder).value;
  318. }
  319. catch (ClassCastException cex)
  320. {
  321. TypeMismatch m = new TypeMismatch();
  322. m.initCause(cex);
  323. throw m;
  324. }
  325. }
  326. /** {@inheritDoc} */
  327. public Object get_reference() throws TypeMismatch
  328. {
  329. try
  330. {
  331. return ((ObjectHolder) holder).value;
  332. }
  333. catch (ClassCastException cex)
  334. {
  335. TypeMismatch m = new TypeMismatch();
  336. m.initCause(cex);
  337. throw m;
  338. }
  339. }
  340. /** {@inheritDoc} */
  341. public short get_short() throws TypeMismatch
  342. {
  343. try
  344. {
  345. return ((ShortHolder) holder).value;
  346. }
  347. catch (ClassCastException cex)
  348. {
  349. TypeMismatch m = new TypeMismatch();
  350. m.initCause(cex);
  351. throw m;
  352. }
  353. }
  354. /** {@inheritDoc} */
  355. public String get_string() throws TypeMismatch
  356. {
  357. try
  358. {
  359. return ((StringHolder) holder).value;
  360. }
  361. catch (ClassCastException cex)
  362. {
  363. TypeMismatch m = new TypeMismatch();
  364. m.initCause(cex);
  365. throw m;
  366. }
  367. }
  368. /** {@inheritDoc} */
  369. public TypeCode get_typecode() throws TypeMismatch
  370. {
  371. try
  372. {
  373. return ((TypeCodeHolder) holder).value;
  374. }
  375. catch (ClassCastException cex)
  376. {
  377. TypeMismatch m = new TypeMismatch();
  378. m.initCause(cex);
  379. throw m;
  380. }
  381. }
  382. /** {@inheritDoc} */
  383. public int get_ulong() throws TypeMismatch
  384. {
  385. check(TCKind.tk_ulong);
  386. return get_long();
  387. }
  388. /** {@inheritDoc} */
  389. public long get_ulonglong() throws TypeMismatch
  390. {
  391. check(TCKind.tk_ulonglong);
  392. return get_longlong();
  393. }
  394. /** {@inheritDoc} */
  395. public short get_ushort() throws TypeMismatch
  396. {
  397. check(TCKind.tk_ushort);
  398. return get_short();
  399. }
  400. /** {@inheritDoc} */
  401. public Serializable get_val() throws TypeMismatch
  402. {
  403. try
  404. {
  405. return ((ValueBaseHolder) holder).value;
  406. }
  407. catch (ClassCastException cex)
  408. {
  409. TypeMismatch m = new TypeMismatch();
  410. m.initCause(cex);
  411. throw m;
  412. }
  413. }
  414. /** {@inheritDoc} */
  415. public char get_wchar() throws TypeMismatch
  416. {
  417. try
  418. {
  419. return ((WCharHolder) holder).value;
  420. }
  421. catch (ClassCastException cex)
  422. {
  423. TypeMismatch m = new TypeMismatch();
  424. m.initCause(cex);
  425. throw m;
  426. }
  427. }
  428. /** {@inheritDoc} */
  429. public String get_wstring() throws TypeMismatch
  430. {
  431. try
  432. {
  433. return ((WStringHolder) holder).value;
  434. }
  435. catch (ClassCastException cex)
  436. {
  437. TypeMismatch m = new TypeMismatch();
  438. m.initCause(cex);
  439. throw m;
  440. }
  441. }
  442. /** {@inheritDoc} */
  443. public void insert_any(Any a_x) throws TypeMismatch, InvalidValue
  444. {
  445. try
  446. {
  447. if (a_x.type().kind().value() == TCKind._tk_null)
  448. ((AnyHolder) holder).value = a_x;
  449. else
  450. {
  451. OutputStream buf = a_x.create_output_stream();
  452. buf.write_any(a_x);
  453. holder._read(buf.create_input_stream());
  454. buf.close();
  455. }
  456. valueChanged();
  457. }
  458. catch (ClassCastException cex)
  459. {
  460. TypeMismatch t = new TypeMismatch();
  461. t.initCause(cex);
  462. throw t;
  463. }
  464. catch (MARSHAL m)
  465. {
  466. InvalidValue v = new InvalidValue();
  467. v.initCause(m);
  468. throw v;
  469. }
  470. catch (IOException ex)
  471. {
  472. throw new Unexpected(ex);
  473. }
  474. }
  475. /** {@inheritDoc} */
  476. public void insert_boolean(boolean a_x) throws InvalidValue, TypeMismatch
  477. {
  478. try
  479. {
  480. ((BooleanHolder) holder).value = a_x;
  481. valueChanged();
  482. }
  483. catch (ClassCastException cex)
  484. {
  485. TypeMismatch t = new TypeMismatch();
  486. t.initCause(cex);
  487. throw t;
  488. }
  489. }
  490. /** {@inheritDoc} */
  491. public void insert_char(char a_x) throws InvalidValue, TypeMismatch
  492. {
  493. try
  494. {
  495. ((CharHolder) holder).value = a_x;
  496. valueChanged();
  497. }
  498. catch (ClassCastException cex)
  499. {
  500. TypeMismatch t = new TypeMismatch();
  501. t.initCause(cex);
  502. throw t;
  503. }
  504. }
  505. /** {@inheritDoc} */
  506. public void insert_double(double a_x) throws InvalidValue, TypeMismatch
  507. {
  508. try
  509. {
  510. ((DoubleHolder) holder).value = a_x;
  511. valueChanged();
  512. }
  513. catch (ClassCastException cex)
  514. {
  515. TypeMismatch t = new TypeMismatch();
  516. t.initCause(cex);
  517. throw t;
  518. }
  519. }
  520. /** {@inheritDoc} */
  521. public void insert_float(float a_x) throws InvalidValue, TypeMismatch
  522. {
  523. try
  524. {
  525. ((FloatHolder) holder).value = a_x;
  526. valueChanged();
  527. }
  528. catch (ClassCastException cex)
  529. {
  530. TypeMismatch t = new TypeMismatch();
  531. t.initCause(cex);
  532. throw t;
  533. }
  534. }
  535. /** {@inheritDoc} */
  536. public void insert_long(int a_x) throws InvalidValue, TypeMismatch
  537. {
  538. try
  539. {
  540. ((IntHolder) holder).value = a_x;
  541. valueChanged();
  542. }
  543. catch (ClassCastException cex)
  544. {
  545. TypeMismatch t = new TypeMismatch();
  546. t.initCause(cex);
  547. throw t;
  548. }
  549. }
  550. /** {@inheritDoc} */
  551. public void insert_longlong(long a_x) throws InvalidValue, TypeMismatch
  552. {
  553. try
  554. {
  555. ((LongHolder) holder).value = a_x;
  556. valueChanged();
  557. }
  558. catch (ClassCastException cex)
  559. {
  560. TypeMismatch t = new TypeMismatch();
  561. t.initCause(cex);
  562. throw t;
  563. }
  564. }
  565. /** {@inheritDoc} */
  566. public void insert_octet(byte a_x) throws InvalidValue, TypeMismatch
  567. {
  568. try
  569. {
  570. ((OctetHolder) holder).value = a_x;
  571. valueChanged();
  572. }
  573. catch (ClassCastException cex)
  574. {
  575. TypeMismatch t = new TypeMismatch();
  576. t.initCause(cex);
  577. throw t;
  578. }
  579. }
  580. /** {@inheritDoc} */
  581. public void insert_reference(Object a_x) throws InvalidValue, TypeMismatch
  582. {
  583. try
  584. {
  585. ((ObjectHolder) holder).value = a_x;
  586. valueChanged();
  587. }
  588. catch (ClassCastException cex)
  589. {
  590. TypeMismatch t = new TypeMismatch();
  591. t.initCause(cex);
  592. throw t;
  593. }
  594. }
  595. /** {@inheritDoc} */
  596. public void insert_short(short a_x) throws InvalidValue, TypeMismatch
  597. {
  598. try
  599. {
  600. ((ShortHolder) holder).value = a_x;
  601. valueChanged();
  602. }
  603. catch (ClassCastException cex)
  604. {
  605. TypeMismatch t = new TypeMismatch();
  606. t.initCause(cex);
  607. throw t;
  608. }
  609. }
  610. /** {@inheritDoc} */
  611. public void insert_string(String a_x) throws InvalidValue, TypeMismatch
  612. {
  613. try
  614. {
  615. if (a_x != null &&
  616. final_type.length() > 0 &&
  617. a_x.length() > final_type.length()
  618. )
  619. throw new InvalidValue(a_x.length() + " exceeds bound, " +
  620. final_type.length()
  621. );
  622. ((StringHolder) holder).value = a_x;
  623. valueChanged();
  624. }
  625. catch (ClassCastException cex)
  626. {
  627. TypeMismatch t = new TypeMismatch();
  628. t.initCause(cex);
  629. throw t;
  630. }
  631. catch (BadKind e)
  632. {
  633. TypeMismatch t = new TypeMismatch();
  634. t.initCause(e);
  635. throw t;
  636. }
  637. }
  638. /** {@inheritDoc} */
  639. public void insert_typecode(TypeCode a_x) throws InvalidValue, TypeMismatch
  640. {
  641. try
  642. {
  643. ((TypeCodeHolder) holder).value = a_x;
  644. valueChanged();
  645. }
  646. catch (ClassCastException cex)
  647. {
  648. TypeMismatch t = new TypeMismatch();
  649. t.initCause(cex);
  650. throw t;
  651. }
  652. }
  653. /** {@inheritDoc} */
  654. public void insert_ulong(int a_x) throws InvalidValue, TypeMismatch
  655. {
  656. try
  657. {
  658. ((IntHolder) holder).value = a_x;
  659. valueChanged();
  660. }
  661. catch (ClassCastException cex)
  662. {
  663. TypeMismatch t = new TypeMismatch();
  664. t.initCause(cex);
  665. throw t;
  666. }
  667. }
  668. /** {@inheritDoc} */
  669. public void insert_ulonglong(long a_x) throws InvalidValue, TypeMismatch
  670. {
  671. try
  672. {
  673. ((LongHolder) holder).value = a_x;
  674. valueChanged();
  675. }
  676. catch (ClassCastException cex)
  677. {
  678. TypeMismatch t = new TypeMismatch();
  679. t.initCause(cex);
  680. throw t;
  681. }
  682. }
  683. /** {@inheritDoc} */
  684. public void insert_ushort(short a_x) throws InvalidValue, TypeMismatch
  685. {
  686. try
  687. {
  688. ((ShortHolder) holder).value = a_x;
  689. valueChanged();
  690. }
  691. catch (ClassCastException cex)
  692. {
  693. TypeMismatch t = new TypeMismatch();
  694. t.initCause(cex);
  695. throw t;
  696. }
  697. }
  698. /** {@inheritDoc} */
  699. public void insert_val(Serializable a_x) throws InvalidValue, TypeMismatch
  700. {
  701. try
  702. {
  703. ((ValueBaseHolder) holder).value = a_x;
  704. valueChanged();
  705. }
  706. catch (ClassCastException cex)
  707. {
  708. TypeMismatch t = new TypeMismatch();
  709. t.initCause(cex);
  710. throw t;
  711. }
  712. }
  713. /** {@inheritDoc} */
  714. public void insert_wchar(char a_x) throws InvalidValue, TypeMismatch
  715. {
  716. try
  717. {
  718. ((WCharHolder) holder).value = a_x;
  719. valueChanged();
  720. }
  721. catch (ClassCastException cex)
  722. {
  723. TypeMismatch t = new TypeMismatch();
  724. t.initCause(cex);
  725. throw t;
  726. }
  727. }
  728. /** {@inheritDoc} */
  729. public void insert_wstring(String a_x) throws InvalidValue, TypeMismatch
  730. {
  731. try
  732. {
  733. if (a_x != null &&
  734. final_type.length() > 0 &&
  735. a_x.length() > type().length()
  736. )
  737. throw new InvalidValue(a_x.length() + " exceeds bound, " +
  738. final_type.length()
  739. );
  740. ((WStringHolder) holder).value = a_x;
  741. valueChanged();
  742. }
  743. catch (ClassCastException cex)
  744. {
  745. TypeMismatch t = new TypeMismatch();
  746. t.initCause(cex);
  747. throw t;
  748. }
  749. catch (BadKind e)
  750. {
  751. TypeMismatch t = new TypeMismatch();
  752. t.initCause(e);
  753. throw t;
  754. }
  755. }
  756. /**
  757. * The objects, enclosed inside this class, have only one component (self).
  758. *
  759. * @return false, always (no other action).
  760. */
  761. public boolean next()
  762. {
  763. return false;
  764. }
  765. /**
  766. * Returns without action.
  767. */
  768. public void rewind()
  769. {
  770. }
  771. /**
  772. * This objects, stored in this wrapper, never have multiple internal
  773. * components to seek.
  774. *
  775. * @return false, always (no other action).
  776. */
  777. public boolean seek(int p)
  778. {
  779. return false;
  780. }
  781. /**
  782. * Returns the enclosed {@link Any}.
  783. *
  784. * @return the enclosed {@link Any}.
  785. */
  786. public Any to_any()
  787. {
  788. Any a = createAny();
  789. a.insert_Streamable(holder);
  790. a.type(official_type);
  791. return a;
  792. }
  793. /** {@inheritDoc} */
  794. public TypeCode type()
  795. {
  796. return official_type;
  797. }
  798. /**
  799. * Compute hashcode in a trivial way.
  800. */
  801. protected int getHashCodeSimple(int maximum)
  802. {
  803. int h = super.hashCode() / 2;
  804. if (h < 0)
  805. h = -h;
  806. return h % maximum;
  807. }
  808. /**
  809. * Inserts Any, contained in the parameter, into Any, contained in this
  810. * DynAny.
  811. */
  812. public void insert_dyn_any(DynAny d) throws TypeMismatch, InvalidValue
  813. {
  814. check(d.type().kind());
  815. Any a = d.to_any();
  816. holder = a.extract_Streamable();
  817. valueChanged();
  818. }
  819. /**
  820. * Checks for equality. The DynAnys are equal if the stored Anys are equal.
  821. */
  822. public boolean equal(DynAny other)
  823. {
  824. if (other instanceof AbstractAny)
  825. {
  826. if (other instanceof gnuDynAny)
  827. {
  828. gnuDynAny x = (gnuDynAny) other;
  829. if (!x.holder.getClass().equals(holder.getClass()))
  830. return false;
  831. BufferedCdrOutput b1 = new BufferedCdrOutput();
  832. x.holder._write(b1);
  833. BufferedCdrOutput b2 = new BufferedCdrOutput(b1.buffer.size() + 10);
  834. holder._write(b2);
  835. return Arrays.equals(b1.buffer.toByteArray(),
  836. b2.buffer.toByteArray()
  837. );
  838. }
  839. else
  840. return false;
  841. }
  842. if (other == null)
  843. return false;
  844. else if (other.component_count() != component_count() ||
  845. !official_type.equal(other.type())
  846. )
  847. return false;
  848. else
  849. return other.to_any().equal(to_any());
  850. }
  851. /**
  852. * This final_type has no components.
  853. *
  854. * @return 0, always.
  855. */
  856. public int component_count()
  857. {
  858. return 0;
  859. }
  860. public DynAny get_dyn_any() throws TypeMismatch, InvalidValue
  861. {
  862. return new gnuDynAny(holder, official_type, final_type, factory, orb);
  863. }
  864. private void check(TCKind t) throws TypeMismatch
  865. {
  866. if (t.value() != final_type.kind().value())
  867. throw new TypeMismatch(t.value() + "!=" + final_type.kind().value());
  868. }
  869. }