1 /*******************************************************************************
2 * ============LICENSE_START========================================================================
3 * ONAP : ccsdk feature sdnr wt
4 * =================================================================================================
5 * Copyright (C) 2019 highstreet technologies GmbH Intellectual Property. All rights reserved.
6 * =================================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
8 * in compliance with the License. You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software distributed under the License
13 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
14 * or implied. See the License for the specific language governing permissions and limitations under
16 * ============LICENSE_END==========================================================================
17 ******************************************************************************/
18 package org.onap.ccsdk.features.sdnr.wt.yangtools;
20 import java.lang.reflect.Field;
21 import java.lang.reflect.InvocationTargetException;
22 import java.lang.reflect.Method;
23 import java.util.ArrayList;
24 import java.util.Arrays;
25 import java.util.List;
27 import javax.annotation.Nullable;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
32 public class YangToolsCloner2 {
34 private static final Logger LOG = LoggerFactory.getLogger(YangToolsCloner2.class);
36 public enum Accessor {
41 private Accessor accessor;
43 public YangToolsCloner2() {
44 LOG.info("Provide new {}",this.getClass().getName());
45 this.accessor = Accessor.ACCESSOR_METHOD;
48 YangToolsCloner2 setAcessor(Accessor accessor) {
49 this.accessor = accessor;
53 Accessor getAccessor() {
57 public interface Builder<T> {
63 * @param source source object
64 * @param clazz Class of return object
65 * @attrList filter for attribute Names to clone
66 * @return list of cloned object
69 public <S, T> List<T> cloneList(List<S> source, Builder<T> builder, String ... attrList) throws Exception {
73 List<T> list = new ArrayList<T>();
75 list.add(copyAttributes(s, builder.build(), attrList));
81 * Copy attributes from source to destination object.
82 * Copy the references.
83 * @param source source object
84 * @param clazz Class of return object
85 * @attrList attribute Names NOT to clone.
86 * @return cloned object
89 @SuppressWarnings("null")
90 public @Nullable <S, T> T copyAttributes(S source, T destination, String ... attributeArray) throws Exception {
92 LOG.debug("copyAttributes source.class {} destination.class {} attributes {}", source, destination, attributeArray.length);
94 if (destination == null || source == null)
97 List<String> attributeList = Arrays.asList(attributeArray);
98 LOG.debug("copyAttributes 2 attributes {}", attributeList);
100 Field[] destinationAttributeFields = source.getClass().getDeclaredFields();
101 String destinationName;
102 Class<?> destinationType;
103 for (Field destinationAttributeField : destinationAttributeFields) {
104 destinationName = destinationAttributeField.getName();
105 destinationType = destinationAttributeField.getType();
106 LOG.debug("Field: {}", destinationName);
107 // check if attr is in exclusion list
108 if (attributeList.contains(destinationName)) {
112 if (destinationName.equals("QNAME")) {
116 destinationAttributeField.setAccessible(true);
117 Object sourceData = null;
118 Class<?> sourceType = null;
119 Class<?> sourceListType = null;
121 if (accessor == Accessor.ACCESSOR_FIELD) {
123 sourceField = source.getClass().getDeclaredField(destinationName);
124 sourceField.setAccessible(true);
125 sourceType = sourceField.getType();
126 sourceData = sourceField.get(source);
127 sourceListType = getListClass(sourceType, sourceData);
129 } else if (accessor == Accessor.ACCESSOR_METHOD) {
131 sourceMethod = source.getClass().getDeclaredMethod(getter(destinationName));
132 sourceMethod.setAccessible(true);
133 sourceType = sourceMethod.getReturnType();
134 sourceData = sourceMethod.invoke(source);
135 sourceListType = getListClass(sourceType, sourceData);
137 LOG.info("Handle {} {} {}", destinationName, destinationType, sourceType);
138 if (destinationType == sourceType) {
139 destinationAttributeField.set(destination, sourceData);
142 "Problem to copy attribute " + destinationName
143 +" Sourceclass:" +sourceType
144 +" Destinationclass:" + destinationType
145 +" Method:"+accessor.name());
147 } catch (NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException
148 | NoSuchMethodException | InvocationTargetException e) {
156 private static String getter(String name) {
157 if (name == null || name.length() == 0) {
159 } else if (name.length() == 1) {
160 return String.format("%s%s", "get", name.substring(1, 2).toUpperCase());
162 return String.format("%s%s%s", "get", name.substring(1, 2).toUpperCase(), name.substring(2));
166 private static Class<?> getListClass(Class<?> sourceType, Object sourceData) {
167 if (sourceData != null && sourceType.equals(List.class)) {
168 List<Object> sourceDataList = (List<Object>)sourceData;
169 if (sourceDataList.size() > 0) {
170 LOG.info("Is list with type"+sourceDataList.get(0).getClass().getName());
172 LOG.info("Is empty list");