2 * Copyright © 2016-2018 European Support Limited
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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 * ================================================================================
19 package org.onap.sdc.tosca.services;
21 import java.lang.reflect.Field;
22 import java.lang.reflect.InvocationTargetException;
23 import java.util.Arrays;
24 import java.util.HashSet;
25 import java.util.List;
27 import java.util.Objects;
28 import java.util.Optional;
30 import com.google.common.collect.ImmutableSet;
32 import com.fasterxml.jackson.databind.ObjectMapper;
33 import org.apache.commons.beanutils.BeanUtils;
35 public class CommonUtil {
37 private static final String DEFAULT = "default";
38 private static final String _DEFAULT = "_default";
39 private static ImmutableSet<Class<?>> complexClassType = ImmutableSet.of(Map.class, String.class, Integer.class, Float.class,
40 Double.class, Set.class, Object.class, List.class);
42 private CommonUtil() {
43 throw new IllegalStateException("Utility class");
46 public static <T> Optional<T> createObjectUsingSetters(Object objectCandidate, Class<? extends T> classToCreate)
48 if (Objects.isNull(objectCandidate)) {
49 return Optional.empty();
51 Map<String, Object> objectAsMap = getObjectAsMap(objectCandidate);
53 Field[] declaredFields = classToCreate.getDeclaredFields();
54 createSubObjectsUsingSetters(objectAsMap, declaredFields);
55 T result = populateBean(objectAsMap, classToCreate);
57 return Optional.of(result);
60 public static void createSubObjectsUsingSetters(Map<String, Object> objectAsMap, Field[] declaredFields)
62 for (Field field : declaredFields) {
63 if (isComplexClass(field)) {
64 Optional<?> objectUsingSetters =
65 createObjectUsingSetters(objectAsMap.get(field.getName()), field.getType());
66 if (objectUsingSetters.isPresent()) {
67 objectAsMap.remove(field.getName());
68 objectAsMap.put(field.getName(), objectUsingSetters.get());
74 public static <T> T populateBean(Map<String, Object> propertiesMap, Class<T> classToCreate)
75 throws IllegalAccessException, InstantiationException, InvocationTargetException {
76 T result = classToCreate.newInstance();
77 BeanUtils.populate(result, propertiesMap);
81 public static Map<String, Object> getObjectAsMap(Object obj) {
82 Map<String, Object> objectAsMap =
83 obj instanceof Map ? (Map<String, Object>) obj : new ObjectMapper().convertValue(obj, Map.class);
85 if (objectAsMap.containsKey(DEFAULT)) {
86 Object defaultValue = objectAsMap.get(DEFAULT);
87 objectAsMap.remove(DEFAULT);
88 objectAsMap.put(_DEFAULT, defaultValue);
93 private static boolean isComplexClass(Field field) {
94 return !complexClassType.contains(field.getType());
97 public static Set<String> getClassFieldNames(Class<? extends Object> classType) {
98 Set<String> fieldNames = new HashSet<>();
99 Class superClass = classType.getSuperclass();
100 if (superClass != null) {
101 fieldNames.addAll(getClassFieldNames(superClass));
103 Arrays.stream(classType.getDeclaredFields()).forEach(field -> fieldNames.add(field.getName()));