Klassen dynamisch mit Java erstellen

Habe ich versucht, Informationen zu finden, diese haben aber kommen mit leeren Händen:

Entnehme ich, es ist möglich, eine Klasse zu erstellen, die dynamisch in Java unter Verwendung von reflektion oder proxies, aber ich kann nicht herausfinden, wie. Ich bin die Implementierung einer einfachen Datenbank-framework, wo ich erstellen Sie die SQL-Abfragen unter Verwendung von reflektion ab. Die Methode bekommt das Objekt mit der Datenbank-Felder als parameter und die Abfrage erstellt, die Grundlage. Aber es wäre sehr hilfreich, wenn ich kann auch das Objekt selbst dynamisch, so würde ich nicht über die Notwendigkeit zu haben, eine einfache Daten-wrapper-Objekt für jeden Tisch.

Den dynamischen Klassen würden nur noch einfache Felder (StringIntegerDouble), z.B.

public class Data {
  public Integer id;
  public String name;
}

Ist das möglich und wie kann ich dies tun?

EDIT: Dies ist, wie würde ich dieses verwenden:

/** Creates an SQL query for updating a row's values in the database.
 *
 * @param entity Table name.
 * @param toUpdate Fields and values to update. All of the fields will be
 * updated, so each field must have a meaningful value!
 * @param idFields Fields used to identify the row(s).
 * @param ids Id values for id fields. Values must be in the same order as
 * the fields.
 * @return
 */
@Override
public String updateItem(String entity, Object toUpdate, String[] idFields,
        String[] ids) {
    StringBuilder sb = new StringBuilder();

    sb.append("UPDATE ");
    sb.append(entity);
    sb.append("SET ");

    for (Field f: toUpdate.getClass().getDeclaredFields()) {
        String fieldName = f.getName();
        String value = new String();
        sb.append(fieldName);
        sb.append("=");
        sb.append(formatValue(f));
        sb.append(",");
    }

    /* Remove last comma */
    sb.deleteCharAt(sb.toString().length()-1);

    /* Add where clause */
    sb.append(createWhereClause(idFields, ids));

    return sb.toString();
}
 /** Formats a value for an sql query.
 *
 * This function assumes that the field type is equivalent to the field
 * in the database. In practice this means that this field support two
 * types of fields: string (varchar) and numeric.
 *
 * A string type field will be escaped with single parenthesis (') because
 * SQL databases expect that. Numbers are returned as-is.
 *
 * If the field is null, a string containing "NULL" is returned instead.
 * 
 * @param f The field where the value is.
 * @return Formatted value.
 */
String formatValue(Field f) {
    String retval = null;
    String type = f.getClass().getName();
    if (type.equals("String")) {
        try {
            String value = (String)f.get(f);
            if (value != null) {
                retval = "'" + value + "'";
            } else {
                retval = "NULL";
            }
        } catch (Exception e) {
            System.err.println("No such field: " + e.getMessage());
        }
    } else if (type.equals("Integer")) {
        try {
            Integer value = (Integer)f.get(f);
            if (value != null) {
                retval = String.valueOf(value);
            } else {
                retval = "NULL";
            }
        } catch (Exception e) {
            System.err.println("No such field: " + e.getMessage());
        }
    } else {
        try {
            String value = (String) f.get(f);
            if (value != null) {
                retval = value;
            } else {
                retval = "NULL";
            }
        } catch (Exception e) {
            System.err.println("No such field: " + e.getMessage());
        }
    }
    return retval;
}

InformationsquelleAutor der Frage Makis | 2010-02-23

Schreibe einen Kommentar