1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
|
package org.openecomp.core.impl;
import org.apache.commons.lang.StringUtils;
import org.codehaus.jackson.map.ObjectMapper;
import org.openecomp.core.converter.errors.CreateToscaObjectErrorBuilder;
import org.openecomp.core.utilities.json.JsonUtil;
import org.openecomp.sdc.common.errors.CoreException;
import org.openecomp.sdc.common.errors.ErrorCategory;
import org.openecomp.sdc.common.errors.ErrorCode;
import java.lang.reflect.Field;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
public class ToscaConverterUtil {
private static final String set = "set";
public static <T> Optional<T> createObjectFromClass(String objectId,
Object objectCandidate,
Class<T> classToCreate) {
try {
return createObjectUsingSetters(objectCandidate, classToCreate);
} catch (Exception e) {
throw new CoreException(
new CreateToscaObjectErrorBuilder(classToCreate.getSimpleName(), objectId, e.getMessage())
.build());
}
}
private static <T> Optional<T> createObjectUsingSetters(Object objectCandidate,
Class<T> classToCreate) throws Exception {
if(!(objectCandidate instanceof Map)){
return Optional.empty();
}
Map<String, Object> objectAsMap = (Map<String, Object>) objectCandidate;
Field[] classFields = classToCreate.getDeclaredFields();
T result = classToCreate.newInstance();
for(Field field : classFields){
Object fieldValueToAssign = objectAsMap.get(field.getName());
String methodName = set + StringUtils.capitalize(field.getName());
if(shouldSetterMethodNeedsToGetInvoked(classToCreate, field, fieldValueToAssign, methodName)){
classToCreate.getMethod(methodName, field.getType()).invoke(result, fieldValueToAssign);
}
}
return Optional.of(result);
}
private static <T> boolean shouldSetterMethodNeedsToGetInvoked(Class<T> classToCreate,
Field field,
Object fieldValueToAssign,
String methodName) {
try {
return Objects.nonNull(fieldValueToAssign)
&& Objects.nonNull(classToCreate.getMethod(methodName, field.getType()));
} catch (NoSuchMethodException e) {
return false;
}
}
}
|