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

Bug 395021

Summary: [introduce factory] Introduce Factory ignores a type parameter
Product: [Eclipse Project] JDT Reporter: Milos Gligoric <milos.gligoric>
Component: UIAssignee: JDT-UI-Inbox <jdt-ui-inbox>
Status: CLOSED WONTFIX QA Contact:
Severity: normal    
Priority: P3 CC: manju656, samrat.dhillon
Version: 4.2.1Flags: manju656: review? (manju656)
Target Milestone: ---   
Hardware: PC   
OS: Linux   
Whiteboard: stalebug
Attachments:
Description Flags
Proposed fix and Test none

Description Milos Gligoric CLA 2012-11-25 09:51:58 EST
Steps to reporoduce:
1. Invoke "Introduce Factory" on "C" constructor in code below
2. Confirm default settings for the transformation
3. The resulting code does not compile ("F cannot be resolved to a type")

class C<E> {
    // Invoke "Introduce Factory" on the following constructor
    <F extends E> C(F p) {
    }
}

(Thanks to Yilong Li for helping with the bug report.)
Comment 1 Martin Mathew CLA 2013-01-16 02:23:59 EST
Issue is reproducible using I20121029-2000. The code refactoring results in compiler error.
Comment 2 Samrat Dhillon CLA 2013-10-21 21:59:41 EDT
Created attachment 236747 [details]
Proposed fix and Test

The existing code only copies the type parameters of the declaring class, but if the constructor itself declares any type parameters they are not copied to the factory method. Now copying the type parameters of the constructor as well. 

This change gives rise to another problem when we have duplicate type parameters in declaring class and the constructor. e.g. consider the code snippets below:

class CtorTypeParam<E> {

	private <E> CtorTypeParam(E p) {
    }
}
 
class CtorTypeParam<E> {

	private <E extends Number> CtorTypeParam(E p) {
    }
}

class CtorTypeParam<E extends Number> {

	private <E> CtorTypeParam(E p) {
    }
}

Here the constructor is hiding the type parameters of the class. While creating the factory method it is difficult to determine which is the right type parameter.  In my changes what I have done is that the code now detects if there are duplicate type parameter names. If so we show a warning to the user with the message "Ambiguous type Parameters on constructor and Declaring Type. Refactoring may lead to compilation error". If the user proceeds, the code builds a unique list of type parameter names, adding the type parameters of the constructor first as they are hiding the type parameters of the class, and then adding the type parameters of the class only if the same parameter name does not already exist. 

This contribution complies with http://www.eclipse.org/legal/CoO.php
Comment 3 Eclipse Genie CLA 2020-01-23 15:43:17 EST
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.