如何在子类上下文中执行从超类继承的方法?
我正在实现一个类似 JPA 的过程:表示表的所有类都必须子类化 DBTable
抽象类,该类包含一个将列名(字符串)链接到 java.lang.reflect.Field< 的 HashMap /code> 我通过在超类构造函数中调用
getClass().getDeclaredFields()
来获得,并从子类构造函数中调用。 这是有效的(我从子类中得到了我想要的成员)。
我想从 ResultSet
动态设置子类的这些成员,该子类将在最初实现的方法中调用 Field.set(this, ResultSet.getXXX(column))
超类作为帮助器,但我得到一个java.lang.IllegalAccessException:Class DBTable can not access a member of class MyTable withmodifiers“protected”
。
所以事实证明该方法是在超类上下文(抽象类!)中执行的:如果我将该方法复制/粘贴到我的子类中,它就可以正常工作。
下面是一个片段:
public abstract class DBTable {
public DBTable() {
hm = new HashMap<String,Field>();
for (Field f : getClass().getDeclaredFields()) {
hm.put(f.getAnnotation(Annot.class).value(), f); // 'annot' gives the column name
}
}
protected boolean setFieldClass(Field f, ResultSet rs, int iCol) {
Class<?> type = f.getType();
if (type == String.class) {
f.set(this, rs.getString(iCol)); // IllegalAccessException
}
}
public boolean read(ResultSet rs) {
ResultSetMetaData meta = rs.getMetaData();
for (int i = 1; i < meta.getColumnCount(); i++) {
Field f = hm.get(meta.getColumnName(i));
setFieldClass(f, rs, i); // IllegalAccessException
}
}
public abstract someAbstractOtherMethod();
}
public class MyTable extends DBTable {
@Annot("DataCol") // Set the column name to "DataCol"
protected String dataStr;
public MyTable() {
super();
}
// 'dataStr' getter/setter
// 'someAbstractOtherMethod' implementation
}
public static void main(String[] args) {
MyTable mt = new MyTable();
ResultSet rs = ...; // Execute SELECT query returning a single row with a "DataCol" column containing a varchar(32)
mt.read(rs); // IllegalAccessException
}
在 MyTable
对象上调用的 read
方法是从 DBTable
类继承的,而该类又调用 setFieldClass
方法抛出错误。 如果我将 setFieldClass
方法从 DBTable
复制/粘贴到 MyTable
,它就可以工作,并且更改 dataStr
成员向公众。
我可以调用 setter 而不是直接设置成员值,但希望避免这样做(我不确定子类是否会实现它,也不确定它们是否都会给出相同的名称)。
感谢您与我分享您的见解! :)
I'm implementing a JPA-like process: all classes representing a table must subclass a DBTable
abstract class which contains a HashMap linking column names (String) to java.lang.reflect.Field
I got from calling getClass().getDeclaredFields()
in the super-class constructor, called from the sub-class constructor.
This is working (I get the members I want from the sub-class).
I want to dynamically set these members of the subclass from a ResultSet
which would call the Field.set(this, ResultSet.getXXX(column))
in a method originally implemented in the super-class as a helper, but I get a java.lang.IllegalAccessException: Class DBTable can not access a member of class MyTable with modifiers "protected"
.
So it turns out the method is executed in the super-class context (the abstract class!): if I copy/paste the method in my sub-class it's working fine.
Here is a snippet:
public abstract class DBTable {
public DBTable() {
hm = new HashMap<String,Field>();
for (Field f : getClass().getDeclaredFields()) {
hm.put(f.getAnnotation(Annot.class).value(), f); // 'annot' gives the column name
}
}
protected boolean setFieldClass(Field f, ResultSet rs, int iCol) {
Class<?> type = f.getType();
if (type == String.class) {
f.set(this, rs.getString(iCol)); // IllegalAccessException
}
}
public boolean read(ResultSet rs) {
ResultSetMetaData meta = rs.getMetaData();
for (int i = 1; i < meta.getColumnCount(); i++) {
Field f = hm.get(meta.getColumnName(i));
setFieldClass(f, rs, i); // IllegalAccessException
}
}
public abstract someAbstractOtherMethod();
}
public class MyTable extends DBTable {
@Annot("DataCol") // Set the column name to "DataCol"
protected String dataStr;
public MyTable() {
super();
}
// 'dataStr' getter/setter
// 'someAbstractOtherMethod' implementation
}
public static void main(String[] args) {
MyTable mt = new MyTable();
ResultSet rs = ...; // Execute SELECT query returning a single row with a "DataCol" column containing a varchar(32)
mt.read(rs); // IllegalAccessException
}
The read
method called on a MyTable
object is inherited from the DBTable
class which, in turns, calls the setFieldClass
method which throws the error.
If I copy/paste the setFieldClass
method from DBTable
to MyTable
it works, as well as changing the dataStr
member to public.
I could call a setter instead of directly setting the member value, but would like to avoid doing that (I'm not sure sub-classes will implement it nor will they all give the same names).
Thank you for sharing your insights with me! :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在调用
Field.set
之前调用Field.setAccessible(true)
。call
Field.setAccessible(true)
before callingField.set
.这是正常的,抽象类无法构造。
那么就不可能构建它。
当您编写
public class MyTable extends DBTable
时,编译器会执行继承的方法。没有 super(); 进行测试我认为它可能会起作用。
It is normal, an abstract class can't be constructed .
Then it is impossible to construct it.
When you write
public class MyTable extends DBTable
, the compiler executes the inherited methods.Test without super(); I think it may work.