1 11 package org.eclipse.jdt.internal.corext.codemanipulation; 12 13 import java.util.ArrayList ; 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 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 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); 106 fOverrideAll= (fOverrideQuery == null); 107 fReplaceAll= (fReplaceQuery == null); 108 109 IMethod[] existingMethods= fType.getMethods(); 110 111 ArrayList createdMethods= new ArrayList (); 112 113 ImportsStructure imports= new ImportsStructure(fType.getCompilationUnit(), fSettings.importOrder, fSettings.importThreshold, true); 114 115 String 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 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 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 continue loop; 134 } 135 } 136 137 IMethod overwrittenMethod= JavaModelUtil.findMethodImplementationInHierarchy(typeHierarchy, fType, curr.getElementName(), curr.getParameterTypes(), curr.isConstructor()); 138 if (overwrittenMethod == null) { 139 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 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 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 sibling= existingMethods[0]; 172 } 173 174 String 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 204 public ISchedulingRule getScheduleRule() { 205 return ResourcesPlugin.getWorkspace().getRoot(); 206 } 207 208 209 } 210 | Popular Tags |