Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Intellij Idea - ambiguous method call

I encountered a strange error related to Java using Intellij Idea.

So there is such interface :

<T> void save(T aEntity, DbTransaction dbTransaction, Class<T> clazz);
<T> void save(Collection<T> aEntities, DbTransaction dbTransaction, Class<T> clazz);

When I try to compile the next code :

@SuppressWarnings("unchecked")
@Override
public void save(Collection<T> aEntities, DbTransaction aDbTransaction) {
    baseDao.save(aEntities, aDbTransaction, getClass((T) aEntities.toArray()[0]));
}

I receive the next compilation error :

reference to save is ambiguous, both method <T>save(T,DbEntityHelper.DbTransaction,java.lang.Class<T>) in xzc.dao.IBaseDao and method <T>save(java.util.Collection<T>,DbEntityHelper.DbTransaction,java.lang.Class<T>) in xzc.dao.IBaseDao match

Do you have any ideas ? Thanks for help in advance.

like image 839
StKiller Avatar asked Aug 06 '12 12:08

StKiller


1 Answers

You have two types called T and it can't assume they are the same. T could be Collection<T> in the second method or it could be T in the first.

You can use

baseDao.<T>save(....

or

baseDao.<Collection<T>>save(....

to make it clear which one it should be. Making them the T same name doesn't help the compiler and may just be confusing.

like image 166
Peter Lawrey Avatar answered Oct 04 '22 00:10

Peter Lawrey