From cd9c776dd8d58cb6807e7e8df2100d14d6fac846 Mon Sep 17 00:00:00 2001 From: Marc Baloup Date: Thu, 11 Apr 2019 15:47:19 +0200 Subject: [PATCH] Try to fix and improve ORM initialisation (more explicit on reasons why a field is not initialized) --- .../fr/pandacube/java/util/orm/SQLElement.java | 18 ++++++++++++++---- .../fr/pandacube/java/util/orm/SQLFKField.java | 7 +++---- 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/src/main/java/fr/pandacube/java/util/orm/SQLElement.java b/src/main/java/fr/pandacube/java/util/orm/SQLElement.java index 02f368b..cb7898c 100644 --- a/src/main/java/fr/pandacube/java/util/orm/SQLElement.java +++ b/src/main/java/fr/pandacube/java/util/orm/SQLElement.java @@ -1,5 +1,6 @@ package fr.pandacube.java.util.orm; +import java.lang.reflect.Modifier; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; @@ -97,14 +98,23 @@ public abstract class SQLElement> { java.lang.reflect.Field[] declaredFields = getClass().getDeclaredFields(); for (java.lang.reflect.Field field : declaredFields) { - if (!java.lang.reflect.Modifier.isStatic(field.getModifiers())) continue; - if (!field.isAccessible()) continue; - + if (!field.getType().isAssignableFrom(SQLField.class)) + 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 { 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 checkedF = (SQLField) val; 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( "SQLField " + checkedF.getName() + " already exist in " + getClass().getName()); checkedF.setSQLElementType((Class) getClass()); diff --git a/src/main/java/fr/pandacube/java/util/orm/SQLFKField.java b/src/main/java/fr/pandacube/java/util/orm/SQLFKField.java index 3e5009e..96559b5 100644 --- a/src/main/java/fr/pandacube/java/util/orm/SQLFKField.java +++ b/src/main/java/fr/pandacube/java/util/orm/SQLFKField.java @@ -51,10 +51,9 @@ public class SQLFKField, T, P extends SQLElement

> ext } catch (ORMInitTableException 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 (!type.equals(fkF.type)) - throw new IllegalArgumentException("foreignKeyField (" + fkF.type.sqlDeclaration + ") and current Field (" + type.sqlDeclaration + ") must have the same SQLType"); + + if (fkF.getSQLElementType() == null) + 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"); sqlPrimaryKeyField = fkF; sqlForeignKeyElemClass = fkEl; }