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 1999-2011 Alibaba Group.
*
* 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.taobao.hsf.util;
import java.lang.reflect.Array;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Hashtable;
import java.util.IdentityHashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.TreeMap;
import java.util.WeakHashMap;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.ConcurrentSkipListMap;
/**
* PojoUtils. Travel object deeply, and convert complex type to simple type.
*
* Simple type below will be remained:
*
*
Primitive Type, also include String, Number(Integer, Long), Date
*
Array of Primitive Type
*
Collection, eg: List, Map, Set etc.
*
*
* Other type will be covert to a map which contains the attributes and value pair of object.
*
* @author william.liangf
* @author ding.lid
*/
public class PojoUtils {
private static final ConcurrentMap, ConcurrentMap> NAME_METHODS_CACHE = new ConcurrentHashMap, ConcurrentMap>();
private static final ConcurrentMap, ConcurrentMap> CLASS_FIELD_CACHE = new ConcurrentHashMap, ConcurrentMap>();
private static boolean isUnSafeEnabled;
private static sun.misc.Unsafe unsafe;
private static List WARP_TYPE = new ArrayList();
static {
WARP_TYPE.add(Byte.class);
WARP_TYPE.add(Character.class);
WARP_TYPE.add(Short.class);
WARP_TYPE.add(Integer.class);
WARP_TYPE.add(Float.class);
WARP_TYPE.add(Double.class);
WARP_TYPE.add(Long.class);
}
static {
try {
unsafe = getUnsafe();
isUnSafeEnabled = unsafe != null;
} catch (Throwable e) {
}
}
public static Object[] generalize(Object[] objs) {
Object[] dests = new Object[objs.length];
for (int i = 0; i < objs.length; i++) {
dests[i] = generalize(objs[i]);
}
return dests;
}
public static Object[] realize(Object[] objs, Class>[] types) {
if (objs.length != types.length)
throw new IllegalArgumentException("args.length != types.length");
Object[] dests = new Object[objs.length];
for (int i = 0; i < objs.length; i++) {
dests[i] = realize(objs[i], types[i]);
}
return dests;
}
public static Object[] realize(Object[] objs, Class>[] types, Type[] gtypes) {
if (objs == null) {
return new Object[types.length];
}
if (objs.length != types.length || objs.length != gtypes.length)
throw new IllegalArgumentException("args.length != types.length");
Object[] dests = new Object[objs.length];
for (int i = 0; i < objs.length; i++) {
dests[i] = realize(objs[i], types[i], gtypes[i]);
}
return dests;
}
//remove class filed from json string
public static Object removeGeneralizedClassInfo(Object appResponse) {
return simplifyPojo(appResponse, true, false);
}
//util function for user to simply pojo
public static Object simplifyPojo(Object appResponse, boolean removeClass, boolean removeNull) {
if (!removeClass && !removeNull) {
return appResponse;
}
if (appResponse == null || ReflectUtils.isPrimitives(appResponse.getClass())) {
return appResponse;
}
if (appResponse.getClass().isArray()) {
int len = Array.getLength(appResponse);
Object[] dest = new Object[len];
for (int i = 0; i < len; i++) {
Object obj = Array.get(appResponse, i);
dest[i] = simplifyPojo(obj, removeClass, removeNull);
}
return dest;
}
if (appResponse instanceof Collection>) {
Collection