我试图找到有关这方面的信息,但空手而归:
我收集可以使用反射或代理在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动态创建类