|
Link Here
|
| 1 |
/******************************************************************************* |
1 |
/******************************************************************************* |
| 2 |
* Copyright (c) 2000, 2012 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 |
| 6 |
* http://www.eclipse.org/legal/epl-v10.html |
6 |
* http://www.eclipse.org/legal/epl-v10.html |
|
|
7 |
* |
| 8 |
* This is an implementation of an early-draft specification developed under the Java |
| 9 |
* Community Process (JCP) and is made available for testing and evaluation purposes |
| 10 |
* only. The code is not compatible with any specification of the JCP. |
| 7 |
* |
11 |
* |
| 8 |
* Contributors: |
12 |
* Contributors: |
| 9 |
* IBM Corporation - initial API and implementation |
13 |
* IBM Corporation - initial API and implementation |
|
Link Here
|
| 75 |
import org.eclipse.jdt.core.dom.MethodDeclaration; |
79 |
import org.eclipse.jdt.core.dom.MethodDeclaration; |
| 76 |
import org.eclipse.jdt.core.dom.MethodInvocation; |
80 |
import org.eclipse.jdt.core.dom.MethodInvocation; |
| 77 |
import org.eclipse.jdt.core.dom.Modifier; |
81 |
import org.eclipse.jdt.core.dom.Modifier; |
| 78 |
import org.eclipse.jdt.core.dom.Name; |
|
|
| 79 |
import org.eclipse.jdt.core.dom.ParenthesizedExpression; |
82 |
import org.eclipse.jdt.core.dom.ParenthesizedExpression; |
| 80 |
import org.eclipse.jdt.core.dom.QualifiedName; |
83 |
import org.eclipse.jdt.core.dom.QualifiedName; |
| 81 |
import org.eclipse.jdt.core.dom.ReturnStatement; |
84 |
import org.eclipse.jdt.core.dom.ReturnStatement; |
|
Link Here
|
| 528 |
TextEditGroup insertDesc= new TextEditGroup(Messages.format(RefactoringCoreMessages.ExtractMethodRefactoring_add_method, BasicElementLabels.getJavaElementName(fMethodName))); |
531 |
TextEditGroup insertDesc= new TextEditGroup(Messages.format(RefactoringCoreMessages.ExtractMethodRefactoring_add_method, BasicElementLabels.getJavaElementName(fMethodName))); |
| 529 |
result.addTextEditGroup(insertDesc); |
532 |
result.addTextEditGroup(insertDesc); |
| 530 |
|
533 |
|
| 531 |
if (fDestination == fDestinations[0]) { |
534 |
if (fDestination == ASTResolving.findParentType(declaration.getParent())) { |
| 532 |
ChildListPropertyDescriptor desc= (ChildListPropertyDescriptor)declaration.getLocationInParent(); |
535 |
ChildListPropertyDescriptor desc= (ChildListPropertyDescriptor)declaration.getLocationInParent(); |
| 533 |
ListRewrite container= fRewriter.getListRewrite(declaration.getParent(), desc); |
536 |
ListRewrite container= fRewriter.getListRewrite(declaration.getParent(), desc); |
| 534 |
container.insertAfter(mm, declaration, insertDesc); |
537 |
container.insertAfter(mm, declaration, insertDesc); |
|
Link Here
|
| 741 |
return fGenerateJavadoc; |
744 |
return fGenerateJavadoc; |
| 742 |
} |
745 |
} |
| 743 |
|
746 |
|
|
|
747 |
public boolean isDestinationInterface() { |
| 748 |
return fDestination instanceof TypeDeclaration && ((TypeDeclaration) fDestination).isInterface(); |
| 749 |
} |
| 750 |
|
| 744 |
//---- Helper methods ------------------------------------------------------------------------ |
751 |
//---- Helper methods ------------------------------------------------------------------------ |
| 745 |
|
752 |
|
| 746 |
private void initializeParameterInfos() { |
753 |
private void initializeParameterInfos() { |
|
Link Here
|
| 789 |
private void initializeDestinations() { |
796 |
private void initializeDestinations() { |
| 790 |
List<ASTNode> result= new ArrayList<ASTNode>(); |
797 |
List<ASTNode> result= new ArrayList<ASTNode>(); |
| 791 |
BodyDeclaration decl= fAnalyzer.getEnclosingBodyDeclaration(); |
798 |
BodyDeclaration decl= fAnalyzer.getEnclosingBodyDeclaration(); |
| 792 |
ASTNode current= getNextParent(decl); |
799 |
ASTNode current= ASTResolving.findParentType(decl.getParent()); |
| 793 |
result.add(current); |
800 |
if (fAnalyzer.isValidDestination(current)) { |
| 794 |
if (decl instanceof MethodDeclaration || decl instanceof Initializer || decl instanceof FieldDeclaration) { |
801 |
result.add(current); |
|
|
802 |
} |
| 803 |
if (current != null && (decl instanceof MethodDeclaration || decl instanceof Initializer || decl instanceof FieldDeclaration)) { |
| 795 |
ITypeBinding binding= ASTNodes.getEnclosingType(current); |
804 |
ITypeBinding binding= ASTNodes.getEnclosingType(current); |
| 796 |
ASTNode next= getNextParent(current); |
805 |
ASTNode next= ASTResolving.findParentType(current.getParent()); |
| 797 |
while (next != null && binding != null && binding.isNested()) { |
806 |
while (next != null && binding != null && binding.isNested()) { |
| 798 |
result.add(next); |
807 |
if (fAnalyzer.isValidDestination(next)) { |
|
|
808 |
result.add(next); |
| 809 |
} |
| 799 |
current= next; |
810 |
current= next; |
| 800 |
binding= ASTNodes.getEnclosingType(current); |
811 |
binding= ASTNodes.getEnclosingType(current); |
| 801 |
next= getNextParent(next); |
812 |
next= ASTResolving.findParentType(next.getParent()); |
| 802 |
} |
813 |
} |
| 803 |
} |
814 |
} |
| 804 |
fDestinations= result.toArray(new ASTNode[result.size()]); |
815 |
fDestinations= result.toArray(new ASTNode[result.size()]); |
| 805 |
fDestination= fDestinations[fDestinationIndex]; |
816 |
fDestination= fDestinations[fDestinationIndex]; |
| 806 |
} |
|
|
| 807 |
|
| 808 |
private ASTNode getNextParent(ASTNode node) { |
| 809 |
do { |
| 810 |
node= node.getParent(); |
| 811 |
} while (node != null && !(node instanceof AbstractTypeDeclaration || node instanceof AnonymousClassDeclaration)); |
| 812 |
return node; |
| 813 |
} |
817 |
} |
| 814 |
|
818 |
|
| 815 |
private RefactoringStatus mergeTextSelectionStatus(RefactoringStatus status) { |
819 |
private RefactoringStatus mergeTextSelectionStatus(RefactoringStatus status) { |
|
Link Here
|
| 974 |
MethodDeclaration result= fAST.newMethodDeclaration(); |
978 |
MethodDeclaration result= fAST.newMethodDeclaration(); |
| 975 |
|
979 |
|
| 976 |
int modifiers= fVisibility; |
980 |
int modifiers= fVisibility; |
| 977 |
ASTNode enclosingBodyDeclaration= fAnalyzer.getEnclosingBodyDeclaration(); |
981 |
BodyDeclaration enclosingBodyDeclaration= fAnalyzer.getEnclosingBodyDeclaration(); |
| 978 |
while (enclosingBodyDeclaration != null && enclosingBodyDeclaration.getParent() != fDestination) { |
982 |
boolean isDestinationInterface= isDestinationInterface(); |
| 979 |
enclosingBodyDeclaration= enclosingBodyDeclaration.getParent(); |
983 |
if (isDestinationInterface && !(enclosingBodyDeclaration instanceof MethodDeclaration && |
|
|
984 |
enclosingBodyDeclaration.getParent() == fDestination && |
| 985 |
Modifier.isPublic(enclosingBodyDeclaration.getModifiers()))) { |
| 986 |
modifiers= Modifier.NONE; |
| 980 |
} |
987 |
} |
| 981 |
if (enclosingBodyDeclaration instanceof BodyDeclaration) { // should always be the case |
988 |
|
| 982 |
int enclosingModifiers= ((BodyDeclaration)enclosingBodyDeclaration).getModifiers(); |
989 |
boolean shouldBeStatic= false; |
| 983 |
boolean shouldBeStatic= Modifier.isStatic(enclosingModifiers) |
990 |
ASTNode currentParent= enclosingBodyDeclaration; |
| 984 |
|| enclosingBodyDeclaration instanceof EnumDeclaration |
991 |
do { |
| 985 |
|| fAnalyzer.getForceStatic(); |
992 |
if (currentParent instanceof BodyDeclaration) { |
| 986 |
if (shouldBeStatic) { |
993 |
shouldBeStatic= shouldBeStatic || JdtFlags.isStatic((BodyDeclaration) currentParent); |
| 987 |
modifiers|= Modifier.STATIC; |
|
|
| 988 |
} |
994 |
} |
|
|
995 |
currentParent= currentParent.getParent(); |
| 996 |
} while (!shouldBeStatic && currentParent != null && currentParent != fDestination); |
| 997 |
|
| 998 |
if (shouldBeStatic || fAnalyzer.getForceStatic()) { |
| 999 |
modifiers|= Modifier.STATIC; |
| 1000 |
} else if (isDestinationInterface) { |
| 1001 |
modifiers|= Modifier.DEFAULT; |
| 989 |
} |
1002 |
} |
| 990 |
|
1003 |
|
| 991 |
ITypeBinding[] typeVariables= computeLocalTypeVariables(); |
1004 |
ITypeBinding[] typeVariables= computeLocalTypeVariables(); |
|
Link Here
|
| 1018 |
parameters.add(parameter); |
1031 |
parameters.add(parameter); |
| 1019 |
} |
1032 |
} |
| 1020 |
|
1033 |
|
| 1021 |
List<Name> exceptions= result.thrownExceptions(); |
1034 |
List<Type> exceptions= result.thrownExceptionTypes(); |
| 1022 |
ITypeBinding[] exceptionTypes= fAnalyzer.getExceptions(fThrowRuntimeExceptions); |
1035 |
ITypeBinding[] exceptionTypes= fAnalyzer.getExceptions(fThrowRuntimeExceptions); |
| 1023 |
for (int i= 0; i < exceptionTypes.length; i++) { |
1036 |
for (int i= 0; i < exceptionTypes.length; i++) { |
| 1024 |
ITypeBinding exceptionType= exceptionTypes[i]; |
1037 |
ITypeBinding exceptionType= exceptionTypes[i]; |
| 1025 |
exceptions.add(ASTNodeFactory.newName(fAST, fImportRewriter.addImport(exceptionType, context))); |
1038 |
exceptions.add(ASTNodeFactory.newType(fAST, fImportRewriter.addImport(exceptionType, context))); |
| 1026 |
} |
1039 |
} |
| 1027 |
return result; |
1040 |
return result; |
| 1028 |
} |
1041 |
} |