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
| public static JavaBeanInfo build(Class<?> clazz, Type type, PropertyNamingStrategy propertyNamingStrategy) { JSONType jsonType = clazz.getAnnotation(JSONType.class);
Class<?> builderClass = getBuilderClass(jsonType);
Field[] declaredFields = clazz.getDeclaredFields();//获取所有字段 Method[] methods = clazz.getMethods();//获取所有public方法
Constructor<?> defaultConstructor = getDefaultConstructor(builderClass == null ? clazz : builderClass); Constructor<?> creatorConstructor = null; Method buildMethod = null;
List<FieldInfo> fieldList = new ArrayList<FieldInfo>();
if (defaultConstructor == null && !(clazz.isInterface() || Modifier.isAbstract(clazz.getModifiers()))) { ... }
if (defaultConstructor != null) { TypeUtils.setAccessible(defaultConstructor); }
if (builderClass != null) { ... }
for (Method method : methods) { // int ordinal = 0, serialzeFeatures = 0, parserFeatures = 0; String methodName = method.getName(); if (methodName.length() < 4) { continue; }//方法名大于4
if (Modifier.isStatic(method.getModifiers())) { continue; }//非静态方法
// support builder set if (!(method.getReturnType().equals(Void.TYPE) || method.getReturnType().equals(method.getDeclaringClass()))) { continue; }//返回值是void 或 clazz类 Class<?>[] types = method.getParameterTypes(); if (types.length != 1) { continue; }//只有一个参数
...
if (!methodName.startsWith("set")) { // TODO "set"的判断放在 JSONField 注解后面,意思是允许非 setter 方法标记 JSONField 注解? continue; }//方法名以set开头
char c3 = methodName.charAt(3);
String propertyName; if (Character.isUpperCase(c3) //set后的第一个字符必须是大写或_或f || c3 > 512 // for unicode method name ) //获得set方法对应的字段 { if (TypeUtils.compatibleWithJavaBean) { propertyName = TypeUtils.decapitalize(methodName.substring(3)); } else { propertyName = Character.toLowerCase(methodName.charAt(3)) + methodName.substring(4); } } else if (c3 == '_') { propertyName = methodName.substring(4); } else if (c3 == 'f') { propertyName = methodName.substring(3); } else if (methodName.length() >= 5 && Character.isUpperCase(methodName.charAt(4))) { propertyName = TypeUtils.decapitalize(methodName.substring(3)); } else { continue; }
Field field = TypeUtils.getField(clazz, propertyName, declaredFields); if (field == null && types[0] == boolean.class) {//字段不存在或是bool型 String isFieldName = "is" + Character.toUpperCase(propertyName.charAt(0)) + propertyName.substring(1); field = TypeUtils.getField(clazz, isFieldName, declaredFields); }
...
add(fieldList, new FieldInfo(propertyName, method, field, clazz, type, ordinal, serialzeFeatures, parserFeatures, annotation, fieldAnnotation, null));//添加到fieldList }
for (Field field : clazz.getFields()) { // 添加非静态字段 int modifiers = field.getModifiers(); if ((modifiers & Modifier.STATIC) != 0) {//不允许STATIC continue; } if((modifiers & Modifier.FINAL) != 0) { Class<?> fieldType = field.getType(); boolean supportReadOnly = Map.class.isAssignableFrom(fieldType) || Collection.class.isAssignableFrom(fieldType) || AtomicLong.class.equals(fieldType) // || AtomicInteger.class.equals(fieldType) // || AtomicBoolean.class.equals(fieldType); if (!supportReadOnly) { continue; } }
boolean contains = false; for (FieldInfo item : fieldList) { if (item.name.equals(field.getName())) { contains = true; break; // 已经是 contains = true,无需继续遍历 } }
if (contains) { continue; }
int ordinal = 0, serialzeFeatures = 0, parserFeatures = 0; String propertyName = field.getName();
... add(fieldList, new FieldInfo(propertyName, null, field, clazz, type, ordinal, serialzeFeatures, parserFeatures, null, fieldAnnotation, null)); }
for (Method method : clazz.getMethods()) { // getter methods String methodName = method.getName(); if (methodName.length() < 4) { continue; }
if (Modifier.isStatic(method.getModifiers())) { continue; }
if (methodName.startsWith("get") && Character.isUpperCase(methodName.charAt(3))) { if (method.getParameterTypes().length != 0) { continue; }
if (Collection.class.isAssignableFrom(method.getReturnType()) // || Map.class.isAssignableFrom(method.getReturnType()) // || AtomicBoolean.class == method.getReturnType() // || AtomicInteger.class == method.getReturnType() // || AtomicLong.class == method.getReturnType() // ) { String propertyName;
... FieldInfo fieldInfo = getField(fieldList, propertyName); if (fieldInfo != null) { continue; }
if (propertyNamingStrategy != null) { propertyName = propertyNamingStrategy.translate(propertyName); } add(fieldList, new FieldInfo(propertyName, method, null, clazz, type, 0, 0, 0, annotation, null, null)); } } }
return new JavaBeanInfo(clazz, builderClass, defaultConstructor, null, null, buildMethod, jsonType, fieldList); }
|