2 Copyright (C) 2005 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
39 package gnu
.CORBA
.DynAn
;
41 import gnu
.CORBA
.Minor
;
42 import gnu
.CORBA
.Unexpected
;
44 import org
.omg
.CORBA
.Any
;
45 import org
.omg
.CORBA
.BAD_PARAM
;
46 import org
.omg
.CORBA
.MARSHAL
;
47 import org
.omg
.CORBA
.ORB
;
48 import org
.omg
.CORBA
.TCKind
;
49 import org
.omg
.CORBA
.TypeCode
;
50 import org
.omg
.CORBA
.VM_TRUNCATABLE
;
51 import org
.omg
.CORBA
.portable
.OutputStream
;
52 import org
.omg
.CORBA
.portable
.ValueFactory
;
53 import org
.omg
.DynamicAny
.DynAny
;
54 import org
.omg
.DynamicAny
.DynAnyPackage
.InvalidValue
;
55 import org
.omg
.DynamicAny
.DynAnyPackage
.TypeMismatch
;
56 import org
.omg
.DynamicAny
.DynStruct
;
57 import org
.omg
.DynamicAny
.DynValue
;
58 import org
.omg
.DynamicAny
.DynValueCommon
;
59 import org
.omg
.DynamicAny
.DynValueOperations
;
60 import org
.omg
.DynamicAny
.NameDynAnyPair
;
61 import org
.omg
.DynamicAny
.NameValuePair
;
63 import java
.io
.Serializable
;
66 * Implementation of DynValue.
68 * @author Audrius Meskauskas, Lithuania (AudriusA@Bioinformatics.org)
70 public class gnuDynValue
extends RecordAny
implements DynValue
,
74 * Use serialVersionUID for interoperability.
76 private static final long serialVersionUID
= 1;
79 * If true, the value of this ValueType is set to null.
86 public gnuDynValue(TypeCode oType
, TypeCode aType
,
87 gnuDynAnyFactory aFactory
, ORB anOrb
90 super(oType
, aType
, aFactory
, anOrb
);
92 // Initialise fields. The array of fields also includes all inherited
96 array
= new DynAny
[ final_type
.member_count() ];
97 fNames
= new String
[ array
.length
];
98 for (int i
= 0; i
< array
.length
; i
++)
101 factory
.create_dyn_any_from_type_code(final_type
.member_type(i
));
102 fNames
[ i
] = final_type
.member_name(i
);
105 // Search of inherited members.
106 if (final_type
.type_modifier() == VM_TRUNCATABLE
.value
)
108 TypeCode parent
= final_type
.concrete_base_type();
109 DynAny ancestor
= factory
.create_dyn_any_from_type_code(parent
);
111 if (ancestor
instanceof DynValue
)
113 // Add members of ancestor in front of the curren members.
114 DynValue anc
= (DynValue
) ancestor
;
117 NameDynAnyPair
[] aar
= anc
.get_members_as_dyn_any();
120 else if (ancestor
instanceof DynStruct
)
122 // Add members of ancestor in front of the curren members.
123 DynStruct anc
= (DynStruct
) ancestor
;
124 NameDynAnyPair
[] aar
= anc
.get_members_as_dyn_any();
128 throw new BAD_PARAM("The parent of " + final_type
.id() + ", " +
129 parent
.id() + ", is not structure nor value."
135 throw new Unexpected(e
);
142 * Inherit the provided fields.
144 private void inheritFields(NameDynAnyPair
[] aar
)
146 DynAny
[] nArray
= new DynAny
[ array
.length
+ aar
.length
];
147 String
[] nNames
= new String
[ array
.length
+ aar
.length
];
149 for (int i
= 0; i
< aar
.length
; i
++)
151 nArray
[ p
] = aar
[ i
].value
;
152 nNames
[ p
] = aar
[ i
].id
;
156 for (int i
= 0; i
< array
.length
; i
++)
158 nArray
[ p
] = array
[ i
];
159 nNames
[ p
] = fNames
[ i
];
168 public TCKind
current_member_kind() throws TypeMismatch
, InvalidValue
171 throw new TypeMismatch(ISNULL
);
173 return super.current_member_kind();
178 public String
current_member_name() throws TypeMismatch
, InvalidValue
181 throw new TypeMismatch(ISNULL
);
183 return super.current_member_name();
188 public NameDynAnyPair
[] get_members_as_dyn_any() throws InvalidValue
191 throw new InvalidValue(ISNULL
);
192 return super.gnu_get_members_as_dyn_any();
197 public NameValuePair
[] get_members() throws InvalidValue
200 throw new InvalidValue(ISNULL
);
202 return super.gnu_get_members();
207 public void set_members_as_dyn_any(NameDynAnyPair
[] value
)
208 throws TypeMismatch
, InvalidValue
210 super.set_members_as_dyn_any(value
);
216 public void set_members(NameValuePair
[] value
)
217 throws TypeMismatch
, InvalidValue
219 super.set_members(value
);
225 public boolean is_null()
231 public void set_to_null()
238 public void set_to_value()
245 * Create a new instance.
247 protected RecordAny
newInstance(TypeCode oType
, TypeCode aType
,
248 gnuDynAnyFactory aFactory
, ORB anOrb
251 gnuDynValue v
= new gnuDynValue(oType
, aType
, aFactory
, anOrb
);
260 * Compare for equality, minding null values.
262 public boolean equal(DynAny other
)
264 if (other
instanceof DynValueOperations
)
266 DynValueCommon o
= (DynValueCommon
) other
;
268 return o
.is_null() && o
.type().equal(official_type
);
270 return !o
.is_null() && super.equal(other
);
277 * Get the focused component, throwing exception if the current value is null.
279 protected DynAny
focused() throws InvalidValue
, TypeMismatch
282 throw new TypeMismatch(ISNULL
);
284 return super.focused();
294 Any a0
= createAny();
295 a0
.type(orb
.get_primitive_tc(TCKind
.tk_null
));
302 ValueFactory factory
=
303 ((org
.omg
.CORBA_2_3
.ORB
) orb
).lookup_value_factory(official_type
.id());
306 MARSHAL m
= new MARSHAL("Factory for " + official_type
.id() +
308 m
.minor
= Minor
.Factory
;
312 OutputStream out
= orb
.create_output_stream();
314 for (int i
= 0; i
< array
.length
; i
++)
315 array
[ i
].to_any().write_value(out
);
317 org
.omg
.CORBA_2_3
.portable
.InputStream in
=
318 (org
.omg
.CORBA_2_3
.portable
.InputStream
) out
.create_input_stream();
319 Serializable v
= factory
.read_value(in
);
322 g
.type(official_type
);
323 g
.insert_Value(v
, official_type
);
329 throw new Unexpected(e
);
335 public void assign(DynAny from
) throws TypeMismatch
337 checkType(official_type
, from
.type());
339 if (from
instanceof DynValue
)
341 DynValue other
= (DynValue
) from
;
349 DynValueOperations src
= (DynValueOperations
) from
;
350 set_members_as_dyn_any(src
.get_members_as_dyn_any());
352 catch (InvalidValue e
)
354 TypeMismatch t
= new TypeMismatch("Invalid value");
361 throw new TypeMismatch("Not a DynValue");
365 * Get the number of components.
367 public int component_count()
369 return isNull ?
0 : super.component_count();
373 public Serializable
get_val() throws TypeMismatch
, InvalidValue
375 return to_any().extract_Value();
379 public void insert_val(Serializable a_x
) throws InvalidValue
, TypeMismatch