我需要为枚举类生成下一个 java.class 参数:
public enum ServiceType {
//....
private final Class<?> mClass; //<<how?
ServiceType(final Class<?> clazz) {
this.mClass = clazz;
}
public Class<?> getCurrentClass() {
return mClass;
}
//...
}
我没有找到 - 如何制作这个字段。
我生成此类的代码:
File path = new File(Path.ROOT + ENUM_PATH);
TypeSpec.Builder servicesEnumBuilder = TypeSpec.enumBuilder(ENUM_CLASS_NAME)
.addModifiers(Modifier.PUBLIC);
for (Map.Entry<String, String> entry: foundedServices.entrySet()) {
servicesEnumBuilder.addEnumConstant(
entry.getKey().toUpperCase() + ENUM_SUFFIX,
TypeSpec.anonymousClassBuilder("$T", ClassName.get(entry.getValue() , entry.getKey())).build());
}
servicesEnumBuilder.addField(); //<< how add this field: private final Class<?> mClass;