Lines 7-13
Link Here
|
7 |
* |
7 |
* |
8 |
* Contributors: |
8 |
* Contributors: |
9 |
* IBM Corporation - initial API and implementation |
9 |
* IBM Corporation - initial API and implementation |
10 |
* Sebastian Davids <sdavids@gmx.de> - Bug 37432 getInvertEqualsProposal |
10 |
* Sebastian Davids <sdavids@gmx.de> - Bug 37432 getInvertEqualsProposal, |
|
|
11 |
* bug 37643 getChangeEqualsToSame, getChangeSameToEquals |
11 |
*******************************************************************************/ |
12 |
*******************************************************************************/ |
12 |
package org.eclipse.jdt.internal.ui.text.correction; |
13 |
package org.eclipse.jdt.internal.ui.text.correction; |
13 |
|
14 |
|
Lines 58-64
Link Here
|
58 |
|| getAddElseProposals(context, coveringNode, null) |
59 |
|| getAddElseProposals(context, coveringNode, null) |
59 |
|| getSplitVariableProposals(context, coveringNode, null) |
60 |
|| getSplitVariableProposals(context, coveringNode, null) |
60 |
|| getAddBlockProposals(context, coveringNode, null) |
61 |
|| getAddBlockProposals(context, coveringNode, null) |
61 |
|| getInvertEqualsProposal(context, coveringNode, null); |
62 |
|| getInvertEqualsProposal(context, coveringNode, null) |
|
|
63 |
|| getChangeEqualsToSameProposal(context, coveringNode, null) |
64 |
|| getChangeSameToEqualsProposal(context, coveringNode, null); |
62 |
} |
65 |
} |
63 |
return false; |
66 |
return false; |
64 |
} |
67 |
} |
Lines 85-90
Link Here
|
85 |
getAddElseProposals(context, coveringNode, resultingCollections); |
88 |
getAddElseProposals(context, coveringNode, resultingCollections); |
86 |
getAddBlockProposals(context, coveringNode, resultingCollections); |
89 |
getAddBlockProposals(context, coveringNode, resultingCollections); |
87 |
getInvertEqualsProposal(context, coveringNode, resultingCollections); |
90 |
getInvertEqualsProposal(context, coveringNode, resultingCollections); |
|
|
91 |
getChangeEqualsToSameProposal(context, coveringNode, resultingCollections); |
92 |
getChangeSameToEqualsProposal(context, coveringNode, resultingCollections); |
88 |
} |
93 |
} |
89 |
return (IJavaCompletionProposal[]) resultingCollections.toArray(new IJavaCompletionProposal[resultingCollections.size()]); |
94 |
return (IJavaCompletionProposal[]) resultingCollections.toArray(new IJavaCompletionProposal[resultingCollections.size()]); |
90 |
} |
95 |
} |
Lines 756-759
Link Here
|
756 |
resultingCollections.add(proposal); |
761 |
resultingCollections.add(proposal); |
757 |
return true; |
762 |
return true; |
758 |
} |
763 |
} |
|
|
764 |
|
765 |
private boolean getChangeEqualsToSameProposal(IInvocationContext context, ASTNode node, Collection resultingCollections) throws CoreException { |
766 |
ASTNode parent= node.getParent(); |
767 |
if (!(parent instanceof MethodInvocation)) { |
768 |
return false; |
769 |
} |
770 |
MethodInvocation method= (MethodInvocation) parent; |
771 |
if (!"equals".equals(method.getName().getIdentifier())) { //$NON-NLS-1$ |
772 |
return false; |
773 |
} |
774 |
List arguments= method.arguments(); |
775 |
if (arguments.size() != 1) { //overloaded equals w/ more than 1 arg |
776 |
return false; |
777 |
} |
778 |
Expression right= (Expression) arguments.get(0); |
779 |
ITypeBinding binding = right.resolveTypeBinding(); |
780 |
if (!(binding.isClass() || binding.isInterface() || binding.isNullType())) { //overloaded equals w/ non-class/interface arg |
781 |
return false; |
782 |
} |
783 |
Expression left= method.getExpression(); |
784 |
if (!(right instanceof NullLiteral)) { |
785 |
System.out.println(binding.getQualifiedName()+":"+left.resolveTypeBinding().getQualifiedName()); |
786 |
if (!binding.getQualifiedName().equals(left.resolveTypeBinding().getQualifiedName())) { |
787 |
return false; |
788 |
} |
789 |
} |
790 |
|
791 |
ASTRewrite rewrite= new ASTRewrite(method.getParent()); |
792 |
AST ast= rewrite.getAST(); |
793 |
InfixExpression replacement= ast.newInfixExpression(); |
794 |
replacement.setLeftOperand((left == null) ? ast.newThisExpression() : (Expression) rewrite.createCopy(left)); |
795 |
replacement.setRightOperand((Expression) rewrite.createCopy(right)); |
796 |
replacement.setOperator(InfixExpression.Operator.EQUALS); |
797 |
rewrite.markAsReplaced(method, replacement); |
798 |
|
799 |
String label= CorrectionMessages.getString("QuickAssistProcessor.replaceequalswithsame.description"); //$NON-NLS-1$ |
800 |
Image image= JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE); |
801 |
|
802 |
LinkedCorrectionProposal proposal= new LinkedCorrectionProposal(label, context.getCompilationUnit(), rewrite, 1, image); |
803 |
proposal.ensureNoModifications(); |
804 |
resultingCollections.add(proposal); |
805 |
return true; |
806 |
} |
807 |
|
808 |
private boolean getChangeSameToEqualsProposal(IInvocationContext context, ASTNode node, Collection resultingCollections) throws CoreException { |
809 |
if (!(node instanceof InfixExpression)) { |
810 |
return false; |
811 |
} |
812 |
InfixExpression same= (InfixExpression) node; |
813 |
if (!same.getOperator().equals(InfixExpression.Operator.EQUALS)) { |
814 |
return false; |
815 |
} |
816 |
Expression right= same.getRightOperand(); |
817 |
ITypeBinding binding = right.resolveTypeBinding(); |
818 |
if (!(binding.isClass() || binding.isInterface() || binding.isNullType())) { |
819 |
return false; |
820 |
} |
821 |
Expression left= same.getLeftOperand(); |
822 |
if (left instanceof NullLiteral) { |
823 |
return false; |
824 |
} |
825 |
|
826 |
ASTRewrite rewrite= new ASTRewrite(same.getParent()); |
827 |
AST ast= rewrite.getAST(); |
828 |
MethodInvocation replacement= ast.newMethodInvocation(); |
829 |
if (!(left instanceof ThisExpression)) { |
830 |
replacement.setExpression((Expression) rewrite.createCopy(left)); |
831 |
} |
832 |
replacement.arguments().add(rewrite.createCopy(right)); |
833 |
replacement.setName(ast.newSimpleName("equals"));//$NON-NLS-1$ |
834 |
rewrite.markAsReplaced(same, replacement); |
835 |
|
836 |
String label= CorrectionMessages.getString("QuickAssistProcessor.replacesamewithequals.description"); //$NON-NLS-1$ |
837 |
Image image= JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE); |
838 |
|
839 |
LinkedCorrectionProposal proposal= new LinkedCorrectionProposal(label, context.getCompilationUnit(), rewrite, 1, image); |
840 |
proposal.ensureNoModifications(); |
841 |
resultingCollections.add(proposal); |
842 |
return true; |
843 |
} |
759 |
} |
844 |
} |