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

Bug 527475

Summary: Inheritance where methods use a generic parameter as their return type fails.
Product: [Eclipse Project] JDT Reporter: Niels Basjes <eclipse>
Component: CoreAssignee: JDT-Core-Inbox <jdt-core-inbox>
Status: CLOSED WONTFIX QA Contact:
Severity: major    
Priority: P3 CC: stephan.herrmann
Version: 4.7.1a   
Target Milestone: ---   
Hardware: PC   
OS: Linux   
Whiteboard: stalebug
Attachments:
Description Flags
The simplest reproduction I could create of the problem. none

Description Niels Basjes CLA 2017-11-20 06:29:10 EST
Created attachment 271556 [details]
The simplest reproduction I could create of the problem.

I wrote some software ( https://github.com/nielsbasjes/yauaa ) where I use the combination of generics and inheritance to make inheriting the Builder pattern workable.

This code works fine in both the commandline java compiler and IntelliJ IDEA (which I normally use).

I got a bug report ( https://github.com/nielsbasjes/yauaa/issues/66 ) indicating a problem when using Eclipse.

Turns out that depending on the ordering of the methods Eclipse either finds the code good or bad.

My code passed the desired return type of the builder methods a parameter via a generics construct.

My best guess right now is that eclipse handles this pattern incorrectly.

I have created the simplest possible reproduction of the problem and put it here https://github.com/nielsbasjes/BugReport-EclipseGenerics and the same code as an attachment to this issue.
Comment 1 Stephan Herrmann CLA 2017-11-20 18:23:04 EST
I see several @SuppressWarnings("rawtypes"), can you reproduce the problem also with fully typed code (without rawtypes)?

(In reply to Niels Basjes from comment #0)
> Turns out that depending on the ordering of the methods Eclipse either finds
> the code good or bad.

This doesn't sound like a surprise to me. In chained method calls of course a lot depends on the order of methods. Why should it not?
Comment 2 Niels Basjes CLA 2017-11-21 04:52:33 EST
About the warnings:
On StackOverflow there are several questions regarding the Builder pattern and inheritance.
https://stackoverflow.com/questions/21086417/builder-pattern-and-inheritance
https://stackoverflow.com/questions/17164375/subclassing-a-java-builder-class
https://stackoverflow.com/questions/10941464/builder-pattern-with-inheritance

Some even indicate they have a solution for these warnings but so far I have not been able to get those directions to work without any warnings.
If you can indicate how to do that that would be nice for my own project also.


About the ordering of the calls:
The trick here is that both the end result class and the builder class are passed as generic parameters up the class hierarchy so that all builder related methods can return the correct type. So in the example case of the GrandChild class these three methods in that hierarchy GrandChildBuilder::withColor, ChildBuilder::withSize and Parent::withName must all return the same type which is passed as the parameter BUILDER. In this case that would be a GrandChildBuilder.
So because all return the same type the ordering of the methods does not matter.

FYI:
I found that this pattern is sometimes called the "Curiously recurring template pattern" https://en.wikipedia.org/wiki/Curiously_recurring_template_pattern
Comment 3 Eclipse Genie CLA 2020-04-21 17:55:38 EDT
This bug hasn't had any activity in quite some time. Maybe the problem got resolved, was a duplicate of something else, or became less pressing for some reason - or maybe it's still relevant but just hasn't been looked at yet. As such, we're closing this bug.

If you have further information on the current state of the bug, please add it and reopen this bug. The information can be, for example, that the problem still occurs, that you still want the feature, that more information is needed, or that the bug is (for whatever reason) no longer relevant.

--
The automated Eclipse Genie.