Skip to content

Commit

Permalink
refactor(proto): #414 Enumeration supports custom value
Browse files Browse the repository at this point in the history
  • Loading branch information
iohao committed Dec 16, 2024
1 parent 2e85b99 commit c68e379
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@
*/
package com.iohao.game.widget.light.protobuf;

import com.baidu.bjf.remoting.protobuf.EnumReadable;
import com.baidu.bjf.remoting.protobuf.annotation.Ignore;
import com.baidu.bjf.remoting.protobuf.annotation.ProtobufClass;
import com.esotericsoftware.reflectasm.FieldAccess;
import com.iohao.game.common.consts.CommonConst;
import com.iohao.game.common.kit.ClassScanner;
import com.iohao.game.common.kit.StrKit;
import com.iohao.game.common.kit.io.FileKit;
Expand All @@ -31,7 +33,6 @@
import lombok.extern.slf4j.Slf4j;

import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.nio.charset.StandardCharsets;
Expand Down Expand Up @@ -93,7 +94,7 @@ private void javaProjectBuilder(String protoSourcePath) {
long count = annotations.stream().filter(annotation -> {
String string = annotation.getType().toString();
return string.contains(ProtobufClass.class.getName())
|| string.contains(ProtoFileMerge.class.getName());
|| string.contains(ProtoFileMerge.class.getName());
}).count();

if (count < 2) {
Expand Down Expand Up @@ -133,59 +134,44 @@ private List<ProtoJava> convert(List<Class<?>> classList) {

private void analyseField(ProtoJava protoJava) {
Class<?> clazz = protoJava.getClazz();
Field[] fields;

if (clazz.isEnum()) {
// 如果是枚举类型,需要单独处理,因为FieldAccess.get(clazz)方法中的Modifier.isStatic(modifiers)判断会过滤枚举内的属性
Field[] enumFields = clazz.getDeclaredFields();
List<Field> fieldList = new ArrayList<>(enumFields.length);

for (Field enumField : enumFields) {
if (Modifier.isPrivate(enumField.getModifiers())) {
// java的Enum有一个隐藏的$VALUES(private),需要过滤掉---枚举其他属性默认public static
continue;
}

fieldList.add(enumField);
}

fields = new Field[fieldList.size()];
for (int i = 0; i < fieldList.size(); i++) {
fields[i] = fieldList.get(i);
}
} else {
fields = FieldAccess.get(clazz).getFields();
}
Field[] fields = clazz.isEnum()
? Arrays.stream(clazz.getDeclaredFields()).filter(field -> field.getType().isEnum()).toArray(Field[]::new)
: FieldAccess.get(clazz).getFields();

JavaClass javaClass = protoJava.getJavaClass();
// 枚举 enum 的下标从 0 开始,message 的下标从 1 开始
int order = clazz.isEnum() ? 0 : 1;
var enumConstants = clazz.isEnum() ? clazz.getEnumConstants() : CommonConst.emptyObjects;

for (Field field : fields) {

for (int i = 0; i < fields.length; i++) {
var field = fields[i];
if (Objects.nonNull(field.getAnnotation(Ignore.class))) {
continue;
}

Class<?> fieldTypeClass = field.getType();
boolean repeated = List.class.equals(fieldTypeClass);

String fieldName = field.getName();
JavaField javaField = javaClass.getFieldByName(fieldName);

ProtoJavaField protoJavaField = new ProtoJavaField()
.setRepeated(repeated)
.setRepeated(List.class.equals(fieldTypeClass))
.setFieldName(fieldName)
.setComment(javaField.getComment())
.setOrder(order++)
.setFieldTypeClass(fieldTypeClass)
.setField(field)
.setProtoJavaParent(protoJava);

// 自定义枚举值
if (clazz.isEnum() && EnumReadable.class.isAssignableFrom(clazz)) {
if (enumConstants[i] instanceof EnumReadable r) {
protoJavaField.setOrder(r.value());
}
}

protoJava.addProtoJavaFiled(protoJavaField);

String fieldProtoType = ProtoFieldTypeHolder.me().getProtoType(fieldTypeClass);

if (Objects.nonNull(fieldProtoType)) {
protoJavaField.setFieldProtoType(fieldProtoType);
continue;
Expand All @@ -198,7 +184,6 @@ private void analyseField(ProtoJava protoJava) {
} else {
processFieldProtoJava(protoJavaField);
}

}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,7 @@
import com.baidu.bjf.remoting.protobuf.EnumReadable;
import com.baidu.bjf.remoting.protobuf.annotation.ProtobufClass;
import com.iohao.game.widget.light.protobuf.ProtoFileMerge;
import lombok.AccessLevel;
import lombok.ToString;
import lombok.experimental.FieldDefaults;

/**
* TestAnimalTypeEnum
Expand All @@ -36,7 +34,9 @@
@ProtobufClass
@ProtoFileMerge(fileName = TempProtoFile.fileName, filePackage = TempProtoFile.filePackage)
public enum AnimalTypeEnum implements EnumReadable {
/** the cat */
cat(0),
/** the tiger */
tiger(10),
;

Expand Down

0 comments on commit c68e379

Please sign in to comment.