#2914 - Query bean generation support for @ElementCollection with validaiton annotations - NPE NullPointerException

This commit is contained in:
Rob Bygrave
2022-12-08 11:10:23 +13:00
parent e290f68b29
commit d2f629e7bb
4 changed files with 32 additions and 9 deletions
@@ -3,6 +3,8 @@ package org.example.domain;
import io.ebean.annotation.DbArray;
import javax.persistence.*;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import java.time.ZonedDateTime;
import java.util.ArrayList;
import java.util.List;
@@ -16,7 +18,11 @@ import java.util.Map;
public class Contact extends BaseModel {
@DbArray
List<String> phoneNumbers = new ArrayList<>();
List<@Size(max=20) String> phoneNumbers = new ArrayList<>();
@ElementCollection
@Size(max=10)
List<@Size(max=20) String> paths;
@Column(length = 50)
String firstName;
@@ -36,7 +42,7 @@ public class Contact extends BaseModel {
Customer customer;
@OneToMany(mappedBy = "contact")
List<ContactNote> notes;
List<@NotNull ContactNote> notes;
@OneToMany(cascade = CascadeType.PERSIST)
@MapKey(name = "key")
@@ -6,8 +6,8 @@ datasource.default=h2
datasource.h2.username=sa
datasource.h2.password=
datasource.h2.url=jdbc:h2:mem:tests;NON_KEYWORDS=KEY
datasource.h2.url=jdbc:h2:mem:tests;NON_KEYWORDS=KEY,VALUE
datasource.pg.username=sa
datasource.pg.password=
datasource.pg.url=jdbc:h2:mem:tests;NON_KEYWORDS=KEY
datasource.pg.url=jdbc:h2:mem:tests;NON_KEYWORDS=KEY,VALUE
@@ -146,6 +146,15 @@ class ProcessingContext implements Constants {
}
}
private String trimAnnotations(String type) {
int pos = type.indexOf("@");
if (pos == -1) {
return type;
}
String remainder = type.substring(0, pos) + type.substring(type.indexOf(' ') + 1);
return trimAnnotations(remainder);
}
/**
* Gather all the fields (properties) for the given bean element.
*/
@@ -324,9 +333,9 @@ class ProcessingContext implements Constants {
}
if (typeInstanceOf(typeMirror, "java.lang.Comparable")) {
return new PropertyTypeScalarComparable(typeMirror.toString());
return new PropertyTypeScalarComparable(trimAnnotations(typeMirror.toString()));
} else {
return new PropertyTypeScalar(typeMirror.toString());
return new PropertyTypeScalar(trimAnnotations(typeMirror.toString()));
}
}
@@ -4,7 +4,6 @@ import javax.annotation.processing.Filer;
import javax.annotation.processing.FilerException;
import javax.annotation.processing.Messager;
import javax.annotation.processing.ProcessingEnvironment;
import javax.lang.model.SourceVersion;
import javax.lang.model.element.AnnotationMirror;
import javax.lang.model.element.AnnotationValue;
import javax.lang.model.element.Element;
@@ -231,6 +230,15 @@ class ProcessingContext implements Constants {
}
}
private String trimAnnotations(String type) {
int pos = type.indexOf("@");
if (pos == -1) {
return type;
}
String remainder = type.substring(0, pos) + type.substring(type.indexOf(' ') + 1);
return trimAnnotations(remainder);
}
PropertyType getPropertyType(VariableElement field) {
TypeMirror typeMirror = field.asType();
@@ -296,9 +304,9 @@ class ProcessingContext implements Constants {
return result;
} else {
if (typeInstanceOf(typeMirror, "java.lang.Comparable")) {
return new PropertyTypeScalarComparable(typeMirror.toString());
return new PropertyTypeScalarComparable(trimAnnotations(typeMirror.toString()));
} else {
return new PropertyTypeScalar(typeMirror.toString());
return new PropertyTypeScalar(trimAnnotations(typeMirror.toString()));
}
}
}