Bug 386692 - Missing "unused" warning on "autowired" fields
Summary: Missing "unused" warning on "autowired" fields
Status: VERIFIED FIXED
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Core (show other bugs)
Version: 4.2   Edit
Hardware: PC Linux
: P3 normal with 11 votes (vote)
Target Milestone: 4.5 M7   Edit
Assignee: Ulrich Grave CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2012-08-06 11:30 EDT by Michael Piefel CLA
Modified: 2020-04-26 07:19 EDT (History)
13 users (show)

See Also:


Attachments
Spring Autowired Patch (106.46 KB, patch)
2014-10-16 05:06 EDT, Ulrich Grave CLA
no flags Details | Diff
Fixed Autowired Patch (14.92 KB, patch)
2014-10-16 09:10 EDT, Ulrich Grave CLA
no flags Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Michael Piefel CLA 2012-08-06 11:30:25 EDT
Consider the following code:
----------------------------

import org.springframework.beans.factory.annotation.Autowired;

public class Auto {
	@Autowired
	private Auto auto1;

	@SuppressWarnings("unused")
	@Autowired
	private Auto auto2;
}


Actual behaviour:
-----------------

There is a warning “Unnecessary @SuppressWarnings("unused")” on field auto2.


Expected behaviour:
-------------------

There should not be a warning on field auto2. There should be “The value of the field Auto.auto1 is not used” warning on field auto1.

Rationale: The @Autowired annotation indeed means it is possible that this field is written at some time, however, the field is never read and therefore it is superfluous. No warnings are left.

This is different for the new version 4.2 – the older 3.8 did not consider @Autowired fields as used. I consider this a regression.
Comment 1 Ayushman Jain CLA 2012-08-07 02:13:13 EDT
Because of the fix for bug 365437, this is expected behaviour. Please see  bug 365437 comment 33 for more detailed explanation. I intend to close as INVALID. (Please also see bug 376590 and let us know if you have a rationale for making an exception for '@Autowired', just like it is requested for '@Inject')
Comment 2 Michael Piefel CLA 2012-08-07 02:47:59 EDT
The annotation @Autowired declares a dependency thus introduce coupling between Spring beans. If these dependencies are unused, the tool should warn me about that because I have the chance to reduce coupling simply by removing these unused fields.

The annotation org.springframework.beans.factory.annotation.Autowired when used on a field will trigger writing (by reflection) at creation time by the Spring container. It does not have any other effect.
Comment 3 Ayushman Jain CLA 2012-08-07 03:16:18 EDT
That way almost all the library annotations should be excluded. I'm not sure how scalable that is. Anyway, marking as dup of bug 376590

*** This bug has been marked as a duplicate of bug 376590 ***
Comment 4 Andrei Aiordachioaie CLA 2013-10-08 03:41:57 EDT
As stated in bug 376590 comment 15, please reopen this bug. 

Goal: also ignore the @Autowired annotation when performing unused field analysis.
Comment 5 Srikanth Sankaran CLA 2013-10-08 04:27:35 EDT
Not promising a fix. Reopening so this is in the radar for 4.4. We have no time to
look at this now. Post March 2014, when the Java 8 work would be complete, we can
take a look - perhaps only to close as WONTFIX.
Comment 6 Jay Arthanareeswaran CLA 2014-02-28 03:08:19 EST
Just housekeeping - assigning the bug to Srikanth.
Comment 7 Srikanth Sankaran CLA 2014-04-08 01:33:59 EDT
(In reply to Srikanth Sankaran from comment #5)
> Not promising a fix. Reopening so this is in the radar for 4.4. We have no
> time to
> look at this now. Post March 2014, when the Java 8 work would be complete,
> we can
> take a look - perhaps only to close as WONTFIX.

Deferring to 4.5 - very busy with maintenance work for Java 8.
Comment 8 Seamus McMorrow CLA 2014-05-15 07:01:09 EDT
This bug is also present when using the javax.annotation.Resource annotation to inject fields. Could that be fixed as part of this also?
Comment 9 Patrick Ruckstuhl CLA 2014-06-05 02:09:24 EDT
Another annotation that should be ignored is @Mock annotation from frameworks such as Mockito, as an alternative, could there simply be an option like "Ignore unused private fields with annotations".
Comment 10 Eduardo Simioni CLA 2014-08-11 06:51:36 EDT
I very much like Patrick's idea. Given the multitude of frameworks one can use nowadays that include annotations, trying to cover each and every case would be an ongoing, frustrating task.

More importantly, people surely will have different opinions on this topic. Myself for instance, never agreed with the fix for the original "issue" (https://bugs.eclipse.org/bugs/show_bug.cgi?id=365437).

Programming tools should, as much as possible, avoid being opinionated, therefore, it would be definitely ideal to give people the freedom to add or remove annotations to the list.
Comment 11 Ulrich Grave CLA 2014-10-16 05:06:59 EDT
Created attachment 247916 [details]
Spring Autowired Patch

I have the same problem with the Spring Autowired annotation. I implemented a patch to fixed this.

Ulrich
Comment 12 Srikanth Sankaran CLA 2014-10-16 05:24:33 EDT
Too many noise diffs in this patch.
Comment 13 Ulrich Grave CLA 2014-10-16 09:10:11 EDT
Created attachment 247924 [details]
Fixed Autowired Patch

I remove the unnecessary changes from the patch. Now the patch contains only the necessary changes. Sorry for that.
Comment 14 Srikanth Sankaran CLA 2014-11-03 21:43:29 EST
Jay, there is a proposed patch - I haven't had time to look at it.

This issue has a colorful history and I don't remember all the details.
Comment 15 Ulrich Grave CLA 2014-11-12 13:14:09 EST
For simpler review commit the same patch to Gerrit: https://git.eclipse.org/r/36356
Comment 16 Jay Arthanareeswaran CLA 2014-11-13 03:52:29 EST
I know I am late in getting into this discussion. If we are fixing this, shouldn't we consider the reported issue in comment #8 also. However, shouldn't we take a moment to think about something that will be future proof? I went through this bug and related ones and found two proposals:

2. Provide a preference to exclude annotated private fields from this analysis.

(or)

2. Provide a way for the user to configure what annotations he would like to be considered.

I prefer the first. The second one will be complicated as it's not just the annotations' name but the members/values too that would dictate the terms.

BTW, I went through the patch and looks good, save few white space issues.
Comment 17 Stephan Herrmann CLA 2014-11-13 16:35:44 EST
I stumbled upon a bogus code comment, see https://git.eclipse.org/r/#/c/36356/1/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/ast/ConstructorDeclaration.java

Regarding "future proof": I don't have more answers than what I already wrote in the discussion of bug 376590, sorry.
Comment 18 Jay Arthanareeswaran CLA 2015-03-25 02:03:21 EDT
(In reply to Ulrich Grave from comment #15)
> For simpler review commit the same patch to Gerrit:
> https://git.eclipse.org/r/36356

Ulrich, can you find some time to look at Stephan's comments on the patch? Thanks!
Comment 19 Jay Arthanareeswaran CLA 2015-04-07 01:47:48 EDT
(In reply to Jay Arthanareeswaran from comment #18)
> (In reply to Ulrich Grave from comment #15)
> > For simpler review commit the same patch to Gerrit:
> > https://git.eclipse.org/r/36356
> 
> Ulrich, can you find some time to look at Stephan's comments on the patch?
> Thanks!

Actually on a close look, the code is still intact. Am I missing something here?
Comment 20 Stephan Herrmann CLA 2015-04-09 09:18:52 EDT
(In reply to Jay Arthanareeswaran from comment #19)
> (In reply to Jay Arthanareeswaran from comment #18)
> > (In reply to Ulrich Grave from comment #15)
> > > For simpler review commit the same patch to Gerrit:
> > > https://git.eclipse.org/r/36356
> > 
> > Ulrich, can you find some time to look at Stephan's comments on the patch?
> > Thanks!
> 
> Actually on a close look, the code is still intact. Am I missing something
> here?

My sincere apology, I don't know what exactly was the problem but it certainly was sitting on my chair.
Comment 22 Jay Arthanareeswaran CLA 2015-04-09 13:09:45 EDT
Thanks Stephan, I have removed the comment and released via Gerrit.
Comment 23 Jay Arthanareeswaran CLA 2015-04-24 06:56:29 EDT
Verified for 4.5 M7 with build I20150422-1000
Comment 24 Stephan Herrmann CLA 2020-04-26 07:19:31 EDT
(In reply to Jay Arthanareeswaran from comment #16)
> [...]
> 2. Provide a way for the user to configure what annotations he would like to
> be considered.
> 
> I prefer the first. The second one will be complicated as it's not just the
> annotations' name but the members/values too that would dictate the terms.

FYI: interested parties may want to join me discussing bug 547451 comment 24.