better cooperation with other Jakarta Data implementations

and with Scott's preprocessor

produce WARNINGs instead of ERRORs in some cases

Signed-off-by: Gavin King <gavin@hibernate.org>
This commit is contained in:
Gavin King 2024-04-06 17:25:11 +02:00
parent baa24d6889
commit 4c1285b609
1 changed files with 38 additions and 3 deletions

View File

@ -402,8 +402,8 @@ public class AnnotationMetaEntity extends AnnotationMeta {
&& !isSessionGetter(method) ) {
final String companionClassName = element.getQualifiedName().toString() + '$';
if ( context.getElementUtils().getTypeElement(companionClassName) == null ) {
message( method, "repository method cannot be implemented",
Diagnostic.Kind.ERROR );
message( method, "repository method cannot be implemented (skipping whole repository)",
Diagnostic.Kind.WARNING );
}
// NOTE EARLY EXIT with initialized = false
return;
@ -411,6 +411,11 @@ public class AnnotationMetaEntity extends AnnotationMeta {
}
primaryEntity = primaryEntity( lifecycleMethods );
if ( primaryEntity != null && !hasAnnotation(primaryEntity, ENTITY)
|| !checkEntities(lifecycleMethods)) {
// NOTE EARLY EXIT with initialized = false
return;
}
if ( !lifecycleMethods.isEmpty() ) {
validateStatelessSessionType();
@ -455,6 +460,36 @@ public class AnnotationMetaEntity extends AnnotationMeta {
initialized = true;
}
private boolean checkEntities(List<ExecutableElement> lifecycleMethods) {
boolean foundPersistenceEntity = false;
VariableElement nonPersistenceParameter = null;
for (ExecutableElement lifecycleMethod : lifecycleMethods) {
final List<? extends VariableElement> parameters = lifecycleMethod.getParameters();
if ( !parameters.isEmpty() ) {
final VariableElement parameter = parameters.get(0);
final TypeMirror declaredType = parameter.asType();
final TypeMirror parameterType = parameterType(parameter);
final DeclaredType type = entityType(parameterType);
if ( type != null ) {
if ( hasAnnotation( type.asElement(), ENTITY ) ) {
foundPersistenceEntity = true;
}
else if ( declaredType == parameterType
&& nonPersistenceParameter==null ) {
nonPersistenceParameter = parameter;
}
}
}
}
if ( foundPersistenceEntity && nonPersistenceParameter != null ) {
message(nonPersistenceParameter,
"parameter type '" + nonPersistenceParameter.asType()
+ "' is not a Jakarta Persistence entity class (skipping entire repository)",
Diagnostic.Kind.WARNING);
}
return nonPersistenceParameter == null;
}
private void validateStatelessSessionType() {
if ( !usingStatelessSession(sessionType) ) {
message( element,
@ -1282,7 +1317,7 @@ public class AnnotationMetaEntity extends AnnotationMeta {
"incorrect parameter type '" + parameterType + "' is not an entity type",
Diagnostic.Kind.ERROR );
}
else if ( !containsAnnotation( declaredType.asElement(), ENTITY )
else if ( !hasAnnotation( declaredType.asElement(), ENTITY )
// TODO: improve this (carefully consider the case of an erased type variable)
&& declaredParameterType == parameterType ) {
message( parameter,