[FIX] correct annotation tool throws
This commit is contained in:
parent
d968a2c48f
commit
0a307f3f6e
@ -7,6 +7,7 @@ import java.util.List;
|
||||
|
||||
import org.kar.archidata.dataAccess.QueryOptions;
|
||||
import org.kar.archidata.dataAccess.options.OverrideTableName;
|
||||
import org.kar.archidata.exception.DataAccessException;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
@ -28,7 +29,7 @@ import jakarta.ws.rs.DefaultValue;
|
||||
public class AnnotationTools {
|
||||
static final Logger LOGGER = LoggerFactory.getLogger(AnnotationTools.class);
|
||||
|
||||
public static String getTableName(final Class<?> clazz, final QueryOptions options) throws Exception {
|
||||
public static String getTableName(final Class<?> clazz, final QueryOptions options) throws DataAccessException {
|
||||
if (options != null) {
|
||||
final List<OverrideTableName> data = options.get(OverrideTableName.class);
|
||||
if (data.size() == 1) {
|
||||
@ -38,14 +39,15 @@ public class AnnotationTools {
|
||||
return AnnotationTools.getTableName(clazz);
|
||||
}
|
||||
|
||||
public static String getTableName(final Class<?> element) throws Exception {
|
||||
public static String getTableName(final Class<?> element) throws DataAccessException {
|
||||
final Annotation[] annotation = element.getDeclaredAnnotationsByType(Table.class);
|
||||
if (annotation.length == 0) {
|
||||
// when no annotation is detected, then the table name is the class name
|
||||
return element.getSimpleName();
|
||||
}
|
||||
if (annotation.length > 1) {
|
||||
throw new Exception("Must not have more than 1 element @Table on " + element.getClass().getCanonicalName());
|
||||
throw new DataAccessException(
|
||||
"Must not have more than 1 element @Table on " + element.getClass().getCanonicalName());
|
||||
}
|
||||
final String tmp = ((Table) annotation[0]).name();
|
||||
if (tmp == null) {
|
||||
@ -54,155 +56,158 @@ public class AnnotationTools {
|
||||
return tmp;
|
||||
}
|
||||
|
||||
public static boolean getSchemaReadOnly(final Field element) throws Exception {
|
||||
public static boolean getSchemaReadOnly(final Field element) throws DataAccessException {
|
||||
final Annotation[] annotation = element.getDeclaredAnnotationsByType(Schema.class);
|
||||
if (annotation.length == 0) {
|
||||
return false;
|
||||
}
|
||||
if (annotation.length > 1) {
|
||||
throw new Exception(
|
||||
throw new DataAccessException(
|
||||
"Must not have more than 1 element @Schema on " + element.getClass().getCanonicalName());
|
||||
}
|
||||
return ((Schema) annotation[0]).readOnly();
|
||||
}
|
||||
|
||||
public static String getSchemaExample(final Class<?> element) throws Exception {
|
||||
public static String getSchemaExample(final Class<?> element) throws DataAccessException {
|
||||
final Annotation[] annotation = element.getDeclaredAnnotationsByType(Schema.class);
|
||||
if (annotation.length == 0) {
|
||||
return null;
|
||||
}
|
||||
if (annotation.length > 1) {
|
||||
throw new Exception(
|
||||
throw new DataAccessException(
|
||||
"Must not have more than 1 element @Schema on " + element.getClass().getCanonicalName());
|
||||
}
|
||||
return ((Schema) annotation[0]).example();
|
||||
}
|
||||
|
||||
public static String getSchemaDescription(final Class<?> element) throws Exception {
|
||||
public static String getSchemaDescription(final Class<?> element) throws DataAccessException {
|
||||
final Annotation[] annotation = element.getDeclaredAnnotationsByType(Schema.class);
|
||||
if (annotation.length == 0) {
|
||||
return null;
|
||||
}
|
||||
if (annotation.length > 1) {
|
||||
throw new Exception(
|
||||
throw new DataAccessException(
|
||||
"Must not have more than 1 element @Schema on " + element.getClass().getCanonicalName());
|
||||
}
|
||||
return ((Schema) annotation[0]).description();
|
||||
}
|
||||
|
||||
public static String getSchemaDescription(final Field element) throws Exception {
|
||||
public static String getSchemaDescription(final Field element) throws DataAccessException {
|
||||
final Annotation[] annotation = element.getDeclaredAnnotationsByType(Schema.class);
|
||||
if (annotation.length == 0) {
|
||||
return null;
|
||||
}
|
||||
if (annotation.length > 1) {
|
||||
throw new Exception(
|
||||
throw new DataAccessException(
|
||||
"Must not have more than 1 element @Schema on " + element.getClass().getCanonicalName());
|
||||
}
|
||||
return ((Schema) annotation[0]).description();
|
||||
}
|
||||
|
||||
public static String getComment(final Field element) throws Exception {
|
||||
public static String getComment(final Field element) throws DataAccessException {
|
||||
final Annotation[] annotation = element.getDeclaredAnnotationsByType(DataComment.class);
|
||||
if (annotation.length == 0) {
|
||||
return getSchemaDescription(element);
|
||||
}
|
||||
if (annotation.length > 1) {
|
||||
throw new Exception(
|
||||
throw new DataAccessException(
|
||||
"Must not have more than 1 element @DataComment on " + element.getClass().getCanonicalName());
|
||||
}
|
||||
return ((DataComment) annotation[0]).value();
|
||||
}
|
||||
|
||||
public static String getDefault(final Field element) throws Exception {
|
||||
public static String getDefault(final Field element) throws DataAccessException {
|
||||
final Annotation[] annotation = element.getDeclaredAnnotationsByType(DefaultValue.class);
|
||||
if (annotation.length == 0) {
|
||||
return null;
|
||||
}
|
||||
if (annotation.length > 1) {
|
||||
throw new Exception(
|
||||
throw new DataAccessException(
|
||||
"Must not have more than 1 element @DataDefault on " + element.getClass().getCanonicalName());
|
||||
}
|
||||
return ((DefaultValue) annotation[0]).value();
|
||||
}
|
||||
|
||||
public static ManyToOne getManyToOne(final Field element) throws Exception {
|
||||
public static ManyToOne getManyToOne(final Field element) throws DataAccessException {
|
||||
final Annotation[] annotation = element.getDeclaredAnnotationsByType(ManyToOne.class);
|
||||
if (annotation.length == 0) {
|
||||
return null;
|
||||
}
|
||||
if (annotation.length > 1) {
|
||||
throw new Exception(
|
||||
throw new DataAccessException(
|
||||
"Must not have more than 1 element @ManyToOne on " + element.getClass().getCanonicalName());
|
||||
}
|
||||
return (ManyToOne) annotation[0];
|
||||
}
|
||||
|
||||
public static DataJson getDataJson(final Field element) throws Exception {
|
||||
public static DataJson getDataJson(final Field element) throws DataAccessException {
|
||||
final Annotation[] annotation = element.getDeclaredAnnotationsByType(DataJson.class);
|
||||
if (annotation.length == 0) {
|
||||
return null;
|
||||
}
|
||||
if (annotation.length > 1) {
|
||||
throw new Exception(
|
||||
throw new DataAccessException(
|
||||
"Must not have more than 1 element @ManyToOne on " + element.getClass().getCanonicalName());
|
||||
}
|
||||
return (DataJson) annotation[0];
|
||||
}
|
||||
|
||||
public static Long getConstraintsMax(final Field element) throws Exception {
|
||||
public static Long getConstraintsMax(final Field element) throws DataAccessException {
|
||||
final Annotation[] annotation = element.getDeclaredAnnotationsByType(Max.class);
|
||||
if (annotation.length == 0) {
|
||||
return null;
|
||||
}
|
||||
if (annotation.length > 1) {
|
||||
throw new Exception("Must not have more than 1 element @Size on " + element.getClass().getCanonicalName());
|
||||
throw new DataAccessException(
|
||||
"Must not have more than 1 element @Size on " + element.getClass().getCanonicalName());
|
||||
}
|
||||
return ((Max) annotation[0]).value();
|
||||
}
|
||||
|
||||
public static Long getConstraintsMin(final Field element) throws Exception {
|
||||
public static Long getConstraintsMin(final Field element) throws DataAccessException {
|
||||
final Annotation[] annotation = element.getDeclaredAnnotationsByType(Min.class);
|
||||
if (annotation.length == 0) {
|
||||
return null;
|
||||
}
|
||||
if (annotation.length > 1) {
|
||||
throw new Exception("Must not have more than 1 element @Size on " + element.getClass().getCanonicalName());
|
||||
throw new DataAccessException(
|
||||
"Must not have more than 1 element @Size on " + element.getClass().getCanonicalName());
|
||||
}
|
||||
return ((Min) annotation[0]).value();
|
||||
}
|
||||
|
||||
public static int getLimitSize(final Field element) throws Exception {
|
||||
public static int getLimitSize(final Field element) throws DataAccessException {
|
||||
final Annotation[] annotation = element.getDeclaredAnnotationsByType(Column.class);
|
||||
if (annotation.length == 0) {
|
||||
return 255;
|
||||
}
|
||||
if (annotation.length > 1) {
|
||||
throw new Exception(
|
||||
throw new DataAccessException(
|
||||
"Must not have more than 1 element @Column on " + element.getClass().getCanonicalName());
|
||||
}
|
||||
final int length = ((Column) annotation[0]).length();
|
||||
return length <= 0 ? 0 : length;
|
||||
}
|
||||
|
||||
public static Size getConstraintsSize(final Field element) throws Exception {
|
||||
public static Size getConstraintsSize(final Field element) throws DataAccessException {
|
||||
final Annotation[] annotation = element.getDeclaredAnnotationsByType(Size.class);
|
||||
if (annotation.length == 0) {
|
||||
return null;
|
||||
}
|
||||
if (annotation.length > 1) {
|
||||
throw new Exception("Must not have more than 1 element @Size on " + element.getClass().getCanonicalName());
|
||||
throw new DataAccessException(
|
||||
"Must not have more than 1 element @Size on " + element.getClass().getCanonicalName());
|
||||
}
|
||||
return (Size) annotation[0];
|
||||
}
|
||||
|
||||
public static String getConstraintsPattern(final Field element) throws Exception {
|
||||
public static String getConstraintsPattern(final Field element) throws DataAccessException {
|
||||
final Annotation[] annotation = element.getDeclaredAnnotationsByType(Pattern.class);
|
||||
if (annotation.length == 0) {
|
||||
return null;
|
||||
}
|
||||
if (annotation.length > 1) {
|
||||
throw new Exception(
|
||||
throw new DataAccessException(
|
||||
"Must not have more than 1 element @Pattern on " + element.getClass().getCanonicalName());
|
||||
}
|
||||
return ((Pattern) annotation[0]).regexp();
|
||||
@ -231,13 +236,13 @@ public class AnnotationTools {
|
||||
return false;
|
||||
}
|
||||
|
||||
public static String getFieldName(final Field element) throws Exception {
|
||||
public static String getFieldName(final Field element) throws DataAccessException {
|
||||
final Annotation[] annotation = element.getDeclaredAnnotationsByType(Column.class);
|
||||
if (annotation.length == 0) {
|
||||
return element.getName();
|
||||
}
|
||||
if (annotation.length > 1) {
|
||||
throw new Exception(
|
||||
throw new DataAccessException(
|
||||
"Must not have more than 1 element @Column on " + element.getClass().getCanonicalName());
|
||||
}
|
||||
final String name = ((Column) annotation[0]).name();
|
||||
@ -247,19 +252,19 @@ public class AnnotationTools {
|
||||
return name;
|
||||
}
|
||||
|
||||
public static boolean getColumnNotNull(final Field element) throws Exception {
|
||||
public static boolean getColumnNotNull(final Field element) throws DataAccessException {
|
||||
final Annotation[] annotation = element.getDeclaredAnnotationsByType(Column.class);
|
||||
if (annotation.length == 0) {
|
||||
return false;
|
||||
}
|
||||
if (annotation.length > 1) {
|
||||
throw new Exception(
|
||||
throw new DataAccessException(
|
||||
"Must not have more than 1 element @Column on " + element.getClass().getCanonicalName());
|
||||
}
|
||||
return !((Column) annotation[0]).nullable();
|
||||
}
|
||||
|
||||
public static boolean getNullable(final Field element) throws Exception {
|
||||
public static boolean getNullable(final Field element) throws DataAccessException {
|
||||
final Annotation[] annotation = element.getDeclaredAnnotationsByType(Nullable.class);
|
||||
if (annotation.length == 0) {
|
||||
return false;
|
||||
@ -267,19 +272,19 @@ public class AnnotationTools {
|
||||
return true;
|
||||
}
|
||||
|
||||
public static boolean getConstraintsNotNull(final Field element) throws Exception {
|
||||
public static boolean getConstraintsNotNull(final Field element) throws DataAccessException {
|
||||
final Annotation[] annotation = element.getDeclaredAnnotationsByType(NotNull.class);
|
||||
if (annotation.length == 0) {
|
||||
return false;
|
||||
}
|
||||
if (annotation.length > 1) {
|
||||
throw new Exception(
|
||||
throw new DataAccessException(
|
||||
"Must not have more than 1 element @NotNull on " + element.getClass().getCanonicalName());
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public static Field getPrimaryKeyField(final Class<?> clazz) throws Exception {
|
||||
public static Field getPrimaryKeyField(final Class<?> clazz) throws DataAccessException {
|
||||
for (final Field field : clazz.getFields()) {
|
||||
// static field is only for internal global declaration ==> remove it ..
|
||||
if (java.lang.reflect.Modifier.isStatic(field.getModifiers())) {
|
||||
@ -292,7 +297,7 @@ public class AnnotationTools {
|
||||
return null;
|
||||
}
|
||||
|
||||
public static boolean isPrimaryKey(final Field element) throws Exception {
|
||||
public static boolean isPrimaryKey(final Field element) throws DataAccessException {
|
||||
final Annotation[] annotation = element.getDeclaredAnnotationsByType(Id.class);
|
||||
if (annotation.length == 0) {
|
||||
return false;
|
||||
@ -300,51 +305,51 @@ public class AnnotationTools {
|
||||
return true;
|
||||
}
|
||||
|
||||
public static boolean isUnique(final Field element) throws Exception {
|
||||
public static boolean isUnique(final Field element) throws DataAccessException {
|
||||
final Annotation[] annotation = element.getDeclaredAnnotationsByType(Column.class);
|
||||
if (annotation.length == 0) {
|
||||
return false;
|
||||
}
|
||||
if (annotation.length > 1) {
|
||||
throw new Exception(
|
||||
throw new DataAccessException(
|
||||
"Must not have more than 1 element @Column on " + element.getClass().getCanonicalName());
|
||||
}
|
||||
return ((Column) annotation[0]).unique();
|
||||
}
|
||||
|
||||
public static GenerationType getStrategy(final Field element) throws Exception {
|
||||
public static GenerationType getStrategy(final Field element) throws DataAccessException {
|
||||
final Annotation[] annotation = element.getDeclaredAnnotationsByType(GeneratedValue.class);
|
||||
if (annotation.length == 0) {
|
||||
return null;
|
||||
}
|
||||
if (annotation.length > 1) {
|
||||
throw new Exception(
|
||||
throw new DataAccessException(
|
||||
"Must not have more than 1 element @Column on " + element.getClass().getCanonicalName());
|
||||
}
|
||||
return ((GeneratedValue) annotation[0]).strategy();
|
||||
}
|
||||
|
||||
public static boolean isDeletedField(final Field element) throws Exception {
|
||||
public static boolean isDeletedField(final Field element) throws DataAccessException {
|
||||
return element.getDeclaredAnnotationsByType(DataDeleted.class).length != 0;
|
||||
}
|
||||
|
||||
public static boolean isCreatedAtField(final Field element) throws Exception {
|
||||
public static boolean isCreatedAtField(final Field element) throws DataAccessException {
|
||||
return element.getDeclaredAnnotationsByType(CreationTimestamp.class).length != 0;
|
||||
}
|
||||
|
||||
public static boolean isUpdateAtField(final Field element) throws Exception {
|
||||
public static boolean isUpdateAtField(final Field element) throws DataAccessException {
|
||||
return element.getDeclaredAnnotationsByType(UpdateTimestamp.class).length != 0;
|
||||
}
|
||||
|
||||
public static boolean isdefaultNotRead(final Field element) throws Exception {
|
||||
public static boolean isdefaultNotRead(final Field element) throws DataAccessException {
|
||||
return element.getDeclaredAnnotationsByType(DataNotRead.class).length != 0;
|
||||
}
|
||||
|
||||
public static boolean isIdField(final Field element) throws Exception {
|
||||
public static boolean isIdField(final Field element) throws DataAccessException {
|
||||
return element.getDeclaredAnnotationsByType(Id.class).length != 0;
|
||||
}
|
||||
|
||||
public static String getDeletedFieldName(final Class<?> clazz) throws Exception {
|
||||
public static String getDeletedFieldName(final Class<?> clazz) throws DataAccessException {
|
||||
try {
|
||||
for (final Field elem : clazz.getFields()) {
|
||||
// static field is only for internal global declaration ==> remove it ..
|
||||
@ -361,7 +366,7 @@ public class AnnotationTools {
|
||||
return null;
|
||||
}
|
||||
|
||||
public static String getUpdatedFieldName(final Class<?> clazz) throws Exception {
|
||||
public static String getUpdatedFieldName(final Class<?> clazz) throws DataAccessException {
|
||||
try {
|
||||
for (final Field elem : clazz.getFields()) {
|
||||
// static field is only for internal global declaration ==> remove it ..
|
||||
@ -395,15 +400,16 @@ public class AnnotationTools {
|
||||
return null;
|
||||
}
|
||||
|
||||
public static List<String> getFieldsNames(final Class<?> clazz) throws Exception {
|
||||
public static List<String> getFieldsNames(final Class<?> clazz) throws DataAccessException {
|
||||
return getFieldsNamesFilter(clazz, false);
|
||||
}
|
||||
|
||||
public static List<String> getAllFieldsNames(final Class<?> clazz) throws Exception {
|
||||
public static List<String> getAllFieldsNames(final Class<?> clazz) throws DataAccessException {
|
||||
return getFieldsNamesFilter(clazz, true);
|
||||
}
|
||||
|
||||
private static List<String> getFieldsNamesFilter(final Class<?> clazz, final boolean full) throws Exception {
|
||||
private static List<String> getFieldsNamesFilter(final Class<?> clazz, final boolean full)
|
||||
throws DataAccessException {
|
||||
final List<String> out = new ArrayList<>();
|
||||
for (final Field field : clazz.getFields()) {
|
||||
// static field is only for internal global declaration ==> remove it ..
|
||||
@ -418,12 +424,12 @@ public class AnnotationTools {
|
||||
return out;
|
||||
}
|
||||
|
||||
public static boolean isGenericField(final Field elem) throws Exception {
|
||||
public static boolean isGenericField(final Field elem) throws DataAccessException {
|
||||
return AnnotationTools.isPrimaryKey(elem) || AnnotationTools.isCreatedAtField(elem)
|
||||
|| AnnotationTools.isUpdateAtField(elem) || AnnotationTools.isDeletedField(elem);
|
||||
}
|
||||
|
||||
public static Field getFieldOfId(final Class<?> clazz) throws Exception {
|
||||
public static Field getFieldOfId(final Class<?> clazz) throws DataAccessException {
|
||||
for (final Field field : clazz.getFields()) {
|
||||
// static field is only for internal global declaration ==> remove it ..
|
||||
if (java.lang.reflect.Modifier.isStatic(field.getModifiers())) {
|
||||
@ -436,7 +442,7 @@ public class AnnotationTools {
|
||||
return null;
|
||||
}
|
||||
|
||||
public static Field getFieldNamed(final Class<?> clazz, final String name) throws Exception {
|
||||
public static Field getFieldNamed(final Class<?> clazz, final String name) throws DataAccessException {
|
||||
for (final Field field : clazz.getFields()) {
|
||||
// static field is only for internal global declaration ==> remove it ..
|
||||
if (java.lang.reflect.Modifier.isStatic(field.getModifiers())) {
|
||||
|
Loading…
x
Reference in New Issue
Block a user