diff --git a/fieldgenerator/fieldgenerator-standard/src/main/java/com/speedment/jpastreamer/fieldgenerator/internal/InternalFieldGeneratorProcessor.java b/fieldgenerator/fieldgenerator-standard/src/main/java/com/speedment/jpastreamer/fieldgenerator/internal/InternalFieldGeneratorProcessor.java index 8fdfb2fc..388ac441 100644 --- a/fieldgenerator/fieldgenerator-standard/src/main/java/com/speedment/jpastreamer/fieldgenerator/internal/InternalFieldGeneratorProcessor.java +++ b/fieldgenerator/fieldgenerator-standard/src/main/java/com/speedment/jpastreamer/fieldgenerator/internal/InternalFieldGeneratorProcessor.java @@ -10,6 +10,7 @@ * * See: https://github.com/speedment/jpa-streamer/blob/master/LICENSE */ + package com.speedment.jpastreamer.fieldgenerator.internal; import com.speedment.common.codegen.Generator; @@ -31,6 +32,7 @@ import javax.annotation.processing.ProcessingEnvironment; import javax.annotation.processing.RoundEnvironment; import javax.lang.model.element.*; +import javax.lang.model.type.TypeKind; import javax.lang.model.type.TypeMirror; import javax.lang.model.util.Elements; import javax.lang.model.util.Types; @@ -126,9 +128,6 @@ void generateFields(final Element annotatedElement, final Writer writer) throws // todo: Filter out methods only returning boolean or Boolean .map(Element::getSimpleName) .map(Object::toString) - .filter(n -> n.startsWith(IS_PREFIX)) - .map(n -> n.substring(2)) - .map(Formatting::lcfirst) .collect(toSet()); // Retrieve all declared non-final instance fields of the annotated class @@ -149,10 +148,6 @@ void generateFields(final Element annotatedElement, final Writer writer) throws throw new UnsupportedEncodingException(isGetters.toString()); }*/ - - //messager.printMessage(Diagnostic.Kind.NOTE, annotatedElement.getSimpleName().toString() + " " +isGetters.size()); - - final PackageElement packageElement = processingEnvironment.getElementUtils().getPackageOf(annotatedElement); String packageName; if (packageElement.isUnnamed()) { @@ -172,6 +167,8 @@ private String findGetter(final Element field, final String entityName, boolean lombokGetterAvailable) { + + // Only looks for non-Lombok getters and is-getters final String fieldName = field.getSimpleName().toString(); final String getterPrefix = isGetters.contains(fieldName) ? IS_PREFIX @@ -183,12 +180,21 @@ private String findGetter(final Element field, final Element standardGetter = getters.get(standardGetterName); - if (standardGetter != null || lombokGetterAvailable) { + if (standardGetter != null) { // We got lucky because the user elected to conform // to the standard JavaBean notation. return entityName + "::" + standardGetterName; } + // Returns a Lombok getter if one exists + if (lombokGetterAvailable) { + TypeKind typeKind = field.asType().getKind(); + // Lombok generates is-getters for primitive booleans and standard getters for Boolean types + return (typeKind.isPrimitive() && typeKind == TypeKind.BOOLEAN) ? + entityName + "::" + IS_PREFIX + standardJavaName : + entityName + "::" + GET_PREFIX + standardJavaName; + } + final String lambdaName = lcfirst(entityName); if (!field.getModifiers().contains(Modifier.PROTECTED) && !field.getModifiers().contains(Modifier.PRIVATE)) { @@ -273,7 +279,7 @@ private void addFieldToClass(final Element field, fieldParams.toArray(new Value[0]) )) .set(Javadoc.of( - "This Field corresponds to the {@link " + entityName + "} field " + fieldName + "." + "This Field corresponds to the {@link " + entityName + "} field \"" + fieldName + "\"." ))); } diff --git a/fieldgenerator/fieldgenerator-standard/src/test/java/com/speedment/jpastreamer/fieldgenerator/internal/typeparser/TypeParserTest.java b/fieldgenerator/fieldgenerator-standard/src/test/java/com/speedment/jpastreamer/fieldgenerator/internal/typeparser/TypeParserTest.java index 84b9f204..242e20c5 100644 --- a/fieldgenerator/fieldgenerator-standard/src/test/java/com/speedment/jpastreamer/fieldgenerator/internal/typeparser/TypeParserTest.java +++ b/fieldgenerator/fieldgenerator-standard/src/test/java/com/speedment/jpastreamer/fieldgenerator/internal/typeparser/TypeParserTest.java @@ -120,7 +120,7 @@ void renderStringWithAnnotation() { Type actual = typeParser.render(type); assertEquals(expected, actual); } - + @Test void renderMapOfStringWithAnnotations() { String type = "@javax.validation.constraints.Email,@javax.validation.constraints.Size(max=255) java.util.Map"; @@ -139,4 +139,12 @@ void renderMapOfStringWithAnnotation() { assertEquals(expected, actual); } + @Test + void renderWithNotEmptyAnnotation() { + String type = "@javax.validation.constraints.NotEmpty java.lang.String"; + SimpleType expected = SimpleType.create("java.lang.String"); + Type actual = typeParser.render(type); + assertEquals(expected, actual); + } + } diff --git a/fieldgenerator/fieldgenerator-test/pom.xml b/fieldgenerator/fieldgenerator-test/pom.xml index 38095f2f..fb8257be 100644 --- a/fieldgenerator/fieldgenerator-test/pom.xml +++ b/fieldgenerator/fieldgenerator-test/pom.xml @@ -59,6 +59,12 @@ provided + + javax.validation + validation-api + 2.0.0.Final + + diff --git a/fieldgenerator/fieldgenerator-test/src/main/java/com/speedment/jpastreamer/fieldgenerator/test/User2.java b/fieldgenerator/fieldgenerator-test/src/main/java/com/speedment/jpastreamer/fieldgenerator/test/User2.java new file mode 100644 index 00000000..174627f0 --- /dev/null +++ b/fieldgenerator/fieldgenerator-test/src/main/java/com/speedment/jpastreamer/fieldgenerator/test/User2.java @@ -0,0 +1,36 @@ +package com.speedment.jpastreamer.fieldgenerator.test; + +import javax.persistence.*; +import javax.validation.constraints.NotEmpty; + +import java.io.Serializable; + +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.NoArgsConstructor; +@Entity +@Table(name="user") +@Data +@Builder +@NoArgsConstructor +@AllArgsConstructor + +public class User2 implements Serializable{ + @Id + @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "seq_shared") + @SequenceGenerator(name = "seq_shared", sequenceName = "seq_shared",allocationSize = 1) + Long id; + + @NotEmpty + private String username; + + @NotEmpty + private String password; + + // Should generate isEnabled + private boolean enabled; + + // Should generate getActive + private Boolean active; +}