summaryrefslogtreecommitdiff
path: root/cpp/src/Slice/JavaUtil.h
blob: 776c8169321225b7588a7d02d66e8a01fadc5f7f (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
// **********************************************************************
//
// Copyright (c) 2003-2016 ZeroC, Inc. All rights reserved.
//
// This copy of Ice is licensed to you under the terms described in the
// ICE_LICENSE file included in this distribution.
//
// **********************************************************************

#ifndef JAVA_UTIL_H
#define JAVA_UTIL_H

#include <Slice/Parser.h>
#include <IceUtil/OutputUtil.h>

namespace Slice
{

//
// Compute Java serialVersionUID for a Slice class
//
long
computeSerialVersionUUID(const ClassDefPtr&);

//
// Compute Java serialVersionUID for a Slice class
//
long
computeSerialVersionUUID(const ExceptionPtr&);

//
// Compute Java serialVersionUID for a Slice struct
//
long
computeSerialVersionUUID(const StructPtr&);

class JavaOutput : public ::IceUtilInternal::Output
{
public:

    JavaOutput();
    JavaOutput(std::ostream&);
    JavaOutput(const char*);

    //
    // Open a file to hold the source for a Java class. The first
    // argument is the class name (including an optional leading
    // package). Intermediate directories will be created as
    // necessary to open the file in the package. The second
    // argument specifies a directory prefix in which to locate
    // the class.
    //
    // After successfully opening the file, the function invokes
    // printHeader() and then emits a "package" statement if
    // necessary.
    //
    void openClass(const std::string&, const std::string&, const std::string& = std::string());

    virtual void printHeader();
};

class JavaCompatGenerator : private ::IceUtil::noncopyable
{
public:

    virtual ~JavaCompatGenerator();

    //
    // Validate all metadata in the unit with a "java:" prefix.
    //
    static void validateMetaData(const UnitPtr&);

    void close();

protected:

    JavaCompatGenerator(const std::string&);

    //
    // Given the fully-scoped Java class name, create any intermediate
    // package directories and open the class file,
    //
    void open(const std::string&, const std::string&);

    ::IceUtilInternal::Output& output() const;

    //
    // Check a symbol against any of the Java keywords. If a
    // match is found, return the symbol with a leading underscore.
    //
    std::string fixKwd(const std::string&) const;

    //
    // Convert a Slice scoped name into a Java name.
    //
    std::string convertScopedName(const std::string&,
                                  const std::string& = std::string(),
                                  const std::string& = std::string()) const;


    //
    // Returns the package prefix for a give Slice file.
    //
    std::string getPackagePrefix(const ContainedPtr&) const;

    //
    // Returns the Java package of a Contained entity.
    //
    std::string getPackage(const ContainedPtr&) const;

    //
    // Returns the Java name for a Contained entity. If the optional
    // package argument matches the entity's package name, then the
    // package is removed from the result.
    //
    std::string getAbsolute(const ContainedPtr&,
                            const std::string& = std::string(),
                            const std::string& = std::string(),
                            const std::string& = std::string()) const;

    //
    // Return the method call necessary to obtain the static type ID for an object type.
    //
    std::string getStaticId(const TypePtr&, const std::string&) const;

    //
    // Determines whether an operation should use the optional mapping.
    //
    bool useOptionalMapping(const OperationPtr&);

    //
    // Returns the optional type corresponding to the given Slice type.
    //
    std::string getOptionalFormat(const TypePtr&);

    //
    // Get the Java name for a type. If an optional scope is provided,
    // the scope will be removed from the result if possible.
    //
    enum TypeMode
    {
        TypeModeIn,
        TypeModeOut,
        TypeModeMember,
        TypeModeReturn
    };
    std::string typeToString(const TypePtr&, TypeMode, const std::string& = std::string(),
                             const StringList& = StringList(), bool = true, bool = false) const;

    //
    // Get the Java object name for a type. For primitive types, this returns the
    // Java class type (e.g., Integer). For all other types, this function delegates
    // to typeToString.
    //
    std::string typeToObjectString(const TypePtr&, TypeMode, const std::string& = std::string(),
                                   const StringList& = StringList(), bool = true) const;

    //
    // Generate code to marshal or unmarshal a type.
    //
    enum OptionalMode
    {
        OptionalNone,
        OptionalInParam,
        OptionalOutParam,
        OptionalReturnParam,
        OptionalMember
    };

    void writeMarshalUnmarshalCode(::IceUtilInternal::Output&, const std::string&, const TypePtr&, OptionalMode,
                                   bool, int, const std::string&, bool, int&, bool = false, const std::string& = "",
                                   const StringList& = StringList(), const std::string& = "");

    //
    // Generate code to marshal or unmarshal a dictionary type.
    //
    void writeDictionaryMarshalUnmarshalCode(::IceUtilInternal::Output&, const std::string&, const DictionaryPtr&,
                                             const std::string&, bool, int&, bool,
                                             const std::string& = "", const StringList& = StringList());

    //
    // Generate code to marshal or unmarshal a sequence type.
    //
    void writeSequenceMarshalUnmarshalCode(::IceUtilInternal::Output&, const std::string&, const SequencePtr&,
                                           const std::string&, bool, int&, bool,
                                           const std::string& = "", const StringList& = StringList());

    //
    // Search metadata for an entry with the given prefix and return the entire string.
    //
    static bool findMetaData(const std::string&, const StringList&, std::string&);

    //
    // Get custom type metadata. If metadata is found, the abstract and
    // concrete types are extracted and the function returns true. If an
    // abstract type is not specified, it is set to an empty string.
    //
    static bool getTypeMetaData(const StringList&, std::string&, std::string&);

    //
    // Determine whether a custom type is defined. The function checks the
    // metadata of the type's original definition, as well as any optional
    // metadata that typically represents a data member or parameter.
    //
    static bool hasTypeMetaData(const TypePtr&, const StringList& = StringList());

    //
    // Obtain the concrete and abstract types for a dictionary or sequence type.
    // The functions return true if a custom type was defined and false to indicate
    // the default mapping was used.
    //
    bool getDictionaryTypes(const DictionaryPtr&, const std::string&, const StringList&,
                            std::string&, std::string&) const;
    bool getSequenceTypes(const SequencePtr&, const std::string&, const StringList&, std::string&, std::string&) const;

    bool sequenceHasHolder(const SequencePtr&) const;

    virtual JavaOutput* createOutput();

    static const std::string _getSetMetaData;

private:

    std::string _dir;
    ::IceUtilInternal::Output* _out;
    mutable std::map<std::string, std::string> _filePackagePrefix;
};

class JavaGenerator : private ::IceUtil::noncopyable
{
public:

    virtual ~JavaGenerator();

    //
    // Validate all metadata in the unit with a "java:" prefix.
    //
    static void validateMetaData(const UnitPtr&);

    void close();

protected:

    JavaGenerator(const std::string&);

    //
    // Given the fully-scoped Java class name, create any intermediate
    // package directories and open the class file,
    //
    void open(const std::string&, const std::string&);

    ::IceUtilInternal::Output& output() const;

    //
    // Check a symbol against any of the Java keywords. If a
    // match is found, return the symbol with a leading underscore.
    //
    std::string fixKwd(const std::string&) const;

    //
    // Convert a Slice scoped name into a Java name.
    //
    std::string convertScopedName(const std::string&,
                                  const std::string& = std::string(),
                                  const std::string& = std::string()) const;


    //
    // Returns the package prefix for a give Slice file.
    //
    std::string getPackagePrefix(const ContainedPtr&) const;

    //
    // Returns the Java package of a Contained entity.
    //
    std::string getPackage(const ContainedPtr&) const;

    //
    // Returns the Java name for a Contained entity. If the optional
    // package argument matches the entity's package name, then the
    // package is removed from the result.
    //
    std::string getAbsolute(const ContainedPtr&,
                            const std::string& = std::string(),
                            const std::string& = std::string(),
                            const std::string& = std::string()) const;

    //
    // Return the method call necessary to obtain the static type ID for an object type.
    //
    std::string getStaticId(const TypePtr&, const std::string&) const;

    //
    // Determines whether an operation should use the optional mapping.
    //
    bool useOptionalMapping(const OperationPtr&);

    //
    // Returns the optional type corresponding to the given Slice type.
    //
    std::string getOptionalFormat(const TypePtr&);

    //
    // Get the Java name for a type. If an optional scope is provided,
    // the scope will be removed from the result if possible.
    //
    enum TypeMode
    {
        TypeModeIn,
        TypeModeOut,
        TypeModeMember,
        TypeModeReturn
    };
    std::string typeToString(const TypePtr&, TypeMode, const std::string& = std::string(),
                             const StringList& = StringList(), bool = true, bool = false, bool = false) const;

    //
    // Get the Java object name for a type. For primitive types, this returns the
    // Java class type (e.g., Integer). For all other types, this function delegates
    // to typeToString.
    //
    std::string typeToObjectString(const TypePtr&, TypeMode, const std::string& = std::string(),
                                   const StringList& = StringList(), bool = true, bool = false) const;

    //
    // Generate code to marshal or unmarshal a type.
    //
    enum OptionalMode
    {
        OptionalNone,
        OptionalInParam,
        OptionalOutParam,
        OptionalReturnParam,
        OptionalMember
    };

    std::string getWriteFunction(const std::string&, const TypePtr&);
    std::string getReadFunction(const std::string&, const TypePtr&);

    void writeMarshalUnmarshalCode(::IceUtilInternal::Output&, const std::string&, const TypePtr&, OptionalMode,
                                   bool, int, const std::string&, bool, int&, const std::string& = "",
                                   const StringList& = StringList(), const std::string& = "");

    //
    // Generate code to marshal or unmarshal a dictionary type.
    //
    void writeDictionaryMarshalUnmarshalCode(::IceUtilInternal::Output&, const std::string&, const DictionaryPtr&,
                                             const std::string&, bool, int&, bool,
                                             const std::string& = "", const StringList& = StringList());

    //
    // Generate code to marshal or unmarshal a sequence type.
    //
    void writeSequenceMarshalUnmarshalCode(::IceUtilInternal::Output&, const std::string&, const SequencePtr&,
                                           const std::string&, bool, int&, bool,
                                           const std::string& = "", const StringList& = StringList());

    //
    // Search metadata for an entry with the given prefix and return the entire string.
    //
    static bool findMetaData(const std::string&, const StringList&, std::string&);

    //
    // Get custom type metadata. If metadata is found, the abstract and
    // concrete types are extracted and the function returns true. If an
    // abstract type is not specified, it is set to an empty string.
    //
    static bool getTypeMetaData(const StringList&, std::string&, std::string&);

    //
    // Determine whether a custom type is defined. The function checks the
    // metadata of the type's original definition, as well as any optional
    // metadata that typically represents a data member or parameter.
    //
    static bool hasTypeMetaData(const TypePtr&, const StringList& = StringList());

    //
    // Obtain the concrete and abstract types for a dictionary or sequence type.
    // The functions return true if a custom type was defined and false to indicate
    // the default mapping was used.
    //
    bool getDictionaryTypes(const DictionaryPtr&, const std::string&, const StringList&,
                            std::string&, std::string&, bool) const;
    bool getSequenceTypes(const SequencePtr&, const std::string&, const StringList&, std::string&, std::string&,
                          bool) const;

    virtual JavaOutput* createOutput();

    static const std::string _getSetMetaData;

private:

    std::string _dir;
    ::IceUtilInternal::Output* _out;
    mutable std::map<std::string, std::string> _filePackagePrefix;
};

}

#endif