minor cleanup to annotation handling in processor

Signed-off-by: Gavin King <gavin@hibernate.org>
This commit is contained in:
Gavin King 2024-03-29 09:36:11 +01:00
parent f32bb7276f
commit 024fd31a3c
6 changed files with 79 additions and 72 deletions

View File

@ -309,8 +309,7 @@ public final class Context {
}
public TypeElement getTypeElementForFullyQualifiedName(String qualifiedName) {
Elements elementUtils = processingEnvironment.getElementUtils();
return elementUtils.getTypeElement( qualifiedName );
return processingEnvironment.getElementUtils().getTypeElement( qualifiedName );
}
void markGenerated(Metamodel metamodel) {

View File

@ -19,6 +19,7 @@ import javax.annotation.processing.SupportedAnnotationTypes;
import javax.annotation.processing.SupportedOptions;
import javax.lang.model.SourceVersion;
import javax.lang.model.element.AnnotationMirror;
import javax.lang.model.element.AnnotationValue;
import javax.lang.model.element.Element;
import javax.lang.model.element.PackageElement;
import javax.lang.model.element.TypeElement;
@ -295,9 +296,10 @@ public class HibernateProcessor extends AbstractProcessor {
final TypeElement typeElement = (TypeElement) element;
final AnnotationMirror repository = getAnnotationMirror( element, JD_REPOSITORY );
if ( repository != null ) {
final String provider = (String) getAnnotationValue( repository, "provider" );
if ( provider == null || provider.isEmpty()
|| provider.equalsIgnoreCase("hibernate") ) {
final AnnotationValue provider = getAnnotationValue( repository, "provider" );
if ( provider == null
|| provider.getValue().toString().isEmpty()
|| provider.getValue().toString().equalsIgnoreCase("hibernate") ) {
context.logMessage( Diagnostic.Kind.OTHER, "Processing repository class '" + element + "'" );
final AnnotationMetaEntity metaEntity =
AnnotationMetaEntity.create( typeElement, context );

View File

@ -26,7 +26,6 @@ import java.util.List;
import static org.hibernate.processor.util.TypeUtils.containsAnnotation;
import static org.hibernate.processor.util.TypeUtils.getAnnotationMirror;
import static org.hibernate.processor.util.TypeUtils.getAnnotationValue;
import static org.hibernate.processor.util.TypeUtils.getAnnotationValueRef;
public abstract class AnnotationMeta implements Metamodel {
@ -69,11 +68,11 @@ public abstract class AnnotationMeta implements Metamodel {
private void handleNamedQueryRepeatableAnnotation(String annotationName, boolean checkHql) {
final AnnotationMirror mirror = getAnnotationMirror( getElement(), annotationName );
if ( mirror != null ) {
final Object value = getAnnotationValue( mirror, "value" );
if ( value instanceof List ) {
final AnnotationValue value = getAnnotationValue( mirror, "value" );
if ( value != null ) {
@SuppressWarnings("unchecked")
final List<? extends AnnotationMirror> values =
(List<? extends AnnotationMirror>) value;
(List<? extends AnnotationMirror>) value.getValue();
for ( AnnotationMirror annotationMirror : values ) {
handleNamedQuery( annotationMirror, checkHql );
}
@ -82,11 +81,11 @@ public abstract class AnnotationMeta implements Metamodel {
}
private void handleNamedQuery(AnnotationMirror mirror, boolean checkHql) {
final Object nameValue = getAnnotationValue( mirror, "name" );
if ( nameValue instanceof String ) {
final String name = nameValue.toString();
final AnnotationValue nameValue = getAnnotationValue( mirror, "name" );
if ( nameValue != null ) {
final String name = nameValue.getValue().toString();
final boolean reportErrors = getContext().checkNamedQuery( name );
final AnnotationValue value = getAnnotationValueRef( mirror, "query" );
final AnnotationValue value = getAnnotationValue( mirror, "query" );
if ( value != null ) {
final Object query = value.getValue();
if ( query instanceof String ) {
@ -132,11 +131,11 @@ public abstract class AnnotationMeta implements Metamodel {
private void addAuxiliaryMembersForRepeatableAnnotation(String annotationName, String prefix) {
final AnnotationMirror mirror = getAnnotationMirror( getElement(), annotationName );
if ( mirror != null ) {
final Object value = getAnnotationValue( mirror, "value" );
if ( value instanceof List ) {
final AnnotationValue value = getAnnotationValue( mirror, "value" );
if ( value != null ) {
@SuppressWarnings("unchecked")
final List<? extends AnnotationMirror> values =
(List<? extends AnnotationMirror>) value;
(List<? extends AnnotationMirror>) value.getValue();
for ( AnnotationMirror annotationMirror : values ) {
addAuxiliaryMembersForMirror( annotationMirror, prefix );
}

View File

@ -75,7 +75,6 @@ import static org.hibernate.grammars.hql.HqlLexer.HAVING;
import static org.hibernate.grammars.hql.HqlLexer.IDENTIFIER;
import static org.hibernate.grammars.hql.HqlLexer.ORDER;
import static org.hibernate.grammars.hql.HqlLexer.WHERE;
import static org.hibernate.internal.util.StringHelper.isEmpty;
import static org.hibernate.internal.util.StringHelper.qualify;
import static org.hibernate.processor.annotation.AbstractQueryMethod.isSessionParameter;
import static org.hibernate.processor.annotation.AbstractQueryMethod.isSpecialParam;
@ -89,7 +88,6 @@ import static org.hibernate.processor.util.TypeUtils.determineAnnotationSpecifie
import static org.hibernate.processor.util.TypeUtils.findMappedSuperClass;
import static org.hibernate.processor.util.TypeUtils.getAnnotationMirror;
import static org.hibernate.processor.util.TypeUtils.getAnnotationValue;
import static org.hibernate.processor.util.TypeUtils.getAnnotationValueRef;
import static org.hibernate.processor.util.TypeUtils.hasAnnotation;
import static org.hibernate.processor.util.TypeUtils.primitiveClassMatchesKind;
import static org.hibernate.processor.util.TypeUtils.propertyName;
@ -500,9 +498,12 @@ public class AnnotationMetaEntity extends AnnotationMeta {
private @Nullable String dataStore() {
final AnnotationMirror repo = getAnnotationMirror( element, JD_REPOSITORY );
if ( repo != null ) {
final String dataStore = (String) getAnnotationValue( repo, "dataStore" );
if ( dataStore != null && !dataStore.isEmpty() ) {
return dataStore;
final AnnotationValue dataStoreValue = getAnnotationValue( repo, "dataStore" );
if (dataStoreValue != null) {
final String dataStore = dataStoreValue.getValue().toString();
if ( !dataStore.isEmpty() ) {
return dataStore;
}
}
}
return null;
@ -866,13 +867,13 @@ public class AnnotationMetaEntity extends AnnotationMeta {
}
private void validateToOneAssociation(Element memberOfClass, AnnotationMirror annotation, TypeMirror type) {
final TypeMirror target = (TypeMirror) getAnnotationValue(annotation, "targetEntity");
validateAssociation(memberOfClass, annotation, target == null ? type : target);
final AnnotationValue target = getAnnotationValue(annotation, "targetEntity");
validateAssociation(memberOfClass, annotation, target == null ? type : (TypeMirror) target.getValue());
}
private void validateToManyAssociation(Element memberOfClass, AnnotationMirror annotation, TypeMirror type) {
final TypeMirror target = (TypeMirror) getAnnotationValue(annotation, "targetEntity");
validateAssociation(memberOfClass, annotation, target == null ? elementType(type) : target);
final AnnotationValue target = getAnnotationValue(annotation, "targetEntity");
validateAssociation(memberOfClass, annotation, target == null ? elementType(type) : (TypeMirror) target.getValue());
}
private void validateAssociation(Element memberOfClass, AnnotationMirror annotation, @Nullable TypeMirror typeMirror) {
@ -888,8 +889,9 @@ public class AnnotationMetaEntity extends AnnotationMeta {
final DeclaredType assocDeclaredType = (DeclaredType) typeMirror;
final TypeElement assocTypeElement = (TypeElement) assocDeclaredType.asElement();
if ( hasAnnotation(assocTypeElement, ENTITY) ) {
final String mappedBy = (String) getAnnotationValue(annotation, "mappedBy");
validateBidirectionalMapping(memberOfClass, annotation, mappedBy, assocTypeElement);
final AnnotationValue mappedBy = getAnnotationValue(annotation, "mappedBy");
final String propertyName = mappedBy == null ? null : mappedBy.getValue().toString();
validateBidirectionalMapping(memberOfClass, annotation, propertyName, assocTypeElement);
}
else {
message(memberOfClass, "type '" + assocTypeElement.getSimpleName()
@ -916,7 +918,7 @@ public class AnnotationMetaEntity extends AnnotationMeta {
return;
}
final AnnotationValue annotationVal =
castNonNull(getAnnotationValueRef(annotation, "mappedBy"));
castNonNull(getAnnotationValue(annotation, "mappedBy"));
for ( Element member : context.getElementUtils().getAllMembers(assocTypeElement) ) {
if ( propertyName(this, member).contentEquals(mappedBy) ) {
validateBackRef(memberOfClass, annotation, assocTypeElement, member, annotationVal);
@ -1517,7 +1519,7 @@ public class AnnotationMetaEntity extends AnnotationMeta {
final List<OrderBy> result = new ArrayList<>();
@SuppressWarnings("unchecked")
final List<AnnotationValue> list = (List<AnnotationValue>)
castNonNull( getAnnotationValue( orderByList, "value" ) );
castNonNull( getAnnotationValue( orderByList, "value" ) ).getValue();
for ( AnnotationValue element : list ) {
result.add( orderByExpression( castNonNull( (AnnotationMirror) element.getValue() ), entityType, method ) );
}
@ -1532,14 +1534,14 @@ public class AnnotationMetaEntity extends AnnotationMeta {
}
private OrderBy orderByExpression(AnnotationMirror orderBy, TypeElement entityType, ExecutableElement method) {
final String fieldName = (String) castNonNull( getAnnotationValue(orderBy, "value") );
final String fieldName = castNonNull( getAnnotationValue(orderBy, "value") ).getValue().toString();
if ( fieldName.equals("<error>") ) {
throw new ProcessLaterException();
}
final Boolean descendingOrNull = (Boolean) getAnnotationValue(orderBy, "descending");
final Boolean ignoreCaseOrNull = (Boolean) getAnnotationValue(orderBy, "ignoreCase");
final boolean descending = descendingOrNull != null && descendingOrNull;
final boolean ignoreCase = ignoreCaseOrNull != null && ignoreCaseOrNull;
final AnnotationValue descendingOrNull = getAnnotationValue(orderBy, "descending");
final AnnotationValue ignoreCaseOrNull = getAnnotationValue(orderBy, "ignoreCase");
final boolean descending = descendingOrNull != null && (Boolean) descendingOrNull.getValue();
final boolean ignoreCase = ignoreCaseOrNull != null && (Boolean) ignoreCaseOrNull.getValue();
final String path = fieldName
.replace('$', '.')
.replace('_', '.'); //Jakarta Data allows _ here
@ -1632,14 +1634,14 @@ public class AnnotationMetaEntity extends AnnotationMeta {
return emptyList();
}
else {
final Object enabledFetchProfiles =
final AnnotationValue enabledFetchProfiles =
getAnnotationValue( findAnnotation, "enabledFetchProfiles" );
if ( enabledFetchProfiles == null ) {
return emptyList();
}
else {
@SuppressWarnings("unchecked")
final List<AnnotationValue> annotationValues = (List<AnnotationValue>) enabledFetchProfiles;
final List<AnnotationValue> annotationValues = (List<AnnotationValue>) enabledFetchProfiles.getValue();
final List<String> result = annotationValues.stream().map(AnnotationValue::toString).collect(toList());
if ( result.stream().anyMatch("<error>"::equals) ) {
throw new ProcessLaterException();
@ -1859,9 +1861,9 @@ public class AnnotationMetaEntity extends AnnotationMeta {
else {
final AnnotationMirror idClass = getAnnotationMirror( entityType, ID_CLASS );
if ( idClass != null ) {
final Object value = getAnnotationValue( idClass, "value" );
if ( value instanceof TypeMirror ) {
if ( context.getTypeUtils().isSameType( param.asType(), (TypeMirror) value ) ) {
final AnnotationValue value = getAnnotationValue( idClass, "value" );
if ( value != null ) {
if ( context.getTypeUtils().isSameType( param.asType(), (TypeMirror) value.getValue() ) ) {
return FieldType.ID;
}
}
@ -2060,7 +2062,7 @@ public class AnnotationMetaEntity extends AnnotationMeta {
AnnotationMirror mirror,
boolean isNative) {
final AnnotationValue value = getAnnotationValueRef( mirror, "value" );
final AnnotationValue value = getAnnotationValue( mirror, "value" );
if ( value != null ) {
final Object query = value.getValue();
if ( query instanceof String ) {
@ -2142,8 +2144,7 @@ public class AnnotationMetaEntity extends AnnotationMeta {
if ( annotation == null ) {
throw new AssertionFailure("@Entity annotation should not be missing");
}
final String name = (String) getAnnotationValue(annotation, "name");
return isEmpty(name) ? resultType.asElement().getSimpleName().toString() : name;
return entityName(resultType, annotation);
}
else if ( primaryEntity != null ) {
return primaryEntity.getSimpleName().toString();
@ -2153,6 +2154,17 @@ public class AnnotationMetaEntity extends AnnotationMeta {
}
}
private static String entityName(DeclaredType resultType, AnnotationMirror annotation) {
final AnnotationValue name = getAnnotationValue(annotation, "name");
if (name != null) {
final String explicitName = name.getValue().toString();
if ( !explicitName.isEmpty() ) {
return explicitName;
}
}
return resultType.asElement().getSimpleName().toString();
}
private static String addFromClauseIfNecessary(String hql, @Nullable String entityType) {
if ( entityType == null ) {
return hql;
@ -2468,8 +2480,7 @@ public class AnnotationMetaEntity extends AnnotationMeta {
final TypeElement typeElement = (TypeElement) declaredType.asElement();
final AnnotationMirror mirror = getAnnotationMirror(typeElement, ENTITY );
if ( mirror != null ) {
final Object value = getAnnotationValue( mirror, "name" );
final String entityName = value instanceof String ? (String) value : typeElement.getSimpleName().toString();
final String entityName = entityName(declaredType, mirror);
return model.getHibernateEntityName().equals( entityName );
}
}
@ -2622,7 +2633,9 @@ public class AnnotationMetaEntity extends AnnotationMeta {
final AnnotationMirror by = getAnnotationMirror( parameter, "jakarta.data.repository.By" );
final AnnotationMirror param = getAnnotationMirror( parameter, "jakarta.data.repository.Param" );
if ( by != null ) {
final String name = (String) castNonNull(getAnnotationValue(by, "value"));
final String name =
castNonNull(getAnnotationValue(by, "value"))
.getValue().toString();
if ( name.contains("<error>") ) {
throw new ProcessLaterException();
}
@ -2631,7 +2644,9 @@ public class AnnotationMetaEntity extends AnnotationMeta {
.replace('_', '.');
}
else if ( param != null ) {
final String name = (String) castNonNull(getAnnotationValue(param, "value"));
final String name =
castNonNull(getAnnotationValue(param, "value"))
.getValue().toString();
if ( name.contains("<error>") ) {
throw new ProcessLaterException();
}
@ -2669,7 +2684,8 @@ public class AnnotationMetaEntity extends AnnotationMeta {
if ( name.contentEquals(Constants.BASIC)
|| name.contentEquals(Constants.MANY_TO_ONE)
|| name.contentEquals(Constants.ONE_TO_ONE)) {
if ( FALSE.equals( getAnnotationValue(mirror, "optional") ) ) {
AnnotationValue optional = getAnnotationValue(mirror, "optional");
if ( optional != null && optional.getValue().equals(FALSE) ) {
nullable = false;
}
}

View File

@ -30,6 +30,7 @@ import java.util.List;
import static org.hibernate.processor.util.Constants.ELEMENT_COLLECTION;
import static org.hibernate.processor.util.Constants.MANY_TO_ANY;
import static org.hibernate.processor.util.Constants.MANY_TO_MANY;
import static org.hibernate.processor.util.Constants.MAP_KEY_CLASS;
import static org.hibernate.processor.util.Constants.ONE_TO_MANY;
import static org.hibernate.processor.util.NullnessUtil.castNonNull;
import static org.hibernate.processor.util.TypeUtils.DEFAULT_ANNOTATION_PARAMETER_NAME;
@ -160,10 +161,11 @@ public class MetaAttributeGenerationVisitor extends SimpleTypeVisitor8<@Nullable
}
private String getMapKeyType(DeclaredType declaredType, Element element) {
final AnnotationMirror annotationMirror = getAnnotationMirror(element, Constants.MAP_KEY_CLASS );
final AnnotationMirror annotationMirror = getAnnotationMirror(element, MAP_KEY_CLASS );
return annotationMirror == null
? getKeyType( declaredType, context )
: castNonNull( getAnnotationValue( annotationMirror, DEFAULT_ANNOTATION_PARAMETER_NAME ) ).toString();
: castNonNull( getAnnotationValue( annotationMirror, DEFAULT_ANNOTATION_PARAMETER_NAME ) )
.getValue().toString();
}
private String getElementType(DeclaredType declaredType, @Nullable String targetEntity) {

View File

@ -232,7 +232,8 @@ public final class TypeUtils {
assert annotationMirror != null;
assert qualifiedName != null;
final Element element = annotationMirror.getAnnotationType().asElement();
return ((TypeElement) element).getQualifiedName().contentEquals( qualifiedName );
final TypeElement typeElement = (TypeElement) element;
return typeElement.getQualifiedName().contentEquals( qualifiedName );
}
/**
@ -268,24 +269,12 @@ public final class TypeUtils {
return false;
}
public static @Nullable Object getAnnotationValue(AnnotationMirror annotationMirror, String parameterValue) {
public static @Nullable AnnotationValue getAnnotationValue(AnnotationMirror annotationMirror, String member) {
assert annotationMirror != null;
assert parameterValue != null;
assert member != null;
for ( Map.Entry<? extends ExecutableElement, ? extends AnnotationValue> entry
: annotationMirror.getElementValues().entrySet() ) {
if ( entry.getKey().getSimpleName().contentEquals( parameterValue ) ) {
return entry.getValue().getValue();
}
}
return null;
}
public static @Nullable AnnotationValue getAnnotationValueRef(AnnotationMirror annotationMirror, String parameterValue) {
assert annotationMirror != null;
assert parameterValue != null;
for ( Map.Entry<? extends ExecutableElement, ? extends AnnotationValue> entry
: annotationMirror.getElementValues().entrySet() ) {
if ( entry.getKey().getSimpleName().contentEquals( parameterValue ) ) {
if ( entry.getKey().getSimpleName().contentEquals(member) ) {
return entry.getValue();
}
}
@ -485,9 +474,9 @@ public final class TypeUtils {
public static @Nullable AccessType determineAnnotationSpecifiedAccessType(Element element) {
final AnnotationMirror mirror = getAnnotationMirror( element, ACCESS );
if ( mirror != null ) {
final Object accessType = getAnnotationValue( mirror, DEFAULT_ANNOTATION_PARAMETER_NAME );
if ( accessType instanceof VariableElement) {
final VariableElement enumValue = (VariableElement) accessType;
final AnnotationValue accessType = getAnnotationValue( mirror, DEFAULT_ANNOTATION_PARAMETER_NAME );
if ( accessType != null ) {
final VariableElement enumValue = (VariableElement) accessType.getValue();
final Name enumValueName = enumValue.getSimpleName();
if ( enumValueName.contentEquals(PROPERTY) ) {
return AccessType.PROPERTY;
@ -558,10 +547,10 @@ public final class TypeUtils {
}
public static @Nullable String getFullyQualifiedClassNameOfTargetEntity(
AnnotationMirror mirror, String parameterName) {
final Object parameterValue = getAnnotationValue( mirror, parameterName );
if ( parameterValue != null ) {
final TypeMirror parameterType = (TypeMirror) parameterValue;
AnnotationMirror mirror, String member) {
final AnnotationValue value = getAnnotationValue( mirror, member);
if ( value != null ) {
final TypeMirror parameterType = (TypeMirror) value.getValue();
if ( parameterType.getKind() != TypeKind.VOID ) {
return parameterType.toString();
}