如何在子类上下文中执行从超类继承的方法?

发布于 2025-01-04 18:38:42 字数 2298 浏览 0 评论 0原文

我正在实现一个类似 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

与他有关 2025-01-11 18:38:42

在调用 Field.set 之前调用 Field.setAccessible(true)

call Field.setAccessible(true) before calling Field.set.

秉烛思 2025-01-11 18:38:42

这是正常的,抽象类无法构造。
那么就不可能构建它。
当您编写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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文