KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 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
15 import org.eclipse.core.resources.IWorkspaceRunnable;
16 import org.eclipse.core.resources.ResourcesPlugin;
17 import org.eclipse.core.runtime.CoreException;
18 import org.eclipse.core.runtime.IProgressMonitor;
19 import org.eclipse.core.runtime.NullProgressMonitor;
20 import org.eclipse.core.runtime.OperationCanceledException;
21 import org.eclipse.core.runtime.SubProgressMonitor;
22 import org.eclipse.core.runtime.jobs.ISchedulingRule;
23
24 import org.eclipse.jdt.core.Flags;
25 import org.eclipse.jdt.core.ICompilationUnit;
26 import org.eclipse.jdt.core.IJavaElement;
27 import org.eclipse.jdt.core.IMethod;
28 import org.eclipse.jdt.core.IType;
29 import org.eclipse.jdt.core.ITypeHierarchy;
30 import org.eclipse.jdt.core.formatter.CodeFormatter;
31
32 import org.eclipse.jdt.internal.corext.util.CodeFormatterUtil;
33 import org.eclipse.jdt.internal.corext.util.JavaModelUtil;
34
35 /**
36  * Add method stubs to a type (the parent type)
37  * Methods are added without checking if they already exist (will result in duplicated methods)
38  * If the parent type is open in an editor, be sure to pass over its working copy.
39  */

40 public class AddMethodStubOperation implements IWorkspaceRunnable {
41         
42     private IType fType;
43     private IMethod[] fMethods;
44     private IMethod[] fCreatedMethods;
45     private boolean fDoSave;
46         
47     private IRequestQuery fOverrideQuery;
48     private IRequestQuery fReplaceQuery;
49     
50     private boolean fOverrideAll;
51     private boolean fReplaceAll;
52     
53     private CodeGenerationSettings fSettings;
54     
55     public AddMethodStubOperation(IType type, IMethod[] methods, CodeGenerationSettings settings, IRequestQuery overrideQuery, IRequestQuery replaceQuery, boolean save) {
56         super();
57         fType= type;
58         fMethods= methods;
59         fCreatedMethods= null;
60         fDoSave= save;
61         fOverrideQuery= overrideQuery;
62         fReplaceQuery= replaceQuery;
63         fSettings= settings;
64     }
65     
66     private boolean queryOverrideFinalMethods(IMethod inheritedMethod) throws OperationCanceledException {
67         if (!fOverrideAll) {
68             switch (fOverrideQuery.doQuery(inheritedMethod)) {
69                 case IRequestQuery.CANCEL:
70                     throw new OperationCanceledException();
71                 case IRequestQuery.NO:
72                     return false;
73                 case IRequestQuery.YES_ALL:
74                     fOverrideAll= true;
75             }
76         }
77         return true;
78     }
79     
80     private boolean queryReplaceMethods(IMethod method) throws OperationCanceledException {
81         if (!fReplaceAll) {
82             switch (fReplaceQuery.doQuery(method)) {
83                 case IRequestQuery.CANCEL:
84                     throw new OperationCanceledException();
85                 case IRequestQuery.NO:
86                     return false;
87                 case IRequestQuery.YES_ALL:
88                     fReplaceAll= true;
89             }
90         }
91         return true;
92     }
93
94     /**
95      * Runs the operation.
96      * @throws OperationCanceledException Runtime error thrown when operation is cancelled.
97      */

98     public void run(IProgressMonitor monitor) throws CoreException, OperationCanceledException {
99         if (monitor == null) {
100             monitor= new NullProgressMonitor();
101         }
102         try {
103             monitor.setTaskName(CodeGenerationMessages.AddMethodStubOperation_description);
104             monitor.beginTask("", fMethods.length + 2); //$NON-NLS-1$
105

106             fOverrideAll= (fOverrideQuery == null);
107             fReplaceAll= (fReplaceQuery == null);
108
109             IMethod[] existingMethods= fType.getMethods();
110             
111             ArrayList JavaDoc createdMethods= new ArrayList JavaDoc();
112             
113             ImportsStructure imports= new ImportsStructure(fType.getCompilationUnit(), fSettings.importOrder, fSettings.importThreshold, true);
114             
115             String JavaDoc lineDelim= StubUtility.getLineDelimiterUsed(fType);
116             int indent= StubUtility.getIndentUsed(fType) + 1;
117             
118             StubUtility.GenStubSettings genStubSetting= new StubUtility.GenStubSettings(fSettings);
119             
120             ICompilationUnit cu= fType.getCompilationUnit();
121             String JavaDoc typeName= fType.getElementName();
122             
123             ITypeHierarchy typeHierarchy= fType.newSupertypeHierarchy(new SubProgressMonitor(monitor, 1));
124             
125             loop: for (int i= 0; i < fMethods.length; i++) {
126                 try {
127                     String JavaDoc content;
128                     IMethod curr= fMethods[i];
129                     for (int k= 0; k < createdMethods.size(); k++) {
130                         IMethod meth= (IMethod) createdMethods.get(k);
131                         if (JavaModelUtil.isSameMethodSignature(meth.getElementName(), meth.getParameterTypes(), meth.isConstructor(), curr)) {
132                             // ignore duplicated methods
133
continue loop;
134                         }
135                     }
136                     
137                     IMethod overwrittenMethod= JavaModelUtil.findMethodImplementationInHierarchy(typeHierarchy, fType, curr.getElementName(), curr.getParameterTypes(), curr.isConstructor());
138                     if (overwrittenMethod == null) {
139                         // create method without super call, no overwrite
140
genStubSetting.callSuper= false;
141                         genStubSetting.methodOverwrites= false;
142                         genStubSetting.noBody= fType.isInterface();
143                         genStubSetting.methodModifiers= curr.getFlags();
144                         
145                         content= StubUtility.genStub(cu, typeName, curr, fType, genStubSetting, imports);
146                     } else {
147                         int flags= overwrittenMethod.getFlags();
148                         if (Flags.isFinal(flags) || Flags.isPrivate(flags)) {
149                             // ask before overwriting final methods
150
if (!queryOverrideFinalMethods(overwrittenMethod)) {
151                                 continue;
152                             }
153                         }
154                         genStubSetting.callSuper= true;
155                         genStubSetting.methodOverwrites= true;
156                     
157                         IMethod declaration= JavaModelUtil.findMethodDeclarationInHierarchy(typeHierarchy, fType, curr.getElementName(), curr.getParameterTypes(), curr.isConstructor());
158                         content= StubUtility.genStub(cu, typeName, overwrittenMethod, declaration.getDeclaringType(), genStubSetting, imports);
159                     }
160                     IJavaElement sibling= null;
161                     IMethod existing= JavaModelUtil.findMethod(curr.getElementName(), curr.getParameterTypes(), curr.isConstructor(), existingMethods);
162                     if (existing != null) {
163                         // ask before replacing a method
164
if (!queryReplaceMethods(existing)) {
165                             continue;
166                         }
167                         sibling= StubUtility.findNextSibling(existing);
168                         existing.delete(false, null);
169                     } else if (curr.isConstructor() && existingMethods.length > 0) {
170                         // add constructors at the beginning
171
sibling= existingMethods[0];
172                     }
173                     
174                     String JavaDoc formattedContent= CodeFormatterUtil.format(CodeFormatter.K_CLASS_BODY_DECLARATIONS, content, indent, null, lineDelim, fType.getJavaProject()) + lineDelim;
175                     IMethod newMethod= fType.createMethod(formattedContent, sibling, true, null);
176                     createdMethods.add(newMethod);
177                 } finally {
178                     monitor.worked(1);
179                     if (monitor.isCanceled()) {
180                         throw new OperationCanceledException();
181                     }
182                 }
183             }
184             
185             int nCreated= createdMethods.size();
186             if (nCreated > 0) {
187                 imports.create(fDoSave, null);
188                 monitor.worked(1);
189                 fCreatedMethods= (IMethod[]) createdMethods.toArray(new IMethod[nCreated]);
190             }
191         } finally {
192             monitor.done();
193         }
194     }
195     
196     
197     public IMethod[] getCreatedMethods() {
198         return fCreatedMethods;
199     }
200
201     /**
202      * @return Returns the scheduling rule for this operation
203      */

204     public ISchedulingRule getScheduleRule() {
205         return ResourcesPlugin.getWorkspace().getRoot();
206     }
207     
208         
209 }
210
Popular Tags