summaryrefslogtreecommitdiff
path: root/ridljar/com/sun/star/uno/Type.java
blob: 9e368500d15a70df28e091d61806468f525302b9 (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
/*************************************************************************
 *
 *  $RCSfile: Type.java,v $
 *
 *  $Revision: 1.9 $
 *
 *  last change: $Author: hr $ $Date: 2003-03-26 15:44:55 $
 *
 *  The Contents of this file are made available subject to the terms of
 *  either of the following licenses
 *
 *         - GNU Lesser General Public License Version 2.1
 *         - Sun Industry Standards Source License Version 1.1
 *
 *  Sun Microsystems Inc., October, 2000
 *
 *  GNU Lesser General Public License Version 2.1
 *  =============================================
 *  Copyright 2000 by Sun Microsystems, Inc.
 *  901 San Antonio Road, Palo Alto, CA 94303, USA
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License version 2.1, as published by the Free Software Foundation.
 *
 *  This library 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
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston,
 *  MA  02111-1307  USA
 *
 *
 *  Sun Industry Standards Source License Version 1.1
 *  =================================================
 *  The contents of this file are subject to the Sun Industry Standards
 *  Source License Version 1.1 (the "License"); You may not use this file
 *  except in compliance with the License. You may obtain a copy of the
 *  License at http://www.openoffice.org/license.html.
 *
 *  Software provided under this License is provided on an "AS IS" basis,
 *  WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
 *  WITHOUT LIMITATION, WARRANTIES THAT THE SOFTWARE IS FREE OF DEFECTS,
 *  MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE, OR NON-INFRINGING.
 *  See the License for the specific provisions governing your rights and
 *  obligations concerning the Software.
 *
 *  The Initial Developer of the Original Code is: Sun Microsystems, Inc.
 *
 *  Copyright: 2000 by Sun Microsystems, Inc.
 *
 *  All Rights Reserved.
 *
 *  Contributor(s): _______________________________________
 *
 *
 ************************************************************************/

package com.sun.star.uno;

import java.util.HashMap;

/**
 * Represents the UNO built-in type <code>TYPE</code>.
 *
 * <p>The UNO type is not directly mapped to <code>java.lang.Class</code> for at
 * least two reasons.  For one, some UNO types (like <code>UNSIGNED
 * SHORT</code>) do not have a matching Java class.  For another, it can be
 * necessary to describe a type which is unknown to the Java runtime system
 * (for example, for delaying the need of a class, so that it is possible to
 * generate it on the fly.)</p>
 *
 * <p>A <code>Type</code> is uniquely determined by its type class (a
 * <code>TypeClass</code>) and its type name (a <code>String</code>); these two
 * will never be <code>null</code>.  A <code>Type</code> may have an additional
 * "z class" (a <code>java.lang.Class</code>), giving a Java class type that
 * corresponds to the UNO type.  Also, a <code>Type</code> can cache a type
 * description (a <code>com.sun.star.uno.ITypeDescription</code>), which can be
 * computed and set by <code>TypeDescription.getTypeDescription</code>.
 *
 * @since UDK1.0
 */
public class Type {
    // The following private static members and static initializer must come
    // first in the class definition, so that the class can be initialized
    // sucessfully:

    // must be sorted same as TypeClass:
    private static final String[] __typeClassToTypeName = new String[] {
        "void",
        "char",
        "boolean",
        "byte",
        "short",
        "unsigned short",
        "long",
        "unsigned long",
        "hyper",
        "unsigned hyper",
        "float",
        "double",
        "string",
        "type",
        "any"
    };

    private static final HashMap __javaClassToTypeName = new HashMap();

    private static final HashMap __typeNameToTypeClass = new HashMap();

    static {
        for (int i = 0; i < __typeClassToTypeName.length; ++i) {
            __typeNameToTypeClass.put(__typeClassToTypeName[i],
                                      TypeClass.fromInt(i));
        }
        __javaClassToTypeName.put(Void.class, "void");
        __javaClassToTypeName.put(void.class, "void");
        __javaClassToTypeName.put(Character.class, "char");
        __javaClassToTypeName.put(char.class, "char");
        __javaClassToTypeName.put(Boolean.class, "boolean");
        __javaClassToTypeName.put(boolean.class, "boolean");
        __javaClassToTypeName.put(Byte.class, "byte");
        __javaClassToTypeName.put(byte.class, "byte");
        __javaClassToTypeName.put(Short.class, "short");
        __javaClassToTypeName.put(short.class, "short");
        __javaClassToTypeName.put(Integer.class, "long");
        __javaClassToTypeName.put(int.class, "long");
        __javaClassToTypeName.put(Long.class, "hyper");
        __javaClassToTypeName.put(long.class, "hyper");
        __javaClassToTypeName.put(Float.class, "float");
        __javaClassToTypeName.put(float.class, "float");
        __javaClassToTypeName.put(Double.class, "double");
        __javaClassToTypeName.put(double.class, "double");
        __javaClassToTypeName.put(String.class, "string");
        __javaClassToTypeName.put(Type.class, "type");
        __javaClassToTypeName.put(Any.class, "any");
        __javaClassToTypeName.put(Object.class, "any");
    }

    public static final Type VOID = new Type(void.class);
    public static final Type CHAR = new Type(char.class);
    public static final Type BOOLEAN = new Type(boolean.class);
    public static final Type BYTE = new Type(byte.class);
    public static final Type SHORT = new Type(short.class);
    public static final Type UNSIGNED_SHORT = new Type(
        "unsigned short", TypeClass.UNSIGNED_SHORT);
    public static final Type LONG = new Type(int.class);
    public static final Type UNSIGNED_LONG = new Type(
        "unsigned long", TypeClass.UNSIGNED_LONG);
    public static final Type HYPER = new Type(long.class);
    public static final Type UNSIGNED_HYPER = new Type(
        "unsigned hyper", TypeClass.UNSIGNED_HYPER);
    public static final Type FLOAT = new Type(float.class);
    public static final Type DOUBLE = new Type(double.class);
    public static final Type STRING = new Type(String.class);
    public static final Type TYPE = new Type(Type.class);
    public static final Type ANY = new Type(Any.class);

    /**
     * Constructs a new <code>Type</code> which defaults to <code>VOID</code>.
     *
     * @since UDK3.0
     */
    public Type() {
        this(void.class);
    }

    /**
     * Constructs a new <code>Type</code> with the given type class and type
     * name.
     *
     * @param typeName the type name.  Must not be <code>null</code>.
     * @param typeClass the type class.  Must not be <code>null</code>, and must
     *     match the <code>typeName</code> (for example, it is illegal to
     *     combine a <code>typeName</code> of <code>"void"</code> with a
     *     <code>typeClass</code> of <code>BOOLEAN</code>).
     */
    public Type(String typeName, TypeClass typeClass) {
        _typeClass = typeClass;
        _typeName = typeName;
    }

    /**
     * Constructs a new <code>Type</code> from the given
     * <code>java.lang.Class</code>.
     *
     * @param zClass the Java class of this type.  Must not be
     *     <code>null</code>.
     *
     * @since UDK3.0
     */
    public Type(Class zClass) {
        _class = zClass;

        _typeName = (String)__javaClassToTypeName.get(zClass);
        if(_typeName != null)
            _typeClass = (TypeClass)__typeNameToTypeClass.get(_typeName);

        else {
            if (XInterface.class.isAssignableFrom(zClass))
            {
                _typeClass = TypeClass.INTERFACE;
                if (zClass.isInterface())
                    _typeName = zClass.getName();
                else
                    _typeName = XInterface.class.getName();
            }
            else if(zClass.isArray()) {
                _typeClass = TypeClass.SEQUENCE;
                _typeName = "[]" + (new Type(_class.getComponentType()).getTypeName());
            }
            else if(Enum.class.isAssignableFrom(zClass)) {
                _typeClass = TypeClass.ENUM;
                _typeName  = zClass.getName();
            }
            else if(Throwable.class.isAssignableFrom(zClass)) {
                _typeClass = TypeClass.EXCEPTION;
                _typeName  = zClass.getName();
            }
            else if(Union.class.isAssignableFrom(zClass)) {
                _typeClass = TypeClass.UNION;
                _typeName  = zClass.getName();
            }
            else {
                _typeClass = TypeClass.STRUCT;
                _typeName  = zClass.getName();
            }
        }
    }

    /**
     * Constructs a new <code>Type</code> from the given type description.
     *
     * @param typeDescription a type description.  Must not be
     *     <code>null</code>.
     *
     * @since UDK3.0
     */
    public Type(ITypeDescription typeDescription) {
        _typeName         = typeDescription.getTypeName();
        _typeClass        = typeDescription.getTypeClass();
        _iTypeDescription = typeDescription;
    }

    /**
     * Constructs a new <code>Type</code> with the given type name.
     *
     * <p>TODO:  This constructor is dangerous, as it can create a
     * <code>Type</code> with an <code>UNKNOWN</code> type class.  It would be
     * better if this constructor threw a <code>IllegalArgumentException</code>
     * instead.</p>
     *
     * @param typeName the name of this type; must not be <code>null</code>.
     *     For simple types (<code>VOID</code>, <code>BOOLEAN</code>,
     *     <code>CHAR</code>, <code>BYTE</code>, <code>SHORT</code>,
     *     <code>UNSIGNED SHORT</code>, <code>LONG</code>, <code>UNSIGNED
     *     LONG</code>, <code>HYPER</code>, <code>UNSIGNED HYPER</code>,
     *     <code>FLOAT</code>, <code>DOUBLE</code>, <code>STRING</code>,
     *     <code>TYPE</code>, <code>ANY</code>), the type class is calculated;
     *     for other types, the type class is set to <code>UNKNOWN</code>.
     *
     * @since UDK3.0
     */
    public Type(String typeName) {
        _typeClass = (TypeClass)__typeNameToTypeClass.get(typeName);

        if(_typeClass == null)
            _typeClass = TypeClass.UNKNOWN;

        _typeName = typeName;
    }

    /**
     * Constructs a new <code>Type</code> with the given type class.
     *
     * @param typeClass the type class of this type; must not be
     *     <code>null</code>.  Only type classes for simple types are allowed
     *     here.
     *
     * @throws IllegalArgumentException if the given <code>typeClass</code> is
     *     not simple (for example, a struct or an interface type).  This
     *     constructor could not find out the type name in such a case.
     *
     * @since UDK3.0
     */
    public Type(TypeClass typeClass) {
        if(__isTypeClassPrimitive(typeClass)) {
            _typeClass = typeClass;
            _typeName = __typeClassToTypeName[typeClass.getValue()];
        }
        else
            throw new IllegalArgumentException(typeClass + " is not primitive");
    }

    /**
     * Gets the type class.
     *
     * @return the type class.  Will never be <code>null</code>, but might be
     *     <code>UNKNOWN</code>.
     *
     * @since UDK1.0
     */
    public TypeClass getTypeClass() {
        return _typeClass;
    }

    /**
     * Gets the type name.
     *
     * @return the type name; will never be <code>null</code>
     *
     * @since UDK1.0
     */
    public String getTypeName() {
        return _typeName;
    }

    /**
     * Gets the Java class.
     *
     * <p>Returns <code>null</code> if this type has not been constructed by
     * <code>Class</code>.</p>
     *
     * @return the type name; may be <code>null</code>
     *
     * @since UDK1.0
     */
    public Class getZClass() {
        return _class;
    }

    /**
     * Gives the type description of this type.
     *
     * @return the type description; may be <code>null</code>
     *
     * @since UDK3.0
     */
    public ITypeDescription getTypeDescription() {
        return _iTypeDescription;
    }

    /**
     * Sets the type description for this type.
     *
     * @param typeDescription the type description
     *
     * @since UDK3.0
     */
    public void setTypeDescription(ITypeDescription typeDescription) {
        _iTypeDescription = typeDescription;
    }

    // @see java.lang.Object#equals
    public boolean equals(Object obj) {
        return this == obj
            || (obj instanceof Type
                && _typeName.equals(((Type) obj)._typeName));
    }

    // @see java.lang.Object#hashCode
    public int hashCode() {
        return _typeName.hashCode();
    }

    // @see java.lang.Object#toString
    public String toString() {
        return "Type[" + _typeName + "]";
    }

    private static boolean __isTypeClassPrimitive(TypeClass typeClass) {
        return typeClass.getValue() < __typeClassToTypeName.length;
    }

    protected TypeClass _typeClass; // TODO should be final
    protected String _typeName; // TODO should be final
    protected Class _class; // TODO should be final

    protected ITypeDescription _iTypeDescription;
}