modernize code in ProcessorSessionFactory

This commit is contained in:
Gavin King 2024-11-20 16:36:35 +01:00
parent e24582a6ef
commit ece2981ae8
2 changed files with 119 additions and 149 deletions

View File

@ -269,13 +269,9 @@ public class HibernateProcessor extends AbstractProcessor {
final String suppressedWarnings = options.get( ADD_SUPPRESS_WARNINGS_ANNOTATION ); final String suppressedWarnings = options.get( ADD_SUPPRESS_WARNINGS_ANNOTATION );
if ( suppressedWarnings != null ) { if ( suppressedWarnings != null ) {
if ( parseBoolean(suppressedWarnings) ) { context.setSuppressedWarnings( parseBoolean( suppressedWarnings )
// legacy behavior from HHH-12068 ? new String[] {"deprecation", "rawtypes"} // legacy behavior from HHH-12068
context.setSuppressedWarnings(new String[] {"deprecation", "rawtypes"}); : suppressedWarnings.replace( " ", "" ).split( ",\\s*" ) );
}
else {
context.setSuppressedWarnings( suppressedWarnings.replace(" ","").split(",\\s*") );
}
} }
context.setInclude( options.getOrDefault( INCLUDE, "*" ) ); context.setInclude( options.getOrDefault( INCLUDE, "*" ) );

View File

@ -121,24 +121,24 @@ public abstract class ProcessorSessionFactory extends MockSessionFactory {
@Override @Override
MockEntityPersister createMockEntityPersister(String entityName) { MockEntityPersister createMockEntityPersister(String entityName) {
TypeElement type = findEntityClass(entityName); final TypeElement type = findEntityClass(entityName);
return type == null ? null : entityPersister.make(entityName, type, this); return type == null ? null : entityPersister.make(entityName, type, this);
} }
@Override @Override
MockCollectionPersister createMockCollectionPersister(String role) { MockCollectionPersister createMockCollectionPersister(String role) {
String entityName = root(role); //only works because entity names don't contain dots final String entityName = root(role); //only works because entity names don't contain dots
String propertyPath = unroot(role); final String propertyPath = unroot(role);
TypeElement entityClass = findEntityClass(entityName); final TypeElement entityClass = findEntityClass(entityName);
AccessType defaultAccessType = getDefaultAccessType(entityClass); final AccessType defaultAccessType = getDefaultAccessType(entityClass);
Element property = findPropertyByPath(entityClass, propertyPath, defaultAccessType); final Element property = findPropertyByPath(entityClass, propertyPath, defaultAccessType);
CollectionType collectionType = collectionType(memberType(property), role); final CollectionType collectionType = collectionType(memberType(property), role);
if (isToManyAssociation(property)) { if (isToManyAssociation(property)) {
return toManyPersister.make(role, collectionType, return toManyPersister.make(role, collectionType,
getToManyTargetEntityName(property), this); getToManyTargetEntityName(property), this);
} }
else if (isElementCollectionProperty(property)) { else if (isElementCollectionProperty(property)) {
Element elementType = asElement(getElementCollectionElementType(property)); final Element elementType = asElement(getElementCollectionElementType(property));
return collectionPersister.make(role, collectionType, return collectionPersister.make(role, collectionType,
elementType, propertyPath, defaultAccessType, this); elementType, propertyPath, defaultAccessType, this);
} }
@ -150,9 +150,9 @@ public abstract class ProcessorSessionFactory extends MockSessionFactory {
@Override @Override
Type propertyType(String typeName, String propertyPath) { Type propertyType(String typeName, String propertyPath) {
TypeElement type = findClassByQualifiedName(typeName); final TypeElement type = findClassByQualifiedName(typeName);
AccessType accessType = getAccessType(type, AccessType.FIELD); final AccessType accessType = getAccessType(type, AccessType.FIELD);
Element propertyByPath = findPropertyByPath(type, propertyPath, accessType); final Element propertyByPath = findPropertyByPath(type, propertyPath, accessType);
return propertyByPath == null ? null return propertyByPath == null ? null
: propertyType(propertyByPath, typeName, propertyPath, accessType); : propertyType(propertyByPath, typeName, propertyPath, accessType);
} }
@ -171,9 +171,8 @@ public abstract class ProcessorSessionFactory extends MockSessionFactory {
return null; return null;
} }
else { else {
Element element = asElement(symbol.asType()); return asElement(symbol.asType()) instanceof TypeElement element
return element instanceof TypeElement ? findProperty(element, segment, defaultAccessType)
? findProperty((TypeElement) element, segment, defaultAccessType)
: null; : null;
} }
} }
@ -223,7 +222,7 @@ public abstract class ProcessorSessionFactory extends MockSessionFactory {
} }
private static JdbcType enumJdbcType(Element member) { private static JdbcType enumJdbcType(Element member) {
VariableElement mapping = (VariableElement) final VariableElement mapping = (VariableElement)
getAnnotationMember(getAnnotation(member,"Enumerated"), "value"); getAnnotationMember(getAnnotation(member,"Enumerated"), "value");
return mapping != null && mapping.getSimpleName().contentEquals("STRING") return mapping != null && mapping.getSimpleName().contentEquals("STRING")
? VarcharJdbcType.INSTANCE ? VarcharJdbcType.INSTANCE
@ -234,7 +233,7 @@ public abstract class ProcessorSessionFactory extends MockSessionFactory {
@Override @Nullable @Override @Nullable
Set<String> getEnumTypesForValue(String value) { Set<String> getEnumTypesForValue(String value) {
Set<String> result = allowedEnumLiteralsToEnumTypeNames.get( value); final Set<String> result = allowedEnumLiteralsToEnumTypeNames.get( value);
if ( result != null ) { if ( result != null ) {
return result; return result;
} }
@ -242,13 +241,13 @@ public abstract class ProcessorSessionFactory extends MockSessionFactory {
.openReader(true); BufferedReader buffered = new BufferedReader(reader) ) { .openReader(true); BufferedReader buffered = new BufferedReader(reader) ) {
return Set.of(split(" ", buffered.readLine())); return Set.of(split(" ", buffered.readLine()));
} }
catch (IOException e) { catch (IOException ignore) {
} }
try (Reader reader = filer.getResource(StandardLocation.CLASS_PATH, ENTITY_INDEX, '.' + value) try (Reader reader = filer.getResource(StandardLocation.CLASS_PATH, ENTITY_INDEX, '.' + value)
.openReader(true); BufferedReader buffered = new BufferedReader(reader) ) { .openReader(true); BufferedReader buffered = new BufferedReader(reader) ) {
return Set.of(split(" ", buffered.readLine())); return Set.of(split(" ", buffered.readLine()));
} }
catch (IOException e) { catch (IOException ignore) {
} }
return null; return null;
} }
@ -282,8 +281,8 @@ public abstract class ProcessorSessionFactory extends MockSessionFactory {
ProcessorSessionFactory factory) { ProcessorSessionFactory factory) {
this.type = type; this.type = type;
List<String> names = new ArrayList<>(); final List<String> names = new ArrayList<>();
List<Type> types = new ArrayList<>(); final List<Type> types = new ArrayList<>();
while (type!=null) { while (type!=null) {
if (isMappedClass(type)) { //ignore unmapped intervening classes if (isMappedClass(type)) { //ignore unmapped intervening classes
@ -387,19 +386,19 @@ public abstract class ProcessorSessionFactory extends MockSessionFactory {
@Override @Override
boolean isSamePersister(MockEntityPersister entityPersister) { boolean isSamePersister(MockEntityPersister entityPersister) {
EntityPersister persister = (EntityPersister) entityPersister; final EntityPersister persister = (EntityPersister) entityPersister;
return typeUtil.isSameType( persister.type.asType(), type.asType() ); return typeUtil.isSameType( persister.type.asType(), type.asType() );
} }
@Override @Override
boolean isSubclassPersister(MockEntityPersister entityPersister) { boolean isSubclassPersister(MockEntityPersister entityPersister) {
EntityPersister persister = (EntityPersister) entityPersister; final EntityPersister persister = (EntityPersister) entityPersister;
return typeUtil.isSubtype( persister.type.asType(), type.asType() ); return typeUtil.isSubtype( persister.type.asType(), type.asType() );
} }
@Override @Override
Type createPropertyType(String propertyPath) { Type createPropertyType(String propertyPath) {
Element symbol = findPropertyByPath(type, propertyPath, defaultAccessType); final Element symbol = findPropertyByPath(type, propertyPath, defaultAccessType);
return symbol == null ? null : return symbol == null ? null :
factory.propertyType(symbol, getEntityName(), propertyPath, defaultAccessType); factory.propertyType(symbol, getEntityName(), propertyPath, defaultAccessType);
} }
@ -471,7 +470,7 @@ public abstract class ProcessorSessionFactory extends MockSessionFactory {
@Override @Override
Type getElementPropertyType(String propertyPath) { Type getElementPropertyType(String propertyPath) {
Element symbol = findPropertyByPath(elementType, propertyPath, defaultAccessType); final Element symbol = findPropertyByPath(elementType, propertyPath, defaultAccessType);
return symbol == null ? null : return symbol == null ? null :
factory.propertyType(symbol, getOwnerEntityName(), propertyPath, defaultAccessType); factory.propertyType(symbol, getOwnerEntityName(), propertyPath, defaultAccessType);
} }
@ -484,13 +483,13 @@ public abstract class ProcessorSessionFactory extends MockSessionFactory {
@Override @Override
String qualifyName(String entityName) { String qualifyName(String entityName) {
TypeElement entityClass = findEntityClass(entityName); final TypeElement entityClass = findEntityClass(entityName);
return entityClass == null ? null : entityClass.getQualifiedName().toString(); return entityClass == null ? null : entityClass.getQualifiedName().toString();
} }
@Override @Override
boolean isAttributeDefined(String entityName, String fieldName) { boolean isAttributeDefined(String entityName, String fieldName) {
TypeElement entityClass = findEntityClass(entityName); final TypeElement entityClass = findEntityClass(entityName);
return entityClass != null return entityClass != null
&& findPropertyByPath(entityClass, fieldName, getDefaultAccessType(entityClass)) != null; && findPropertyByPath(entityClass, fieldName, getDefaultAccessType(entityClass)) != null;
} }
@ -508,7 +507,7 @@ public abstract class ProcessorSessionFactory extends MockSessionFactory {
} }
private TypeElement findEntityByQualifiedName(String entityName) { private TypeElement findEntityByQualifiedName(String entityName) {
TypeElement type = findClassByQualifiedName(entityName); final TypeElement type = findClassByQualifiedName(entityName);
return type != null && isEntity(type) ? type : null; return type != null && isEntity(type) ? type : null;
} }
@ -516,24 +515,24 @@ public abstract class ProcessorSessionFactory extends MockSessionFactory {
private final Map<String,TypeElement> entityCache = new HashMap<>(); private final Map<String,TypeElement> entityCache = new HashMap<>();
private TypeElement findEntityByUnqualifiedName(String entityName) { private TypeElement findEntityByUnqualifiedName(String entityName) {
TypeElement cached = entityCache.get(entityName); final TypeElement cached = entityCache.get(entityName);
if ( cached != null ) { if ( cached != null ) {
return cached; return cached;
} }
String qualifiedName = entityNameMappings.get(entityName); final String qualifiedName = entityNameMappings.get(entityName);
if ( qualifiedName != null ) { if ( qualifiedName != null ) {
TypeElement result = elementUtil.getTypeElement(qualifiedName); TypeElement result = elementUtil.getTypeElement(qualifiedName);
entityCache.put(entityName, result); entityCache.put(entityName, result);
return result; return result;
} }
StandardLocation location = StandardLocation.SOURCE_OUTPUT; final StandardLocation location = StandardLocation.SOURCE_OUTPUT;
try (Reader reader = filer.getResource(location, ENTITY_INDEX, entityName) try (Reader reader = filer.getResource(location, ENTITY_INDEX, entityName)
.openReader(true); BufferedReader buffered = new BufferedReader(reader) ) { .openReader(true); BufferedReader buffered = new BufferedReader(reader) ) {
TypeElement result = elementUtil.getTypeElement(buffered.readLine()); TypeElement result = elementUtil.getTypeElement(buffered.readLine());
entityCache.put(entityName, result); entityCache.put(entityName, result);
return result; return result;
} }
catch (IOException e) { catch (IOException ignore) {
} }
try (Reader reader = filer.getResource(StandardLocation.CLASS_PATH, ENTITY_INDEX, entityName) try (Reader reader = filer.getResource(StandardLocation.CLASS_PATH, ENTITY_INDEX, entityName)
.openReader(true); BufferedReader buffered = new BufferedReader(reader) ) { .openReader(true); BufferedReader buffered = new BufferedReader(reader) ) {
@ -541,7 +540,7 @@ public abstract class ProcessorSessionFactory extends MockSessionFactory {
entityCache.put(entityName, result); entityCache.put(entityName, result);
return result; return result;
} }
catch (IOException e) { catch (IOException ignore) {
} }
TypeElement symbol = TypeElement symbol =
@ -572,7 +571,7 @@ public abstract class ProcessorSessionFactory extends MockSessionFactory {
} }
} }
} }
catch (Exception e) { catch (Exception ignore) {
} }
} }
} }
@ -581,7 +580,7 @@ public abstract class ProcessorSessionFactory extends MockSessionFactory {
private static boolean isMatchingEntity(Element symbol, String entityName) { private static boolean isMatchingEntity(Element symbol, String entityName) {
if (symbol.getKind() == ElementKind.CLASS) { if (symbol.getKind() == ElementKind.CLASS) {
TypeElement type = (TypeElement) symbol; final TypeElement type = (TypeElement) symbol;
return isEntity(type) return isEntity(type)
&& getEntityName(type).equals(entityName); && getEntityName(type).equals(entityName);
} }
@ -594,7 +593,7 @@ public abstract class ProcessorSessionFactory extends MockSessionFactory {
//iterate up the superclass hierarchy //iterate up the superclass hierarchy
while (type!=null) { while (type!=null) {
if (isMappedClass(type)) { //ignore unmapped intervening classes if (isMappedClass(type)) { //ignore unmapped intervening classes
AccessType accessType = getAccessType(type, defaultAccessType); final AccessType accessType = getAccessType(type, defaultAccessType);
for (Element member: type.getEnclosedElements()) { for (Element member: type.getEnclosedElements()) {
if (isMatchingProperty(member, propertyName, accessType)) { if (isMatchingProperty(member, propertyName, accessType)) {
return member; return member;
@ -634,11 +633,11 @@ public abstract class ProcessorSessionFactory extends MockSessionFactory {
private static AnnotationMirror getAnnotation(Element member, String annotationName) { private static AnnotationMirror getAnnotation(Element member, String annotationName) {
for (AnnotationMirror mirror : member.getAnnotationMirrors()) { for (AnnotationMirror mirror : member.getAnnotationMirrors()) {
TypeElement annotationType = (TypeElement) mirror.getAnnotationType().asElement(); final TypeElement annotationType = (TypeElement) mirror.getAnnotationType().asElement();
if ( annotationType.getSimpleName().contentEquals(annotationName) if ( annotationType.getSimpleName().contentEquals(annotationName)
&& annotationType.getNestingKind() == NestingKind.TOP_LEVEL ) { && annotationType.getNestingKind() == NestingKind.TOP_LEVEL ) {
PackageElement pack = (PackageElement) annotationType.getEnclosingElement(); final PackageElement pack = (PackageElement) annotationType.getEnclosingElement();
Name packageName = pack.getQualifiedName(); final Name packageName = pack.getQualifiedName();
if (packageName.contentEquals(jakartaPersistence) if (packageName.contentEquals(jakartaPersistence)
|| packageName.contentEquals(javaxPersistence)) { || packageName.contentEquals(javaxPersistence)) {
return mirror; return mirror;
@ -705,7 +704,7 @@ public abstract class ProcessorSessionFactory extends MockSessionFactory {
@Override @Override
boolean isEnum(String className) { boolean isEnum(String className) {
TypeElement typeElement = elementUtil.getTypeElement( className ); final TypeElement typeElement = elementUtil.getTypeElement( className );
return typeElement != null && typeElement.getKind() == ElementKind.ENUM; return typeElement != null && typeElement.getKind() == ElementKind.ENUM;
} }
@ -727,36 +726,28 @@ public abstract class ProcessorSessionFactory extends MockSessionFactory {
if ( typeElement == null ) { if ( typeElement == null ) {
return null; return null;
} }
final TypeMirror typeMirror = typeElement.getEnclosedElements() final TypeMirror typeMirror =
.stream() typeElement.getEnclosedElements()
.filter( e -> fieldName.equals( e.getSimpleName().toString() ) ) .stream()
.filter( ProcessorSessionFactory::isStaticFinalField ) .filter( e -> fieldName.equals( e.getSimpleName().toString() ) )
.findFirst().map( Element::asType ) .filter( ProcessorSessionFactory::isStaticFinalField )
.orElse( null ); .findFirst().map( Element::asType )
.orElse( null );
if ( typeMirror == null ) { if ( typeMirror == null ) {
return null; return null;
} }
try { try {
switch ( typeMirror.getKind() ) { return switch ( typeMirror.getKind() ) {
case BYTE: case BYTE -> byte.class;
return byte.class; case SHORT -> short.class;
case SHORT: case INT -> int.class;
return short.class; case LONG -> long.class;
case INT: case FLOAT -> float.class;
return int.class; case DOUBLE -> double.class;
case LONG: case BOOLEAN -> boolean.class;
return long.class; case CHAR -> char.class;
case FLOAT: default -> Class.forName( typeMirror.toString() );
return float.class; };
case DOUBLE:
return double.class;
case BOOLEAN:
return boolean.class;
case CHAR:
return char.class;
default:
return Class.forName( typeMirror.toString() );
}
} }
catch (ClassNotFoundException ignored) { catch (ClassNotFoundException ignored) {
return null; return null;
@ -765,8 +756,8 @@ public abstract class ProcessorSessionFactory extends MockSessionFactory {
private static boolean isStaticFinalField(Element e) { private static boolean isStaticFinalField(Element e) {
return e.getKind() == ElementKind.FIELD return e.getKind() == ElementKind.FIELD
&& e.getModifiers().contains( Modifier.STATIC ) && e.getModifiers().contains( Modifier.STATIC )
&& e.getModifiers().contains( Modifier.FINAL ); && e.getModifiers().contains( Modifier.FINAL );
} }
private static boolean isEmbeddableType(TypeElement type) { private static boolean isEmbeddableType(TypeElement type) {
@ -799,12 +790,12 @@ public abstract class ProcessorSessionFactory extends MockSessionFactory {
} }
private static AnnotationMirror toOneAnnotation(Element member) { private static AnnotationMirror toOneAnnotation(Element member) {
AnnotationMirror manyToOne = final AnnotationMirror manyToOne =
getAnnotation(member, "ManyToOne"); getAnnotation(member, "ManyToOne");
if (manyToOne!=null) { if (manyToOne!=null) {
return manyToOne; return manyToOne;
} }
AnnotationMirror oneToOne = final AnnotationMirror oneToOne =
getAnnotation(member, "OneToOne"); getAnnotation(member, "OneToOne");
if (oneToOne!=null) { if (oneToOne!=null) {
return oneToOne; return oneToOne;
@ -813,12 +804,12 @@ public abstract class ProcessorSessionFactory extends MockSessionFactory {
} }
private static AnnotationMirror toManyAnnotation(Element member) { private static AnnotationMirror toManyAnnotation(Element member) {
AnnotationMirror manyToMany = final AnnotationMirror manyToMany =
getAnnotation(member, "ManyToMany"); getAnnotation(member, "ManyToMany");
if (manyToMany!=null) { if (manyToMany!=null) {
return manyToMany; return manyToMany;
} }
AnnotationMirror oneToMany = final AnnotationMirror oneToMany =
getAnnotation(member, "OneToMany"); getAnnotation(member, "OneToMany");
if (oneToMany!=null) { if (oneToMany!=null) {
return oneToMany; return oneToMany;
@ -843,14 +834,14 @@ public abstract class ProcessorSessionFactory extends MockSessionFactory {
} }
private static String qualifiedName(Element type) { private static String qualifiedName(Element type) {
if ( type instanceof PackageElement ) { if ( type instanceof PackageElement packageElement ) {
return ((PackageElement) type).getQualifiedName().toString(); return packageElement.getQualifiedName().toString();
} }
else if ( type instanceof TypeElement ) { else if ( type instanceof TypeElement typeElement ) {
return ((TypeElement) type).getQualifiedName().toString(); return typeElement.getQualifiedName().toString();
} }
else { else {
Element enclosingElement = type.getEnclosingElement(); final Element enclosingElement = type.getEnclosingElement();
return enclosingElement != null return enclosingElement != null
? qualifiedName(enclosingElement) + '.' + simpleName(type) ? qualifiedName(enclosingElement) + '.' + simpleName(type)
: simpleName(type); : simpleName(type);
@ -858,25 +849,22 @@ public abstract class ProcessorSessionFactory extends MockSessionFactory {
} }
private static AccessType getAccessType(TypeElement type, AccessType defaultAccessType) { private static AccessType getAccessType(TypeElement type, AccessType defaultAccessType) {
AnnotationMirror annotation = final AnnotationMirror annotation =
getAnnotation(type, "Access"); getAnnotation(type, "Access");
if (annotation==null) { if (annotation==null) {
return defaultAccessType; return defaultAccessType;
} }
else { else {
VariableElement member = (VariableElement) final VariableElement member = (VariableElement)
getAnnotationMember(annotation, "value"); getAnnotationMember(annotation, "value");
if (member==null) { if (member==null) {
return defaultAccessType; //does not occur return defaultAccessType; //does not occur
} }
switch (member.getSimpleName().toString()) { return switch (member.getSimpleName().toString()) {
case "PROPERTY": case "PROPERTY" -> AccessType.PROPERTY;
return AccessType.PROPERTY; case "FIELD" -> AccessType.FIELD;
case "FIELD": default -> throw new IllegalStateException();
return AccessType.FIELD; };
default:
throw new IllegalStateException();
}
} }
} }
@ -884,14 +872,14 @@ public abstract class ProcessorSessionFactory extends MockSessionFactory {
if ( type == null ) { if ( type == null ) {
return null; return null;
} }
AnnotationMirror entityAnnotation = final AnnotationMirror entityAnnotation =
getAnnotation(type, "Entity"); getAnnotation(type, "Entity");
if (entityAnnotation==null) { if (entityAnnotation==null) {
//not an entity! //not an entity!
return null; return null;
} }
else { else {
String name = (String) final String name = (String)
getAnnotationMember(entityAnnotation, "name"); getAnnotationMember(entityAnnotation, "name");
//entity names are unqualified class names //entity names are unqualified class names
return name==null ? simpleName(type) : name; return name==null ? simpleName(type) : name;
@ -899,23 +887,23 @@ public abstract class ProcessorSessionFactory extends MockSessionFactory {
} }
private TypeMirror getCollectionElementType(Element property) { private TypeMirror getCollectionElementType(Element property) {
DeclaredType declaredType = (DeclaredType) memberType(property); final DeclaredType declaredType = (DeclaredType) memberType(property);
List<? extends TypeMirror> typeArguments = declaredType.getTypeArguments(); final List<? extends TypeMirror> typeArguments = declaredType.getTypeArguments();
TypeMirror elementType = typeArguments.get(typeArguments.size()-1); final TypeMirror elementType = typeArguments.get(typeArguments.size()-1);
return elementType==null return elementType==null
? elementUtil.getTypeElement(JAVA_OBJECT).asType() ? elementUtil.getTypeElement(JAVA_OBJECT).asType()
: elementType; : elementType;
} }
private static String getToOneTargetEntity(Element property) { private static String getToOneTargetEntity(Element property) {
AnnotationMirror annotation = toOneAnnotation(property); final AnnotationMirror annotation = toOneAnnotation(property);
TypeMirror classType = (TypeMirror) final TypeMirror classType = (TypeMirror)
getAnnotationMember(annotation, "targetEntity"); getAnnotationMember(annotation, "targetEntity");
TypeMirror targetType = final TypeMirror targetType =
classType == null || classType.getKind() == TypeKind.VOID classType == null || classType.getKind() == TypeKind.VOID
? memberType(property) ? memberType(property)
: classType; : classType;
Element element = asElement(targetType); final Element element = asElement(targetType);
return element != null && element.getKind() == ElementKind.CLASS return element != null && element.getKind() == ElementKind.CLASS
//entity names are unqualified class names //entity names are unqualified class names
? getEntityName((TypeElement) element) ? getEntityName((TypeElement) element)
@ -923,14 +911,14 @@ public abstract class ProcessorSessionFactory extends MockSessionFactory {
} }
private String getToManyTargetEntityName(Element property) { private String getToManyTargetEntityName(Element property) {
AnnotationMirror annotation = toManyAnnotation(property); final AnnotationMirror annotation = toManyAnnotation(property);
TypeMirror classType = (TypeMirror) final TypeMirror classType = (TypeMirror)
getAnnotationMember(annotation, "targetEntity"); getAnnotationMember(annotation, "targetEntity");
TypeMirror targetType = final TypeMirror targetType =
classType == null || classType.getKind() == TypeKind.VOID classType == null || classType.getKind() == TypeKind.VOID
? getCollectionElementType(property) ? getCollectionElementType(property)
: classType; : classType;
Element element = asElement(targetType); final Element element = asElement(targetType);
return element != null && element.getKind() == ElementKind.CLASS return element != null && element.getKind() == ElementKind.CLASS
//entity names are unqualified class names //entity names are unqualified class names
? getEntityName((TypeElement) element) ? getEntityName((TypeElement) element)
@ -938,9 +926,8 @@ public abstract class ProcessorSessionFactory extends MockSessionFactory {
} }
private TypeMirror getElementCollectionElementType(Element property) { private TypeMirror getElementCollectionElementType(Element property) {
AnnotationMirror annotation = getAnnotation(property, final AnnotationMirror annotation = getAnnotation(property, "ElementCollection");
"ElementCollection"); final TypeMirror classType = (TypeMirror)
TypeMirror classType = (TypeMirror)
getAnnotationMember(annotation, "getElementCollectionClass"); getAnnotationMember(annotation, "getElementCollectionClass");
return classType == null return classType == null
|| classType.getKind() == TypeKind.VOID || classType.getKind() == TypeKind.VOID
@ -967,7 +954,7 @@ public abstract class ProcessorSessionFactory extends MockSessionFactory {
@Override @Override
boolean isFieldDefined(String qualifiedClassName, String fieldName) { boolean isFieldDefined(String qualifiedClassName, String fieldName) {
TypeElement type = findClassByQualifiedName(qualifiedClassName); final TypeElement type = findClassByQualifiedName(qualifiedClassName);
return type != null return type != null
&& type.getEnclosedElements().stream() && type.getEnclosedElements().stream()
.anyMatch(element -> element.getKind() == ElementKind.FIELD .anyMatch(element -> element.getKind() == ElementKind.FIELD
@ -976,21 +963,21 @@ public abstract class ProcessorSessionFactory extends MockSessionFactory {
@Override @Override
boolean isConstructorDefined(String qualifiedClassName, List<Type> argumentTypes) { boolean isConstructorDefined(String qualifiedClassName, List<Type> argumentTypes) {
TypeElement symbol = findClassByQualifiedName(qualifiedClassName); final TypeElement symbol = findClassByQualifiedName(qualifiedClassName);
if (symbol==null) { if (symbol==null) {
return false; return false;
} }
for (Element cons: symbol.getEnclosedElements()) { for (Element cons: symbol.getEnclosedElements()) {
if ( cons.getKind() == ElementKind.CONSTRUCTOR ) { if ( cons.getKind() == ElementKind.CONSTRUCTOR ) {
ExecutableElement constructor = (ExecutableElement) cons; final ExecutableElement constructor = (ExecutableElement) cons;
List<? extends VariableElement> parameters = constructor.getParameters(); final List<? extends VariableElement> parameters = constructor.getParameters();
if (parameters.size()==argumentTypes.size()) { if (parameters.size()==argumentTypes.size()) {
boolean argumentsCheckOut = true; boolean argumentsCheckOut = true;
for (int i=0; i<argumentTypes.size(); i++) { for (int i=0; i<argumentTypes.size(); i++) {
Type type = argumentTypes.get(i); final Type type = argumentTypes.get(i);
VariableElement param = parameters.get(i); final VariableElement param = parameters.get(i);
if (param.asType().getKind().isPrimitive()) { if (param.asType().getKind().isPrimitive()) {
Class<?> primitive; final Class<?> primitive;
try { try {
primitive = toPrimitiveClass( type.getReturnedClass() ); primitive = toPrimitiveClass( type.getReturnedClass() );
} }
@ -1003,10 +990,9 @@ public abstract class ProcessorSessionFactory extends MockSessionFactory {
} }
} }
else { else {
TypeElement typeClass; final TypeElement typeClass;
if (type instanceof EntityType) { if ( type instanceof EntityType entityType ) {
EntityType entityType = (EntityType) type; final String entityName = entityType.getAssociatedEntityName();
String entityName = entityType.getAssociatedEntityName();
typeClass = findEntityClass(entityName); typeClass = findEntityClass(entityName);
} }
//TODO: //TODO:
@ -1014,7 +1000,7 @@ public abstract class ProcessorSessionFactory extends MockSessionFactory {
// typeClass = ((Component) ((CompositeCustomType) type).getUserType()).type; // typeClass = ((Component) ((CompositeCustomType) type).getUserType()).type;
// } // }
else if (type instanceof BasicType) { else if (type instanceof BasicType) {
String className; final String className;
//TODO: custom impl of getReturnedClassName() //TODO: custom impl of getReturnedClassName()
// for many more Hibernate types! // for many more Hibernate types!
try { try {
@ -1047,26 +1033,17 @@ public abstract class ProcessorSessionFactory extends MockSessionFactory {
} }
private static Class<?> toPrimitiveClass(VariableElement param) { private static Class<?> toPrimitiveClass(VariableElement param) {
switch (param.asType().getKind()) { return switch ( param.asType().getKind() ) {
case BOOLEAN: case BOOLEAN -> boolean.class;
return boolean.class; case CHAR -> char.class;
case CHAR: case INT -> int.class;
return char.class; case SHORT -> short.class;
case INT: case BYTE -> byte.class;
return int.class; case LONG -> long.class;
case SHORT: case FLOAT -> float.class;
return short.class; case DOUBLE -> double.class;
case BYTE: default -> Object.class;
return byte.class; };
case LONG:
return long.class;
case FLOAT:
return float.class;
case DOUBLE:
return double.class;
default:
return Object.class;
}
} }
private TypeElement findClassByQualifiedName(String path) { private TypeElement findClassByQualifiedName(String path) {
@ -1141,14 +1118,11 @@ public abstract class ProcessorSessionFactory extends MockSessionFactory {
return null; return null;
} }
else { else {
switch (type.getKind()) { return switch ( type.getKind() ) {
case DECLARED: case DECLARED -> ((DeclaredType) type).asElement();
return ((DeclaredType)type).asElement(); case TYPEVAR -> ((TypeVariable) type).asElement();
case TYPEVAR: default -> null;
return ((TypeVariable)type).asElement(); };
default:
return null;
}
} }
} }
} }