下面列出了com.alibaba.fastjson.annotation.JSONField#name ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private static String getJSONFieldName(Field field) {
JSONField annotation = field.getAnnotation(JSONField.class);
if(annotation!=null){
return annotation.name();
}
return UNKNOWN;
}
private static void computeFields(
Class<?> clazz, //
Map<String,String> aliasMap, //
PropertyNamingStrategy propertyNamingStrategy, //
Map<String,FieldInfo> fieldInfoMap, //
Field[] fields){
for(Field field : fields){
if(Modifier.isStatic(field.getModifiers())){
continue;
}
JSONField fieldAnnotation = field.getAnnotation(JSONField.class);
int ordinal = 0, serialzeFeatures = 0, parserFeatures = 0;
String propertyName = field.getName();
String label = null;
if(fieldAnnotation != null){
if(!fieldAnnotation.serialize()){
continue;
}
ordinal = fieldAnnotation.ordinal();
serialzeFeatures = SerializerFeature.of(fieldAnnotation.serialzeFeatures());
parserFeatures = Feature.of(fieldAnnotation.parseFeatures());
if(fieldAnnotation.name().length() != 0){
propertyName = fieldAnnotation.name();
}
if(fieldAnnotation.label().length() != 0){
label = fieldAnnotation.label();
}
}
if(aliasMap != null){
propertyName = aliasMap.get(propertyName);
if(propertyName == null){
continue;
}
}
if(propertyNamingStrategy != null){
propertyName = propertyNamingStrategy.translate(propertyName);
}
if(!fieldInfoMap.containsKey(propertyName)){
FieldInfo fieldInfo = new FieldInfo(propertyName, null, field, clazz, null, ordinal, serialzeFeatures, parserFeatures,
null, fieldAnnotation, label);
fieldInfoMap.put(propertyName, fieldInfo);
}
}
}
public static String[] lookupParameterNames(AccessibleObject methodOrCtor) {
if (IS_ANDROID) {
return new String[0];
}
final Class<?>[] types;
final Class<?> declaringClass;
final String name;
Annotation[][] parameterAnnotations;
if (methodOrCtor instanceof Method) {
Method method = (Method) methodOrCtor;
types = method.getParameterTypes();
name = method.getName();
declaringClass = method.getDeclaringClass();
parameterAnnotations = method.getParameterAnnotations();
} else {
Constructor<?> constructor = (Constructor<?>) methodOrCtor;
types = constructor.getParameterTypes();
declaringClass = constructor.getDeclaringClass();
name = "<init>";
parameterAnnotations = constructor.getParameterAnnotations();
}
if (types.length == 0) {
return new String[0];
}
ClassLoader classLoader = declaringClass.getClassLoader();
if (classLoader == null) {
classLoader = ClassLoader.getSystemClassLoader();
}
String className = declaringClass.getName();
String resourceName = className.replace('.', '/') + ".class";
InputStream is = classLoader.getResourceAsStream(resourceName);
if (is == null) {
return new String[0];
}
try {
ClassReader reader = new ClassReader(is, false);
TypeCollector visitor = new TypeCollector(name, types);
reader.accept(visitor);
String[] parameterNames = visitor.getParameterNamesForMethod();
for (int i = 0; i < parameterNames.length; i++) {
Annotation[] annotations = parameterAnnotations[i];
if (annotations != null) {
for (int j = 0; j < annotations.length; j++) {
if (annotations[j] instanceof JSONField) {
JSONField jsonField = (JSONField) annotations[j];
String fieldName = jsonField.name();
if (fieldName != null && fieldName.length() > 0) {
parameterNames[i] = fieldName;
}
}
}
}
}
return parameterNames;
} catch (IOException e) {
return new String[0];
} finally {
IOUtils.close(is);
}
}
private static void computeFields(Class<?> clazz, Type type, PropertyNamingStrategy propertyNamingStrategy, List<FieldInfo> fieldList, Field[] fields) {
for (Field field : fields) { // public static fields
int modifiers = field.getModifiers();
if ((modifiers & Modifier.STATIC) != 0) {
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();
JSONField fieldAnnotation = field.getAnnotation(JSONField.class);
if (fieldAnnotation != null) {
if (!fieldAnnotation.deserialize()) {
continue;
}
ordinal = fieldAnnotation.ordinal();
serialzeFeatures = SerializerFeature.of(fieldAnnotation.serialzeFeatures());
parserFeatures = Feature.of(fieldAnnotation.parseFeatures());
if (fieldAnnotation.name().length() != 0) {
propertyName = fieldAnnotation.name();
}
}
if (propertyNamingStrategy != null) {
propertyName = propertyNamingStrategy.translate(propertyName);
}
add(fieldList, new FieldInfo(propertyName, null, field, clazz, type, ordinal, serialzeFeatures, parserFeatures, null,
fieldAnnotation, null));
}
}