From 114d0c3895e36678ad7c1ce810ff62792be47002 Mon Sep 17 00:00:00 2001 From: Per Minborg Date: Mon, 7 Sep 2020 12:43:48 +0200 Subject: [PATCH] Evaluate both "is" and "get" prefix for boolean getters, Fix #60 --- .../InternalFieldGeneratorProcessor.java | 76 +++++++++++++------ .../jpastreamer/fieldgenerator/test/User.java | 38 ++++++++++ 2 files changed, 90 insertions(+), 24 deletions(-) create mode 100644 fieldgenerator/fieldgenerator-test/src/main/java/com/speedment/jpastreamer/fieldgenerator/test/User.java 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 f2e36b64..b9e1719e 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 @@ -20,6 +20,7 @@ import com.speedment.common.codegen.model.Class; import com.speedment.common.codegen.model.Field; import com.speedment.common.codegen.model.*; +import com.speedment.common.codegen.util.Formatting; import com.speedment.jpastreamer.fieldgenerator.exception.FieldGeneratorProcessorException; import com.speedment.jpastreamer.fieldgenerator.internal.typeparser.TypeParser; import com.speedment.jpastreamer.field.*; @@ -38,12 +39,15 @@ import javax.tools.Diagnostic; import javax.tools.JavaFileObject; import java.io.IOException; +import java.io.UnsupportedEncodingException; import java.io.Writer; import java.lang.reflect.Type; import java.util.*; -import java.util.stream.Collectors; +import java.util.function.Function; import static com.speedment.common.codegen.util.Formatting.*; +import static java.util.stream.Collectors.toMap; +import static java.util.stream.Collectors.toSet; /** * JPAStreamer standard annotation processor that generates fields for classes annotated @@ -55,7 +59,8 @@ public final class InternalFieldGeneratorProcessor extends AbstractProcessor { - protected static final String GETTER_METHOD_PREFIX = "get"; + protected static final String GET_PREFIX = "get"; + protected static final String IS_PREFIX = "is"; private static final Generator generator = Generator.forJava(); @@ -80,7 +85,7 @@ public synchronized void init(ProcessingEnvironment env) { @Override public boolean process(Set annotations, RoundEnvironment roundEnv) { - if(annotations.size() == 0 || roundEnv.processingOver()) { + if (annotations.size() == 0 || roundEnv.processingOver()) { // Allow other processors to run return false; } @@ -94,7 +99,7 @@ public boolean process(Set annotations, RoundEnvironment Writer writer = builderFile.openWriter(); generateFields(ae, writer); writer.close(); - } catch(IOException e) { + } catch (IOException e) { e.printStackTrace(); } }); @@ -104,12 +109,37 @@ public boolean process(Set annotations, RoundEnvironment void generateFields(Element annotatedElement, Writer writer) throws IOException { + final Set isGetters = annotatedElement.getEnclosedElements().stream() + .filter(ee -> ee.getKind() == ElementKind.METHOD) + // Only consider methods with no parameters + .filter(ee -> ee.getEnclosedElements().stream().noneMatch(eee -> eee.getKind() == ElementKind.PARAMETER)) + .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 - Set enclosedFields = annotatedElement.getEnclosedElements().stream() + Map enclosedFields = annotatedElement.getEnclosedElements().stream() .filter(ee -> ee.getKind().isField() && !ee.getModifiers().contains(Modifier.STATIC) // Ignore static fields && !ee.getModifiers().contains(Modifier.FINAL)) // Ignore final fields - .collect(Collectors.toSet()); + .collect( + toMap( + Function.identity(), + ee -> isGetters.contains(ee.getSimpleName().toString()) ? IS_PREFIX : GET_PREFIX) + ); + +/* if (annotatedElement.getSimpleName().toString().contains("User")) { + messager.printMessage(Diagnostic.Kind.NOTE, " "+ isGetters.size()); + messager.printMessage(Diagnostic.Kind.NOTE, " "+ isGetters.iterator().next()); + messager.printMessage(Diagnostic.Kind.NOTE, enclosedFields.toString()); + throw new UnsupportedEncodingException(isGetters.toString()); + }*/ + + + //messager.printMessage(Diagnostic.Kind.NOTE, annotatedElement.getSimpleName().toString() + " " +isGetters.size()); String entityName = shortName(annotatedElement.asType().toString()); String genEntityName = entityName + "$"; @@ -127,11 +157,11 @@ void generateFields(Element annotatedElement, Writer writer) throws IOException writer.write(generator.on(file).get()); } - private File generatedEntity(Set enclosedFields, String entityName, String genEntityName, String packageName) { - File file = packageName.isEmpty() ? + private File generatedEntity(Map enclosedFields, String entityName, String genEntityName, String packageName) { + final File file = packageName.isEmpty() ? File.of(genEntityName + ".java") : File.of(packageName + "/" + genEntityName + ".java"); - Class clazz = Class.of(genEntityName).public_() + final Class clazz = Class.of(genEntityName).public_() .set(Javadoc.of( "The generated base for entity {@link " + entityName + "} representing entities of the" + " {@code " + lcfirst(entityName) + "}-table in the database." + @@ -139,19 +169,16 @@ private File generatedEntity(Set enclosedFields, String entit ).author("JPAStreamer")); enclosedFields - .forEach(field -> { - addFieldToClass(field, clazz, entityName); - }); + .forEach((field, prefix) -> addFieldToClass(field, prefix, clazz, entityName)); file.add(clazz); file.call(new AutoImports(generator.getDependencyMgr())).call(new AlignTabs<>()); return file; } - private void addFieldToClass(Element field, Class clazz, String entityName) { - String fieldName = field.getSimpleName().toString(); - - Type referenceType = referenceType(field, entityName); + private void addFieldToClass(final Element field, final String prefix, final Class clazz, final String entityName) { + final String fieldName = field.getSimpleName().toString(); + final Type referenceType = referenceType(field, entityName); // Begin building the field value parameters final List> fieldParams = new ArrayList<>(); @@ -164,19 +191,19 @@ private void addFieldToClass(Element field, Class clazz, String entityName) { // Add getter method reference fieldParams.add(Value.ofReference( - entityName + "::" + GETTER_METHOD_PREFIX + ucfirst(fieldName))); + entityName + "::" + prefix + ucfirst(fieldName))); - TypeElement typeElement = elementUtils.getTypeElement(fieldType(field).getTypeName()); - TypeMirror enumType = elementUtils.getTypeElement("java.lang.Enum").asType(); + final TypeElement typeElement = elementUtils.getTypeElement(fieldType(field).getTypeName()); + final TypeMirror enumType = elementUtils.getTypeElement("java.lang.Enum").asType(); if (typeElement != null && typeUtils.isAssignable(typeElement.asType(), enumType)) { - String fieldTypeName = shortName(fieldType(field).getTypeName()); + final String fieldTypeName = shortName(fieldType(field).getTypeName()); // Add enum class fieldParams.add(Value.ofReference(fieldTypeName + ".class")); } else { // Add the 'unique' boolean to the end for all field but enum - Column col = field.getAnnotation(Column.class); + final Column col = field.getAnnotation(Column.class); fieldParams.add(Value.ofBoolean(col != null && col.unique())); } @@ -264,9 +291,10 @@ private Type primitiveFieldType(Type fieldType, Type entityType) throws Unsuppor case "boolean": primitiveFieldType = BooleanField.class; break; - default : throw new UnsupportedOperationException( - "Unknown primitive type: '" + fieldType.getTypeName() + "'." - ); + default: + throw new UnsupportedOperationException( + "Unknown primitive type: '" + fieldType.getTypeName() + "'." + ); } return SimpleParameterizedType.create( primitiveFieldType, diff --git a/fieldgenerator/fieldgenerator-test/src/main/java/com/speedment/jpastreamer/fieldgenerator/test/User.java b/fieldgenerator/fieldgenerator-test/src/main/java/com/speedment/jpastreamer/fieldgenerator/test/User.java new file mode 100644 index 00000000..c27906a7 --- /dev/null +++ b/fieldgenerator/fieldgenerator-test/src/main/java/com/speedment/jpastreamer/fieldgenerator/test/User.java @@ -0,0 +1,38 @@ +package com.speedment.jpastreamer.fieldgenerator.test; + +import javax.persistence.*; + +@Entity +public class User { + + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + @Column(name = "user_id", nullable = false, unique = true, updatable = false, columnDefinition = "smallint(5)") + private int userId; + + @Column(name = "name", nullable = false, columnDefinition = "varchar(255)") + private String name; + + private boolean member; + + private boolean large; + + public int getUserId() { + return userId; + } + + public String getName() { + return name; + } + + // Use "get" rather than "is" + public boolean getMember() { + return member; + } + + // Use "is" rather than "get" + public boolean isLarge() { + return member; + } + +}