KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > text > correction > RemoveDeclarationCorrectionProposal


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jdt.internal.ui.text.correction;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.List JavaDoc;
15
16 import org.eclipse.ui.ISharedImages;
17
18 import org.eclipse.jdt.core.ICompilationUnit;
19
20 import org.eclipse.jdt.core.dom.*;
21 import org.eclipse.jdt.core.dom.rewrite.ASTRewrite;
22
23 import org.eclipse.jdt.internal.corext.dom.ASTNodes;
24 import org.eclipse.jdt.internal.corext.dom.LinkedNodeFinder;
25 import org.eclipse.jdt.internal.corext.dom.NodeFinder;
26 import org.eclipse.jdt.internal.corext.util.Messages;
27
28 import org.eclipse.jdt.internal.ui.JavaPlugin;
29 import org.eclipse.jdt.internal.ui.javaeditor.ASTProvider;
30
31 public class RemoveDeclarationCorrectionProposal extends ASTRewriteCorrectionProposal {
32
33     private static class SideEffectFinder extends ASTVisitor {
34
35         private ArrayList JavaDoc fSideEffectNodes;
36
37         public SideEffectFinder(ArrayList JavaDoc res) {
38             fSideEffectNodes= res;
39         }
40
41         public boolean visit(Assignment node) {
42             fSideEffectNodes.add(node);
43             return false;
44         }
45
46         public boolean visit(PostfixExpression node) {
47             fSideEffectNodes.add(node);
48             return false;
49         }
50
51         public boolean visit(PrefixExpression node) {
52             Object JavaDoc operator= node.getOperator();
53             if (operator == PrefixExpression.Operator.INCREMENT || operator == PrefixExpression.Operator.DECREMENT) {
54                 fSideEffectNodes.add(node);
55             }
56             return false;
57         }
58
59         public boolean visit(MethodInvocation node) {
60             fSideEffectNodes.add(node);
61             return false;
62         }
63
64         public boolean visit(ClassInstanceCreation node) {
65             fSideEffectNodes.add(node);
66             return false;
67         }
68
69         public boolean visit(SuperMethodInvocation node) {
70             fSideEffectNodes.add(node);
71             return false;
72         }
73     }
74
75
76     private SimpleName fName;
77
78     public RemoveDeclarationCorrectionProposal(ICompilationUnit cu, SimpleName name, int relevance) {
79         super("", cu, null, relevance, JavaPlugin.getDefault().getWorkbench().getSharedImages().getImage(ISharedImages.IMG_TOOL_DELETE)); //$NON-NLS-1$
80
fName= name;
81     }
82
83     public String JavaDoc getDisplayString() {
84         IBinding binding= fName.resolveBinding();
85         String JavaDoc name= fName.getIdentifier();
86         switch (binding.getKind()) {
87             case IBinding.TYPE:
88                 return Messages.format(CorrectionMessages.RemoveDeclarationCorrectionProposal_removeunusedtype_description, name);
89             case IBinding.METHOD:
90                 if (((IMethodBinding) binding).isConstructor()) {
91                     return Messages.format(CorrectionMessages.RemoveDeclarationCorrectionProposal_removeunusedconstructor_description, name);
92                 } else {
93                     return Messages.format(CorrectionMessages.RemoveDeclarationCorrectionProposal_removeunusedmethod_description, name);
94                 }
95             case IBinding.VARIABLE:
96                 if (((IVariableBinding) binding).isField()) {
97                     return Messages.format(CorrectionMessages.RemoveDeclarationCorrectionProposal_removeunusedfield_description, name);
98                 } else {
99                     return Messages.format(CorrectionMessages.RemoveDeclarationCorrectionProposal_removeunusedvar_description, name);
100                 }
101             default:
102                 return super.getDisplayString();
103         }
104     }
105
106     /*(non-Javadoc)
107      * @see org.eclipse.jdt.internal.ui.text.correction.ASTRewriteCorrectionProposal#getRewrite()
108      */

109     protected ASTRewrite getRewrite() {
110         IBinding binding= fName.resolveBinding();
111         CompilationUnit root= (CompilationUnit) fName.getRoot();
112         ASTRewrite rewrite;
113         if (binding.getKind() == IBinding.METHOD) {
114             IMethodBinding decl= ((IMethodBinding) binding).getMethodDeclaration();
115             ASTNode declaration= root.findDeclaringNode(decl);
116             rewrite= ASTRewrite.create(root.getAST());
117             rewrite.remove(declaration, null);
118         } else if (binding.getKind() == IBinding.TYPE) {
119             ITypeBinding decl= ((ITypeBinding) binding).getTypeDeclaration();
120             ASTNode declaration= root.findDeclaringNode(decl);
121             rewrite= ASTRewrite.create(root.getAST());
122             rewrite.remove(declaration, null);
123         } else if (binding.getKind() == IBinding.VARIABLE) {
124             // needs full AST
125
CompilationUnit completeRoot= JavaPlugin.getDefault().getASTProvider().getAST(getCompilationUnit(), ASTProvider.WAIT_YES, null);
126
127             SimpleName nameNode= (SimpleName) NodeFinder.perform(completeRoot, fName.getStartPosition(), fName.getLength());
128
129             rewrite= ASTRewrite.create(completeRoot.getAST());
130             SimpleName[] references= LinkedNodeFinder.findByBinding(completeRoot, nameNode.resolveBinding());
131             for (int i= 0; i < references.length; i++) {
132                 removeVariableReferences(rewrite, references[i]);
133             }
134
135             IVariableBinding bindingDecl= ((IVariableBinding) nameNode.resolveBinding()).getVariableDeclaration();
136             ASTNode declaringNode= completeRoot.findDeclaringNode(bindingDecl);
137             if (declaringNode instanceof SingleVariableDeclaration) {
138                 removeParamTag(rewrite, (SingleVariableDeclaration) declaringNode);
139             }
140         } else {
141             throw new IllegalArgumentException JavaDoc("Unexpected binding"); //$NON-NLS-1$
142
}
143         return rewrite;
144     }
145
146     private void removeParamTag(ASTRewrite rewrite, SingleVariableDeclaration varDecl) {
147         if (varDecl.getParent() instanceof MethodDeclaration) {
148             Javadoc javadoc= ((MethodDeclaration) varDecl.getParent()).getJavadoc();
149             if (javadoc != null) {
150                 TagElement tagElement= JavadocTagsSubProcessor.findParamTag(javadoc, varDecl.getName().getIdentifier());
151                 if (tagElement != null) {
152                     rewrite.remove(tagElement, null);
153                 }
154             }
155         }
156     }
157
158     /**
159      * Remove the field or variable declaration including the initializer.
160      */

161     private void removeVariableReferences(ASTRewrite rewrite, SimpleName reference) {
162         ASTNode parent= reference.getParent();
163         while (parent instanceof QualifiedName) {
164             parent= parent.getParent();
165         }
166         if (parent instanceof FieldAccess) {
167             parent= parent.getParent();
168         }
169
170         int nameParentType= parent.getNodeType();
171         if (nameParentType == ASTNode.ASSIGNMENT) {
172             Assignment assignment= (Assignment) parent;
173             Expression rightHand= assignment.getRightHandSide();
174
175             ASTNode assignParent= assignment.getParent();
176             if (assignParent.getNodeType() == ASTNode.EXPRESSION_STATEMENT && rightHand.getNodeType() != ASTNode.ASSIGNMENT) {
177                 removeVariableWithInitializer(rewrite, rightHand, assignParent);
178             } else {
179                 rewrite.replace(assignment, rewrite.createCopyTarget(rightHand), null);
180             }
181         } else if (nameParentType == ASTNode.SINGLE_VARIABLE_DECLARATION) {
182             rewrite.remove(parent, null);
183         } else if (nameParentType == ASTNode.VARIABLE_DECLARATION_FRAGMENT) {
184             VariableDeclarationFragment frag= (VariableDeclarationFragment) parent;
185             ASTNode varDecl= frag.getParent();
186             List JavaDoc fragments;
187             if (varDecl instanceof VariableDeclarationExpression) {
188                 fragments= ((VariableDeclarationExpression) varDecl).fragments();
189             } else if (varDecl instanceof FieldDeclaration) {
190                 fragments= ((FieldDeclaration) varDecl).fragments();
191             } else {
192                 fragments= ((VariableDeclarationStatement) varDecl).fragments();
193             }
194             if (fragments.size() == 1) {
195                 rewrite.remove(varDecl, null);
196             } else {
197                 rewrite.remove(frag, null); // don't try to preserve
198
}
199         }
200     }
201
202     private void removeVariableWithInitializer(ASTRewrite rewrite, ASTNode initializerNode, ASTNode statementNode) {
203         ArrayList JavaDoc sideEffectNodes= new ArrayList JavaDoc();
204         initializerNode.accept(new SideEffectFinder(sideEffectNodes));
205         int nSideEffects= sideEffectNodes.size();
206         if (nSideEffects == 0) {
207             if (ASTNodes.isControlStatementBody(statementNode.getLocationInParent())) {
208                 rewrite.replace(statementNode, rewrite.getAST().newBlock(), null);
209             } else {
210                 rewrite.remove(statementNode, null);
211             }
212         } else {
213             // do nothing yet
214
}
215     }
216
217 }
218
Popular Tags