Skip to content

Commit 9059928

Browse files
jmeznaTFaga
authored andcommitted
code cleanup, fixed docs
1 parent 0331ba9 commit 9059928

1 file changed

Lines changed: 49 additions & 38 deletions

File tree

components/cdi/weld/src/main/java/com/kumuluz/ee/configuration/cdi/interceptors/ConfigBundleInterceptor.java

Lines changed: 49 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -75,30 +75,36 @@ public Object loadConfiguration(InvocationContext ic) throws Exception {
7575
}
7676

7777
/**
78-
* Processes and invokes all setters in Bean anotated with @ConfigBundle
78+
* Processes and invokes all setters in Bean annotated with @ConfigBundle
7979
*
80-
* @param target target object
81-
* @param targetClass target class
80+
* @param target target object
81+
* @param targetClass target class
82+
* @param keyPrefix a prefix for generating key names
83+
* @param processedClassRelations class pairs that have already been processed (for cycle detection)
84+
* @param watchAllFields if true, enable watch on all fields
8285
* @throws Exception
8386
*/
8487
private void processConfigBundleBeanSetters(Object target, Class targetClass, String keyPrefix, Map<Class, Class>
8588
processedClassRelations, boolean watchAllFields) throws Exception {
8689

8790
// invoke setters
88-
for (Method m : targetClass.getMethods()) {
91+
for (Method method : targetClass.getMethods()) {
8992

90-
if (m.getName().substring(0, 3).equals("set") && m.getParameters().length == 1) {
93+
if (method.getName().substring(0, 3).equals("set") && method.getParameters().length == 1) {
9194

92-
Class parameterType = m.getParameters()[0].getType();
95+
Class parameterType = method.getParameters()[0].getType();
9396

94-
// get field annotation
95-
Field field = targetClass.getDeclaredField(setterToField(m.getName()));
97+
// get field annotation - @ConfigValue
98+
Field field = targetClass.getDeclaredField(setterToField(method.getName()));
9699
ConfigValue fieldAnnotation = null;
100+
if (field != null) {
101+
fieldAnnotation = field.getAnnotation(ConfigValue.class);
102+
}
103+
104+
// watch nested class or list if all fields in the bean are annotated with watch or if a field is
105+
// annnotated with watch
97106
boolean watchNestedClass = watchAllFields;
98-
if (watchAllFields == false) {
99-
if (field != null) {
100-
fieldAnnotation = field.getAnnotation(ConfigValue.class);
101-
}
107+
if (watchNestedClass == false) {
102108
if (fieldAnnotation != null) {
103109
watchNestedClass = fieldAnnotation.watch();
104110
}
@@ -107,50 +113,53 @@ private void processConfigBundleBeanSetters(Object target, Class targetClass, St
107113
// process primitives
108114
if (Arrays.asList(primitives).contains(parameterType)) {
109115

110-
Optional<String> value = getValueOfPrimitive(parameterType, getKeyName(targetClass, m.getName(),
111-
keyPrefix));
116+
Optional<String> value = getValueOfPrimitive(parameterType, getKeyName(targetClass, method
117+
.getName(), keyPrefix));
112118

113119
if (value.isPresent()) {
114-
m.invoke(target, value.get());
120+
method.invoke(target, value.get());
115121
}
116122

117123
if (watchAllFields || (fieldAnnotation != null && fieldAnnotation.watch())) {
118-
deployWatcher(target, m, getKeyName(targetClass, m.getName(), keyPrefix));
124+
deployWatcher(target, method, getKeyName(targetClass, method.getName(), keyPrefix));
119125
}
120126

121127
// process nested objeccts
122128
} else if (!parameterType.isArray()) {
123129

124130
processedClassRelations.put(targetClass, parameterType);
125131

126-
Object nestedTarget = processNestedObject(targetClass, m, parameterType, keyPrefix,
132+
Object nestedTarget = processNestedObject(targetClass, method, parameterType, keyPrefix,
127133
processedClassRelations, -1, watchNestedClass);
128134

129135
// invoke setter method with initialised instance
130-
m.invoke(target, nestedTarget);
136+
method.invoke(target, nestedTarget);
131137

132138
// process arrays
133139
} else {
134140

135141
Class componentType = parameterType.getComponentType();
136142

137143
Object array = Array.newInstance(componentType, configurationUtil.getListSize(getKeyName
138-
(targetClass, m.getName(), keyPrefix)).orElse(0));
144+
(targetClass, method.getName(), keyPrefix)).orElse(0));
139145

146+
// process list of primitives
140147
if (Arrays.asList(primitives).contains(componentType)) {
141148
for (int i = 0; i < Array.getLength(array); i++) {
142-
Array.set(array, i, getValueOfPrimitive(componentType, getKeyName(targetClass, m.getName(),
143-
keyPrefix) + "[" + i + "]").orElse(null));
149+
Array.set(array, i, getValueOfPrimitive(componentType, getKeyName(targetClass, method
150+
.getName(), keyPrefix) + "[" + i + "]").get());
144151
}
152+
153+
// process list of nested classes
145154
} else {
146155
for (int i = 0; i < Array.getLength(array); i++) {
147-
Array.set(array, i, processNestedObject(targetClass, m, componentType, keyPrefix,
156+
Array.set(array, i, processNestedObject(targetClass, method, componentType, keyPrefix,
148157
processedClassRelations, i, watchNestedClass));
149158

150159
}
151160
}
152161

153-
m.invoke(target, array);
162+
method.invoke(target, array);
154163

155164
}
156165
}
@@ -159,10 +168,10 @@ private void processConfigBundleBeanSetters(Object target, Class targetClass, St
159168
}
160169

161170
/**
162-
* Get value of a primitive configuration type
171+
* Returns a value of a primitive configuration type
163172
*
164-
* @param type
165-
* @param key
173+
* @param type configuration value type
174+
* @param key configuration value key
166175
* @return
167176
*/
168177
private Optional getValueOfPrimitive(Class type, String key) {
@@ -186,26 +195,26 @@ private Optional getValueOfPrimitive(Class type, String key) {
186195
}
187196

188197
/**
189-
* Create new instance for nested class, check for cycles and populate nested instance.
198+
* Create a new instance for nested class, check for cycles and populate nested instance.
190199
*
191-
* @param targetClass
192-
* @param method
193-
* @param parameterType
194-
* @param keyPrefix Prefix used for generation of configuration key.
195-
* @param processedClassRelations Relations of nested classes that have already been procees. Needed for
196-
* detecting cycles.
197-
* @param arrayIndex Array index for arrays of nested objects.
200+
* @param targetClass target class
201+
* @param method processed method
202+
* @param parameterType parameter type
203+
* @param keyPrefix prefix used for generation of a configuration key
204+
* @param processedClassRelations class pairs that have already been processed (for cycle detection)
205+
* @param arrayIndex array index for arrays of nested objects
206+
* @param watchAllFields if true, enable watch on all fields
198207
* @return
199208
* @throws Exception
200209
*/
201210
private Object processNestedObject(Class targetClass, Method method, Class parameterType, String keyPrefix,
202211
Map<Class, Class> processedClassRelations, int arrayIndex, boolean
203-
watchAllFields)
204-
throws Exception {
212+
watchAllFields) throws Exception {
205213

206214
Object nestedTarget = parameterType.getConstructor().newInstance();
207215
Class nestedTargetClass = nestedTarget.getClass();
208216

217+
// check for cycles
209218
if (processedClassRelations.containsKey(nestedTargetClass) && processedClassRelations.get(nestedTargetClass)
210219
.equals(targetClass)) {
211220
log.warning("There is a cycle in the configuration class tree. ConfigBundle class may not " +
@@ -221,6 +230,7 @@ private Object processNestedObject(Class targetClass, Method method, Class param
221230
processConfigBundleBeanSetters(nestedTarget, nestedTargetClass, key, processedClassRelations,
222231
watchAllFields);
223232
}
233+
224234
return nestedTarget;
225235
}
226236

@@ -229,6 +239,7 @@ private Object processNestedObject(Class targetClass, Method method, Class param
229239
*
230240
* @param targetClass target class
231241
* @param setter name of the setter method
242+
* @param keyPrefix prefix used for generation of a configuration key
232243
* @return key in format prefix.key-name
233244
*/
234245
private String getKeyName(Class targetClass, String setter, String keyPrefix) throws Exception {
@@ -255,10 +266,10 @@ private String getKeyName(Class targetClass, String setter, String keyPrefix) th
255266
}
256267

257268
/**
258-
* Generate key prefix from annotation, class name, or parent prefix in case of nested classes.
269+
* Generate a key prefix from annotation, class name, or parent prefix in case of nested classes.
259270
*
260271
* @param targetClass target class
261-
* @param keyPrefix key prefix used in nested classes
272+
* @param keyPrefix prefix used for generation of a configuration key
262273
* @return key prefix
263274
*/
264275
private String getKeyPrefix(Class targetClass, String keyPrefix) {

0 commit comments

Comments
 (0)