Skip to content

Commit

Permalink
Generate is-getters for primitive booleans, fix #273
Browse files Browse the repository at this point in the history
  • Loading branch information
julgus committed Sep 2, 2022
1 parent 4f9e455 commit 8c79f3c
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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
Expand All @@ -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()) {
Expand All @@ -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
Expand All @@ -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)) {
Expand Down Expand Up @@ -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 + "\"."
)));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<java.lang.String>";
Expand All @@ -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);
}

}
6 changes: 6 additions & 0 deletions fieldgenerator/fieldgenerator-test/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,12 @@
<scope>provided</scope>
</dependency>

<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<version>2.0.0.Final</version>
</dependency>

</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}

0 comments on commit 8c79f3c

Please sign in to comment.