|
Lines 1-5
Link Here
|
| 1 |
/******************************************************************************* |
1 |
/******************************************************************************* |
| 2 |
* Copyright (c) 2000, 2011 IBM Corporation and others. |
2 |
* Copyright (c) 2000, 2013 IBM Corporation and others. |
| 3 |
* All rights reserved. This program and the accompanying materials |
3 |
* All rights reserved. This program and the accompanying materials |
| 4 |
* are made available under the terms of the Eclipse Public License v1.0 |
4 |
* are made available under the terms of the Eclipse Public License v1.0 |
| 5 |
* which accompanies this distribution, and is available at |
5 |
* which accompanies this distribution, and is available at |
|
Lines 7-12
Link Here
|
| 7 |
* |
7 |
* |
| 8 |
* Contributors: |
8 |
* Contributors: |
| 9 |
* IBM Corporation - initial API and implementation |
9 |
* IBM Corporation - initial API and implementation |
|
|
10 |
* Samrat Dhillon samrat.dhillon@gmail.com [introduce factory] Introduce Factory ignores a type parameter https://bugs.eclipse.org/bugs/show_bug.cgi?id=395021 |
| 10 |
*******************************************************************************/ |
11 |
*******************************************************************************/ |
| 11 |
package org.eclipse.jdt.internal.corext.refactoring.code; |
12 |
package org.eclipse.jdt.internal.corext.refactoring.code; |
| 12 |
|
13 |
|
|
Lines 238-243
Link Here
|
| 238 |
private String fFactoryClassName; |
239 |
private String fFactoryClassName; |
| 239 |
|
240 |
|
| 240 |
private int fConstructorVisibility= Modifier.PRIVATE; |
241 |
private int fConstructorVisibility= Modifier.PRIVATE; |
|
|
242 |
|
| 243 |
private boolean hasDuplicateTypeParamName = false; |
| 241 |
|
244 |
|
| 242 |
/** |
245 |
/** |
| 243 |
* Creates a new <code>IntroduceFactoryRefactoring</code> with the given selection |
246 |
* Creates a new <code>IntroduceFactoryRefactoring</code> with the given selection |
|
Lines 545-550
Link Here
|
| 545 |
|
548 |
|
| 546 |
if (fCallSitesInBinaryUnits) |
549 |
if (fCallSitesInBinaryUnits) |
| 547 |
result.merge(RefactoringStatus.createWarningStatus(RefactoringCoreMessages.IntroduceFactory_callSitesInBinaryClass)); |
550 |
result.merge(RefactoringStatus.createWarningStatus(RefactoringCoreMessages.IntroduceFactory_callSitesInBinaryClass)); |
|
|
551 |
|
| 552 |
if(hasDuplicateTypeParamName()){ |
| 553 |
result.merge(RefactoringStatus.createWarningStatus(RefactoringCoreMessages.IntroduceFactory_ambiguousTypeParams)); |
| 554 |
hasDuplicateTypeParamName = true; |
| 555 |
} |
| 548 |
|
556 |
|
| 549 |
return result; |
557 |
return result; |
| 550 |
} finally { |
558 |
} finally { |
|
Lines 720-726
Link Here
|
| 720 |
} |
728 |
} |
| 721 |
|
729 |
|
| 722 |
/** |
730 |
/** |
| 723 |
* Copies the constructor's parent type's type parameters, if any, as |
731 |
* Copies the type parameters of the constructor and its parent, if any, as |
| 724 |
* method type parameters of the new static factory method. (Recall |
732 |
* method type parameters of the new static factory method. (Recall |
| 725 |
* that static methods can't refer to type arguments of the enclosing |
733 |
* that static methods can't refer to type arguments of the enclosing |
| 726 |
* class, since they have no instance to serve as a context.)<br> |
734 |
* class, since they have no instance to serve as a context.)<br> |
|
Lines 738-750
Link Here
|
| 738 |
*/ |
746 |
*/ |
| 739 |
private void copyTypeParameters(AST ast, MethodDeclaration newMethod) { |
747 |
private void copyTypeParameters(AST ast, MethodDeclaration newMethod) { |
| 740 |
ITypeBinding[] ctorOwnerTypeParms= fCtorBinding.getDeclaringClass().getTypeParameters(); |
748 |
ITypeBinding[] ctorOwnerTypeParms= fCtorBinding.getDeclaringClass().getTypeParameters(); |
|
|
749 |
ITypeBinding[] ctorTypeParams= fCtorBinding.getTypeParameters(); |
| 750 |
if(hasDuplicateTypeParamName){ |
| 751 |
ITypeBinding[] typeParams= filterDuplicates(ctorTypeParams,ctorOwnerTypeParms); |
| 752 |
copyTypeParameters(ast, newMethod, typeParams); |
| 753 |
}else{ |
| 754 |
copyTypeParameters(ast, newMethod, ctorOwnerTypeParms); |
| 755 |
copyTypeParameters(ast, newMethod, ctorTypeParams); |
| 756 |
} |
| 757 |
} |
| 758 |
|
| 759 |
private void copyTypeParameters(AST ast, MethodDeclaration newMethod,ITypeBinding[] typeParms) { |
| 741 |
List<TypeParameter> factoryMethodTypeParms= newMethod.typeParameters(); |
760 |
List<TypeParameter> factoryMethodTypeParms= newMethod.typeParameters(); |
| 742 |
for(int i= 0; i < ctorOwnerTypeParms.length; i++) { |
761 |
for(int i= 0; i < typeParms.length; i++) { |
| 743 |
TypeParameter newParm= ast.newTypeParameter(); |
762 |
TypeParameter newParm= ast.newTypeParameter(); |
| 744 |
ITypeBinding[] parmTypeBounds= ctorOwnerTypeParms[i].getTypeBounds(); |
763 |
ITypeBinding[] parmTypeBounds= typeParms[i].getTypeBounds(); |
| 745 |
List<Type> newParmBounds= newParm.typeBounds(); |
764 |
List<Type> newParmBounds= newParm.typeBounds(); |
| 746 |
|
765 |
|
| 747 |
newParm.setName(ast.newSimpleName(ctorOwnerTypeParms[i].getName())); |
766 |
newParm.setName(ast.newSimpleName(typeParms[i].getName())); |
| 748 |
for(int b=0; b < parmTypeBounds.length; b++) { |
767 |
for(int b=0; b < parmTypeBounds.length; b++) { |
| 749 |
if (parmTypeBounds[b].isClass() && parmTypeBounds[b].getSuperclass() == null) |
768 |
if (parmTypeBounds[b].isClass() && parmTypeBounds[b].getSuperclass() == null) |
| 750 |
continue; |
769 |
continue; |
|
Lines 756-761
Link Here
|
| 756 |
factoryMethodTypeParms.add(newParm); |
775 |
factoryMethodTypeParms.add(newParm); |
| 757 |
} |
776 |
} |
| 758 |
} |
777 |
} |
|
|
778 |
|
| 779 |
private ITypeBinding[] filterDuplicates(ITypeBinding[] ctorTypeParams, ITypeBinding[] ctorOwnerTypeParms) { |
| 780 |
Map<String, ITypeBinding> uniqueTypeParamMap = new HashMap<String, ITypeBinding>(); |
| 781 |
for(ITypeBinding binding: ctorTypeParams){ |
| 782 |
uniqueTypeParamMap.put(binding.getName(), binding); |
| 783 |
} |
| 784 |
for(ITypeBinding binding: ctorOwnerTypeParms){ |
| 785 |
if(uniqueTypeParamMap.get(binding.getName())==null){ |
| 786 |
uniqueTypeParamMap.put(binding.getName(), binding); |
| 787 |
} |
| 788 |
} |
| 789 |
Collection<ITypeBinding> typeParams= uniqueTypeParamMap.values(); |
| 790 |
return typeParams.toArray(new ITypeBinding[typeParams.size()]); |
| 791 |
} |
| 792 |
|
| 793 |
private boolean hasDuplicateTypeParamName() { |
| 794 |
ITypeBinding[] ctorOwnerTypeParms= fCtorBinding.getDeclaringClass().getTypeParameters(); |
| 795 |
ITypeBinding[] ctorTypeParams = fCtorBinding.getTypeParameters(); |
| 796 |
Map<String, ITypeBinding> uniqueTypeParamMap = new HashMap<String, ITypeBinding>(); |
| 797 |
for(ITypeBinding binding: ctorTypeParams){ |
| 798 |
uniqueTypeParamMap.put(binding.getName(), binding); |
| 799 |
} |
| 800 |
for(ITypeBinding binding: ctorOwnerTypeParms){ |
| 801 |
if(uniqueTypeParamMap.get(binding.getName())!=null){ |
| 802 |
return true; |
| 803 |
} |
| 804 |
} |
| 805 |
return false; |
| 806 |
} |
| 759 |
|
807 |
|
| 760 |
/** |
808 |
/** |
| 761 |
* @param argType |
809 |
* @param argType |