|
1 | 1 | package com.avaje.ebeaninternal.server.deploy.parse; |
2 | 2 |
|
3 | | -import java.lang.annotation.Annotation; |
4 | | -import java.sql.Types; |
5 | | -import java.util.LinkedHashMap; |
6 | | -import java.util.List; |
7 | | -import java.util.Map; |
| 3 | +import com.avaje.ebeaninternal.server.core.BootupClasses; |
| 4 | +import com.avaje.ebeaninternal.server.deploy.InheritInfo; |
| 5 | +import com.avaje.ebeaninternal.server.deploy.meta.DeployBeanDescriptor; |
8 | 6 |
|
9 | 7 | import javax.persistence.DiscriminatorColumn; |
10 | 8 | import javax.persistence.DiscriminatorType; |
11 | 9 | import javax.persistence.DiscriminatorValue; |
12 | 10 | import javax.persistence.Inheritance; |
13 | | - |
14 | | -import com.avaje.ebeaninternal.server.core.BootupClasses; |
15 | | -import com.avaje.ebeaninternal.server.deploy.InheritInfo; |
16 | | -import com.avaje.ebeaninternal.server.deploy.meta.DeployBeanDescriptor; |
| 11 | +import java.lang.annotation.Annotation; |
| 12 | +import java.sql.Types; |
| 13 | +import java.util.LinkedHashMap; |
| 14 | +import java.util.List; |
| 15 | +import java.util.Map; |
17 | 16 |
|
18 | 17 | /** |
19 | 18 | * Builds the InheritInfo deployment information. |
20 | 19 | */ |
21 | 20 | public class DeployInherit { |
22 | 21 |
|
23 | | - private final Map<Class<?>, DeployInheritInfo> deployMap = new LinkedHashMap<Class<?>, DeployInheritInfo>(); |
24 | | - |
25 | | - private final Map<Class<?>, InheritInfo> finalMap = new LinkedHashMap<Class<?>, InheritInfo>(); |
26 | | - |
27 | | - private final BootupClasses bootupClasses; |
28 | | - |
29 | | - /** |
30 | | - * Create the InheritInfoDeploy. |
31 | | - */ |
32 | | - public DeployInherit(BootupClasses bootupClasses) { |
33 | | - this.bootupClasses = bootupClasses; |
34 | | - initialise(); |
35 | | - } |
36 | | - |
37 | | - public void process(DeployBeanDescriptor<?> desc) { |
38 | | - InheritInfo inheritInfo = finalMap.get(desc.getBeanType()); |
39 | | - desc.setInheritInfo(inheritInfo); |
40 | | - } |
41 | | - |
42 | | - private void initialise() { |
43 | | - List<Class<?>> entityList = bootupClasses.getEntities(); |
44 | | - |
45 | | - findInheritClasses(entityList); |
46 | | - buildDeployTree(); |
47 | | - buildFinalTree(); |
48 | | - } |
49 | | - |
50 | | - private void findInheritClasses(List<Class<?>> entityList) { |
51 | | - |
52 | | - // go through each class and initialise the info object... |
53 | | - for (Class<?> cls : entityList) { |
54 | | - if (isInheritanceClass(cls)) { |
55 | | - DeployInheritInfo info = createInfo(cls); |
56 | | - deployMap.put(cls, info); |
57 | | - } |
58 | | - } |
59 | | - } |
60 | | - |
61 | | - private void buildDeployTree() { |
62 | | - |
63 | | - for (DeployInheritInfo info : deployMap.values()) { |
64 | | - if (!info.isRoot()) { |
65 | | - DeployInheritInfo parent = getInfo(info.getParent()); |
66 | | - parent.addChild(info); |
67 | | - } |
68 | | - } |
69 | | - } |
70 | | - |
71 | | - private void buildFinalTree() { |
72 | | - |
73 | | - for (DeployInheritInfo deploy : deployMap.values()) { |
74 | | - if (deploy.isRoot()) { |
75 | | - // build tree top down... |
76 | | - createFinalInfo(null, null, deploy); |
77 | | - } |
78 | | - } |
79 | | - } |
80 | | - |
81 | | - private void createFinalInfo(InheritInfo root, InheritInfo parent, DeployInheritInfo deploy) { |
82 | | - |
83 | | - InheritInfo node = new InheritInfo(root, parent, deploy); |
84 | | - if (parent != null) { |
85 | | - parent.addChild(node); |
86 | | - } |
87 | | - finalMap.put(node.getType(), node); |
88 | | - |
89 | | - if (root == null) { |
90 | | - root = node; |
91 | | - } |
92 | | - |
93 | | - // buildFinalChildren(root, child, deploy); |
94 | | - for (DeployInheritInfo childDeploy : deploy.children()) { |
95 | | - createFinalInfo(root, node, childDeploy); |
96 | | - } |
97 | | - } |
98 | | - |
99 | | - /** |
100 | | - * Build the InheritInfo for a given class. |
101 | | - */ |
102 | | - private DeployInheritInfo getInfo(Class<?> cls) { |
103 | | - return deployMap.get(cls); |
104 | | - } |
105 | | - |
106 | | - private DeployInheritInfo createInfo(Class<?> cls) { |
107 | | - |
108 | | - DeployInheritInfo info = new DeployInheritInfo(cls); |
109 | | - |
110 | | - Class<?> parent = findParent(cls); |
111 | | - if (parent != null) { |
112 | | - info.setParent(parent); |
113 | | - } |
114 | | - |
115 | | - Inheritance ia = cls.getAnnotation(Inheritance.class); |
116 | | - if (ia != null) { |
117 | | - ia.strategy(); |
118 | | - } |
119 | | - DiscriminatorColumn da = cls.getAnnotation(DiscriminatorColumn.class); |
120 | | - if (da != null) { |
121 | | - // lowercase the discriminator column for RawSql and JSON |
122 | | - info.setDiscriminatorColumn(da.name().toLowerCase()); |
123 | | - DiscriminatorType discriminatorType = da.discriminatorType(); |
124 | | - if (discriminatorType.equals(DiscriminatorType.INTEGER)){ |
125 | | - info.setDiscriminatorType(Types.INTEGER); |
126 | | - } else { |
127 | | - info.setDiscriminatorType(Types.VARCHAR); |
128 | | - } |
129 | | - info.setDiscriminatorLength(da.length()); |
130 | | - } |
131 | | - |
132 | | - DiscriminatorValue dv = cls.getAnnotation(DiscriminatorValue.class); |
133 | | - if (dv != null) { |
134 | | - info.setDiscriminatorValue(dv.value()); |
135 | | - } |
136 | | - |
137 | | - return info; |
138 | | - } |
139 | | - |
140 | | - private Class<?> findParent(Class<?> cls) { |
141 | | - Class<?> superCls = cls.getSuperclass(); |
142 | | - if (isInheritanceClass(superCls)) { |
143 | | - return superCls; |
144 | | - } else { |
145 | | - return null; |
146 | | - } |
147 | | - } |
148 | | - |
149 | | - private boolean isInheritanceClass(Class<?> cls) { |
150 | | - if (cls.equals(Object.class)) { |
151 | | - return false; |
152 | | - } |
153 | | - Annotation a = cls.getAnnotation(Inheritance.class); |
154 | | - if (a != null) { |
155 | | - return true; |
156 | | - } |
157 | | - // search up the inheritance heirarchy |
158 | | - return isInheritanceClass(cls.getSuperclass()); |
159 | | - } |
| 22 | + private final Map<Class<?>, DeployInheritInfo> deployMap = new LinkedHashMap<Class<?>, DeployInheritInfo>(); |
| 23 | + |
| 24 | + private final Map<Class<?>, InheritInfo> finalMap = new LinkedHashMap<Class<?>, InheritInfo>(); |
| 25 | + |
| 26 | + private final BootupClasses bootupClasses; |
| 27 | + |
| 28 | + /** |
| 29 | + * Create the InheritInfoDeploy. |
| 30 | + */ |
| 31 | + public DeployInherit(BootupClasses bootupClasses) { |
| 32 | + this.bootupClasses = bootupClasses; |
| 33 | + initialise(); |
| 34 | + } |
| 35 | + |
| 36 | + public void process(DeployBeanDescriptor<?> desc) { |
| 37 | + InheritInfo inheritInfo = finalMap.get(desc.getBeanType()); |
| 38 | + desc.setInheritInfo(inheritInfo); |
| 39 | + } |
| 40 | + |
| 41 | + private void initialise() { |
| 42 | + List<Class<?>> entityList = bootupClasses.getEntities(); |
| 43 | + |
| 44 | + findInheritClasses(entityList); |
| 45 | + buildDeployTree(); |
| 46 | + buildFinalTree(); |
| 47 | + } |
| 48 | + |
| 49 | + private void findInheritClasses(List<Class<?>> entityList) { |
| 50 | + |
| 51 | + // go through each class and initialise the info object... |
| 52 | + for (Class<?> cls : entityList) { |
| 53 | + if (isInheritanceClass(cls)) { |
| 54 | + DeployInheritInfo info = createInfo(cls); |
| 55 | + deployMap.put(cls, info); |
| 56 | + } |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + private void buildDeployTree() { |
| 61 | + |
| 62 | + for (DeployInheritInfo info : deployMap.values()) { |
| 63 | + if (!info.isRoot()) { |
| 64 | + DeployInheritInfo parent = getInfo(info.getParent()); |
| 65 | + parent.addChild(info); |
| 66 | + } |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + private void buildFinalTree() { |
| 71 | + |
| 72 | + for (DeployInheritInfo deploy : deployMap.values()) { |
| 73 | + if (deploy.isRoot()) { |
| 74 | + // build tree top down... |
| 75 | + createFinalInfo(null, null, deploy); |
| 76 | + } |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + private void createFinalInfo(InheritInfo root, InheritInfo parent, DeployInheritInfo deploy) { |
| 81 | + |
| 82 | + InheritInfo node = new InheritInfo(root, parent, deploy); |
| 83 | + if (parent != null) { |
| 84 | + parent.addChild(node); |
| 85 | + } |
| 86 | + finalMap.put(node.getType(), node); |
| 87 | + |
| 88 | + if (root == null) { |
| 89 | + root = node; |
| 90 | + } |
| 91 | + |
| 92 | + // buildFinalChildren(root, child, deploy); |
| 93 | + for (DeployInheritInfo childDeploy : deploy.children()) { |
| 94 | + createFinalInfo(root, node, childDeploy); |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + /** |
| 99 | + * Build the InheritInfo for a given class. |
| 100 | + */ |
| 101 | + private DeployInheritInfo getInfo(Class<?> cls) { |
| 102 | + return deployMap.get(cls); |
| 103 | + } |
| 104 | + |
| 105 | + private DeployInheritInfo createInfo(Class<?> cls) { |
| 106 | + |
| 107 | + DeployInheritInfo info = new DeployInheritInfo(cls); |
| 108 | + |
| 109 | + Class<?> parent = findParent(cls); |
| 110 | + if (parent != null) { |
| 111 | + info.setParent(parent); |
| 112 | + } |
| 113 | + |
| 114 | + Inheritance ia = cls.getAnnotation(Inheritance.class); |
| 115 | + if (ia != null) { |
| 116 | + ia.strategy(); |
| 117 | + } |
| 118 | + DiscriminatorColumn da = cls.getAnnotation(DiscriminatorColumn.class); |
| 119 | + if (da != null) { |
| 120 | + // lowercase the discriminator column for RawSql and JSON |
| 121 | + info.setDiscriminatorColumn(da.name().toLowerCase()); |
| 122 | + DiscriminatorType discriminatorType = da.discriminatorType(); |
| 123 | + if (discriminatorType.equals(DiscriminatorType.INTEGER)) { |
| 124 | + info.setDiscriminatorType(Types.INTEGER); |
| 125 | + } else { |
| 126 | + info.setDiscriminatorType(Types.VARCHAR); |
| 127 | + } |
| 128 | + info.setDiscriminatorLength(da.length()); |
| 129 | + } |
| 130 | + |
| 131 | + DiscriminatorValue dv = cls.getAnnotation(DiscriminatorValue.class); |
| 132 | + if (dv != null) { |
| 133 | + info.setDiscriminatorValue(dv.value()); |
| 134 | + } |
| 135 | + |
| 136 | + return info; |
| 137 | + } |
| 138 | + |
| 139 | + private Class<?> findParent(Class<?> cls) { |
| 140 | + Class<?> superCls = cls.getSuperclass(); |
| 141 | + if (isInheritanceClass(superCls)) { |
| 142 | + return superCls; |
| 143 | + } else { |
| 144 | + return null; |
| 145 | + } |
| 146 | + } |
| 147 | + |
| 148 | + private boolean isInheritanceClass(Class<?> cls) { |
| 149 | + if (cls.equals(Object.class)) { |
| 150 | + return false; |
| 151 | + } |
| 152 | + Annotation a = cls.getAnnotation(Inheritance.class); |
| 153 | + if (a != null) { |
| 154 | + return true; |
| 155 | + } |
| 156 | + // search up the inheritance heirarchy |
| 157 | + return isInheritanceClass(cls.getSuperclass()); |
| 158 | + } |
160 | 159 |
|
161 | 160 | } |
0 commit comments