KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > corext > codemanipulation > AddUnimplementedMethodsOperation


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.corext.codemanipulation;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.List JavaDoc;
15
16 import org.eclipse.text.edits.MultiTextEdit;
17 import org.eclipse.text.edits.TextEdit;
18
19 import org.eclipse.core.runtime.CoreException;
20 import org.eclipse.core.runtime.IProgressMonitor;
21 import org.eclipse.core.runtime.NullProgressMonitor;
22 import org.eclipse.core.runtime.SubProgressMonitor;
23 import org.eclipse.core.runtime.jobs.ISchedulingRule;
24
25 import org.eclipse.core.resources.IWorkspaceRunnable;
26 import org.eclipse.core.resources.ResourcesPlugin;
27
28 import org.eclipse.jdt.core.ICompilationUnit;
29 import org.eclipse.jdt.core.dom.AST;
30 import org.eclipse.jdt.core.dom.ASTNode;
31 import org.eclipse.jdt.core.dom.AbstractTypeDeclaration;
32 import org.eclipse.jdt.core.dom.AnonymousClassDeclaration;
33 import org.eclipse.jdt.core.dom.ChildListPropertyDescriptor;
34 import org.eclipse.jdt.core.dom.CompilationUnit;
35 import org.eclipse.jdt.core.dom.IMethodBinding;
36 import org.eclipse.jdt.core.dom.ITypeBinding;
37 import org.eclipse.jdt.core.dom.MethodDeclaration;
38 import org.eclipse.jdt.core.dom.rewrite.ASTRewrite;
39 import org.eclipse.jdt.core.dom.rewrite.ImportRewrite;
40 import org.eclipse.jdt.core.dom.rewrite.ListRewrite;
41 import org.eclipse.jdt.core.dom.rewrite.ImportRewrite.ImportRewriteContext;
42
43 import org.eclipse.jdt.internal.corext.util.JavaModelUtil;
44
45 import org.eclipse.jdt.internal.ui.preferences.JavaPreferencesSettings;
46
47 /**
48  * Workspace runnable to add unimplemented methods.
49  *
50  * @since 3.1
51  */

52 public final class AddUnimplementedMethodsOperation implements IWorkspaceRunnable {
53
54     /** Should the resulting edit be applied? */
55     private final boolean fApply;
56
57     /** The qualified names of the generated imports */
58     private String JavaDoc[] fCreatedImports;
59
60     /** The method binding keys for which a method was generated */
61     private final List JavaDoc fCreatedMethods= new ArrayList JavaDoc();
62
63     /** Should the import edits be applied? */
64     private final boolean fImports;
65
66     /** The insertion point, or <code>-1</code> */
67     private final int fInsertPos;
68
69     /** The method bindings to implement */
70     private final IMethodBinding[] fMethodsToImplement;
71
72     /** Should the compilation unit content be saved? */
73     private final boolean fSave;
74
75     /** Specified if comments should be created */
76     private boolean fDoCreateComments;
77
78     /** The type declaration to add the methods to */
79     private final ITypeBinding fType;
80
81     /** The compilation unit AST node */
82     private final CompilationUnit fASTRoot;
83
84     /**
85      * Creates a new add unimplemented methods operation.
86      *
87      * @param astRoot the compilation unit AST node
88      * @param type the type to add the methods to
89      * @param methodsToImplement the method bindings to implement or <code>null</code> to implement all unimplemented methods
90      * @param insertPos the insertion point, or <code>-1</code>
91      * @param imports <code>true</code> if the import edits should be applied, <code>false</code> otherwise
92      * @param apply <code>true</code> if the resulting edit should be applied, <code>false</code> otherwise
93      * @param save <code>true</code> if the changed compilation unit should be saved, <code>false</code> otherwise
94      */

95     public AddUnimplementedMethodsOperation(CompilationUnit astRoot, ITypeBinding type, IMethodBinding[] methodsToImplement, int insertPos, final boolean imports, final boolean apply, final boolean save) {
96         if (astRoot == null || !(astRoot.getJavaElement() instanceof ICompilationUnit)) {
97             throw new IllegalArgumentException JavaDoc("AST must not be null and has to be created from a ICompilationUnit"); //$NON-NLS-1$
98
}
99         if (type == null) {
100             throw new IllegalArgumentException JavaDoc("The type must not be null"); //$NON-NLS-1$
101
}
102         ASTNode node= astRoot.findDeclaringNode(type);
103         if (!(node instanceof AnonymousClassDeclaration || node instanceof AbstractTypeDeclaration)) {
104             throw new IllegalArgumentException JavaDoc("type has to map to a type declaration in the AST"); //$NON-NLS-1$
105
}
106         
107         fType= type;
108         fInsertPos= insertPos;
109         fASTRoot= astRoot;
110         fMethodsToImplement= methodsToImplement;
111         fSave= save;
112         fApply= apply;
113         fImports= imports;
114         
115         fDoCreateComments= StubUtility.doAddComments(astRoot.getJavaElement().getJavaProject());
116     }
117     
118     public void setCreateComments(boolean createComments) {
119         fDoCreateComments= createComments;
120     }
121     
122     
123     /**
124      * Returns the qualified names of the generated imports.
125      *
126      * @return the generated imports
127      */

128     public final String JavaDoc[] getCreatedImports() {
129         if (fCreatedImports != null) {
130             return fCreatedImports;
131         }
132         return new String JavaDoc[0];
133     }
134
135     /**
136      * Returns the method binding keys for which a method has been generated.
137      *
138      * @return the method binding keys
139      */

140     public final String JavaDoc[] getCreatedMethods() {
141         final String JavaDoc[] keys= new String JavaDoc[fCreatedMethods.size()];
142         fCreatedMethods.toArray(keys);
143         return keys;
144     }
145
146     /**
147      * Returns the scheduling rule for this operation.
148      *
149      * @return the scheduling rule
150      */

151     public final ISchedulingRule getSchedulingRule() {
152         return ResourcesPlugin.getWorkspace().getRoot();
153     }
154
155     /*
156      * @see org.eclipse.core.resources.IWorkspaceRunnable#run(org.eclipse.core.runtime.IProgressMonitor)
157      */

158     public final void run(IProgressMonitor monitor) throws CoreException {
159         if (monitor == null)
160             monitor= new NullProgressMonitor();
161         try {
162             monitor.beginTask("", 2); //$NON-NLS-1$
163
monitor.setTaskName(CodeGenerationMessages.AddUnimplementedMethodsOperation_description);
164             fCreatedMethods.clear();
165             ICompilationUnit cu= (ICompilationUnit) fASTRoot.getJavaElement();
166             
167             AST ast= fASTRoot.getAST();
168             
169             ASTRewrite astRewrite= ASTRewrite.create(ast);
170             ImportRewrite importRewrite= StubUtility.createImportRewrite(fASTRoot, true);
171             
172             ITypeBinding currTypeBinding= fType;
173             ListRewrite memberRewriter= null;
174             
175             ASTNode node= fASTRoot.findDeclaringNode(currTypeBinding);
176             if (node instanceof AnonymousClassDeclaration) {
177                 memberRewriter= astRewrite.getListRewrite(node, AnonymousClassDeclaration.BODY_DECLARATIONS_PROPERTY);
178             } else if (node instanceof AbstractTypeDeclaration) {
179                 ChildListPropertyDescriptor property= ((AbstractTypeDeclaration) node).getBodyDeclarationsProperty();
180                 memberRewriter= astRewrite.getListRewrite(node, property);
181             } else {
182                 throw new IllegalArgumentException JavaDoc();
183                 // not possible, we checked this in the constructor
184
}
185             
186             final CodeGenerationSettings settings= JavaPreferencesSettings.getCodeGenerationSettings(cu.getJavaProject());
187             settings.createComments= fDoCreateComments;
188
189             ASTNode insertion= getNodeToInsertBefore(memberRewriter);
190             
191             IMethodBinding[] methodsToImplement= fMethodsToImplement;
192             if (methodsToImplement == null) {
193                 methodsToImplement= StubUtility2.getUnimplementedMethods(currTypeBinding);
194             }
195             
196             ImportRewriteContext context= null;
197             int insertionPosition= fInsertPos;
198             if (insertionPosition == -1 && fASTRoot.types().size() > 0) {
199                 AbstractTypeDeclaration firstTypeDecl= (AbstractTypeDeclaration)fASTRoot.types().get(0);
200                 insertionPosition= firstTypeDecl.getStartPosition();
201                 if (insertionPosition != -1) {
202                      context= new ContextSensitiveImportRewriteContext(fASTRoot, insertionPosition, importRewrite);
203                 }
204             }
205
206             for (int i= 0; i < methodsToImplement.length; i++) {
207                 IMethodBinding curr= methodsToImplement[i];
208                 MethodDeclaration stub= StubUtility2.createImplementationStub(cu, astRewrite, importRewrite, ast, curr, currTypeBinding.getName(), settings, currTypeBinding.isInterface(), context);
209                 if (stub != null) {
210                     fCreatedMethods.add(curr.getKey());
211                     if (insertion != null)
212                         memberRewriter.insertBefore(stub, insertion, null);
213                     else
214                         memberRewriter.insertLast(stub, null);
215                 }
216             }
217             MultiTextEdit edit= new MultiTextEdit();
218             
219             TextEdit importEdits= importRewrite.rewriteImports(new SubProgressMonitor(monitor, 1));
220             fCreatedImports= importRewrite.getCreatedImports();
221             if (fImports) {
222                 edit.addChild(importEdits);
223             }
224             edit.addChild(astRewrite.rewriteAST());
225             
226             if (fApply) {
227                 JavaModelUtil.applyEdit(cu, edit, fSave, new SubProgressMonitor(monitor, 1));
228             }
229         } finally {
230             monitor.done();
231         }
232     }
233         
234     private ASTNode getNodeToInsertBefore(ListRewrite rewriter) {
235         if (fInsertPos != -1) {
236             List JavaDoc members= rewriter.getOriginalList();
237             for (int i= 0; i < members.size(); i++) {
238                 ASTNode curr= (ASTNode) members.get(i);
239                 if (curr.getStartPosition() >= fInsertPos) {
240                     return curr;
241                 }
242             }
243         }
244         return null;
245     }
246         
247 }
248
Popular Tags