[FEAT] simplify annotation tool

This commit is contained in:
Edouard DUPIN 2025-01-29 23:09:49 +01:00
parent 3e81673d38
commit c04660c01a

View File

@ -41,6 +41,24 @@ import jakarta.ws.rs.DefaultValue;
public class AnnotationTools { public class AnnotationTools {
static final Logger LOGGER = LoggerFactory.getLogger(AnnotationTools.class); static final Logger LOGGER = LoggerFactory.getLogger(AnnotationTools.class);
public static <TYPE extends Annotation> TYPE get(final Field element, final Class<TYPE> clazz) {
final TYPE[] annotations = element.getDeclaredAnnotationsByType(clazz);
if (annotations.length == 0) {
return null;
}
return annotations[0];
}
public static <TYPE extends Annotation> TYPE[] gets(final Field element, final Class<TYPE> clazz) {
final TYPE[] annotations = element.getDeclaredAnnotationsByType(clazz);
if (annotations.length == 0) {
return null;
}
return annotations;
}
// For SQL declaration table Name // For SQL declaration table Name
public static String getTableName(final Class<?> clazz, final QueryOptions options) throws DataAccessException { public static String getTableName(final Class<?> clazz, final QueryOptions options) throws DataAccessException {
if (options != null) { if (options != null) {
@ -92,27 +110,15 @@ public class AnnotationTools {
} }
public static CollectionItemNotNull getCollectionItemNotNull(final Field element) { public static CollectionItemNotNull getCollectionItemNotNull(final Field element) {
final Annotation[] annotation = element.getDeclaredAnnotationsByType(CollectionItemNotNull.class); return get(element, CollectionItemNotNull.class);
if (annotation.length == 0) {
return null;
}
return (CollectionItemNotNull) annotation[0];
} }
public static CollectionItemUnique getCollectionItemUnique(final Field element) { public static CollectionItemUnique getCollectionItemUnique(final Field element) {
final Annotation[] annotation = element.getDeclaredAnnotationsByType(CollectionItemUnique.class); return get(element, CollectionItemUnique.class);
if (annotation.length == 0) {
return null;
}
return (CollectionItemUnique) annotation[0];
} }
public static CollectionNotEmpty getCollectionNotEmpty(final Field element) { public static CollectionNotEmpty getCollectionNotEmpty(final Field element) {
final Annotation[] annotation = element.getDeclaredAnnotationsByType(CollectionNotEmpty.class); return get(element, CollectionNotEmpty.class);
if (annotation.length == 0) {
return null;
}
return (CollectionNotEmpty) annotation[0];
} }
public static boolean getSchemaReadOnly(final Field element) { public static boolean getSchemaReadOnly(final Field element) {
@ -164,75 +170,39 @@ public class AnnotationTools {
} }
public static ManyToOne getManyToOne(final Field element) { public static ManyToOne getManyToOne(final Field element) {
final Annotation[] annotation = element.getDeclaredAnnotationsByType(ManyToOne.class); return get(element, ManyToOne.class);
if (annotation.length == 0) {
return null;
}
return (ManyToOne) annotation[0];
} }
public static ManyToMany getManyToMany(final Field element) { public static ManyToMany getManyToMany(final Field element) {
final Annotation[] annotation = element.getDeclaredAnnotationsByType(ManyToMany.class); return get(element, ManyToMany.class);
if (annotation.length == 0) {
return null;
}
return (ManyToMany) annotation[0];
} }
public static OneToMany getOneToMany(final Field element) { public static OneToMany getOneToMany(final Field element) {
final Annotation[] annotation = element.getDeclaredAnnotationsByType(OneToMany.class); return get(element, OneToMany.class);
if (annotation.length == 0) {
return null;
}
return (OneToMany) annotation[0];
} }
public static DataJson getDataJson(final Field element) { public static DataJson getDataJson(final Field element) {
final Annotation[] annotation = element.getDeclaredAnnotationsByType(DataJson.class); return get(element, DataJson.class);
if (annotation.length == 0) {
return null;
}
return (DataJson) annotation[0];
} }
public static Checker[] getConstraintsCheckers(final Field element) { public static Checker[] getConstraintsCheckers(final Field element) {
final Annotation[] annotation = element.getDeclaredAnnotationsByType(Checker.class); return gets(element, Checker.class);
if (annotation.length == 0) {
return null;
}
return (Checker[]) annotation;
} }
public static DecimalMin getConstraintsDecimalMin(final Field element) { public static DecimalMin getConstraintsDecimalMin(final Field element) {
final Annotation[] annotation = element.getDeclaredAnnotationsByType(DecimalMin.class); return get(element, DecimalMin.class);
if (annotation.length == 0) {
return null;
}
return ((DecimalMin) annotation[0]);
} }
public static DecimalMax getConstraintsDecimalMax(final Field element) { public static DecimalMax getConstraintsDecimalMax(final Field element) {
final Annotation[] annotation = element.getDeclaredAnnotationsByType(DecimalMax.class); return get(element, DecimalMax.class);
if (annotation.length == 0) {
return null;
}
return ((DecimalMax) annotation[0]);
} }
public static Max getConstraintsMax(final Field element) { public static Max getConstraintsMax(final Field element) {
final Annotation[] annotation = element.getDeclaredAnnotationsByType(Max.class); return get(element, Max.class);
if (annotation.length == 0) {
return null;
}
return ((Max) annotation[0]);
} }
public static Min getConstraintsMin(final Field element) { public static Min getConstraintsMin(final Field element) {
final Annotation[] annotation = element.getDeclaredAnnotationsByType(Min.class); return get(element, Min.class);
if (annotation.length == 0) {
return null;
}
return ((Min) annotation[0]);
} }
public static int getLimitSize(final Field element) { public static int getLimitSize(final Field element) {
@ -245,27 +215,15 @@ public class AnnotationTools {
} }
public static Size getConstraintsSize(final Field element) { public static Size getConstraintsSize(final Field element) {
final Annotation[] annotation = element.getDeclaredAnnotationsByType(Size.class); return get(element, Size.class);
if (annotation.length == 0) {
return null;
}
return (Size) annotation[0];
} }
public static Pattern getConstraintsPattern(final Field element) { public static Pattern getConstraintsPattern(final Field element) {
final Annotation[] annotation = element.getDeclaredAnnotationsByType(Pattern.class); return get(element, Pattern.class);
if (annotation.length == 0) {
return null;
}
return (Pattern) annotation[0];
} }
public static Email getConstraintsEmail(final Field element) { public static Email getConstraintsEmail(final Field element) {
final Annotation[] annotation = element.getDeclaredAnnotationsByType(Email.class); return get(element, Email.class);
if (annotation.length == 0) {
return null;
}
return (Email) annotation[0];
} }
public static boolean isAnnotationGroup(final Field field, final Class<?> annotationType) { public static boolean isAnnotationGroup(final Field field, final Class<?> annotationType) {