Przeglądaj źródła

Ajout fonction recupération class Table

Arthur Brandao 5 lat temu
rodzic
commit
045d54a0b0
1 zmienionych plików z 21 dodań i 0 usunięć
  1. 21 0
      src/db/DatabaseUtils.java

+ 21 - 0
src/db/DatabaseUtils.java

@@ -2,6 +2,7 @@ package db;
 
 import java.lang.annotation.Annotation;
 import java.lang.reflect.Field;
+import java.lang.reflect.InvocationTargetException;
 
 import db.annotation.DbField;
 import db.annotation.DbId;
@@ -78,6 +79,26 @@ public class DatabaseUtils {
 		}
 	}
 	
+	public static DatabaseTable<?> getDatabaseTable(Field f) {
+		if (!(isDbLink(f) && isPersistable(f.getType()))) {
+			return null;
+		}
+		String pckg = getDbLink(f);
+		String[] split = f.getType().getName().split("\\.");
+		String className = split[split.length - 1];
+		return getDatabaseTable(pckg, className);
+	}
+	
+	public static DatabaseTable<?> getDatabaseTable(String pckg, String className) {
+		Class<?> clazz;
+		try {
+			clazz = Class.forName(pckg + "." + className + "Table");
+			return (DatabaseTable<?>) clazz.getMethod("getInstance").invoke(null);
+		} catch (ClassNotFoundException | IllegalAccessException | IllegalArgumentException | InvocationTargetException | NoSuchMethodException | SecurityException e) {
+			throw new IllegalStateException("Unable to load " + className + "Table in the package " + pckg, e);
+		}
+	}
+	
 	private static boolean isAnnotation(Field f, Class<? extends Annotation> clazz) {
 		return f.isAnnotationPresent(clazz);
 	}