This commit is contained in:
@@ -0,0 +1,83 @@
|
||||
package com.codechecker.redis.schema;
|
||||
|
||||
import com.github.javaparser.ast.body.FieldDeclaration;
|
||||
import com.github.javaparser.ast.expr.AnnotationExpr;
|
||||
import com.github.javaparser.ast.expr.MemberValuePair;
|
||||
import com.github.javaparser.ast.expr.NormalAnnotationExpr;
|
||||
import com.github.javaparser.ast.expr.SingleMemberAnnotationExpr;
|
||||
import com.github.javaparser.ast.expr.StringLiteralExpr;
|
||||
|
||||
/**
|
||||
* 处理 Fastjson / Jackson 序列化相关注解:字段忽略与字段名映射。
|
||||
*/
|
||||
public final class AnnotationSupport {
|
||||
|
||||
private AnnotationSupport() {
|
||||
}
|
||||
|
||||
/**
|
||||
* 字段是否参与序列化(未被 @JSONField(serialize=false)/@JsonIgnore 等排除)。
|
||||
*/
|
||||
public static boolean isSerialized(FieldDeclaration field) {
|
||||
for (AnnotationExpr annotation : field.getAnnotations()) {
|
||||
String name = annotation.getNameAsString();
|
||||
if (name.equals("JsonIgnore")) {
|
||||
return false;
|
||||
}
|
||||
if (name.equals("JSONField") && annotation instanceof NormalAnnotationExpr) {
|
||||
for (MemberValuePair pair : ((NormalAnnotationExpr) annotation).getPairs()) {
|
||||
if (pair.getNameAsString().equals("serialize")
|
||||
&& pair.getValue().toString().equals("false")) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 解析字段序列化后的 JSON 名称:优先 @JSONField(name=)/@JsonProperty(),否则用原字段名。
|
||||
*/
|
||||
public static String jsonName(FieldDeclaration field, String defaultName) {
|
||||
for (AnnotationExpr annotation : field.getAnnotations()) {
|
||||
String name = annotation.getNameAsString();
|
||||
if (name.equals("JsonProperty")) {
|
||||
String v = singleStringValue(annotation);
|
||||
if (v != null && !v.isEmpty()) {
|
||||
return v;
|
||||
}
|
||||
}
|
||||
if (name.equals("JSONField") && annotation instanceof NormalAnnotationExpr) {
|
||||
for (MemberValuePair pair : ((NormalAnnotationExpr) annotation).getPairs()) {
|
||||
if (pair.getNameAsString().equals("name")
|
||||
&& pair.getValue() instanceof StringLiteralExpr) {
|
||||
String v = ((StringLiteralExpr) pair.getValue()).asString();
|
||||
if (!v.isEmpty()) {
|
||||
return v;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return defaultName;
|
||||
}
|
||||
|
||||
private static String singleStringValue(AnnotationExpr annotation) {
|
||||
if (annotation instanceof SingleMemberAnnotationExpr) {
|
||||
if (((SingleMemberAnnotationExpr) annotation).getMemberValue() instanceof StringLiteralExpr) {
|
||||
return ((StringLiteralExpr) ((SingleMemberAnnotationExpr) annotation)
|
||||
.getMemberValue()).asString();
|
||||
}
|
||||
}
|
||||
if (annotation instanceof NormalAnnotationExpr) {
|
||||
for (MemberValuePair pair : ((NormalAnnotationExpr) annotation).getPairs()) {
|
||||
if (pair.getNameAsString().equals("value")
|
||||
&& pair.getValue() instanceof StringLiteralExpr) {
|
||||
return ((StringLiteralExpr) pair.getValue()).asString();
|
||||
}
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user