| 1 |
/*- |
| 2 |
* See the file LICENSE for redistribution information. |
| 3 |
* |
| 4 |
* Copyright (c) 2002, 2013 Oracle and/or its affiliates. All rights reserved. |
| 5 |
* |
| 6 |
*/ |
| 7 |
|
| 8 |
package com.sleepycat.persist.model; |
| 9 |
|
| 10 |
import java.lang.reflect.Field; |
| 11 |
import java.lang.reflect.Modifier; |
| 12 |
import java.lang.reflect.ParameterizedType; |
| 13 |
import java.lang.reflect.Type; |
| 14 |
import java.util.ArrayList; |
| 15 |
import java.util.Collection; |
| 16 |
import java.util.Collections; |
| 17 |
import java.util.HashMap; |
| 18 |
import java.util.HashSet; |
| 19 |
import java.util.List; |
| 20 |
import java.util.Map; |
| 21 |
import java.util.Set; |
| 22 |
|
| 23 |
import com.sleepycat.compat.DbCompat; |
| 24 |
|
| 25 |
/** |
| 26 |
* The default annotation-based entity model. An <code>AnnotationModel</code> |
| 27 |
* is based on annotations that are specified for entity classes and their key |
| 28 |
* fields. |
| 29 |
* |
| 30 |
* <p>{@code AnnotationModel} objects are thread-safe. Multiple threads may |
| 31 |
* safely call the methods of a shared {@code AnnotationModel} object.</p> |
| 32 |
* |
| 33 |
* <p>The set of persistent classes in the annotation model is the set of all |
| 34 |
* classes with the {@link Persistent} or {@link Entity} annotation.</p> |
| 35 |
* |
| 36 |
* <p>The annotations used to define persistent classes are: {@link Entity}, |
| 37 |
* {@link Persistent}, {@link PrimaryKey}, {@link SecondaryKey} and {@link |
| 38 |
* KeyField}. A good starting point is {@link Entity}.</p> |
| 39 |
* |
| 40 |
* @author Mark Hayes |
| 41 |
*/ |
| 42 |
public class AnnotationModel extends EntityModel { |
| 43 |
|
| 44 |
private static class EntityInfo { |
| 45 |
PrimaryKeyMetadata priKey; |
| 46 |
Map<String, SecondaryKeyMetadata> secKeys = |
| 47 |
new HashMap<String, SecondaryKeyMetadata>(); |
| 48 |
} |
| 49 |
|
| 50 |
private Map<String, ClassMetadata> classMap; |
| 51 |
private Map<String, EntityInfo> entityMap; |
| 52 |
|
| 53 |
/* |
| 54 |
* This set records the special classes, i.e., enum and array type. |
| 55 |
* [#19377] |
| 56 |
*/ |
| 57 |
private Set<String> registeredSpecialClasses; |
| 58 |
|
| 59 |
/** |
| 60 |
* Constructs a model for annotated entity classes. |
| 61 |
*/ |
| 62 |
public AnnotationModel() { |
| 63 |
super(); |
| 64 |
classMap = new HashMap<String, ClassMetadata>(); |
| 65 |
entityMap = new HashMap<String, EntityInfo>(); |
| 66 |
registeredSpecialClasses = new HashSet<String>(); |
| 67 |
} |
| 68 |
|
| 69 |
/* EntityModel methods */ |
| 70 |
|
| 71 |
@Override |
| 72 |
public synchronized Set<String> getKnownClasses() { |
| 73 |
return Collections.unmodifiableSet |
| 74 |
(new HashSet<String>(classMap.keySet())); |
| 75 |
} |
| 76 |
|
| 77 |
@Override |
| 78 |
public Set<String> getKnownSpecialClasses() { |
| 79 |
return Collections.unmodifiableSet(registeredSpecialClasses); |
| 80 |
} |
| 81 |
|
| 82 |
@Override |
| 83 |
public synchronized EntityMetadata getEntityMetadata(String className) { |
| 84 |
/* Call getClassMetadata to collect metadata. */ |
| 85 |
getClassMetadata(className); |
| 86 |
/* Return the collected entity metadata. */ |
| 87 |
EntityInfo info = entityMap.get(className); |
| 88 |
if (info != null) { |
| 89 |
return new EntityMetadata |
| 90 |
(className, info.priKey, |
| 91 |
Collections.unmodifiableMap(info.secKeys)); |
| 92 |
} else { |
| 93 |
return null; |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
@Override |
| 98 |
public synchronized ClassMetadata getClassMetadata(String className) { |
| 99 |
ClassMetadata metadata = classMap.get(className); |
| 100 |
if (metadata == null) { |
| 101 |
Class<?> type; |
| 102 |
try { |
| 103 |
type = resolveClass(className); |
| 104 |
} catch (ClassNotFoundException e) { |
| 105 |
return null; |
| 106 |
} |
| 107 |
|
| 108 |
/* |
| 109 |
* Adds enum or array types to registeredSpecialClasses set, and |
| 110 |
* does not create metadata for them. [#19377] |
| 111 |
*/ |
| 112 |
if (type.isEnum() || |
| 113 |
type.isArray()) { |
| 114 |
registeredSpecialClasses.add(className); |
| 115 |
} |
| 116 |
|
| 117 |
/* Get class annotation. */ |
| 118 |
Entity entity = type.getAnnotation(Entity.class); |
| 119 |
Persistent persistent = type.getAnnotation(Persistent.class); |
| 120 |
if (entity == null && persistent == null) { |
| 121 |
return null; |
| 122 |
} |
| 123 |
if (type.isEnum() || |
| 124 |
type.isInterface() || |
| 125 |
type.isPrimitive()) { |
| 126 |
throw new IllegalArgumentException |
| 127 |
("@Entity and @Persistent not allowed for enum, " + |
| 128 |
"interface, or primitive type: " + type.getName()); |
| 129 |
} |
| 130 |
if (entity != null && persistent != null) { |
| 131 |
throw new IllegalArgumentException |
| 132 |
("Both @Entity and @Persistent are not allowed: " + |
| 133 |
type.getName()); |
| 134 |
} |
| 135 |
boolean isEntity; |
| 136 |
int version; |
| 137 |
String proxiedClassName; |
| 138 |
if (entity != null) { |
| 139 |
isEntity = true; |
| 140 |
version = entity.version(); |
| 141 |
proxiedClassName = null; |
| 142 |
} else { |
| 143 |
isEntity = false; |
| 144 |
version = persistent.version(); |
| 145 |
Class proxiedClass = persistent.proxyFor(); |
| 146 |
proxiedClassName = (proxiedClass != void.class) ? |
| 147 |
proxiedClass.getName() : null; |
| 148 |
} |
| 149 |
/* Get instance fields. */ |
| 150 |
List<Field> fields = new ArrayList<Field>(); |
| 151 |
boolean nonDefaultRules = getInstanceFields(fields, type); |
| 152 |
Collection<FieldMetadata> nonDefaultFields = null; |
| 153 |
if (nonDefaultRules) { |
| 154 |
nonDefaultFields = new ArrayList<FieldMetadata>(fields.size()); |
| 155 |
for (Field field : fields) { |
| 156 |
nonDefaultFields.add(new FieldMetadata |
| 157 |
(field.getName(), field.getType().getName(), |
| 158 |
type.getName())); |
| 159 |
} |
| 160 |
nonDefaultFields = |
| 161 |
Collections.unmodifiableCollection(nonDefaultFields); |
| 162 |
} |
| 163 |
/* Get the rest of the metadata and save it. */ |
| 164 |
metadata = new ClassMetadata |
| 165 |
(className, version, proxiedClassName, isEntity, |
| 166 |
getPrimaryKey(type, fields), |
| 167 |
getSecondaryKeys(type, fields), |
| 168 |
getCompositeKeyFields(type, fields), |
| 169 |
nonDefaultFields); |
| 170 |
classMap.put(className, metadata); |
| 171 |
/* Add any new information about entities. */ |
| 172 |
updateEntityInfo(metadata); |
| 173 |
} |
| 174 |
return metadata; |
| 175 |
} |
| 176 |
|
| 177 |
/** |
| 178 |
* Fills in the fields array and returns true if the default rules for |
| 179 |
* field persistence were overridden. |
| 180 |
*/ |
| 181 |
private boolean getInstanceFields(List<Field> fields, Class<?> type) { |
| 182 |
boolean nonDefaultRules = false; |
| 183 |
for (Field field : type.getDeclaredFields()) { |
| 184 |
boolean notPersistent = |
| 185 |
(field.getAnnotation(NotPersistent.class) != null); |
| 186 |
boolean notTransient = |
| 187 |
(field.getAnnotation(NotTransient.class) != null); |
| 188 |
if (notPersistent && notTransient) { |
| 189 |
throw new IllegalArgumentException |
| 190 |
("Both @NotTransient and @NotPersistent not allowed"); |
| 191 |
} |
| 192 |
if (notPersistent || notTransient) { |
| 193 |
nonDefaultRules = true; |
| 194 |
} |
| 195 |
int mods = field.getModifiers(); |
| 196 |
|
| 197 |
if (!Modifier.isStatic(mods) && |
| 198 |
!notPersistent && |
| 199 |
(!Modifier.isTransient(mods) || notTransient)) { |
| 200 |
/* Field is DPL persistent. */ |
| 201 |
fields.add(field); |
| 202 |
} else { |
| 203 |
/* If non-persistent, no other annotations should be used. */ |
| 204 |
if (field.getAnnotation(PrimaryKey.class) != null || |
| 205 |
field.getAnnotation(SecondaryKey.class) != null || |
| 206 |
field.getAnnotation(KeyField.class) != null) { |
| 207 |
throw new IllegalArgumentException |
| 208 |
("@PrimaryKey, @SecondaryKey and @KeyField not " + |
| 209 |
"allowed on non-persistent field"); |
| 210 |
} |
| 211 |
} |
| 212 |
} |
| 213 |
return nonDefaultRules; |
| 214 |
} |
| 215 |
|
| 216 |
private PrimaryKeyMetadata getPrimaryKey(Class<?> type, |
| 217 |
List<Field> fields) { |
| 218 |
Field foundField = null; |
| 219 |
String sequence = null; |
| 220 |
for (Field field : fields) { |
| 221 |
PrimaryKey priKey = field.getAnnotation(PrimaryKey.class); |
| 222 |
if (priKey != null) { |
| 223 |
if (foundField != null) { |
| 224 |
throw new IllegalArgumentException |
| 225 |
("Only one @PrimaryKey allowed: " + type.getName()); |
| 226 |
} else { |
| 227 |
foundField = field; |
| 228 |
sequence = priKey.sequence(); |
| 229 |
if (sequence.length() == 0) { |
| 230 |
sequence = null; |
| 231 |
} |
| 232 |
} |
| 233 |
} |
| 234 |
} |
| 235 |
if (foundField != null) { |
| 236 |
return new PrimaryKeyMetadata |
| 237 |
(foundField.getName(), foundField.getType().getName(), |
| 238 |
type.getName(), sequence); |
| 239 |
} else { |
| 240 |
return null; |
| 241 |
} |
| 242 |
} |
| 243 |
|
| 244 |
private Map<String, SecondaryKeyMetadata> |
| 245 |
getSecondaryKeys(Class<?> type, List<Field> fields) { |
| 246 |
|
| 247 |
Map<String, SecondaryKeyMetadata> map = null; |
| 248 |
for (Field field : fields) { |
| 249 |
SecondaryKey secKey = field.getAnnotation(SecondaryKey.class); |
| 250 |
if (secKey != null) { |
| 251 |
Relationship rel = secKey.relate(); |
| 252 |
String elemClassName = null; |
| 253 |
if (rel == Relationship.ONE_TO_MANY || |
| 254 |
rel == Relationship.MANY_TO_MANY) { |
| 255 |
elemClassName = getElementClass(field); |
| 256 |
} |
| 257 |
String keyName = secKey.name(); |
| 258 |
if (keyName.length() == 0) { |
| 259 |
keyName = field.getName(); |
| 260 |
} |
| 261 |
Class<?> relatedClass = secKey.relatedEntity(); |
| 262 |
String relatedEntity = (relatedClass != void.class) ? |
| 263 |
relatedClass.getName() : null; |
| 264 |
DeleteAction deleteAction = (relatedEntity != null) ? |
| 265 |
secKey.onRelatedEntityDelete() : null; |
| 266 |
SecondaryKeyMetadata metadata = new SecondaryKeyMetadata |
| 267 |
(field.getName(), field.getType().getName(), |
| 268 |
type.getName(), elemClassName, keyName, rel, |
| 269 |
relatedEntity, deleteAction); |
| 270 |
if (map == null) { |
| 271 |
map = new HashMap<String, SecondaryKeyMetadata>(); |
| 272 |
} |
| 273 |
if (map.put(keyName, metadata) != null) { |
| 274 |
throw new IllegalArgumentException |
| 275 |
("Only one @SecondaryKey with the same name allowed: " |
| 276 |
+ type.getName() + '.' + keyName); |
| 277 |
} |
| 278 |
} |
| 279 |
} |
| 280 |
if (map != null) { |
| 281 |
map = Collections.unmodifiableMap(map); |
| 282 |
} |
| 283 |
return map; |
| 284 |
} |
| 285 |
|
| 286 |
private String getElementClass(Field field) { |
| 287 |
Class cls = field.getType(); |
| 288 |
if (cls.isArray()) { |
| 289 |
return cls.getComponentType().getName(); |
| 290 |
} |
| 291 |
if (Collection.class.isAssignableFrom(cls)) { |
| 292 |
Type[] typeArgs = null; |
| 293 |
if (field.getGenericType() instanceof ParameterizedType) { |
| 294 |
typeArgs = ((ParameterizedType) field.getGenericType()). |
| 295 |
getActualTypeArguments(); |
| 296 |
} |
| 297 |
if (typeArgs == null || |
| 298 |
typeArgs.length != 1 || |
| 299 |
!(typeArgs[0] instanceof Class)) { |
| 300 |
throw new IllegalArgumentException |
| 301 |
("Collection typed secondary key field must have a" + |
| 302 |
" single generic type argument and a wildcard or" + |
| 303 |
" type bound is not allowed: " + |
| 304 |
field.getDeclaringClass().getName() + '.' + |
| 305 |
field.getName()); |
| 306 |
} |
| 307 |
return ((Class) typeArgs[0]).getName(); |
| 308 |
} |
| 309 |
throw new IllegalArgumentException |
| 310 |
("ONE_TO_MANY or MANY_TO_MANY secondary key field must have" + |
| 311 |
" an array or Collection type: " + |
| 312 |
field.getDeclaringClass().getName() + '.' + field.getName()); |
| 313 |
} |
| 314 |
|
| 315 |
private List<FieldMetadata> getCompositeKeyFields(Class<?> type, |
| 316 |
List<Field> fields) { |
| 317 |
List<FieldMetadata> list = null; |
| 318 |
for (Field field : fields) { |
| 319 |
KeyField keyField = field.getAnnotation(KeyField.class); |
| 320 |
if (keyField != null) { |
| 321 |
int value = keyField.value(); |
| 322 |
if (value < 1 || value > fields.size()) { |
| 323 |
throw new IllegalArgumentException |
| 324 |
("Unreasonable @KeyField index value " + value + |
| 325 |
": " + type.getName()); |
| 326 |
} |
| 327 |
if (list == null) { |
| 328 |
list = new ArrayList<FieldMetadata>(fields.size()); |
| 329 |
} |
| 330 |
if (value <= list.size() && list.get(value - 1) != null) { |
| 331 |
throw new IllegalArgumentException |
| 332 |
("@KeyField index value " + value + |
| 333 |
" is used more than once: " + type.getName()); |
| 334 |
} |
| 335 |
while (value > list.size()) { |
| 336 |
list.add(null); |
| 337 |
} |
| 338 |
FieldMetadata metadata = new FieldMetadata |
| 339 |
(field.getName(), field.getType().getName(), |
| 340 |
type.getName()); |
| 341 |
list.set(value - 1, metadata); |
| 342 |
} |
| 343 |
} |
| 344 |
if (list != null) { |
| 345 |
if (list.size() < fields.size()) { |
| 346 |
throw new IllegalArgumentException |
| 347 |
("@KeyField is missing on one or more instance fields: " + |
| 348 |
type.getName()); |
| 349 |
} |
| 350 |
for (int i = 0; i < list.size(); i += 1) { |
| 351 |
if (list.get(i) == null) { |
| 352 |
throw new IllegalArgumentException |
| 353 |
("@KeyField is missing for index value " + (i + 1) + |
| 354 |
": " + type.getName()); |
| 355 |
} |
| 356 |
} |
| 357 |
} |
| 358 |
if (list != null) { |
| 359 |
list = Collections.unmodifiableList(list); |
| 360 |
} |
| 361 |
return list; |
| 362 |
} |
| 363 |
|
| 364 |
/** |
| 365 |
* Add newly discovered metadata to our stash of entity info. This info |
| 366 |
* is maintained as it is discovered because it would be expensive to |
| 367 |
* create it on demand -- all class metadata would have to be traversed. |
| 368 |
*/ |
| 369 |
private void updateEntityInfo(ClassMetadata metadata) { |
| 370 |
|
| 371 |
/* |
| 372 |
* Find out whether this class or its superclass is an entity. In the |
| 373 |
* process, traverse all superclasses to load their metadata -- this |
| 374 |
* will populate as much entity info as possible. |
| 375 |
*/ |
| 376 |
String entityClass = null; |
| 377 |
PrimaryKeyMetadata priKey = null; |
| 378 |
Map<String, SecondaryKeyMetadata> secKeys = |
| 379 |
new HashMap<String, SecondaryKeyMetadata>(); |
| 380 |
for (ClassMetadata data = metadata; data != null;) { |
| 381 |
if (data.isEntityClass()) { |
| 382 |
if (entityClass != null) { |
| 383 |
throw new IllegalArgumentException |
| 384 |
("An entity class may not be derived from another" + |
| 385 |
" entity class: " + entityClass + |
| 386 |
' ' + data.getClassName()); |
| 387 |
} |
| 388 |
entityClass = data.getClassName(); |
| 389 |
} |
| 390 |
/* Save first primary key encountered. */ |
| 391 |
if (priKey == null) { |
| 392 |
priKey = data.getPrimaryKey(); |
| 393 |
} |
| 394 |
/* Save all secondary keys encountered by key name. */ |
| 395 |
Map<String, SecondaryKeyMetadata> classSecKeys = |
| 396 |
data.getSecondaryKeys(); |
| 397 |
if (classSecKeys != null) { |
| 398 |
for (SecondaryKeyMetadata secKey : classSecKeys.values()) { |
| 399 |
secKeys.put(secKey.getKeyName(), secKey); |
| 400 |
} |
| 401 |
} |
| 402 |
/* Load superclass metadata. */ |
| 403 |
Class cls; |
| 404 |
try { |
| 405 |
cls = resolveClass(data.getClassName()); |
| 406 |
} catch (ClassNotFoundException e) { |
| 407 |
throw DbCompat.unexpectedException(e); |
| 408 |
} |
| 409 |
cls = cls.getSuperclass(); |
| 410 |
if (cls != Object.class) { |
| 411 |
data = getClassMetadata(cls.getName()); |
| 412 |
if (data == null) { |
| 413 |
throw new IllegalArgumentException |
| 414 |
("Persistent class has non-persistent superclass: " + |
| 415 |
cls.getName()); |
| 416 |
} |
| 417 |
} else { |
| 418 |
data = null; |
| 419 |
} |
| 420 |
} |
| 421 |
|
| 422 |
/* Add primary and secondary key entity info. */ |
| 423 |
if (entityClass != null) { |
| 424 |
EntityInfo info = entityMap.get(entityClass); |
| 425 |
if (info == null) { |
| 426 |
info = new EntityInfo(); |
| 427 |
entityMap.put(entityClass, info); |
| 428 |
} |
| 429 |
if (priKey == null) { |
| 430 |
throw new IllegalArgumentException |
| 431 |
("Entity class has no primary key: " + entityClass); |
| 432 |
} |
| 433 |
info.priKey = priKey; |
| 434 |
info.secKeys.putAll(secKeys); |
| 435 |
} |
| 436 |
} |
| 437 |
} |