Alex
Alex

Reputation: 3423

How to fix Collection<type> import issues in Eclipse

In an Spring MVC AOP project I have two interfaces and two classes, beyond a Model class as follows:

  1. ActivityDao (Interface)
  2. ActivityDaoImpl (Class)
  3. ActivityService (Interface)
  4. ActivityServiceImpl (Class)
  5. Activity (Class)

Model class (n. 5):

package com.mycompany.myproject.model;
//... imports
@Entity
public class Activity {
  @NotNull
  private long id;
  private String description;
  public Activity() {
  }
  //... getters and setters
}

Dao Interface (n. 1):

package com.mycompany.myproject.dao;
import com.mycompany.myproject.model.Activity;
import java.util.Collection;

public interface ActivityDao {
  public Collection<Activity> findAll();
  public void delete(Activity activity);
  public void create(Activity activity);
  public Activity update(Activity activity);
  }
}

Dao Implementation (n. 2):

package com.mycompany.myproject.dao.impl;

import com.mycompany.myproject.model.Activity;
import com.mycompany.myproject.dao.ActivityDao;
import java.util.Collection;
// ... other imports
@Repository
public class ActivityDaoImpl implements ActivityDao {

  //... Spring autowiring, etc.

  public Collection<Activity> findAll(){ //...
  }
  // other methods implementation
}

Service interface (n. 3):

package com.mycompany.myproject.service;
import com.mycompany.myproject.model.Activity;
import java.util.Collection;
//... other imports

public interface ActivityService {
  public Collection<Activity> getAllActivities();
  // other method statements
}

Service Class (n. 4):

package com.mycompany.myproject.service.impl;
import com.mycompany.myproject.model.Activity;
import com.mycompany.myproject.dao.ActivityDao;
import java.util.Collection;
import com.mycompany.myproject.service.ActivityService;
// ... other imports
@Service
@Transactional(readOnly = true) 
public class ActivityServiceImpl implements ActivityService {

  @Autowired
  ActivityDao activityDao;      

  public Collection<Activity> getAllActivities(){ //<=== ERROR!
     return activityDao.findAll(); 
  }
  // other methods implementation
}

Java grammar checkingg of Eclipse is showin errors in every method with Collection returning type in Service Class implementation (n.4) warning that the return type is incompatible with Dao's method return.

When accepting suggestions from quick-fix dialog, Eclipse change service's method type to Collection<com.mycompany.myproject.service.Activity>, instead of Collection<com.mycompany.myproject.model.Activity>, that is the right place for the Model Class (or even better just Collection<Activity>, as it would be reasonable).

At a first glance, it seems that the java grammar cheker in Eclipse environment is confused about the Collection type from interface and Collection from Dao class, threating it as different ones, when they aren't.

Any idea on how to fix this very annoyng issue, or what I could be doing wrong? Thanks a lot!

Upvotes: 0

Views: 609

Answers (1)

Alex
Alex

Reputation: 3423

I've fixed the issue. Apparently Eclipse was doing some confusion with classes from another project in buildpath. Forcing it to update import statements throung fix pannel suggestions, it lauched the import statements from the related project in build path. Then again writting the right import statements seems to be updated the compiler links and now errors are not being shown anymore.

Upvotes: 1

Related Questions