Field generator: getCollectionElementClass method not generated when no addElement method is present in interface

Closes #16255
This commit is contained in:
vramik 2023-01-04 12:12:26 +01:00 committed by Hynek Mlnařík
parent 10f2ae3411
commit 380df3bedf
2 changed files with 20 additions and 11 deletions

View file

@ -177,15 +177,14 @@ public class GenerateEntityImplementationsProcessor extends AbstractGenerateEnti
pw.println(" return FIELD_NAME_CAMEL_CASE;");
pw.println(" }");
FieldAccessorType.getMethod(FieldAccessorType.COLLECTION_ADD, methods, fieldName, types, fieldType).ifPresent(method -> {
TypeMirror firstParameterType = method.getParameters().get(0).asType();
pw.println(" @SuppressWarnings(\"unchecked\") @Override public Class<?> getCollectionElementClass() {");
pw.println(" return " + types.erasure(firstParameterType) + ".class;");
pw.println(" }");
});
FieldAccessorType.getMethod(FieldAccessorType.GETTER, methods, fieldName, types, fieldType).ifPresent(method -> {
if (Util.isMapType((TypeElement) types.asElement(types.erasure(fieldType)))) {
if (Util.isCollectionType((TypeElement) types.asElement(types.erasure(fieldType)))) {
TypeMirror firstParameterType = Util.getGenericsDeclaration(method.getReturnType()).get(0);
pw.println(" @SuppressWarnings(\"unchecked\") @Override public Class<?> getCollectionElementClass() {");
pw.println(" return " + types.erasure(firstParameterType) + ".class;");
pw.println(" }");
} else if (Util.isMapType((TypeElement) types.asElement(types.erasure(fieldType)))) {
TypeMirror firstParameterType = Util.getGenericsDeclaration(method.getReturnType()).get(0);
TypeMirror secondParameterType = Util.getGenericsDeclaration(method.getReturnType()).get(1);

View file

@ -17,7 +17,7 @@
package org.keycloak.models.map.processor;
import org.keycloak.models.map.annotations.IgnoreForEntityImplementationGenerator;
import java.util.Arrays;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashSet;
@ -45,8 +45,9 @@ import javax.lang.model.util.SimpleTypeVisitor8;
*/
public class Util {
private static final HashSet<String> SET_TYPES = new HashSet<>(Arrays.asList(Set.class.getCanonicalName(), TreeSet.class.getCanonicalName(), HashSet.class.getCanonicalName(), LinkedHashSet.class.getCanonicalName()));
private static final HashSet<String> MAP_TYPES = new HashSet<>(Arrays.asList(Map.class.getCanonicalName(), HashMap.class.getCanonicalName()));
private static final Set<String> LIST_TYPES = Set.of(List.class.getCanonicalName(), ArrayList.class.getCanonicalName(), LinkedList.class.getCanonicalName());
private static final Set<String> SET_TYPES = Set.of(Set.class.getCanonicalName(), TreeSet.class.getCanonicalName(), HashSet.class.getCanonicalName(), LinkedHashSet.class.getCanonicalName());
private static final Set<String> MAP_TYPES = Set.of(Map.class.getCanonicalName(), HashMap.class.getCanonicalName());
public static List<TypeMirror> getGenericsDeclaration(TypeMirror fieldType) {
List<TypeMirror> res = new LinkedList<>();
@ -69,6 +70,15 @@ public class Util {
.collect(Collectors.joining(", "));
}
public static boolean isCollectionType(TypeElement typeElement) {
return isListType(typeElement) || isSetType(typeElement);
}
public static boolean isListType(TypeElement typeElement) {
Name name = typeElement.getQualifiedName();
return LIST_TYPES.contains(name.toString());
}
public static boolean isSetType(TypeElement typeElement) {
Name name = typeElement.getQualifiedName();
return SET_TYPES.contains(name.toString());