我大约有300个JPA实体,其中的getter带有持久性注释。我想找到一种方法将所有这些注释移至属性,然后删除所有的获取器和设置器。我为大约100个此类手动进行了此操作,但是这非常耗时并且让人麻木。
我正在寻找Spoon之类的源代码转换工具,但仍不确定它可以完成我需要做的事情。
更具体地说,如何转换此代码:
@Entity
@Table(name = "crm_ticket")
public class CrmTicket implements Serializable {
private static final long serialVersionUID = -902718555957517699L;
private CrmAccount crmAccount;
private ItsType subType;
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "account")
public CrmAccount getCrmAccount() {
return crmAccount;
}
public void setCrmAccount(CrmAccount crmAccount) {
this.crmAccount = crmAccount;
}
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "sub_type")
public ItsType getSubType() {
return subType;
}
public void setSubType(ItsType type) {
this.subType = type;
}
}
对此:
@Entity
@Table(name = "crm_ticket")
@Data
public class CrmTicket implements Serializable {
private static final long serialVersionUID = -902718555957517699L;
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "account")
private CrmAccount crmAccount;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "sub_type")
private ItsType subType;
}
答案 0 :(得分:1)
勺子可以很好地工作,您可以使用aField。addAnnotation和aSetter。delete。
答案 1 :(得分:0)
我最终使用了Spoon。并不是那么痛苦。我配置了他们的maven插件来运行我的处理器,它转换了我的Entity类的代码。然后,我将生成的代码复制回我的项目,并删除了插件配置。
这是我的处理器代码:
public class JpaAnnotationMover extends AbstractProcessor<CtMethod> {
Pattern p1 = Pattern.compile("return.*this\\.(.*?)$");
Pattern p2 = Pattern.compile("return(.*?)$");
@Override
public boolean isToBeProcessed(CtMethod method) {
return isInEntity(method) && isAGetter(method) && hasOneStatement(method) && !isTransient(method);
}
@Override
public void process(CtMethod ctMethod) {
CtType parentClass = ctMethod.getParent(CtType.class);
String fieldName = getFieldName(ctMethod);
if (fieldName == null) {
log.warn(String.format("expected field name for %s not found.", ctMethod.getSimpleName()));
return;
}
CtField field = parentClass.getField(fieldName);
if (field == null) {
log.warn(String.format("Expected field %s not found.", fieldName));
return;
}
for (CtAnnotation<? extends Annotation> annotation : ctMethod.getAnnotations()) {
field.addAnnotation(annotation);
}
parentClass.removeMethod(ctMethod);
log.info(String.format("Processed method %s:%s", parentClass.getSimpleName(), ctMethod.getSimpleName()));
// find corresponding setter
String setterName = "set" + WordUtils.capitalize(fieldName);
@SuppressWarnings("unchecked") CtMethod setter = parentClass
.getMethod(getFactory().Type().createReference("void"), setterName, ctMethod.getType());
if (setter == null) {
log.warn(String.format("Unable to find setter for %s", fieldName));
return;
}
parentClass.removeMethod(setter);
if (!parentClass.hasAnnotation(Data.class)) {
parentClass.addAnnotation(getFactory().createAnnotation(getFactory().Type().createReference(Data.class)));
}
}
private Boolean isInEntity(CtMethod method) {
CtType parentClass = method.getParent(CtType.class);
return parentClass.hasAnnotation(Entity.class);
}
private Boolean isAGetter(CtMethod method) {
return method.getSimpleName().contains("get");
}
private Boolean hasOneStatement(CtMethod method) {
return method.getBody().getStatements().size() == 1;
}
private Boolean isTransient(CtMethod method) {
return method.hasAnnotation(Transient.class);
}
private String getFieldName(CtMethod method) {
String statement = method.getBody().getLastStatement().toString();
Matcher m = p1.matcher(statement);
Matcher m2 = p2.matcher(statement);
return m.matches() ? m.group(1).trim() : m2.matches() ? m2.group(1).trim() : null;
}
}