Try to fix and improve ORM initialisation (more explicit on reasons why a field is not initialized)

This commit is contained in:
Marc Baloup 2019-04-11 15:47:19 +02:00
parent a956f5143e
commit cd9c776dd8
2 changed files with 17 additions and 8 deletions

View File

@ -1,5 +1,6 @@
package fr.pandacube.java.util.orm; package fr.pandacube.java.util.orm;
import java.lang.reflect.Modifier;
import java.sql.PreparedStatement; import java.sql.PreparedStatement;
import java.sql.ResultSet; import java.sql.ResultSet;
import java.sql.SQLException; import java.sql.SQLException;
@ -97,14 +98,23 @@ public abstract class SQLElement<E extends SQLElement<E>> {
java.lang.reflect.Field[] declaredFields = getClass().getDeclaredFields(); java.lang.reflect.Field[] declaredFields = getClass().getDeclaredFields();
for (java.lang.reflect.Field field : declaredFields) { for (java.lang.reflect.Field field : declaredFields) {
if (!java.lang.reflect.Modifier.isStatic(field.getModifiers())) continue; if (!field.getType().isAssignableFrom(SQLField.class))
if (!field.isAccessible()) continue; continue;
if (!Modifier.isStatic(field.getModifiers())) {
Log.severe("[ORM] The field " + field.getDeclaringClass().getName() + "." + field.getName() + " can't be initialized because it is not static.");
continue;
}
field.setAccessible(true);
try { try {
Object val = field.get(null); Object val = field.get(null);
if (val == null || !(val instanceof SQLField)) continue; if (val == null || !(val instanceof SQLField)) {
Log.severe("[ORM] The field " + field.getDeclaringClass().getName() + "." + field.getName() + " can't be initialized because its value is null.");
continue;
}
SQLField<E, ?> checkedF = (SQLField<E, ?>) val; SQLField<E, ?> checkedF = (SQLField<E, ?>) val;
checkedF.setName(field.getName()); checkedF.setName(field.getName());
if (!Modifier.isPublic(field.getModifiers()))
Log.warning("[ORM] The field " + field.getDeclaringClass().getName() + "." + field.getName() + " should be public !");
if (listToFill.containsKey(checkedF.getName())) throw new IllegalArgumentException( if (listToFill.containsKey(checkedF.getName())) throw new IllegalArgumentException(
"SQLField " + checkedF.getName() + " already exist in " + getClass().getName()); "SQLField " + checkedF.getName() + " already exist in " + getClass().getName());
checkedF.setSQLElementType((Class<E>) getClass()); checkedF.setSQLElementType((Class<E>) getClass());

View File

@ -51,10 +51,9 @@ public class SQLFKField<F extends SQLElement<F>, T, P extends SQLElement<P>> ext
} catch (ORMInitTableException e) { } catch (ORMInitTableException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} }
if (!fkEl.equals(fkF.getSQLElementType()))
throw new IllegalArgumentException("foreignKeyField (" + fkF.getSQLElementType().getName() + ") must be from supplied foreignKeyElement (" + fkEl.getName() + ")"); if (fkF.getSQLElementType() == null)
if (!type.equals(fkF.type)) throw new RuntimeException("Can't initialize foreign key. The primary key in the table " + fkEl.getName() + " is not properly initialized and can't be targetted by a forein key");
throw new IllegalArgumentException("foreignKeyField (" + fkF.type.sqlDeclaration + ") and current Field (" + type.sqlDeclaration + ") must have the same SQLType");
sqlPrimaryKeyField = fkF; sqlPrimaryKeyField = fkF;
sqlForeignKeyElemClass = fkEl; sqlForeignKeyElemClass = fkEl;
} }