1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 /***
18 * TypeSupport.java
19 *
20 */
21
22 package org.apache.jdo.impl.model.jdo.util;
23
24 import java.util.Map;
25 import java.util.Set;
26 import java.util.HashMap;
27 import java.util.HashSet;
28
29 import org.apache.jdo.model.java.JavaModel;
30 import org.apache.jdo.model.java.JavaType;
31 import org.apache.jdo.model.jdo.JDOModel;
32
33 /***
34 *
35 */
36 public class TypeSupport
37 {
38 /*** */
39 private final static Set primitiveTypeNames = new HashSet();
40
41 /*** */
42 private final static Map singleFieldObjectIdClassNames = new HashMap();
43
44 static
45 {
46
47 primitiveTypeNames.add("byte");
48 primitiveTypeNames.add("short");
49 primitiveTypeNames.add("int");
50 primitiveTypeNames.add("long");
51 primitiveTypeNames.add("char");
52 primitiveTypeNames.add("float");
53 primitiveTypeNames.add("double");
54 primitiveTypeNames.add("boolean");
55
56
57 singleFieldObjectIdClassNames.put(
58 "byte",
59 "javax.jdo.identity.ByteIdentity");
60 singleFieldObjectIdClassNames.put(
61 "java.lang.Byte",
62 "javax.jdo.identity.ByteIdentity");
63 singleFieldObjectIdClassNames.put(
64 "char",
65 "javax.jdo.identity.CharIdentity");
66 singleFieldObjectIdClassNames.put(
67 "java.lang.Char",
68 "javax.jdo.identity.CharIdentity");
69 singleFieldObjectIdClassNames.put(
70 "int",
71 "javax.jdo.identity.IntIdentity");
72 singleFieldObjectIdClassNames.put(
73 "java.lang.Integer",
74 "javax.jdo.identity.IntIdentity");
75 singleFieldObjectIdClassNames.put(
76 "long",
77 "javax.jdo.identity.LongIdentity");
78 singleFieldObjectIdClassNames.put(
79 "java.lang.Long",
80 "javax.jdo.identity.LongIdentity");
81 singleFieldObjectIdClassNames.put(
82 "short",
83 "javax.jdo.identity.ShortIdentity");
84 singleFieldObjectIdClassNames.put(
85 "java.lang.Short",
86 "javax.jdo.identity.ShortIdentity");
87 singleFieldObjectIdClassNames.put(
88 "java.lang.String",
89 "javax.jdo.identity.StringIdentity");
90 }
91
92 /*** Fully qualified class name of the ObjectIdentity class. */
93 private static final String OBJECT_IDENTITY_NAME =
94 "javax.jdo.identity.ObjectIdentity";
95
96 /***
97 * Returns <code>true</code> if the persistence-modifier of a field
98 * having the specified type defaults to <code>true</code>.
99 * @param type the type to be checked
100 * @return <code>true</code> if type is a value type;
101 * <code>false</code> otherwise
102 */
103 public static boolean isPersistenceFieldType(JavaType type)
104 {
105 return type.isValue() ||
106 type.isJDOSupportedCollection() ||
107 type.isJDOSupportedMap() ||
108 type.isPersistenceCapable() ||
109 isPersistenceArrayType(type);
110 }
111
112 /***
113 * Returns <code>true</code> if the embedded-element property of a field
114 * having the specified type defaults to <code>true</code>.
115 * @param type the type to be checked
116 * @return <code>true</code> if type is a embedded-element type;
117 * <code>false</code> otherwise
118 */
119 public static boolean isEmbeddedElementType(JavaType type)
120 {
121 return !type.isPersistenceCapable() && !type.isInterface();
122 }
123
124 /***
125 * Returns <code>true</code> if the embedded property of a field having
126 * the specified type defaults to <code>true</code>.
127 * @param type the type to be checked
128 * @return <code>true</code> if type is a embedded type;
129 * <code>false</code> otherwise
130 */
131 public static boolean isEmbeddedFieldType(JavaType type)
132 {
133 return type.isValue() ||
134 isPersistenceArrayType(type);
135 }
136
137 /***
138 * Returns a JavaType representation for the specified type name.
139 * The method delegates the request to the JavaModel attached to the
140 * specified JDOModel. An unqualified name is qualified using first the
141 * specified packagePrefix and then "java.lang.", but only if the type
142 * name is the the name of a primitive type. If the method still does
143 * not find a valid type, then it returns <code>null</code>.
144 * @param jdoModel the owning JDOModel
145 * @param typeName the name of the type to be checked
146 * @param packagePrefix the package prefix used to qualify the type name
147 * @return the JavaType representation of the specified type name or
148 * <code>null</code> if it cannot be resolved.
149 */
150 public static JavaType resolveType(JDOModel jdoModel, String typeName,
151 String packagePrefix)
152 {
153 JavaType type = null;
154 JavaModel javaModel = jdoModel.getJavaModel();
155 if (primitiveTypeNames.contains(typeName) ||
156 (typeName.indexOf('.') != -1) ||
157 (packagePrefix == null) || (packagePrefix.length() == 0)) {
158
159
160
161 type = javaModel.getJavaType(typeName);
162 }
163 else {
164
165
166 type = javaModel.getJavaType(packagePrefix + typeName);
167 if (type == null) {
168
169
170 type = javaModel.getJavaType("java.lang." + typeName);
171 }
172 }
173 return type;
174 }
175
176 /***
177 * Returns <code>true</code> if the specified type represents an array
178 * and its element type is a value type.
179 * @param type the JavaType to be checked
180 * @return <code>true</code> if type is a value array;
181 * <code>false</code> otherwise.
182 */
183 public static boolean isValueArrayType(JavaType type)
184 {
185 if (type.isArray()) {
186 JavaType elementType = type.getArrayComponentType();
187 return elementType.isValue();
188 }
189 return false;
190 }
191
192 /***
193 * Returns the name of a single field ObjectId class, if the specified
194 * type name denotes a type that is suitable for single field identity.
195 * @param typeName the type to be checked
196 */
197 public static String getSingleFieldObjectIdClassName(String typeName) {
198 if (typeName == null)
199 return null;
200 String singleFieldObjectIdClassName =
201 (String) singleFieldObjectIdClassNames.get(typeName);
202 if (singleFieldObjectIdClassName == null)
203 singleFieldObjectIdClassName = OBJECT_IDENTITY_NAME;
204 return singleFieldObjectIdClassName;
205 }
206
207
208
209 /***
210 * Returns <code>true</code> if the specified type represents an array
211 * and its element type is a persistence capable class.
212 * @param type the JavaType to be checked
213 * @return <code>true</code> if type is a persistent array;
214 * <code>false</code> otherwise.
215 */
216 private static boolean isPersistenceArrayType(JavaType type)
217 {
218 if (type.isArray()) {
219 JavaType elementType = type.getArrayComponentType();
220 return elementType.isValue() ||
221 elementType.isPersistenceCapable();
222 }
223 return false;
224 }
225
226 }