use of org.hibernate.annotations.DialectOverride.OverridesAnnotation in project hibernate-orm by hibernate.
the class AnnotationBinder method getOverridableAnnotation.
public static <T extends Annotation> T getOverridableAnnotation(XAnnotatedElement element, Class<T> annotationType, MetadataBuildingContext context) {
Dialect dialect = context.getMetadataCollector().getDatabase().getDialect();
Iterator<Annotation> annotations = Arrays.stream(element.getAnnotations()).flatMap(annotation -> {
try {
Method value = annotation.annotationType().getDeclaredMethod("value");
Class<?> returnType = value.getReturnType();
if (returnType.isArray() && returnType.getComponentType().isAnnotationPresent(Repeatable.class) && returnType.getComponentType().isAnnotationPresent(OverridesAnnotation.class)) {
return Stream.of((Annotation[]) value.invoke(annotation));
}
} catch (NoSuchMethodException ignored) {
} catch (Exception e) {
throw new AssertionFailure("could not read @DialectOverride annotation", e);
}
return Stream.of(annotation);
}).iterator();
while (annotations.hasNext()) {
Annotation annotation = annotations.next();
Class<? extends Annotation> type = annotation.annotationType();
OverridesAnnotation overridesAnnotation = type.getAnnotation(OverridesAnnotation.class);
if (overridesAnnotation != null && overridesAnnotation.value().equals(annotationType)) {
try {
// noinspection unchecked
Class<? extends Dialect> overrideDialect = (Class<? extends Dialect>) type.getDeclaredMethod("dialect").invoke(annotation);
if (overrideDialect.isAssignableFrom(dialect.getClass())) {
DialectOverride.Version before = (DialectOverride.Version) type.getDeclaredMethod("before").invoke(annotation);
DialectOverride.Version sameOrAfter = (DialectOverride.Version) type.getDeclaredMethod("sameOrAfter").invoke(annotation);
if (dialect.getVersion().isBefore(before.major(), before.minor()) && dialect.getVersion().isSameOrAfter(sameOrAfter.major(), sameOrAfter.minor())) {
// noinspection unchecked
return (T) type.getDeclaredMethod("override").invoke(annotation);
}
}
} catch (Exception e) {
throw new AssertionFailure("could not read @DialectOverride annotation", e);
}
}
}
return element.getAnnotation(annotationType);
}
Aggregations