我在堆棧上尋找了一段時間的答案。所有的答案看起來像他們說我已經有了正確的答案,但我仍然繼續在下面的構造函數中獲得第一行的類拋出異常。類拋出異常泛型反射參數化類型
SEVERE: Exception while loading the app : EJB Container initialization error
java.lang.ClassCastException: java.lang.Class cannot be cast to java.lang.reflect.ParameterizedType
at com.domain.security.logging.ElsAbstractCrudClass.<init>(ElsAbstractCrudClass.java:54)
這是代碼。看完文檔後,我仍然無法弄清楚。我對泛型和思考比較陌生,所以需要一些幫助。 TIA。
public abstract class ElsAbstractCrudClass<T> {
Class<T> entity;
public ElsAbstractCrudClass() {
[line 54] ParameterizedType genericSuperclass = (ParameterizedType) getClass().getGenericSuperclass();
Type type = genericSuperclass.getActualTypeArguments()[0];
this.entity = (Class<T>) type;
}
}
這裏是抽象的CRUD類的子類(SessionLog是JPA實體):
@Stateless
public class SessionLogger extends ElsAbstractCrudClass<SessionLog> {
@PersistenceContext(unitName = "ELS_Soulard_PU")
private EntityManager em;
@EJB
DozerInstantiator di;
//SessionLog entity;
//SessionLog sessionLog = new SessionLog();
static final Logger logger = Logger.getLogger(SessionLogger.class.getSimpleName());
public SessionLogger() {
}
...
打印出'getClass()'和'getClass()。getGenericSuperclass()'來查看它們的效果可能很有用 – newacct 2012-07-21 02:38:30
好主意。我會在今天晚些時候嘗試,也許或明天取決於時間...但我會嘗試。 – BillR 2012-07-21 17:20:26
僅供參考......我的開發箱彈了一下,我正在重建......所以我還沒有試過。 :) – BillR 2012-07-25 15:18:19