Bug 534892 - Warn about duplicate application of @PostConstruct
Summary: Warn about duplicate application of @PostConstruct
Status: NEW
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Core (show other bugs)
Version: 4.6   Edit
Hardware: PC Windows 10
: P3 enhancement (vote)
Target Milestone: ---   Edit
Assignee: JDT-Core-Inbox CLA
QA Contact:
URL:
Whiteboard:
Keywords: ui
Depends on:
Blocks:
 
Reported: 2018-05-19 14:06 EDT by Subash Janarthanan CLA
Modified: 2020-06-05 18:37 EDT (History)
5 users (show)

See Also:


Attachments
Attachment contains the sample project I have created a part with 3 methods that are annotated with @PostConstruct annotation (15.21 KB, application/x-zip-compressed)
2018-05-19 14:06 EDT, Subash Janarthanan CLA
no flags Details

Note You need to log in before you can comment on or make changes to this bug.
Description Subash Janarthanan CLA 2018-05-19 14:06:57 EDT
Created attachment 274110 [details]
Attachment contains the sample project I have created a part with 3 methods that are annotated with @PostConstruct annotation

According to the JAVA API specification (https://docs.oracle.com/javaee/7/api/javax/annotation/PostConstruct.html), 
A method annotated with @PostConstruct is called after a class is initialized with its constructor and after all fields have been injected. Only one method can be annotated with this annotation.
I have referred  

But in my sample part class code I have annotated 3 different methods with @PostConstruct annotation. And it dosen't give neither warning nor error. When I try to execute the application it runs without any exception/error and displays UI with all 3 @PostConstruct annotated method creation. 
Even the order of the method execution is not stable i.e., sometimes second method annotated with postConstruct gets executes and few times first method annotated with postConstruct gets executes and UI provides different output according to this execution.

I was expecting a warning/error for this sort of situation. Please let me know am if I misunderstood something different here.

Steps To Reproduce:
Create a basic eclipse 4 RCP application with a sample part.
In the sample part class add two more methods and annotate with @PostConstruct like below and run the product.

public class SamplePart {

	// postConstruct one
	@PostConstruct
	public void createComposite(Composite parentOne) {
		parentOne.setLayout(new GridLayout(1, false));
		Text txtInputOne = new Text(parentOne, SWT.BORDER);
		txtInputOne.setMessage("postConstruct one");
	}

	// postConstruct Two
	@PostConstruct
	public void createCompositeTwo(Composite parentTwo) {
		parentTwo.setLayout(new GridLayout(1, false));
		Text txtInputTwo = new Text(parentTwo, SWT.BORDER);
		txtInputTwo.setMessage("postConstruct two");
	}
	
	// postConstruct Three
	@PostConstruct
	public void createCompositeThree(Composite parentThree) {
		parentThree.setLayout(new GridLayout(1, false));
		Text txtInputThree = new Text(parentThree, SWT.BORDER);
		txtInputThree.setMessage("postConstruct three");
	}
}


More information:
I have attached the code snippet that demonstrates (what is in my opinion) the problem.

Environment I have used: 
Eclipse Version: Neon.2 Release (4.6.2)
Java : "1.8.0_111"
Eclispe e4 tools version installed:   Eclipse e4 Tools	4.5.100.v20160503-0947
Comment 1 Jay Arthanareeswaran CLA 2018-05-21 04:16:34 EDT
On quick look, the behavior is same as Javac. Moving to JDT Core for further discussion.
Comment 2 Stephan Herrmann CLA 2018-05-24 03:15:24 EDT
Given that the javadoc explicitly disallows multiple methods with this annotation, it seems fair to let the compiler raise a configurable problem. Should we worry about the transition from https://docs.oracle.com/javase/9/docs/api/java.xml.ws.annotation-summary.html to https://github.com/eclipse-ee4j/common-annotations-api ?