Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* Copyright (c) 2014-2015 VMware, Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed
* under the License is distributed on an "AS IS" BASIS, without warranties or
* conditions of any kind, EITHER EXPRESS OR IMPLIED. See the License for the
* specific language governing permissions and limitations under the License.
*/
package com.vmware.xenon.common;
import static java.util.function.Function.identity;
import static java.util.stream.Collectors.toMap;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.SortedSet;
import java.util.TreeSet;
import java.util.concurrent.ConcurrentHashMap;
import com.vmware.xenon.common.ServiceDocumentDescription.PropertyDescription;
public final class ReflectionUtils {
private static final ConcurrentHashMap, Map> DECLARED_FIELDS_CACHE = new ConcurrentHashMap<>();
private ReflectionUtils() {
}
public static T instantiate(Class clazz) {
try {
Constructor ctor = clazz.getDeclaredConstructor();
if (!ctor.isAccessible()) {
ctor.setAccessible(true);
}
return ctor.newInstance();
} catch (Exception e) {
Utils.logWarning("Reflection error: %s", Utils.toString(e));
}
return null;
}
public static Object getPropertyValue(PropertyDescription pd, Object instance) {
try {
return pd.accessor.get(instance);
} catch (Exception e) {
Utils.logWarning("Reflection error: %s", Utils.toString(e));
}
return null;
}
public static void setPropertyValue(PropertyDescription pd, Object instance, Object value) {
try {
pd.accessor.set(instance, value);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
@SuppressWarnings("unchecked")
/**
* This method sets or updates the value of a collection or map
* If a property with this name does not exist, the object passed in is assigned
* If a property with the same name exists, the input object is merged with the
* existing input. For collections, the behavior of the merge will depend on the type
* of collection - for lists, the new values are appended to the existing collection
* and the new values replace existing entries for a set. If a property is a map
* it will delete all key/value pairs where the value is null.
* @param pd
* @param instance
* @param value
* @return {@code true} if the property value was changed
*/
public static boolean setOrUpdatePropertyValue(PropertyDescription pd, Object instance,
Object value) {
try {
boolean hasValueChanged = false;
Object currentObj = pd.accessor.get(instance);
if (currentObj != null) {
if (currentObj instanceof Collection) {
Collection