1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382
| package org.liuyehcf.reflect;
import java.lang.reflect.*; import java.util.*;
public class JavaBeanInitializerUtils { private static final Byte BYTE_DEFAULT_VALUE = 1; private static final Character CHAR_DEFAULT_VALUE = 'a'; private static final Short SHORT_DEFAULT_VALUE = 2; private static final Integer INTEGER_DEFAULT_VALUE = 3; private static final Long LONG_DEFAULT_VALUE = 6L; private static final Float FLOAT_DEFAULT_VALUE = 1.0F; private static final Double DOUBLE_DEFAULT_VALUE = 2.0D; private static final String STRING_DEFAULT_VALUE = "default";
private static final Map<Class, Object> DEFAULT_VALUE_OF_BASIC_CLASS = new HashMap<>();
private static final String SET_METHOD_PREFIX = "set"; private static final Integer SET_METHOD_PARAM_COUNT = 1; private static final Class SET_METHOD_RETURN_TYPE = void.class;
private static final Set<Class> CONTAINER_CLASS_SET = new HashSet<>(); private static final Integer CONTAINER_DEFAULT_SIZE = 3;
static { DEFAULT_VALUE_OF_BASIC_CLASS.put(Byte.class, BYTE_DEFAULT_VALUE); DEFAULT_VALUE_OF_BASIC_CLASS.put(byte.class, BYTE_DEFAULT_VALUE);
DEFAULT_VALUE_OF_BASIC_CLASS.put(Character.class, CHAR_DEFAULT_VALUE); DEFAULT_VALUE_OF_BASIC_CLASS.put(char.class, CHAR_DEFAULT_VALUE);
DEFAULT_VALUE_OF_BASIC_CLASS.put(Short.class, SHORT_DEFAULT_VALUE); DEFAULT_VALUE_OF_BASIC_CLASS.put(short.class, SHORT_DEFAULT_VALUE);
DEFAULT_VALUE_OF_BASIC_CLASS.put(Integer.class, INTEGER_DEFAULT_VALUE); DEFAULT_VALUE_OF_BASIC_CLASS.put(int.class, INTEGER_DEFAULT_VALUE);
DEFAULT_VALUE_OF_BASIC_CLASS.put(Long.class, LONG_DEFAULT_VALUE); DEFAULT_VALUE_OF_BASIC_CLASS.put(long.class, LONG_DEFAULT_VALUE);
DEFAULT_VALUE_OF_BASIC_CLASS.put(Float.class, FLOAT_DEFAULT_VALUE); DEFAULT_VALUE_OF_BASIC_CLASS.put(float.class, FLOAT_DEFAULT_VALUE);
DEFAULT_VALUE_OF_BASIC_CLASS.put(Double.class, DOUBLE_DEFAULT_VALUE); DEFAULT_VALUE_OF_BASIC_CLASS.put(double.class, DOUBLE_DEFAULT_VALUE);
DEFAULT_VALUE_OF_BASIC_CLASS.put(Boolean.class, false); DEFAULT_VALUE_OF_BASIC_CLASS.put(boolean.class, false); }
static { CONTAINER_CLASS_SET.add(List.class); CONTAINER_CLASS_SET.add(Map.class); CONTAINER_CLASS_SET.add(Set.class); CONTAINER_CLASS_SET.add(Queue.class); }
private final Type type;
private Map<String, Type> genericTypes;
private JavaBeanInitializerUtils(Type type, Map<String, Type> superClassGenericTypes) { this.type = type; genericTypes = new HashMap<>(); init(superClassGenericTypes); }
@SuppressWarnings("unchecked") public static <T> T createJavaBean(TypeReference<T> typeReference) { if (typeReference == null) { throw new NullPointerException(); } return (T) createJavaBean(typeReference.getType(), null, null); }
private static Object createJavaBean(Type type, Map<String, Type> superClassGenericTypes, Type superType) { if (type == null) { throw new NullPointerException(); } if (type.equals(superType)) { return null; } return new JavaBeanInitializerUtils(type, superClassGenericTypes) .doCreateJavaBean(); }
private void init(Map<String, Type> superClassGenericTypes) { if (type instanceof ParameterizedType) { ParameterizedType parameterizedType = (ParameterizedType) type;
Class clazz = (Class) parameterizedType.getRawType();
TypeVariable[] typeVariables = clazz.getTypeParameters();
Type[] actualTypeArguments = parameterizedType.getActualTypeArguments(); for (int i = 0; i < actualTypeArguments.length; i++) { Type actualTypeArgument = actualTypeArguments[i]; if (actualTypeArgument instanceof TypeVariable) { if (superClassGenericTypes == null || (actualTypeArgument = superClassGenericTypes.get(getNameOfTypeVariable(actualTypeArgument))) == null) { throw new RuntimeException(); } actualTypeArguments[i] = actualTypeArgument; } }
for (int i = 0; i < typeVariables.length; i++) { genericTypes.put( getNameOfTypeVariable(typeVariables[i]), actualTypeArguments[i] ); } } }
private String getNameOfTypeVariable(Type typeVariable) { return ((TypeVariable) typeVariable).getName(); }
private Object doCreateJavaBean() { if (type instanceof Class) { return createJavaBeanWithClass((Class) type); } else if (type instanceof ParameterizedType) { return createJavaBeanWithGenericType((ParameterizedType) type); } else { throw new UnsupportedOperationException("暂不支持此类型的默认初始化,type: " + type); } }
private Object createJavaBeanWithClass(Class clazz) {
if (DEFAULT_VALUE_OF_BASIC_CLASS.containsKey(clazz)) { return DEFAULT_VALUE_OF_BASIC_CLASS.get(clazz); } else if (String.class.equals(clazz)) { return STRING_DEFAULT_VALUE; }
Object obj = createInstance(clazz);
for (Method setMethod : getSetMethods(clazz)) {
Type paramType = setMethod.getGenericParameterTypes()[0];
setDefaultValue(obj, setMethod, paramType); }
return obj; }
private Object createJavaBeanWithGenericType(ParameterizedType type) {
Class clazz = (Class) type.getRawType();
Object obj = createInstance(clazz);
for (Method setMethod : getSetMethods(clazz)) { Type paramType = setMethod.getGenericParameterTypes()[0];
if (paramType instanceof TypeVariable) { Type actualType = genericTypes.get(getNameOfTypeVariable(paramType)); setDefaultValue(obj, setMethod, actualType); } else { setDefaultValue(obj, setMethod, paramType); } }
return obj; }
private Object createInstance(Class clazz) { Object obj; try { obj = clazz.newInstance(); } catch (IllegalAccessException | InstantiationException e) { throw new RuntimeException("不能实例化接口/抽象类/没有无参构造方法的类"); } return obj; }
private List<Method> getSetMethods(Class clazz) { List<Method> setMethods = new ArrayList<>(); Method[] methods = clazz.getMethods();
for (Method method : methods) { if (method.getName().startsWith(SET_METHOD_PREFIX) && SET_METHOD_PARAM_COUNT.equals(method.getParameterCount()) && SET_METHOD_RETURN_TYPE.equals(method.getReturnType())) { setMethods.add(method); } } return setMethods; }
private void setDefaultValue(Object obj, Method method, Type paramType) { try { if (paramType instanceof Class) { setDefaultValueOfNormal(obj, method, (Class) paramType); } else if (paramType instanceof ParameterizedType) { setDefaultValueOfGeneric(obj, method, (ParameterizedType) paramType); } else { throw new UnsupportedOperationException(); } } catch (IllegalAccessException | InvocationTargetException e) { throw new RuntimeException(); } }
private String getFieldName(Method method) { return method.getName().substring(3); }
private void setDefaultValueOfNormal(Object obj, Method method, Class paramClass) throws IllegalAccessException, InvocationTargetException { if (DEFAULT_VALUE_OF_BASIC_CLASS.containsKey(paramClass)) { method.invoke(obj, DEFAULT_VALUE_OF_BASIC_CLASS.get(paramClass)); } else if (String.class.equals(paramClass)) { method.invoke(obj, STRING_DEFAULT_VALUE + getFieldName(method)); } else { method.invoke(obj, createJavaBean(paramClass, genericTypes, type)); } }
private void setDefaultValueOfGeneric(Object obj, Method method, ParameterizedType paramType) throws IllegalAccessException, InvocationTargetException { Class clazz = (Class) paramType.getRawType();
if (instanceOfContainer(clazz)) { setDefaultValueForContainer(obj, method, paramType); } else { method.invoke(obj, createJavaBean(paramType, genericTypes, type)); } }
private boolean instanceOfContainer(Class clazz) { return CONTAINER_CLASS_SET.contains(clazz); }
@SuppressWarnings("unchecked") private void setDefaultValueForContainer(Object obj, Method method, ParameterizedType paramType) throws IllegalAccessException, InvocationTargetException { Class clazz = (Class) paramType.getRawType();
if (List.class.equals(clazz)) { List list = new ArrayList();
Type genericParam = paramType.getActualTypeArguments()[0];
for (int i = 0; i < CONTAINER_DEFAULT_SIZE; i++) { list.add(createJavaBeanWithTypeVariable(genericParam)); }
method.invoke(obj, list); } else if (Set.class.equals(clazz)) { Set set = new HashSet();
Type genericParam = paramType.getActualTypeArguments()[0];
for (int i = 0; i < CONTAINER_DEFAULT_SIZE; i++) { set.add(createJavaBeanWithTypeVariable(genericParam)); }
method.invoke(obj, set); } else if (Queue.class.equals(clazz)) { Queue queue = new LinkedList();
Type genericParam = paramType.getActualTypeArguments()[0];
for (int i = 0; i < CONTAINER_DEFAULT_SIZE; i++) { queue.add(createJavaBeanWithTypeVariable(genericParam)); }
method.invoke(obj, queue); } else if (Map.class.equals(clazz)) { Map map = new HashMap();
Type genericParam1 = paramType.getActualTypeArguments()[0]; Type genericParam2 = paramType.getActualTypeArguments()[1];
Object key; Object value;
for (int i = 0; i < CONTAINER_DEFAULT_SIZE; i++) { key = createJavaBeanWithTypeVariable(genericParam1); value = createJavaBeanWithTypeVariable(genericParam2);
map.put(key, value); }
method.invoke(obj, map); } else { throw new UnsupportedOperationException(); } }
private Object createJavaBeanWithTypeVariable(Type type) { if (type instanceof TypeVariable) { return createJavaBean(genericTypes.get(getNameOfTypeVariable(type)), genericTypes, this.type); } else { return createJavaBean(type, genericTypes, this.type); } }
public static abstract class TypeReference<T> { private final Type type;
protected TypeReference() { Type superClass = getClass().getGenericSuperclass();
Type type = ((ParameterizedType) superClass).getActualTypeArguments()[0];
this.type = type; }
public final Type getType() { return type; } } }
|