Skip to content

Commit 024fd31

Browse files
committed
minor cleanup to annotation handling in processor
Signed-off-by: Gavin King <[email protected]>
1 parent f32bb72 commit 024fd31

File tree

6 files changed

+79
-72
lines changed

6 files changed

+79
-72
lines changed

tooling/metamodel-generator/src/main/java/org/hibernate/processor/Context.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -309,8 +309,7 @@ public void addAccessTypeInformation(String qualifiedName, AccessTypeInformation
309309
}
310310

311311
public TypeElement getTypeElementForFullyQualifiedName(String qualifiedName) {
312-
Elements elementUtils = processingEnvironment.getElementUtils();
313-
return elementUtils.getTypeElement( qualifiedName );
312+
return processingEnvironment.getElementUtils().getTypeElement( qualifiedName );
314313
}
315314

316315
void markGenerated(Metamodel metamodel) {

tooling/metamodel-generator/src/main/java/org/hibernate/processor/HibernateProcessor.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import javax.annotation.processing.SupportedOptions;
2020
import javax.lang.model.SourceVersion;
2121
import javax.lang.model.element.AnnotationMirror;
22+
import javax.lang.model.element.AnnotationValue;
2223
import javax.lang.model.element.Element;
2324
import javax.lang.model.element.PackageElement;
2425
import javax.lang.model.element.TypeElement;
@@ -295,9 +296,10 @@ else if ( element instanceof TypeElement ) {
295296
final TypeElement typeElement = (TypeElement) element;
296297
final AnnotationMirror repository = getAnnotationMirror( element, JD_REPOSITORY );
297298
if ( repository != null ) {
298-
final String provider = (String) getAnnotationValue( repository, "provider" );
299-
if ( provider == null || provider.isEmpty()
300-
|| provider.equalsIgnoreCase("hibernate") ) {
299+
final AnnotationValue provider = getAnnotationValue( repository, "provider" );
300+
if ( provider == null
301+
|| provider.getValue().toString().isEmpty()
302+
|| provider.getValue().toString().equalsIgnoreCase("hibernate") ) {
301303
context.logMessage( Diagnostic.Kind.OTHER, "Processing repository class '" + element + "'" );
302304
final AnnotationMetaEntity metaEntity =
303305
AnnotationMetaEntity.create( typeElement, context );

tooling/metamodel-generator/src/main/java/org/hibernate/processor/annotation/AnnotationMeta.java

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
import static org.hibernate.processor.util.TypeUtils.containsAnnotation;
2727
import static org.hibernate.processor.util.TypeUtils.getAnnotationMirror;
2828
import static org.hibernate.processor.util.TypeUtils.getAnnotationValue;
29-
import static org.hibernate.processor.util.TypeUtils.getAnnotationValueRef;
3029

3130
public abstract class AnnotationMeta implements Metamodel {
3231

@@ -69,11 +68,11 @@ private void handleNamedQueryAnnotation(String annotationName, boolean checkHql)
6968
private void handleNamedQueryRepeatableAnnotation(String annotationName, boolean checkHql) {
7069
final AnnotationMirror mirror = getAnnotationMirror( getElement(), annotationName );
7170
if ( mirror != null ) {
72-
final Object value = getAnnotationValue( mirror, "value" );
73-
if ( value instanceof List ) {
71+
final AnnotationValue value = getAnnotationValue( mirror, "value" );
72+
if ( value != null ) {
7473
@SuppressWarnings("unchecked")
7574
final List<? extends AnnotationMirror> values =
76-
(List<? extends AnnotationMirror>) value;
75+
(List<? extends AnnotationMirror>) value.getValue();
7776
for ( AnnotationMirror annotationMirror : values ) {
7877
handleNamedQuery( annotationMirror, checkHql );
7978
}
@@ -82,11 +81,11 @@ private void handleNamedQueryRepeatableAnnotation(String annotationName, boolean
8281
}
8382

8483
private void handleNamedQuery(AnnotationMirror mirror, boolean checkHql) {
85-
final Object nameValue = getAnnotationValue( mirror, "name" );
86-
if ( nameValue instanceof String ) {
87-
final String name = nameValue.toString();
84+
final AnnotationValue nameValue = getAnnotationValue( mirror, "name" );
85+
if ( nameValue != null ) {
86+
final String name = nameValue.getValue().toString();
8887
final boolean reportErrors = getContext().checkNamedQuery( name );
89-
final AnnotationValue value = getAnnotationValueRef( mirror, "query" );
88+
final AnnotationValue value = getAnnotationValue( mirror, "query" );
9089
if ( value != null ) {
9190
final Object query = value.getValue();
9291
if ( query instanceof String ) {
@@ -132,11 +131,11 @@ private static boolean isQueryMethodName(String name) {
132131
private void addAuxiliaryMembersForRepeatableAnnotation(String annotationName, String prefix) {
133132
final AnnotationMirror mirror = getAnnotationMirror( getElement(), annotationName );
134133
if ( mirror != null ) {
135-
final Object value = getAnnotationValue( mirror, "value" );
136-
if ( value instanceof List ) {
134+
final AnnotationValue value = getAnnotationValue( mirror, "value" );
135+
if ( value != null ) {
137136
@SuppressWarnings("unchecked")
138137
final List<? extends AnnotationMirror> values =
139-
(List<? extends AnnotationMirror>) value;
138+
(List<? extends AnnotationMirror>) value.getValue();
140139
for ( AnnotationMirror annotationMirror : values ) {
141140
addAuxiliaryMembersForMirror( annotationMirror, prefix );
142141
}

tooling/metamodel-generator/src/main/java/org/hibernate/processor/annotation/AnnotationMetaEntity.java

Lines changed: 47 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,6 @@
7575
import static org.hibernate.grammars.hql.HqlLexer.IDENTIFIER;
7676
import static org.hibernate.grammars.hql.HqlLexer.ORDER;
7777
import static org.hibernate.grammars.hql.HqlLexer.WHERE;
78-
import static org.hibernate.internal.util.StringHelper.isEmpty;
7978
import static org.hibernate.internal.util.StringHelper.qualify;
8079
import static org.hibernate.processor.annotation.AbstractQueryMethod.isSessionParameter;
8180
import static org.hibernate.processor.annotation.AbstractQueryMethod.isSpecialParam;
@@ -89,7 +88,6 @@
8988
import static org.hibernate.processor.util.TypeUtils.findMappedSuperClass;
9089
import static org.hibernate.processor.util.TypeUtils.getAnnotationMirror;
9190
import static org.hibernate.processor.util.TypeUtils.getAnnotationValue;
92-
import static org.hibernate.processor.util.TypeUtils.getAnnotationValueRef;
9391
import static org.hibernate.processor.util.TypeUtils.hasAnnotation;
9492
import static org.hibernate.processor.util.TypeUtils.primitiveClassMatchesKind;
9593
import static org.hibernate.processor.util.TypeUtils.propertyName;
@@ -500,9 +498,12 @@ private void addDefaultConstructor() {
500498
private @Nullable String dataStore() {
501499
final AnnotationMirror repo = getAnnotationMirror( element, JD_REPOSITORY );
502500
if ( repo != null ) {
503-
final String dataStore = (String) getAnnotationValue( repo, "dataStore" );
504-
if ( dataStore != null && !dataStore.isEmpty() ) {
505-
return dataStore;
501+
final AnnotationValue dataStoreValue = getAnnotationValue( repo, "dataStore" );
502+
if (dataStoreValue != null) {
503+
final String dataStore = dataStoreValue.getValue().toString();
504+
if ( !dataStore.isEmpty() ) {
505+
return dataStore;
506+
}
506507
}
507508
}
508509
return null;
@@ -866,13 +867,13 @@ private static TypeMirror attributeType(Element memberOfClass) {
866867
}
867868

868869
private void validateToOneAssociation(Element memberOfClass, AnnotationMirror annotation, TypeMirror type) {
869-
final TypeMirror target = (TypeMirror) getAnnotationValue(annotation, "targetEntity");
870-
validateAssociation(memberOfClass, annotation, target == null ? type : target);
870+
final AnnotationValue target = getAnnotationValue(annotation, "targetEntity");
871+
validateAssociation(memberOfClass, annotation, target == null ? type : (TypeMirror) target.getValue());
871872
}
872873

873874
private void validateToManyAssociation(Element memberOfClass, AnnotationMirror annotation, TypeMirror type) {
874-
final TypeMirror target = (TypeMirror) getAnnotationValue(annotation, "targetEntity");
875-
validateAssociation(memberOfClass, annotation, target == null ? elementType(type) : target);
875+
final AnnotationValue target = getAnnotationValue(annotation, "targetEntity");
876+
validateAssociation(memberOfClass, annotation, target == null ? elementType(type) : (TypeMirror) target.getValue());
876877
}
877878

878879
private void validateAssociation(Element memberOfClass, AnnotationMirror annotation, @Nullable TypeMirror typeMirror) {
@@ -888,8 +889,9 @@ private void validateAssociation(Element memberOfClass, AnnotationMirror annotat
888889
final DeclaredType assocDeclaredType = (DeclaredType) typeMirror;
889890
final TypeElement assocTypeElement = (TypeElement) assocDeclaredType.asElement();
890891
if ( hasAnnotation(assocTypeElement, ENTITY) ) {
891-
final String mappedBy = (String) getAnnotationValue(annotation, "mappedBy");
892-
validateBidirectionalMapping(memberOfClass, annotation, mappedBy, assocTypeElement);
892+
final AnnotationValue mappedBy = getAnnotationValue(annotation, "mappedBy");
893+
final String propertyName = mappedBy == null ? null : mappedBy.getValue().toString();
894+
validateBidirectionalMapping(memberOfClass, annotation, propertyName, assocTypeElement);
893895
}
894896
else {
895897
message(memberOfClass, "type '" + assocTypeElement.getSimpleName()
@@ -916,7 +918,7 @@ private void validateBidirectionalMapping(
916918
return;
917919
}
918920
final AnnotationValue annotationVal =
919-
castNonNull(getAnnotationValueRef(annotation, "mappedBy"));
921+
castNonNull(getAnnotationValue(annotation, "mappedBy"));
920922
for ( Element member : context.getElementUtils().getAllMembers(assocTypeElement) ) {
921923
if ( propertyName(this, member).contentEquals(mappedBy) ) {
922924
validateBackRef(memberOfClass, annotation, assocTypeElement, member, annotationVal);
@@ -1517,7 +1519,7 @@ private List<OrderBy> orderByList(ExecutableElement method, TypeElement entityTy
15171519
final List<OrderBy> result = new ArrayList<>();
15181520
@SuppressWarnings("unchecked")
15191521
final List<AnnotationValue> list = (List<AnnotationValue>)
1520-
castNonNull( getAnnotationValue( orderByList, "value" ) );
1522+
castNonNull( getAnnotationValue( orderByList, "value" ) ).getValue();
15211523
for ( AnnotationValue element : list ) {
15221524
result.add( orderByExpression( castNonNull( (AnnotationMirror) element.getValue() ), entityType, method ) );
15231525
}
@@ -1532,14 +1534,14 @@ private List<OrderBy> orderByList(ExecutableElement method, TypeElement entityTy
15321534
}
15331535

15341536
private OrderBy orderByExpression(AnnotationMirror orderBy, TypeElement entityType, ExecutableElement method) {
1535-
final String fieldName = (String) castNonNull( getAnnotationValue(orderBy, "value") );
1537+
final String fieldName = castNonNull( getAnnotationValue(orderBy, "value") ).getValue().toString();
15361538
if ( fieldName.equals("<error>") ) {
15371539
throw new ProcessLaterException();
15381540
}
1539-
final Boolean descendingOrNull = (Boolean) getAnnotationValue(orderBy, "descending");
1540-
final Boolean ignoreCaseOrNull = (Boolean) getAnnotationValue(orderBy, "ignoreCase");
1541-
final boolean descending = descendingOrNull != null && descendingOrNull;
1542-
final boolean ignoreCase = ignoreCaseOrNull != null && ignoreCaseOrNull;
1541+
final AnnotationValue descendingOrNull = getAnnotationValue(orderBy, "descending");
1542+
final AnnotationValue ignoreCaseOrNull = getAnnotationValue(orderBy, "ignoreCase");
1543+
final boolean descending = descendingOrNull != null && (Boolean) descendingOrNull.getValue();
1544+
final boolean ignoreCase = ignoreCaseOrNull != null && (Boolean) ignoreCaseOrNull.getValue();
15431545
final String path = fieldName
15441546
.replace('$', '.')
15451547
.replace('_', '.'); //Jakarta Data allows _ here
@@ -1632,14 +1634,14 @@ private static List<String> enabledFetchProfiles(ExecutableElement method) {
16321634
return emptyList();
16331635
}
16341636
else {
1635-
final Object enabledFetchProfiles =
1637+
final AnnotationValue enabledFetchProfiles =
16361638
getAnnotationValue( findAnnotation, "enabledFetchProfiles" );
16371639
if ( enabledFetchProfiles == null ) {
16381640
return emptyList();
16391641
}
16401642
else {
16411643
@SuppressWarnings("unchecked")
1642-
final List<AnnotationValue> annotationValues = (List<AnnotationValue>) enabledFetchProfiles;
1644+
final List<AnnotationValue> annotationValues = (List<AnnotationValue>) enabledFetchProfiles.getValue();
16431645
final List<String> result = annotationValues.stream().map(AnnotationValue::toString).collect(toList());
16441646
if ( result.stream().anyMatch("<error>"::equals) ) {
16451647
throw new ProcessLaterException();
@@ -1859,9 +1861,9 @@ else if ( containsAnnotation( member, NATURAL_ID ) ) {
18591861
else {
18601862
final AnnotationMirror idClass = getAnnotationMirror( entityType, ID_CLASS );
18611863
if ( idClass != null ) {
1862-
final Object value = getAnnotationValue( idClass, "value" );
1863-
if ( value instanceof TypeMirror ) {
1864-
if ( context.getTypeUtils().isSameType( param.asType(), (TypeMirror) value ) ) {
1864+
final AnnotationValue value = getAnnotationValue( idClass, "value" );
1865+
if ( value != null ) {
1866+
if ( context.getTypeUtils().isSameType( param.asType(), (TypeMirror) value.getValue() ) ) {
18651867
return FieldType.ID;
18661868
}
18671869
}
@@ -2060,7 +2062,7 @@ private void addQueryMethod(
20602062
AnnotationMirror mirror,
20612063
boolean isNative) {
20622064

2063-
final AnnotationValue value = getAnnotationValueRef( mirror, "value" );
2065+
final AnnotationValue value = getAnnotationValue( mirror, "value" );
20642066
if ( value != null ) {
20652067
final Object query = value.getValue();
20662068
if ( query instanceof String ) {
@@ -2142,8 +2144,7 @@ private void addQueryMethod(
21422144
if ( annotation == null ) {
21432145
throw new AssertionFailure("@Entity annotation should not be missing");
21442146
}
2145-
final String name = (String) getAnnotationValue(annotation, "name");
2146-
return isEmpty(name) ? resultType.asElement().getSimpleName().toString() : name;
2147+
return entityName(resultType, annotation);
21472148
}
21482149
else if ( primaryEntity != null ) {
21492150
return primaryEntity.getSimpleName().toString();
@@ -2153,6 +2154,17 @@ else if ( primaryEntity != null ) {
21532154
}
21542155
}
21552156

2157+
private static String entityName(DeclaredType resultType, AnnotationMirror annotation) {
2158+
final AnnotationValue name = getAnnotationValue(annotation, "name");
2159+
if (name != null) {
2160+
final String explicitName = name.getValue().toString();
2161+
if ( !explicitName.isEmpty() ) {
2162+
return explicitName;
2163+
}
2164+
}
2165+
return resultType.asElement().getSimpleName().toString();
2166+
}
2167+
21562168
private static String addFromClauseIfNecessary(String hql, @Nullable String entityType) {
21572169
if ( entityType == null ) {
21582170
return hql;
@@ -2468,8 +2480,7 @@ private boolean checkReturnedEntity(EntityDomainType<?> model, TypeMirror return
24682480
final TypeElement typeElement = (TypeElement) declaredType.asElement();
24692481
final AnnotationMirror mirror = getAnnotationMirror(typeElement, ENTITY );
24702482
if ( mirror != null ) {
2471-
final Object value = getAnnotationValue( mirror, "name" );
2472-
final String entityName = value instanceof String ? (String) value : typeElement.getSimpleName().toString();
2483+
final String entityName = entityName(declaredType, mirror);
24732484
return model.getHibernateEntityName().equals( entityName );
24742485
}
24752486
}
@@ -2622,7 +2633,9 @@ private static String parameterName(VariableElement parameter) {
26222633
final AnnotationMirror by = getAnnotationMirror( parameter, "jakarta.data.repository.By" );
26232634
final AnnotationMirror param = getAnnotationMirror( parameter, "jakarta.data.repository.Param" );
26242635
if ( by != null ) {
2625-
final String name = (String) castNonNull(getAnnotationValue(by, "value"));
2636+
final String name =
2637+
castNonNull(getAnnotationValue(by, "value"))
2638+
.getValue().toString();
26262639
if ( name.contains("<error>") ) {
26272640
throw new ProcessLaterException();
26282641
}
@@ -2631,7 +2644,9 @@ private static String parameterName(VariableElement parameter) {
26312644
.replace('_', '.');
26322645
}
26332646
else if ( param != null ) {
2634-
final String name = (String) castNonNull(getAnnotationValue(param, "value"));
2647+
final String name =
2648+
castNonNull(getAnnotationValue(param, "value"))
2649+
.getValue().toString();
26352650
if ( name.contains("<error>") ) {
26362651
throw new ProcessLaterException();
26372652
}
@@ -2669,7 +2684,8 @@ private static boolean isNullable(Element member) {
26692684
if ( name.contentEquals(Constants.BASIC)
26702685
|| name.contentEquals(Constants.MANY_TO_ONE)
26712686
|| name.contentEquals(Constants.ONE_TO_ONE)) {
2672-
if ( FALSE.equals( getAnnotationValue(mirror, "optional") ) ) {
2687+
AnnotationValue optional = getAnnotationValue(mirror, "optional");
2688+
if ( optional != null && optional.getValue().equals(FALSE) ) {
26732689
nullable = false;
26742690
}
26752691
}

tooling/metamodel-generator/src/main/java/org/hibernate/processor/annotation/MetaAttributeGenerationVisitor.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import static org.hibernate.processor.util.Constants.ELEMENT_COLLECTION;
3131
import static org.hibernate.processor.util.Constants.MANY_TO_ANY;
3232
import static org.hibernate.processor.util.Constants.MANY_TO_MANY;
33+
import static org.hibernate.processor.util.Constants.MAP_KEY_CLASS;
3334
import static org.hibernate.processor.util.Constants.ONE_TO_MANY;
3435
import static org.hibernate.processor.util.NullnessUtil.castNonNull;
3536
import static org.hibernate.processor.util.TypeUtils.DEFAULT_ANNOTATION_PARAMETER_NAME;
@@ -160,10 +161,11 @@ private void setAccessType(TypeMirror collectionElementType, TypeElement collect
160161
}
161162

162163
private String getMapKeyType(DeclaredType declaredType, Element element) {
163-
final AnnotationMirror annotationMirror = getAnnotationMirror(element, Constants.MAP_KEY_CLASS );
164+
final AnnotationMirror annotationMirror = getAnnotationMirror(element, MAP_KEY_CLASS );
164165
return annotationMirror == null
165166
? getKeyType( declaredType, context )
166-
: castNonNull( getAnnotationValue( annotationMirror, DEFAULT_ANNOTATION_PARAMETER_NAME ) ).toString();
167+
: castNonNull( getAnnotationValue( annotationMirror, DEFAULT_ANNOTATION_PARAMETER_NAME ) )
168+
.getValue().toString();
167169
}
168170

169171
private String getElementType(DeclaredType declaredType, @Nullable String targetEntity) {

tooling/metamodel-generator/src/main/java/org/hibernate/processor/util/TypeUtils.java

Lines changed: 12 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,8 @@ public static boolean isAnnotationMirrorOfType(AnnotationMirror annotationMirror
232232
assert annotationMirror != null;
233233
assert qualifiedName != null;
234234
final Element element = annotationMirror.getAnnotationType().asElement();
235-
return ((TypeElement) element).getQualifiedName().contentEquals( qualifiedName );
235+
final TypeElement typeElement = (TypeElement) element;
236+
return typeElement.getQualifiedName().contentEquals( qualifiedName );
236237
}
237238

238239
/**
@@ -268,24 +269,12 @@ public static boolean hasAnnotation(Element element, String... qualifiedNames) {
268269
return false;
269270
}
270271

271-
public static @Nullable Object getAnnotationValue(AnnotationMirror annotationMirror, String parameterValue) {
272+
public static @Nullable AnnotationValue getAnnotationValue(AnnotationMirror annotationMirror, String member) {
272273
assert annotationMirror != null;
273-
assert parameterValue != null;
274+
assert member != null;
274275
for ( Map.Entry<? extends ExecutableElement, ? extends AnnotationValue> entry
275276
: annotationMirror.getElementValues().entrySet() ) {
276-
if ( entry.getKey().getSimpleName().contentEquals( parameterValue ) ) {
277-
return entry.getValue().getValue();
278-
}
279-
}
280-
return null;
281-
}
282-
283-
public static @Nullable AnnotationValue getAnnotationValueRef(AnnotationMirror annotationMirror, String parameterValue) {
284-
assert annotationMirror != null;
285-
assert parameterValue != null;
286-
for ( Map.Entry<? extends ExecutableElement, ? extends AnnotationValue> entry
287-
: annotationMirror.getElementValues().entrySet() ) {
288-
if ( entry.getKey().getSimpleName().contentEquals( parameterValue ) ) {
277+
if ( entry.getKey().getSimpleName().contentEquals(member) ) {
289278
return entry.getValue();
290279
}
291280
}
@@ -485,9 +474,9 @@ private static boolean isIdAnnotation(AnnotationMirror annotationMirror) {
485474
public static @Nullable AccessType determineAnnotationSpecifiedAccessType(Element element) {
486475
final AnnotationMirror mirror = getAnnotationMirror( element, ACCESS );
487476
if ( mirror != null ) {
488-
final Object accessType = getAnnotationValue( mirror, DEFAULT_ANNOTATION_PARAMETER_NAME );
489-
if ( accessType instanceof VariableElement) {
490-
final VariableElement enumValue = (VariableElement) accessType;
477+
final AnnotationValue accessType = getAnnotationValue( mirror, DEFAULT_ANNOTATION_PARAMETER_NAME );
478+
if ( accessType != null ) {
479+
final VariableElement enumValue = (VariableElement) accessType.getValue();
491480
final Name enumValueName = enumValue.getSimpleName();
492481
if ( enumValueName.contentEquals(PROPERTY) ) {
493482
return AccessType.PROPERTY;
@@ -558,10 +547,10 @@ public static boolean isBasicAttribute(Element element, Element returnedElement,
558547
}
559548

560549
public static @Nullable String getFullyQualifiedClassNameOfTargetEntity(
561-
AnnotationMirror mirror, String parameterName) {
562-
final Object parameterValue = getAnnotationValue( mirror, parameterName );
563-
if ( parameterValue != null ) {
564-
final TypeMirror parameterType = (TypeMirror) parameterValue;
550+
AnnotationMirror mirror, String member) {
551+
final AnnotationValue value = getAnnotationValue( mirror, member);
552+
if ( value != null ) {
553+
final TypeMirror parameterType = (TypeMirror) value.getValue();
565554
if ( parameterType.getKind() != TypeKind.VOID ) {
566555
return parameterType.toString();
567556
}

0 commit comments

Comments
 (0)