Skip to content

Commit

Permalink
bug fixed for JKD 5、6、7 compatible
Browse files Browse the repository at this point in the history
  • Loading branch information
CodePlayer committed Mar 16, 2016
1 parent 7bce02b commit ae2b5d6
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 38 deletions.
Expand Up @@ -108,7 +108,7 @@ public Object createInstance(DefaultJSONParser parser, Type type) {
Object object;
try {
Constructor<?> constructor = beanInfo.getDefaultConstructor();
if (constructor.getParameterCount() == 0) {
if (constructor.getParameterTypes().length == 0) {
object = constructor.newInstance();
} else {
object = constructor.newInstance(parser.getContext().getObject());
Expand Down
Expand Up @@ -127,19 +127,18 @@ private Throwable createException(String message, Throwable cause, Class<?> exCl
Constructor<?> messageConstructor = null;
Constructor<?> causeConstructor = null;
for (Constructor<?> constructor : exClass.getConstructors()) {
if (constructor.getParameterCount() == 0) {
Class<?>[] types = constructor.getParameterTypes();
if (types.length == 0) {
defaultConstructor = constructor;
continue;
}

if (constructor.getParameterCount() == 1 && constructor.getParameterTypes()[0] == String.class) {
if (types.length == 1 && types[0] == String.class) {
messageConstructor = constructor;
continue;
}

Class<?>[] types;
if (constructor.getParameterCount() == 2 && (types = constructor.getParameterTypes())[0] == String.class
&& types[1] == Throwable.class) {
if (types.length == 2 && types[0] == String.class && types[1] == Throwable.class) {
causeConstructor = constructor;
continue;
}
Expand Down
31 changes: 15 additions & 16 deletions src/main/java/com/alibaba/fastjson/util/DeserializeBeanInfo.java
Expand Up @@ -142,10 +142,10 @@ public static DeserializeBeanInfo computeSetters(Class<?> clazz, Type type) {
TypeUtils.setAccessible(creatorConstructor);
beanInfo.setCreatorConstructor(creatorConstructor);

int count = creatorConstructor.getParameterCount();
if (count > 0) {
Class<?>[] types = creatorConstructor.getParameterTypes();
if (types.length > 0) {
Annotation[][] paramAnnotationArrays = creatorConstructor.getParameterAnnotations();
for (int i = 0; i < count; ++i) {
for (int i = 0; i < types.length; ++i) {
Annotation[] paramAnnotations = paramAnnotationArrays[i];
JSONField fieldAnnotation = null;
for (Annotation paramAnnotation : paramAnnotations) {
Expand All @@ -157,7 +157,7 @@ public static DeserializeBeanInfo computeSetters(Class<?> clazz, Type type) {
if (fieldAnnotation == null) {
throw new JSONException("illegal json creator");
}
Class<?> fieldClass = creatorConstructor.getParameterTypes()[i];
Class<?> fieldClass = types[i];
Type fieldType = creatorConstructor.getGenericParameterTypes()[i];
Field field = TypeUtils.getField(clazz, fieldAnnotation.name());
final int ordinal = fieldAnnotation.ordinal();
Expand All @@ -174,11 +174,10 @@ public static DeserializeBeanInfo computeSetters(Class<?> clazz, Type type) {
if (factoryMethod != null) {
TypeUtils.setAccessible(factoryMethod);
beanInfo.setFactoryMethod(factoryMethod);

int count = factoryMethod.getParameterCount();
if (count > 0) {
Class<?>[] types = factoryMethod.getParameterTypes();
if (types.length > 0) {
Annotation[][] paramAnnotationArrays = factoryMethod.getParameterAnnotations();
for (int i = 0; i < count; ++i) {
for (int i = 0; i < types.length; ++i) {
Annotation[] paramAnnotations = paramAnnotationArrays[i];
JSONField fieldAnnotation = null;
for (Annotation paramAnnotation : paramAnnotations) {
Expand All @@ -191,7 +190,7 @@ public static DeserializeBeanInfo computeSetters(Class<?> clazz, Type type) {
throw new JSONException("illegal json creator");
}

Class<?> fieldClass = factoryMethod.getParameterTypes()[i];
Class<?> fieldClass = types[i];
Type fieldType = factoryMethod.getGenericParameterTypes()[i];
Field field = TypeUtils.getField(clazz, fieldAnnotation.name());
final int ordinal = fieldAnnotation.ordinal();
Expand Down Expand Up @@ -328,8 +327,8 @@ public static DeserializeBeanInfo computeSetters(Class<?> clazz, Type type) {
if (!(method.getReturnType().equals(Void.TYPE) || method.getReturnType().equals(clazz))) {
continue;
}

if (method.getParameterCount() != 1) {
Class<?>[] types = method.getParameterTypes();
if (types.length != 1) {
continue;
}

Expand Down Expand Up @@ -380,7 +379,7 @@ public static DeserializeBeanInfo computeSetters(Class<?> clazz, Type type) {
}

Field field = TypeUtils.getField(clazz, propertyName);
if (field == null && method.getParameterTypes()[0] == boolean.class) {
if (field == null && types[0] == boolean.class) {
String isFieldName = "is" + Character.toUpperCase(propertyName.charAt(0)) + propertyName.substring(1);
field = TypeUtils.getField(clazz, isFieldName);
}
Expand Down Expand Up @@ -448,7 +447,7 @@ public static DeserializeBeanInfo computeSetters(Class<?> clazz, Type type) {
}

if (methodName.startsWith("get") && Character.isUpperCase(methodName.charAt(3))) {
if (method.getParameterCount() != 0) {
if (method.getParameterTypes().length != 0) {
continue;
}

Expand Down Expand Up @@ -489,17 +488,17 @@ public static Constructor<?> getDefaultConstructor(Class<?> clazz) {
final Constructor<?>[] constructors = clazz.getDeclaredConstructors();

for (Constructor<?> constructor : constructors) {
if (constructor.getParameterCount() == 0) { // getParameterTypes() 内部是调用数组的克隆方法,开销相对较大
if (constructor.getParameterTypes().length == 0) {
defaultConstructor = constructor;
break;
}
}

if (defaultConstructor == null) {
if (clazz.isMemberClass() && !Modifier.isStatic(clazz.getModifiers())) {
Class<?>[] types;
for (Constructor<?> constructor : constructors) {
if (constructor.getParameterCount() == 1
&& constructor.getParameterTypes()[0].equals(clazz.getDeclaringClass())) {
if ( (types = constructor.getParameterTypes()).length == 1 && types[0].equals(clazz.getDeclaringClass())) {
defaultConstructor = constructor;
break;
}
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/com/alibaba/fastjson/util/FieldInfo.java
Expand Up @@ -84,8 +84,9 @@ public FieldInfo(String name, Method method, Field field, Class<?> clazz, Type t
Type fieldType;
Class<?> fieldClass;
if (method != null) {
if (method.getParameterTypes().length == 1) {
fieldClass = method.getParameterTypes()[0];
Class<?>[] types;
if ((types = method.getParameterTypes()).length == 1) {
fieldClass = types[0];
fieldType = method.getGenericParameterTypes()[0];
} else {
fieldClass = method.getReturnType();
Expand Down
22 changes: 8 additions & 14 deletions src/main/java/com/alibaba/fastjson/util/TypeUtils.java
Expand Up @@ -1330,28 +1330,22 @@ public static List<FieldInfo> computeGetters(Class<?> clazz, Map<String, String>
public static JSONField getSupperMethodAnnotation(final Class<?> clazz, final Method method) {
Class<?>[] interfaces = clazz.getInterfaces();
if (interfaces.length > 0) {
int paramsCount = method.getParameterCount();
Class<?>[] methodParamTypes = null;
Class<?>[] types = method.getParameterTypes();
for (Class<?> interfaceClass : interfaces) {
for (Method interfaceMethod : interfaceClass.getMethods()) {
if (interfaceMethod.getParameterCount() != paramsCount) {
Class<?>[] interfaceTypes = interfaceMethod.getParameterTypes();
if (interfaceTypes.length != types.length) {
continue;
}
if (!interfaceMethod.getName().equals(method.getName())) {
continue;
}
boolean match = true;
if (paramsCount > 0) {
Class<?>[] interfaceTypes = interfaceMethod.getParameterTypes();
if (methodParamTypes == null) {
methodParamTypes = method.getParameterTypes();
}
for (int i = 0; i < paramsCount; ++i) {
if (!interfaceTypes[i].equals(methodParamTypes[i])) {
match = false;
break;
}
}
for (int i = 0; i < types.length; ++i) {
if (!interfaceTypes[i].equals(types[i])) {
match = false;
break;
}
}

if (!match) {
Expand Down

0 comments on commit ae2b5d6

Please sign in to comment.