Bug 353380 - Produce compilation warning when concrete class is used instead of interface
Summary: Produce compilation warning when concrete class is used instead of interface
Status: NEW
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Core (show other bugs)
Version: 3.7   Edit
Hardware: All All
: P3 enhancement (vote)
Target Milestone: ---   Edit
Assignee: JDT-Core-Inbox CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2011-07-29 02:24 EDT by Alexander Radzin CLA
Modified: 2011-07-31 00:25 EDT (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Alexander Radzin CLA 2011-07-29 02:24:36 EDT
Build Identifier: 20110218-0911

There are several situations when people by mistake use concrete classes where interface should be used:
// Using concrete class to the left of the assignment
ArrayList<String> a = new ArrayList<String>();

// Using concrete class into the generics
new ArrayList<ArrayList<String>>

// Using concrete class as an method parameter or return type
HashSet<Integer> foo(ArrayList<String> list){}



Probably more complicated case is when people use "wrong" interface, e.g.

List<String> list = new ArrayList<String>();
for (String s : list) {/*do somthing*/}

List is not needed here. Collection is enough:

Collection<String> list = new ArrayList<String>();
for (String s : list) {/*do somthing*/}


I think that it could be a good feature if IDE can produce such types of warnings. It can help people to write better and more reusable code. 

This link provides additional details and an attempt to solve the problem using regular expressions: 
http://alexradzin.blogspot.com/2011/07/find-bad-coding-practices-using-regular.html



Reproducible: Always