Skip to content

Commit

Permalink
@Serdeable annotation added to generated entities.
Browse files Browse the repository at this point in the history
  • Loading branch information
dbalek committed Dec 7, 2023
1 parent 5b768b8 commit fa3c1db
Showing 1 changed file with 16 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,6 @@
import org.openide.filesystems.FileUtil;
import org.openide.loaders.DataObject;
import org.openide.loaders.TemplateWizard;
import org.openide.util.Cancellable;
import org.openide.util.NbBundle;

/**
Expand Down Expand Up @@ -465,6 +464,7 @@ private abstract class ClassGenerator {

protected final boolean generateJPA;
protected final boolean generateValidationConstraints;
protected final boolean generateSerdeable;

private ClassGenerator(WorkingCopy copy, EntityClass entityClass, boolean jpaSupported, boolean beanValidationSupported) throws IOException {
copy.toPhase(JavaSource.Phase.RESOLVED);
Expand All @@ -487,6 +487,7 @@ private ClassGenerator(WorkingCopy copy, EntityClass entityClass, boolean jpaSup
genUtils = GenerationUtils.newInstance(copy);
generateJPA = jpaSupported;
generateValidationConstraints = beanValidationSupported;
generateSerdeable = copy.getElements().getTypeElement("io.micronaut.serde.annotation.Serdeable") != null;
}

protected String createFieldName(String capitalizedFieldName) {
Expand Down Expand Up @@ -534,7 +535,7 @@ protected Property createProperty(EntityMember m) throws IOException {

List<ExpressionTree> columnAnnArguments = new ArrayList<>();
String memberName = m.getMemberName();
String memberType = getMemberType(m);
String memberType = m.getMemberType();

String columnName = dbMappings.getCMPFieldMapping().get(memberName);
if (!memberName.equalsIgnoreCase(columnName)){
Expand Down Expand Up @@ -571,19 +572,7 @@ protected Property createProperty(EntityMember m) throws IOException {
}

protected VariableTree createVariable(EntityMember m) {
return genUtils.createVariable(typeElement, m.getMemberName(), getMemberType(m));
}

String getMemberType(EntityMember m) {
String memberType = m.getMemberType();
if ("java.sql.Date".equals(memberType)) { //NOI18N
memberType = "java.util.Date";
} else if ("java.sql.Time".equals(memberType)) { //NOI18N
memberType = "java.util.Date";
} else if ("java.sql.Timestamp".equals(memberType)) { //NOI18N
memberType = "java.util.Date";
}
return memberType;
return genUtils.createVariable(typeElement, m.getMemberName(), m.getMemberType());
}

private boolean isCharacterType(String type) {
Expand Down Expand Up @@ -739,6 +728,9 @@ protected void initialize() throws IOException {
pkFieldName);
properties.add(pkProperty);
}
if (generateSerdeable) {
newClassTree = genUtils.addAnnotation(newClassTree, genUtils.createAnnotation("io.micronaut.serde.annotation.Serdeable")); //NOI18N
}
if (generateJPA) {
newClassTree = genUtils.addAnnotation(newClassTree, genUtils.createAnnotation("javax.persistence.Entity")); //NOI18N
if (dbMappings.getTableName() != null && !entityClassName.equalsIgnoreCase(dbMappings.getTableName())) {
Expand Down Expand Up @@ -784,9 +776,12 @@ protected void generateMember(EntityMember m) throws IOException {
protected void generateRelationship(RelationshipRole role) throws IOException {
String memberName = role.getFieldName();
if (role.isMany() && !role.isToMany()) {
String pkMemberName = getPkMemberName(beanMap.get(role.getParent().getRoleB().getEntityName()));
if (pkMemberName != null) {
memberName = pkMemberName;
String roleName = role.getRoleName();
if (roleName.endsWith("Id")) {
roleName = roleName.substring(0, roleName.length() - 2);
}
if (!roleName.isEmpty()) {
memberName = Character.toLowerCase(roleName.charAt(0)) + roleName.substring(1);
}
}
String typeName = getRelationshipFieldType(role, entityClass.getPackage());
Expand Down Expand Up @@ -970,6 +965,9 @@ public PKClassGenerator(WorkingCopy copy, EntityClass entityClass, boolean jpaSu
@Override
protected void initialize() throws IOException {
newClassTree = genUtils.ensureNoArgConstructor(newClassTree);
if (generateSerdeable) {
newClassTree = genUtils.addAnnotation(newClassTree, genUtils.createAnnotation("io.micronaut.serde.annotation.Serdeable")); //NOI18N
}
newClassTree = genUtils.addAnnotation(newClassTree, genUtils.createAnnotation(generateJPA ? "javax.persistence.Embeddable" : "io.micronaut.data.annotation.Embeddable")); // NOI18N
}

Expand Down

0 comments on commit fa3c1db

Please sign in to comment.