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! :)
在调用Field.set
之前调用Field.set
Field.setAccessible(true)
。
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.