用Java動態建立類
我試圖找到有關這方面的資訊,但空手而歸:
我收集可以使用反射或代理在Java中動態建立一個類,但是我無法找出如何.我正在實現一個簡單的資料庫框架,我在這裡建立使用反射的SQL查詢.該方法使用資料庫欄位作為引數獲取物件,並基於此建立查詢.但是如果我也可以動態地建立物件本身是非常有用的,所以我不需要為每個表都有一個簡單的資料包裝器物件.
動態類只需要簡單的欄位(String,Integer,Double),例如
public class Data { public Integer id; public String name; }
這是可能的,我該怎麼做?
編輯:這是我將如何使用:
/** 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; }
),但是不應該這樣做.為什麼?
>使用庫的程式碼應該期望型別Object並使用反射獲取所有的欄位 – 不是一個好主意
> java是靜態型別的語言,你想介紹動態型別 – 這不是地方.
如果您只是想以未定義格式的資料,則可以將其返回到陣列中,例如Object []或Map<String,Object>如果你想要他們命名,並從那裡得到它 – 它將節省你不必要的類生成的麻煩,唯一的目的是包含一些將通過反射獲得的資料.
可以做的是具有預定義的類來儲存資料,並將它們作為引數傳遞給查詢方法.例如:
public <T> T executeQuery(Class<T> expectedResultClass, String someArg, Object.. otherArgs) {..}
因此,您可以在傳遞的expectedResultClass上使用反射來建立該型別的新物件,並使用查詢的結果進行填充.
也就是說,我想你可以使用現有的東西,如ORM框架(Hibernate,EclipseLink),spring的JdbcTemplate等.
http://stackoverflow.com/questions/2320404/creating-classes-dynamically-with-java
本站文章除註明轉載外,均為本站原創或編譯
轉載請明顯位置註明出處:用Java動態建立類