Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.

Bug 353380

Summary: Produce compilation warning when concrete class is used instead of interface
Product: [Eclipse Project] JDT Reporter: Alexander Radzin <alexander_radzin>
Component: CoreAssignee: JDT-Core-Inbox <jdt-core-inbox>
Status: NEW --- QA Contact:
Severity: enhancement    
Priority: P3 CC: abunetta
Version: 3.7   
Target Milestone: ---   
Hardware: All   
OS: All   
Whiteboard:

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