Rabbit-R1/switch port/java/sources/com/alibaba/fastjson/parser/EnumDeserializer.java

97 lines
3.4 KiB
Java
Raw Normal View History

2024-05-21 16:08:36 -05:00
package com.alibaba.fastjson.parser;
import com.alibaba.fastjson.JSONException;
import com.alibaba.fastjson.parser.deserializer.ObjectDeserializer;
import java.lang.reflect.Type;
import java.util.Arrays;
/* loaded from: classes2.dex */
public class EnumDeserializer implements ObjectDeserializer {
private final Class<?> enumClass;
protected long[] enumNameHashCodes;
protected final Enum[] enums;
protected final Enum[] ordinalEnums;
public EnumDeserializer(Class<?> cls) {
this.enumClass = cls;
Enum[] enumArr = (Enum[]) cls.getEnumConstants();
this.ordinalEnums = enumArr;
int length = enumArr.length;
long[] jArr = new long[length];
this.enumNameHashCodes = new long[enumArr.length];
int i = 0;
while (true) {
Enum[] enumArr2 = this.ordinalEnums;
if (i >= enumArr2.length) {
break;
}
long j = -3750763034362895579L;
for (int i2 = 0; i2 < enumArr2[i].name().length(); i2++) {
j = (j ^ r3.charAt(i2)) * 1099511628211L;
}
jArr[i] = j;
this.enumNameHashCodes[i] = j;
i++;
}
Arrays.sort(this.enumNameHashCodes);
this.enums = new Enum[this.ordinalEnums.length];
for (int i3 = 0; i3 < this.enumNameHashCodes.length; i3++) {
int i4 = 0;
while (true) {
if (i4 >= length) {
break;
}
if (this.enumNameHashCodes[i3] == jArr[i4]) {
this.enums[i3] = this.ordinalEnums[i4];
break;
}
i4++;
}
}
}
@Override // com.alibaba.fastjson.parser.deserializer.ObjectDeserializer
public <T> T deserialze(DefaultJSONParser defaultJSONParser, Type type, Object obj) {
try {
JSONLexer jSONLexer = defaultJSONParser.lexer;
int i = jSONLexer.token;
if (i == 2) {
int intValue = jSONLexer.intValue();
jSONLexer.nextToken(16);
if (intValue >= 0) {
Object[] objArr = this.ordinalEnums;
if (intValue <= objArr.length) {
return (T) objArr[intValue];
}
}
throw new JSONException("parse enum " + this.enumClass.getName() + " error, value : " + intValue);
}
if (i != 4) {
if (i == 8) {
jSONLexer.nextToken(16);
return null;
}
throw new JSONException("parse enum " + this.enumClass.getName() + " error, value : " + defaultJSONParser.parse());
}
String stringVal = jSONLexer.stringVal();
jSONLexer.nextToken(16);
if (stringVal.length() == 0) {
return null;
}
long j = -3750763034362895579L;
for (int i2 = 0; i2 < stringVal.length(); i2++) {
j = (j ^ stringVal.charAt(i2)) * 1099511628211L;
}
int binarySearch = Arrays.binarySearch(this.enumNameHashCodes, j);
if (binarySearch < 0) {
return null;
}
return (T) this.enums[binarySearch];
} catch (JSONException e) {
throw e;
} catch (Exception e2) {
throw new JSONException(e2.getMessage(), e2);
}
}
}