01: package com.bm.utils;
02:
03: import java.lang.annotation.Annotation;
04: import java.lang.reflect.Field;
05:
06: import javax.persistence.EmbeddedId;
07: import javax.persistence.Entity;
08: import javax.persistence.Id;
09: import javax.persistence.MappedSuperclass;
10:
11: /**
12: * This class will find out a access type (FIELD, METHOD).
13: *
14: * @author Daniel Wiese
15: *
16: */
17: public final class AccessTypeFinder {
18:
19: private static final org.apache.log4j.Logger log = org.apache.log4j.Logger
20: .getLogger(AccessTypeFinder.class);
21:
22: /**
23: * Constructor.
24: */
25: private AccessTypeFinder() {
26: // intentionally left blank
27: }
28:
29: /**
30: * Finds the accesstype.
31: *
32: * @param toFind -
33: * the access type to find
34: * @return the accesstype
35: */
36: public static AccessType findAccessType(Class toFind) {
37: AccessType back = null;
38:
39: Field[] fields = toFind.getDeclaredFields();
40: for (Field aktField : fields) {
41: Annotation[] fieldAnnotations = aktField.getAnnotations();
42:
43: // look into the annotations
44: for (Annotation a : fieldAnnotations) {
45: if (a instanceof Id) {
46: back = AccessType.FIELD;
47: break;
48: } else if (a instanceof EmbeddedId) {
49: back = AccessType.FIELD;
50: break;
51: }
52: }
53: }
54:
55: if (back == null) {
56: // Not found: try super class (if entity inheritance is used).
57: // Specification 2.1.1: "A single access type applies to an entity hierarchy"
58: Class super Class = toFind.getSuperclass();
59: if (super Class != null
60: && (super Class.getAnnotation(Entity.class) != null || super Class
61: .getAnnotation(MappedSuperclass.class) != null)) {
62: return findAccessType(super Class);
63: }
64: }
65:
66: if (back == null) {
67: // default access type
68: back = AccessType.METHOD;
69: }
70:
71: log.debug("Accesstype: " + back);
72: return back;
73: }
74:
75: }
|