1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_codemaker.hxx"
26 
27 #include "sal/config.h"
28 
29 #include "codemaker/commonjava.hxx"
30 
31 #include "codemaker/options.hxx"
32 #include "codemaker/typemanager.hxx"
33 #include "codemaker/unotype.hxx"
34 
35 #include "osl/diagnose.h"
36 #include "registry/reader.hxx"
37 #include "registry/types.h"
38 #include "rtl/strbuf.h"
39 #include "rtl/string.h"
40 #include "rtl/string.hxx"
41 #include "rtl/ustring.hxx"
42 #include "sal/types.h"
43 
44 #include <vector>
45 
46 namespace codemaker { namespace java {
47 
translateUnoToJavaType(codemaker::UnoType::Sort sort,RTTypeClass typeClass,rtl::OString const & nucleus,bool referenceType)48 rtl::OString translateUnoToJavaType(
49     codemaker::UnoType::Sort sort, RTTypeClass typeClass,
50     rtl::OString const & nucleus, bool referenceType)
51 {
52     rtl::OStringBuffer buf;
53     if (sort == codemaker::UnoType::SORT_COMPLEX) {
54         if (typeClass == RT_TYPE_INTERFACE
55             && nucleus == rtl::OString("com/sun/star/uno/XInterface"))
56         {
57             buf.append(RTL_CONSTASCII_STRINGPARAM("java/lang/Object"));
58         } else {
59             //TODO: check that nucleus is a valid (Java-modified UTF-8)
60             // identifier
61             buf.append(nucleus);
62         }
63     } else {
64         rtl::OString const javaTypes[codemaker::UnoType::SORT_ANY + 1][2] = {
65             { rtl::OString(RTL_CONSTASCII_STRINGPARAM("void")),
66               rtl::OString(RTL_CONSTASCII_STRINGPARAM("java/lang/Void")) },
67             { rtl::OString(RTL_CONSTASCII_STRINGPARAM("boolean")),
68               rtl::OString(RTL_CONSTASCII_STRINGPARAM("java/lang/Boolean")) },
69             { rtl::OString(RTL_CONSTASCII_STRINGPARAM("byte")),
70               rtl::OString(RTL_CONSTASCII_STRINGPARAM("java/lang/Byte")) },
71             { rtl::OString(RTL_CONSTASCII_STRINGPARAM("short")),
72               rtl::OString(RTL_CONSTASCII_STRINGPARAM("java/lang/Short")) },
73             { rtl::OString(RTL_CONSTASCII_STRINGPARAM("short")),
74               rtl::OString(RTL_CONSTASCII_STRINGPARAM("java/lang/Short")) },
75             { rtl::OString(RTL_CONSTASCII_STRINGPARAM("int")),
76               rtl::OString(RTL_CONSTASCII_STRINGPARAM("java/lang/Integer")) },
77             { rtl::OString(RTL_CONSTASCII_STRINGPARAM("int")),
78               rtl::OString(RTL_CONSTASCII_STRINGPARAM("java/lang/Integer")) },
79             { rtl::OString(RTL_CONSTASCII_STRINGPARAM("long")),
80               rtl::OString(RTL_CONSTASCII_STRINGPARAM("java/lang/Long")) },
81             { rtl::OString(RTL_CONSTASCII_STRINGPARAM("long")),
82               rtl::OString(RTL_CONSTASCII_STRINGPARAM("java/lang/Long")) },
83             { rtl::OString(RTL_CONSTASCII_STRINGPARAM("float")),
84               rtl::OString(RTL_CONSTASCII_STRINGPARAM("java/lang/Float")) },
85             { rtl::OString(RTL_CONSTASCII_STRINGPARAM("double")),
86               rtl::OString(RTL_CONSTASCII_STRINGPARAM("java/lang/Double")) },
87             { rtl::OString(RTL_CONSTASCII_STRINGPARAM("char")),
88               rtl::OString(RTL_CONSTASCII_STRINGPARAM("java/lang/Character")) },
89             { rtl::OString(RTL_CONSTASCII_STRINGPARAM("java/lang/String")),
90               rtl::OString(RTL_CONSTASCII_STRINGPARAM("java/lang/String")) },
91             { rtl::OString(RTL_CONSTASCII_STRINGPARAM("com/sun/star/uno/Type")),
92               rtl::OString(RTL_CONSTASCII_STRINGPARAM("com/sun/star/uno/Type")) },
93             { rtl::OString(RTL_CONSTASCII_STRINGPARAM("java/lang/Object")),
94               rtl::OString(RTL_CONSTASCII_STRINGPARAM("java/lang/Object")) } };
95         buf.append(javaTypes[sort][referenceType]);
96     }
97     return buf.makeStringAndClear();
98 }
99 
translateUnoToJavaIdentifier(rtl::OString const & identifier,rtl::OString const & prefix)100 rtl::OString translateUnoToJavaIdentifier(
101     rtl::OString const & identifier, rtl::OString const & prefix)
102 {
103     if (identifier == "abstract"
104         || identifier == "assert" // since Java 1.4
105         || identifier == "boolean"
106         || identifier == "break"
107         || identifier == "byte"
108         || identifier == "case"
109         || identifier == "catch"
110         || identifier == "char"
111         || identifier == "class"
112         || identifier == "const"
113         || identifier == "continue"
114         || identifier == "default"
115         || identifier == "do"
116         || identifier == "double"
117         || identifier == "else"
118         || identifier == "enum" // probable addition in Java 1.5
119         || identifier == "extends"
120         || identifier == "final"
121         || identifier == "finally"
122         || identifier == "float"
123         || identifier == "for"
124         || identifier == "goto"
125         || identifier == "if"
126         || identifier == "implements"
127         || identifier == "import"
128         || identifier == "instanceof"
129         || identifier == "int"
130         || identifier == "interface"
131         || identifier == "long"
132         || identifier == "native"
133         || identifier == "new"
134         || identifier == "package"
135         || identifier == "private"
136         || identifier == "protected"
137         || identifier == "public"
138         || identifier == "return"
139         || identifier == "short"
140         || identifier == "static"
141         || identifier == "strictfp"
142         || identifier == "super"
143         || identifier == "switch"
144         || identifier == "synchronized"
145         || identifier == "this"
146         || identifier == "throw"
147         || identifier == "throws"
148         || identifier == "transient"
149         || identifier == "try"
150         || identifier == "void"
151         || identifier == "volatile"
152         || identifier == "while")
153     {
154         rtl::OStringBuffer buf(prefix);
155         buf.append('_');
156         buf.append(identifier);
157         return buf.makeStringAndClear();
158     } else {
159         return identifier;
160     }
161 }
162 
163 } }
164