All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.alibaba.dubbo.common.utils.PojoUtils Maven / Gradle / Ivy

There is a newer version: 1.8.3
Show newest version
/*
 * 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.alibaba.dubbo.common.utils;

import java.lang.reflect.*;
import java.util.*;
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 NAME_METHODS_CACHE = new ConcurrentHashMap(); private static final ConcurrentMap, ConcurrentMap> CLASS_FIELD_CACHE = new ConcurrentHashMap, ConcurrentMap>(); 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.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; } public static Object generalize(Object pojo) { return generalize(pojo, new IdentityHashMap()); } @SuppressWarnings("unchecked") private static Object generalize(Object pojo, Map history) { return null; } public static Object realize(Object pojo, Class type) { return realize0(pojo, type, null , new IdentityHashMap()); } public static Object realize(Object pojo, Class type, Type genericType) { return realize0(pojo, type, genericType, new IdentityHashMap()); } private static class PojoInvocationHandler implements InvocationHandler { private Map map; public PojoInvocationHandler(Map map) { this.map = map; } @SuppressWarnings("unchecked") public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { if (method.getDeclaringClass() == Object.class) { return method.invoke(map, args); } String methodName = method.getName(); Object value = null; if (methodName.length() > 3 && methodName.startsWith("get")) { value = map.get(methodName.substring(3, 4).toLowerCase() + methodName.substring(4)); } else if (methodName.length() > 2 && methodName.startsWith("is")) { value = map.get(methodName.substring(2, 3).toLowerCase() + methodName.substring(3)); } else { value = map.get(methodName.substring(0, 1).toLowerCase() + methodName.substring(1)); } if (value instanceof Map && ! Map.class.isAssignableFrom(method.getReturnType())) { value = realize0((Map) value, method.getReturnType(), null, new IdentityHashMap()); } return value; } } @SuppressWarnings("unchecked") private static Collection createCollection(Class type, int len) { if (type.isAssignableFrom(ArrayList.class)) { return new ArrayList(len); } if (type.isAssignableFrom(HashSet.class)) { return new HashSet(len); } if (! type.isInterface() && ! Modifier.isAbstract(type.getModifiers())) { try { return (Collection) type.newInstance(); } catch (Exception e) { // ignore } } return new ArrayList(); } private static Map createMap(Map src) { Class cl = src.getClass(); Map result = null; if (HashMap.class == cl) { result = new HashMap(); } else if (Hashtable.class == cl) { result = new Hashtable(); } else if (IdentityHashMap.class == cl) { result = new IdentityHashMap(); } else if (LinkedHashMap.class == cl) { result = new LinkedHashMap(); } else if (Properties.class == cl) { result = new Properties(); } else if (TreeMap.class == cl) { result = new TreeMap(); } else if (WeakHashMap.class == cl) { return new WeakHashMap(); } else if (ConcurrentHashMap.class == cl) { result = new ConcurrentHashMap(); } else if (ConcurrentSkipListMap.class == cl) { result = new ConcurrentSkipListMap(); } else { try { result = cl.newInstance(); } catch (Exception e) { /* ignore */ } if (result == null) { try { Constructor constructor = cl.getConstructor(Map.class); result = (Map)constructor.newInstance(Collections.EMPTY_MAP); } catch (Exception e) { /* ignore */ } } } if (result == null) { result = new HashMap(); } return result; } @SuppressWarnings({ "unchecked", "rawtypes" }) private static Object realize0(Object pojo, Class type, Type genericType, final Map history) { return null; } /** * 获取范型的类型 * @param genericType * @param index * @return List 返回Person.class ,Map index=0 返回String.class index=1 返回Person.class */ private static Type getGenericClassByIndex(Type genericType, int index){ Type clazz = null ; //范型参数转换 if (genericType instanceof ParameterizedType){ ParameterizedType t = (ParameterizedType)genericType; Type[] types = t.getActualTypeArguments(); clazz = types[index]; } return clazz; } private static Object newInstance(Class cls) { try { return cls.newInstance(); } catch (Throwable t) { try { Constructor[] constructors = cls.getConstructors(); if (constructors != null && constructors.length == 0) { throw new RuntimeException("Illegal constructor: " + cls.getName()); } Constructor constructor = constructors[0]; if (constructor.getParameterTypes().length > 0) { for (Constructor c : constructors) { if (c.getParameterTypes().length < constructor.getParameterTypes().length) { constructor = c; if (constructor.getParameterTypes().length == 0) { break; } } } } return constructor.newInstance(new Object[constructor.getParameterTypes().length]); } catch (InstantiationException e) { throw new RuntimeException(e.getMessage(), e); } catch (IllegalAccessException e) { throw new RuntimeException(e.getMessage(), e); } catch (InvocationTargetException e) { throw new RuntimeException(e.getMessage(), e); } } } private static Method getSetterMethod(Class cls, String property, Class valueCls) { return null; } private static Field getField(Class cls, String fieldName) { return null; } public static boolean isPojo(Class cls) { return false; } }