Deep copy using metadata.

@param source
the source
@param copiedObjectMap
the copied object map
@param kunderaMetadata
the kundera metadata
@return the object
194369
private static Object deepCopyUsingMetadata(Object source, Map<Object, Object> copiedObjectMap,
            final KunderaMetadata kunderaMetadata)
    {
        Object target = null;
        try
        {
            if (source == null)
            {
                return null;
            }
            Class<?> sourceObjectClass = source.getClass();
            EntityMetadata metadata = KunderaMetadataManager.getEntityMetadata(kunderaMetadata, sourceObjectClass);
            if (metadata == null)
            {
                return source;
            }

            MetamodelImpl metaModel = (MetamodelImpl) kunderaMetadata.getApplicationMetadata().getMetamodel(
                    metadata.getPersistenceUnit());

            EntityType entityType = metaModel.entity(sourceObjectClass);

            // May break for mapped super class.

            Object id = null;
            if (metadata.getRelations() != null && !metadata.getRelations().isEmpty())
            {

                id = PropertyAccessorHelper.getId(source, metadata);

                StringBuilder keyBuilder = new StringBuilder(sourceObjectClass.getName());
                keyBuilder.append("#");
                keyBuilder.append(id);
                Object copiedObjectInMap = copiedObjectMap.get(keyBuilder.toString());
                if (copiedObjectInMap != null)
                {
                    return copiedObjectInMap;
                }
            }
            // Copy Columns (in a table that doesn't have any embedded objects

            target = KunderaCoreUtils.createNewInstance(sourceObjectClass);

            Iterator<Attribute> iter = entityType.getAttributes().iterator();
            while (iter.hasNext())
            {
                Attribute attrib = iter.next();

                Field columnField = (Field) attrib.getJavaMember();
                if (attrib.getPersistentAttributeType().equals(PersistentAttributeType.EMBEDDED)
                        || attrib.getPersistentAttributeType().equals(PersistentAttributeType.ELEMENT_COLLECTION))
                {
                    EmbeddableType embeddedColumn = metaModel.embeddable(((AbstractAttribute) attrib)
                            .getBindableJavaType());

                    Object sourceEmbeddedObj = PropertyAccessorHelper.getObject(source, columnField);
                    if (sourceEmbeddedObj != null)
                    {
                        if (metaModel.isEmbeddable(((AbstractAttribute) attrib).getBindableJavaType()))
                        {
                            if (attrib.isCollection())
                            {
                                Class<?> ecDeclaredClass = columnField.getType();
                                Class<?> actualEcObjectClass = sourceEmbeddedObj.getClass();

                                Object targetCollectionObject = actualEcObjectClass.newInstance();

                                // Copy element collections for List and Set
                                if (sourceEmbeddedObj instanceof Collection)
                                {
                                    Class<?> genericClass = PropertyAccessorHelper.getGenericClass(columnField);

                                    for (Object sourceEcObj : (Collection) sourceEmbeddedObj)
                                    {
                                        Object targetEcObj = null;
                                        if (sourceEcObj != null
                                                && PersistenceType.BASIC.equals(embeddedColumn.getPersistenceType()))
                                        {
                                            PropertyAccessor accessor = PropertyAccessorFactory
                                                    .getPropertyAccessor(sourceEcObj.getClass());
                                            if (accessor != null)
                                            {
                                                targetEcObj = accessor.getCopy(sourceEcObj);
                                            }
                                        }
                                        else if (sourceEcObj != null
                                                && PersistenceType.EMBEDDABLE.equals(embeddedColumn
                                                        .getPersistenceType()))
                                        {
                                            targetEcObj = genericClass.newInstance();

                                            for (Field f : genericClass.getDeclaredFields())
                                            {
                                                if (f != null && !Modifier.isStatic(f.getModifiers()))
                                                {
                                                    PropertyAccessorHelper.set(targetEcObj, f,
                                                            PropertyAccessorHelper.getObjectCopy(sourceEcObj, f));
                                                }
                                            }
                                        }
                                        if (List.class.isAssignableFrom(ecDeclaredClass))
                                        {
                                            Method m = actualEcObjectClass.getMethod("add", Object.class);
                                            m.invoke(targetCollectionObject, targetEcObj);

                                        }
                                        else if (Set.class.isAssignableFrom(ecDeclaredClass))
                                        {
                                            Method m = actualEcObjectClass.getMethod("add", Object.class);
                                            m.invoke(targetCollectionObject, targetEcObj);
                                        }
                                    }
                                }

                                // Copy element collection for Map
                                else if (sourceEmbeddedObj instanceof Map)
                                {
                                    for (Object sourceKey : ((Map) sourceEmbeddedObj).keySet())
                                    {
                                        Object targetKey = null;
                                        Object targetValue = null;
                                        if (PersistenceType.BASIC.equals(embeddedColumn.getPersistenceType())
                                                || PersistenceType.EMBEDDABLE.equals(embeddedColumn
                                                        .getPersistenceType()))
                                        {
                                            // Create copy of map key
                                            if (sourceKey != null)
                                            {
                                                PropertyAccessor keyAccessor = PropertyAccessorFactory
                                                        .getPropertyAccessor(sourceKey.getClass());
                                                if (keyAccessor != null)
                                                {
                                                    targetKey = keyAccessor.getCopy(sourceKey);
                                                }
                                            }
                                            else
                                            {
                                                targetKey = null;
                                            }

                                            // Create copy of map value
                                            Object sourceValue = ((Map) sourceEmbeddedObj).get(sourceKey);
                                            if (sourceValue != null)
                                            {
                                                PropertyAccessor valueAccessor = PropertyAccessorFactory
                                                        .getPropertyAccessor(sourceValue.getClass());
                                                if (valueAccessor != null)
                                                {
                                                    targetValue = valueAccessor.getCopy(sourceValue);
                                                }
                                            }
                                            else
                                            {
                                                targetValue = null;
                                            }

                                            if (Map.class.isAssignableFrom(ecDeclaredClass))
                                            {
                                                Method m = actualEcObjectClass.getMethod("put", new Class[] {
                                                        Object.class, Object.class });
                                                m.invoke(targetCollectionObject,
                                                        new Object[] { targetKey, targetValue });
                                            }
                                        }
                                    }
                                }
                                PropertyAccessorHelper.set(target, columnField, targetCollectionObject);
                            }
                            else
                            {
                                // Copy embedded objects
                                Class<?> embeddedColumnClass = columnField.getType();
                                Object targetEmbeddedObj = embeddedColumnClass.newInstance();

                                Set<Attribute> columns = embeddedColumn.getAttributes();
                                for (Attribute column : columns)
                                {
                                    PropertyAccessorHelper.set(
                                            targetEmbeddedObj,
                                            (Field) column.getJavaMember(),
                                            PropertyAccessorHelper.getObjectCopy(sourceEmbeddedObj,
                                                    (Field) column.getJavaMember()));
                                }

                                PropertyAccessorHelper.set(target, columnField, targetEmbeddedObj);
                            }
                        }
                        else if (((AbstractAttribute) attrib).getJPAColumnName() != null)
                        {
                            // Copy columns
                            PropertyAccessorHelper.set(target, columnField, sourceEmbeddedObj);
                        }
                    }

                }
                else if (attrib.getPersistentAttributeType().equals(PersistentAttributeType.BASIC))
                {

                    PropertyAccessorHelper.set(target, columnField,
                            PropertyAccessorHelper.getObjectCopy(source, columnField));
                }
            }

            // Put this object into copied object map
            if (id != null)
            {
                StringBuilder keyBuilder = new StringBuilder(sourceObjectClass.getName());
                keyBuilder.append("#");
                keyBuilder.append(id);
                copiedObjectMap.put(keyBuilder.toString(), target);
            }
            // Copy Relationships recursively
            for (Relation relation : metadata.getRelations())
            {
                if (relation != null)
                {
                    Field relationField = relation.getProperty();
                    Object sourceRelationObject = PropertyAccessorHelper.getObject(source, relationField);

                    if (sourceRelationObject != null)
                    {
                        if (sourceRelationObject instanceof KunderaProxy)
                        {
                            PropertyAccessorHelper.set(target, relationField, sourceRelationObject);
                            continue;
                        }
                        else if (ProxyHelper.isPersistentCollection(sourceRelationObject))
                        {
                            PropertyAccessorHelper.set(target, relationField, sourceRelationObject);
                            continue;
                        }
                        else if (ProxyHelper.isKunderaProxyCollection(sourceRelationObject))
                        {
                            ProxyCollection pc = ((ProxyCollection) sourceRelationObject).getCopy();
                            pc.setOwner(target);
                            PropertyAccessorHelper.set(target, relationField, pc);
                            continue;
                        }

                        Object targetRelationObject = null;

                        Class<?> relationObjectClass = relation.getProperty().getType();
                        Class<?> actualRelationObjectClass = sourceRelationObject.getClass();

                        if (Collection.class.isAssignableFrom(relationObjectClass))
                        {
                            targetRelationObject = actualRelationObjectClass.newInstance();
                            Method m = actualRelationObjectClass.getMethod("add", Object.class);

                            for (Object obj : (Collection) sourceRelationObject)
                            {
                                Object copyTargetRelObj = searchInCacheThenCopy(copiedObjectMap, obj, kunderaMetadata);
                                m.invoke(targetRelationObject, copyTargetRelObj);
                            }
                        }
                        else if (Map.class.isAssignableFrom(relationObjectClass))
                        {
                            targetRelationObject = actualRelationObjectClass.newInstance();
                            Method m = actualRelationObjectClass.getMethod("put", new Class<?>[] { Object.class,
                                    Object.class });

                            for (Object keyObj : ((Map) sourceRelationObject).keySet())
                            {
                                Object valObj = ((Map) sourceRelationObject).get(keyObj);

                                Object copyTargetKeyObj = searchInCacheThenCopy(copiedObjectMap, keyObj,
                                        kunderaMetadata);

                                Object copyTargetValueObj = searchInCacheThenCopy(copiedObjectMap, valObj,
                                        kunderaMetadata);

                                m.invoke(targetRelationObject, new Object[] { copyTargetKeyObj, copyTargetValueObj });
                            }
                        }
                        else
                        {
                            targetRelationObject = searchInCacheThenCopy(copiedObjectMap, sourceRelationObject,
                                    kunderaMetadata);
                        }
                        PropertyAccessorHelper.set(target, relationField, targetRelationObject);
                    }
                }
            }
        }
        catch (InstantiationException e)
        {
            log.warn("Error while instantiating entity/ embeddable class, did you define no-arg constructor?, Caused by:"
                    + e.getMessage());
            return null;
        }
        catch (Exception e)
        {
            log.warn("Returning null as error during clone, Caused by:" + e.getMessage());
            return null;
        }

        return target;
    }
1257652
public static DoubleVector copyOf(DoubleVector source) {
        if (source == null)
            return null;
        DoubleVector result = null;

        if (source instanceof SparseDoubleVector) {
            result = new CompactSparseVector(source.length());
            copyFromSparseVector(result, source);
        } else if (source instanceof DenseVector ||
                   source instanceof ScaledDoubleVector) {
            result = new DenseVector(source.length());
            for (int i = 0; i < source.length(); ++i)
                result.set(i, source.get(i));
        } else if (source instanceof AmortizedSparseVector) {
            result = new AmortizedSparseVector(source.length());
            copyFromSparseVector(result, source);
        } else if (source instanceof DoubleVectorView) {
            DoubleVectorView view = (DoubleVectorView) source;
            return copyOf(view.getOriginalVector());
        } else {
            // Create a copy of the given class using reflection.  

            // First check whether we can find a copy contructor that accepts an
            // instance of Vector
            Class<? extends DoubleVector> sourceClazz = source.getClass();
            try {
                Constructor<? extends DoubleVector> constructor =
                    sourceClazz.getConstructor(DoubleVector.class);
                result = (DoubleVector) constructor.newInstance(source);
            } 
            // If there wasn't a copy constructor, see if there's one that
            // accepts the length and then we'll copy the data over manually
            catch (NoSuchMethodException nsme) {
                try {
                    Constructor<? extends DoubleVector> constructor =
                        sourceClazz.getConstructor(Integer.TYPE);
                    int length = source.length();
                    result = (DoubleVector) constructor.newInstance(
                        Integer.valueOf(length));
                    // Copy the data over
                    for (int i = 0; i < length; ++i) 
                        result.set(i, source.get(i));                    
                }
                catch (Exception e) {
                    throw new UnsupportedOperationException(
                        "Could not find applicalble way to copy a vector " +
                        "of type " + source.getClass(), e);
                }
            }
            catch (Exception e) {
                throw new UnsupportedOperationException(
                    "Could not find applicalble way to copy a vector " +
                    "of type " + source.getClass(), e);
            }
        }
        return result;
    }
24155
public <T extends Parameter> T copy() throws URISyntaxException {
        if (factory == null) {
            throw new UnsupportedOperationException("No factory specified");
        }
        return (T) factory.createParameter(getValue());
    }
668536
function(source) {
    var copy, i, len, prop;
    if (typeof source !== "object" || source == null || typeof source.nodeType === "number") {
      copy = source;
    } else if (typeof source.length === "number") {
      copy = [];
      for (i = 0, len = source.length; i < len; i++) {
        if (_hasOwn.call(source, i)) {
          copy[i] = _deepCopy(source[i]);
        }
      }
    } else {
      copy = {};
      for (prop in source) {
        if (_hasOwn.call(source, prop)) {
          copy[prop] = _deepCopy(source[prop]);
        }
      }
    }
    return copy;
  }
158709
public static Kam copy(final Kam kam) {
        if (kam == null) {
            return null;
        }

        final Kam copy = new KamImpl(kam.getKamInfo());

        final Collection<KamNode> nodes = kam.getNodes();
        if (hasItems(nodes)) {
            for (final KamNode node : nodes) {
                copy.createNode(node.getId(), node.getFunctionType(),
                        node.getLabel());
            }
        }

        final Collection<KamEdge> edges = kam.getEdges();
        if (hasItems(edges)) {
            for (final KamEdge edge : edges) {
                final KamNode source = copy.findNode(edge.getSourceNode()
                        .getId());
                final KamNode target = copy.findNode(edge.getTargetNode()
                        .getId());

                assert source != null;
                assert target != null;

                copy.createEdge(edge.getId(), source,
                        edge.getRelationshipType(), target);
            }
        }

        return copy;
    }
473729
public static void copyFromTo(Object from, Object to, String fieldName) {

        if (from == null || to == null) {
            log.info("object deep copy : from or to is null ");
            return;
        }

        try {
            Method getter = getGetterMethod(from.getClass(), fieldName);

            if (getter == null) {
                //log.info("getter method not found : " + fieldName);
                return;
            }

            Method setter = getSetterMethod(to.getClass(), fieldName, getter.getReturnType());
            if (setter == null) {
                //log.info("setter method not found : " + fieldName);
                return;
            }

            setter.invoke(to, getter.invoke(from, EMPTY_CLASS));

        } catch (IllegalAccessException | InvocationTargetException e) {
            log.info("set method invoke error : " + fieldName);
        }
    }
918484
public MapField<K, V> copy() {
        return new MapField<K, V>(converter, StorageMode.MAP, MapFieldLite.copy(getMap()), null);
    }
1039413
public static void copyPropertiesToMap(Properties source,Map<String,String> target)
    {
        if(source!=null)
        {
            //convert to map
            Iterator<Entry<Object,Object>> iterator=source.entrySet().iterator();
            Entry<Object,Object> entry=null;
            String key=null;
            String value=null;
            while(iterator.hasNext())
            {
                //get next entry
                entry=iterator.next();
                
                //get next key/value
                key=(String)entry.getKey();
                value=(String)entry.getValue();
                
                //put in map
                target.put(key,value);
            }
        }
    }
770194
public static void populateColumnAndSuperColumnMaps(EntityMetadata m, Map<String, Field> columnNameToFieldMap,
            Map<String, Field> superColumnNameToFieldMap, final KunderaMetadata kunderaMetadata)
    {

        getEmbeddableType(m, columnNameToFieldMap, superColumnNameToFieldMap, kunderaMetadata);
    }
591125
public ItemDocument copy(ItemDocument object) {
		return dataObjectFactory.getItemDocument(
				copy(object.getEntityId()),
				copyMonoLingualTextValues(object.getLabels().values()),
				copyMonoLingualTextValues(object.getDescriptions().values()),
				copyAliasMap(object.getAliases()),
				copyStatementGroups(object.getStatementGroups()),
				copySiteLinks(object.getSiteLinks()),
				object.getRevisionId());
	}
107858
@Deprecated
  public static DatasetDescriptor fromDataMap(Map<String, String> dataMap) {
    DatasetDescriptor descriptor = new DatasetDescriptor(dataMap.get(PLATFORM_KEY), dataMap.get(NAME_KEY));
    dataMap.forEach((key, value) -> {
      if (!key.equals(PLATFORM_KEY) && !key.equals(NAME_KEY)) {
        descriptor.addMetadata(key, value);
      }
    });
    return descriptor;
  }
608394
public static <K, V> Map<K, V> copyOf(Map<K, V> map) {
        Preconditions.checkNotNull(map);
        return ImmutableMap.<K, V> builder().putAll(map).build();
    }
874673
public static <T> BeanCopier<T> create(Object source, T dest, Type destType, CopyOptions copyOptions) {
		return new BeanCopier<>(source, dest, destType, copyOptions);
	}
809002
@Override
	public Map<String, Object> parseMap(InputStream source) {
		Map<String, Object> map = new LinkedHashMap<>();

		try (ParserWrapper parserWrapper = createParser(source)) {
			Map<String, Object> result = BindMapHelper.parseMap(this, parserWrapper, map);
			return result;
		} catch (Exception e) {
			e.printStackTrace();
			throw new KriptonRuntimeException(e);
		}
	}
1119403
@SuppressWarnings("unchecked")
    <T> T deduplicate(T object) {
        if (deduplicateMap.containsKey(object) == false) {
            deduplicateMap.put(object, object);
        }
        return (T) deduplicateMap.get(object);
    }
216385
@Cmd
	public void copyFormEntry(final String srcDataSetKey, final String srcKey, final String destDataSetKey, final String destKey) {
		Map<String, DataSet> currentDataSets = dataSourceProvider.get().getCurrentDataSets();
		DataSet srcDataSet = currentDataSets.get(srcDataSetKey);
		DataSet destDataSet = currentDataSets.get(destDataSetKey);
		destDataSet.setFixedValue(destKey, srcDataSet.getValue(srcKey));
	}
1143012
function copyDeep(source) {
    for (var
      key, descriptor, value,
      target = create(gPO(source)),
      names = oK(source),
      i = 0; i < names.length; i++
    ) {
      key = names[i];
      descriptor = gOPD(source, key);
      if (hOP.call(descriptor, VALUE)) {
        copyValueIfObject(descriptor, copyDeep);
      }
      defineProperty(target, key, descriptor);
    }
    return target;
  }