Reputation: 3
Sorry didnt know who to formulate the question best, but i will try to explain my Problem here, i have following class:
public class ReadOnlyTable<T extends Model> implements ReadOnly<T> {
...
protected ReadOnlyTable() {
initTable();
}
protected void reloadDataSource() {
initTable();
}
...
@Override
public ArrayList<T> findAll() {
ArrayList<T> result = null;
try {
long startTime = System.currentTimeMillis();
result = datasource.getAllEntries();
// // Logger.getLogger().write("FindAllQuery time was " + (System.currentTimeMillis() - startTime) + "ms");
} catch (Exception e) {
e.printStackTrace();
return null;
}
return result;
}
Lets say T is class "Galaxy", then i tried to loop over all elements in the returned Array, with following Code
for (Galaxy gal : gDAO.findAll()) {
}
Why does it give me an error that Galaxy is required, but Object is found?? What am i doing wrong, that i have return type ArrayList and not ArrayList<T>
EDIT 1:
gDAO is defined this way
private static GalaxyDAO gDAO = (GalaxyDAO) DAOFactory.get(GalaxyDAO.class);
And DAOFactory looks like this
public static <T> T get(Class clazz) {
if (!instanceList.containsKey(clazz)) {
try {
GenericDAO genericDAO = null;
Constructor constructor;
constructor = clazz.getDeclaredConstructor();
constructor.setAccessible(true);
genericDAO = (GenericDAO) constructor.newInstance();
instanceList.put(clazz, genericDAO);
return (T)genericDAO;
} catch (NoSuchMethodException ex) {
DebugBuffer.writeStackTrace(DAOFactory.class.getName(), ex);
} catch (SecurityException ex) {
DebugBuffer.writeStackTrace(DAOFactory.class.getName(), ex);
} catch (InstantiationException ex) {
DebugBuffer.writeStackTrace(DAOFactory.class.getName(), ex);
} catch (IllegalAccessException ex) {
DebugBuffer.writeStackTrace(DAOFactory.class.getName(), ex);
} catch (InvocationTargetException ex) {
DebugBuffer.writeStackTrace(DAOFactory.class.getName(), ex);
}
}else{
return (T)instanceList.get(clazz);
}
return null;
}
and finally
public class GalaxyDAO extends ReadWriteTable<Galaxy> implements GenericDAO {
and yeah .. ReadWriteTable extends ReadOnlyTable
public abstract class ReadWriteTable<T extends Model> extends ReadOnlyTable implements ReadWrite<T> {
public ReadWriteTable() {
super();
}
Appendix to acknowledged solution
I had a mistake in my inferfaces which prevented to give the Type to ReadOnlyTable see
public interface ReadWrite<T> extends ReadOnly {
instead of
public interface ReadWrite<T> extends ReadOnly<T> {
after fixing that i could also change following line
public abstract class ReadWriteTable<T extends Model> extends ReadOnlyTable<T> implements ReadWrite<T> {
Upvotes: 0
Views: 1269
Reputation: 32273
gDAO
is an instance of your class ReadOnlyTable<T extends Model>
? What is it's type parameter (how did you declare it)?
It has to be Galaxy
, otherwise it will not work.
Update: Now that you posted more code, the problem is that you have to pass the type parameter Galaxy
to the class ReadOnlyTable<T extends Model>
, where the findAll
method is, otherwise it doesn't know what T is, and will return ArrayList<Object>
.
This line is the problem:
public abstract class ReadWriteTable<T extends Model> extends ReadOnlyTable implements ReadWrite<T>
The class ReadOnlyTable also has to receive the type parameter:
ReadOnlyTable<T extends Model>
So the line has to be:
public abstract class ReadWriteTable<T extends Model> extends ReadOnlyTable<T extends Model> implements ReadWrite<T>
Upvotes: 1
Reputation: 4712
the problem is the you declare GalaxyDAO as a row type, it should be declared like that:
public class GalaxyDAO<Galaxy> extends ReadWriteTable<Galaxy> implements GenericDAO
I'm suer that you got a compiler warning on it, don't ignore warning, you should threat them almost like errors!
Upvotes: 0
Reputation: 122364
The problem is the ReadWriteTable
declaration, you've declared it as
public abstract class ReadWriteTable<T extends Model> extends ReadOnlyTable implements ReadWrite<T>
extending the raw type ReadOnlyTable
rather than the parameterized ReadOnlyTable<T>
. Change it to
public abstract class ReadWriteTable<T extends Model> extends ReadOnlyTable<T> implements ReadWrite<T>
Upvotes: 0