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

Bug 416001

Summary: APT creates unnecessary element instances for each annotation on it
Product: [Eclipse Project] JDT Reporter: Jay Arthanareeswaran <jarthana>
Component: APTAssignee: Jay Arthanareeswaran <jarthana>
Status: RESOLVED FIXED QA Contact:
Severity: normal    
Priority: P3 CC: eclipse, srikanth_sankaran
Version: 4.3   
Target Milestone: BETA J8   
Hardware: PC   
OS: Windows 7   
Whiteboard:

Description Jay Arthanareeswaran CLA 2013-08-28 00:35:49 EDT
Consider this code fed to the APT:

@TypeUseAnnotation("class") @Deprecated
public class TypeAnnotationTest extends @TypeUseAnnotation("super") Object {}

And the following code in AnnotationDiscoveryVisitor:

private void resolveAnnotations(
		BlockScope scope,
		Annotation[] annotations,
		Binding currentBinding) {
	ASTNode.resolveAnnotations(scope, annotations, currentBinding);
	
	for (Annotation annotation : annotations) {
		AnnotationBinding binding = annotation.getCompilerAnnotation();
		if (binding != null) {
			TypeElement anno = (TypeElement)_factory.newElement(binding.getAnnotationType()); 
			Element element = _factory.newElement(currentBinding);
			_annoToElement.put(anno, element);
		}
	}
}

Looks like the new element instance created for the currentBinding is just not necessary.
Comment 1 Walter Harley CLA 2013-08-30 01:58:03 EDT
I'm not sure I understand what you're proposing?  Is your intention to reuse the same Element for multiple bindings, or ... ?
Comment 2 Jay Arthanareeswaran CLA 2013-08-30 02:03:46 EDT
(In reply to comment #1)
> I'm not sure I understand what you're proposing?  Is your intention to reuse
> the same Element for multiple bindings, or ... ?

No, I was saying we can do with just one Element for a binding (not annotation binding). To be specific, this line:

Element element = _factory.newElement(currentBinding);

creates one element instance for the same binding multiple times depending on number of annotations. In the example I mentioned in comment #0, we will end up create two TypeElement for 'TypeAnnotationTest' because the type declaration has two annotations.
Comment 3 Walter Harley CLA 2013-08-30 03:47:40 EDT
Oh, I see.  Yes, that *should* be safe - after all, those elements would have all had the same _referenceBinding internally anyway, so having them be distinct objects doesn't seem to be serving any purpose.

I have to say that in real-world code it seems moderately rare for an element to have more than one annotation (though certainly not unheard of) so I am not sure how much this actually costs us.
Comment 4 Jay Arthanareeswaran CLA 2013-08-30 04:05:18 EDT
(In reply to comment #3)
> I have to say that in real-world code it seems moderately rare for an
> element to have more than one annotation (though certainly not unheard of)
> so I am not sure how much this actually costs us.

Not much I guess. Since I will be touching this part of the code soon, I just thought will fix this along the way.
Comment 5 Srikanth Sankaran CLA 2013-10-09 15:01:55 EDT
Subsumed by bug 413613