转换 Java 源代码以将注释从 getter 移动到属性



我有大约 300 个 JPA 实体,其中 getter 使用持久性注释进行注释。我想找到一种方法来将所有此类注释移动到属性中,并删除所有 getter 和 setter。我为大约 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;
}

Spoon 可以很好地解决这个问题,你可以使用 aField.addAnnotation 和 aSetter.delete。

我最终使用了Spoon。没那么痛苦。我配置了他们的 maven 插件来运行我的处理器,它改变了我的实体类的代码。然后,我将生成的代码复制回我的项目并删除了插件配置。

这是我的处理器代码:

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;
}
}

相关内容

  • 没有找到相关文章

最新更新