访问静态内部类的Java可能会抛出NoSuchFieldException

时间:2017-07-31 08:26:47

标签: java inner-classes

在Java中,为什么调用静态嵌套类将无法编译,因为潜在的NoSuchFieldException和IllegalArgumentEception?

这是我的课程:

public class DBRef {
    public static class CMS_FILE_ROOM extends BuildableDatabaseTable {

        public static String _table_name = "cms_file_ROOM";
        public static BuildableColumn _ALL = new BuildableColumn._ALL(getCurrentClass());

    }


    public static SelectQuery SELECT(final BuildableColumn... columnsToSelect) {
        return new SelectQuery(columnsToSelect);
    }
}

public class SelectQuery extends Query {

    public SelectQuery(final BuildableColumn... columnsToSelect) {
        super();
        for (final BuildableColumn column : columnsToSelect) {
            this.columns.add(column.toSQL());
        }
    }

    public Query FROM(final Class<? extends BuildableDatabaseTable> tableClass) throws NoSuchFieldException, IllegalAccessException {
        this.froms.add(DatabaseAccesser.toSQL(tableClass));
        return this;
    }
}

// Method called in both cases just above by some poor designed methods redirection (my bad). But exceptions are catched.
public static String toSQL(final Class<? extends BuildableDatabaseTable> table) {
    try {
        return (String) table.getField("_table_name").get(null);
    } catch (final IllegalAccessException e) {
        e.printStackTrace();
    } catch (final NoSuchFieldException e) {
        e.printStackTrace();
    }
    return "ERROR";
}

当我从我的代码中的任何其他地方开始:

SelectQuery lSelectQuery = (SelectQuery) DBRef.SELECT(DBRef.CMS_FILE_ROOM._ALL)
            .FROM(DBRef.CMS_FILE_ROOM.class);

我得到以下(编译时)错误(在.class调用上): Error message

我找不到原因,我可以把它嵌入试试中,但我想了解原因?

0 个答案:

没有答案