BeanUtils upgrade to 1.9.x
[sdc.git] / catalog-be / src / main / java / org / openecomp / sdc / externalupload / utils / ServiceUtils.java
1 /*
2  * Copyright © 2016-2018 European Support Limited
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  * ============LICENSE_END=========================================================
16  * Modifications copyright (c) 2019 Nokia
17  * ================================================================================
18  */
19
20 package org.openecomp.sdc.externalupload.utils;
21
22 import com.fasterxml.jackson.databind.ObjectMapper;
23 import com.google.common.collect.ImmutableSet;
24
25 import java.lang.reflect.Field;
26 import java.util.*;
27 import org.onap.sdc.tosca.services.CommonUtil;
28
29 public class ServiceUtils {
30
31   private static ImmutableSet<Class> collectionClasses = ImmutableSet.of(Map.class, List.class, Set.class);
32   private static ImmutableSet<Class> primitiveTypesClasses = ImmutableSet.of(String.class, Integer.class, Double.class, Float.class);
33
34   private ServiceUtils() {}
35
36   public static <T> Optional<T> createObjectUsingSetters(Object objectCandidate,
37                                                          Class<T> classToCreate)
38       throws Exception {
39     if (Objects.isNull(objectCandidate)) {
40       return Optional.empty();
41     }
42
43     Map<String, Object> objectAsMap = getObjectAsMap(objectCandidate);
44
45     List<Field> declaredFields = getAllFields(classToCreate);
46
47     CommonUtil.createSubObjectsUsingSetters(objectAsMap, declaredFields.toArray(new Field[0]));
48     T result = CommonUtil.populateBean(objectAsMap, classToCreate);
49
50     return Optional.of(result);
51   }
52
53   private static <T> List<Field> getAllFields(Class<T> clazz) {
54     List<Field> fields = new ArrayList<>();
55     for(Class<?> c = clazz; c != null; c = c.getSuperclass()) {
56       fields.addAll(Arrays.asList(c.getDeclaredFields()));
57     }
58
59     return fields;
60   }
61
62   private static boolean isComplexClass(Field field) {
63     return !isCollectionClass(field)
64         && !isPrimitiveClass(field)
65         && !field.getType().equals(Object.class);
66   }
67
68   private static boolean isCollectionClass(Field field) {
69     return collectionClasses.contains(field.getType());
70   }
71
72   private static boolean isPrimitiveClass(Field field) {
73     return primitiveTypesClasses.contains(field.getType());
74   }
75
76   public static Map<String, Object> getObjectAsMap(Object obj) {
77     return new ObjectMapper().convertValue(obj, Map.class);
78   }
79
80   public static Set<String> getClassFieldNames(Class<? extends Object> classType) {
81     Set<String> fieldNames = new HashSet<>();
82     List<Field> allFields = getAllFields(classType);
83     allFields.forEach(field -> fieldNames.add(field.getName()));
84
85     return fieldNames;
86   }
87 }