1 11 package org.eclipse.jdt.internal.core; 12 13 import java.util.HashMap ; 14 import java.util.Map ; 15 16 import org.eclipse.core.runtime.IStatus; 17 import org.eclipse.jdt.core.*; 18 19 28 public abstract class MultiOperation extends JavaModelOperation { 29 35 protected Map insertBeforeElements = new HashMap (1); 36 42 protected Map newParents; 43 47 protected Map renamings; 48 51 protected String [] renamingsList = null; 52 55 protected MultiOperation(IJavaElement[] elementsToProcess, boolean force) { 56 super(elementsToProcess, force); 57 } 58 61 protected MultiOperation(IJavaElement[] elementsToProcess, IJavaElement[] parentElements, boolean force) { 62 super(elementsToProcess, parentElements, force); 63 this.newParents = new HashMap (elementsToProcess.length); 64 if (elementsToProcess.length == parentElements.length) { 65 for (int i = 0; i < elementsToProcess.length; i++) { 66 this.newParents.put(elementsToProcess[i], parentElements[i]); 67 } 68 } else { for (int i = 0; i < elementsToProcess.length; i++) { 70 this.newParents.put(elementsToProcess[i], parentElements[0]); 71 } 72 } 73 74 } 75 79 protected void error(int code, IJavaElement element) throws JavaModelException { 80 throw new JavaModelException(new JavaModelStatus(code, element)); 81 } 82 89 protected void executeOperation() throws JavaModelException { 90 processElements(); 91 } 92 95 protected IJavaElement getDestinationParent(IJavaElement child) { 96 return (IJavaElement)this.newParents.get(child); 97 } 98 101 protected abstract String getMainTaskName(); 102 106 protected String getNewNameFor(IJavaElement element) throws JavaModelException { 107 String newName = null; 108 if (this.renamings != null) 109 newName = (String ) this.renamings.get(element); 110 if (newName == null && element instanceof IMethod && ((IMethod) element).isConstructor()) 111 newName = getDestinationParent(element).getElementName(); 112 return newName; 113 } 114 118 private void initializeRenamings() { 119 if (this.renamingsList != null && this.renamingsList.length == this.elementsToProcess.length) { 120 this.renamings = new HashMap (this.renamingsList.length); 121 for (int i = 0; i < this.renamingsList.length; i++) { 122 if (this.renamingsList[i] != null) { 123 this.renamings.put(this.elementsToProcess[i], this.renamingsList[i]); 124 } 125 } 126 } 127 } 128 133 protected boolean isMove() { 134 return false; 135 } 136 140 protected boolean isRename() { 141 return false; 142 } 143 144 147 protected abstract void processElement(IJavaElement element) throws JavaModelException; 148 155 protected void processElements() throws JavaModelException { 156 try { 157 beginTask(getMainTaskName(), this.elementsToProcess.length); 158 IJavaModelStatus[] errors = new IJavaModelStatus[3]; 159 int errorsCounter = 0; 160 for (int i = 0; i < this.elementsToProcess.length; i++) { 161 try { 162 verify(this.elementsToProcess[i]); 163 processElement(this.elementsToProcess[i]); 164 } catch (JavaModelException jme) { 165 if (errorsCounter == errors.length) { 166 System.arraycopy(errors, 0, (errors = new IJavaModelStatus[errorsCounter*2]), 0, errorsCounter); 168 } 169 errors[errorsCounter++] = jme.getJavaModelStatus(); 170 } finally { 171 worked(1); 172 } 173 } 174 if (errorsCounter == 1) { 175 throw new JavaModelException(errors[0]); 176 } else if (errorsCounter > 1) { 177 if (errorsCounter != errors.length) { 178 System.arraycopy(errors, 0, (errors = new IJavaModelStatus[errorsCounter]), 0, errorsCounter); 180 } 181 throw new JavaModelException(JavaModelStatus.newMultiStatus(errors)); 182 } 183 } finally { 184 done(); 185 } 186 } 187 194 public void setInsertBefore(IJavaElement modifiedElement, IJavaElement newSibling) { 195 this.insertBeforeElements.put(modifiedElement, newSibling); 196 } 197 209 public void setRenamings(String [] renamingsList) { 210 this.renamingsList = renamingsList; 211 initializeRenamings(); 212 } 213 218 protected abstract void verify(IJavaElement element) throws JavaModelException; 219 223 protected void verifyDestination(IJavaElement element, IJavaElement destination) throws JavaModelException { 224 if (destination == null || !destination.exists()) 225 error(IJavaModelStatusConstants.ELEMENT_DOES_NOT_EXIST, destination); 226 227 int destType = destination.getElementType(); 228 switch (element.getElementType()) { 229 case IJavaElement.PACKAGE_DECLARATION : 230 case IJavaElement.IMPORT_DECLARATION : 231 if (destType != IJavaElement.COMPILATION_UNIT) 232 error(IJavaModelStatusConstants.INVALID_DESTINATION, element); 233 break; 234 case IJavaElement.TYPE : 235 if (destType != IJavaElement.COMPILATION_UNIT && destType != IJavaElement.TYPE) 236 error(IJavaModelStatusConstants.INVALID_DESTINATION, element); 237 break; 238 case IJavaElement.METHOD : 239 case IJavaElement.FIELD : 240 case IJavaElement.INITIALIZER : 241 if (destType != IJavaElement.TYPE || destination instanceof BinaryType) 242 error(IJavaModelStatusConstants.INVALID_DESTINATION, element); 243 break; 244 case IJavaElement.COMPILATION_UNIT : 245 if (destType != IJavaElement.PACKAGE_FRAGMENT) 246 error(IJavaModelStatusConstants.INVALID_DESTINATION, element); 247 else { 248 CompilationUnit cu = (CompilationUnit)element; 249 if (isMove() && cu.isWorkingCopy() && !cu.isPrimary()) 250 error(IJavaModelStatusConstants.INVALID_ELEMENT_TYPES, element); 251 } 252 break; 253 case IJavaElement.PACKAGE_FRAGMENT : 254 IPackageFragment fragment = (IPackageFragment) element; 255 IJavaElement parent = fragment.getParent(); 256 if (parent.isReadOnly()) 257 error(IJavaModelStatusConstants.READ_ONLY, element); 258 else if (destType != IJavaElement.PACKAGE_FRAGMENT_ROOT) 259 error(IJavaModelStatusConstants.INVALID_DESTINATION, element); 260 break; 261 default : 262 error(IJavaModelStatusConstants.INVALID_ELEMENT_TYPES, element); 263 } 264 } 265 269 protected void verifyRenaming(IJavaElement element) throws JavaModelException { 270 String newName = getNewNameFor(element); 271 boolean isValid = true; 272 IJavaProject project = element.getJavaProject(); 273 String sourceLevel = project.getOption(JavaCore.COMPILER_SOURCE, true); 274 String complianceLevel = project.getOption(JavaCore.COMPILER_COMPLIANCE, true); 275 switch (element.getElementType()) { 276 case IJavaElement.PACKAGE_FRAGMENT : 277 if (((IPackageFragment) element).isDefaultPackage()) { 278 throw new JavaModelException(new JavaModelStatus(IJavaModelStatusConstants.NAME_COLLISION, element)); 280 } 281 isValid = JavaConventions.validatePackageName(newName, sourceLevel, complianceLevel).getSeverity() != IStatus.ERROR; 282 break; 283 case IJavaElement.COMPILATION_UNIT : 284 isValid = JavaConventions.validateCompilationUnitName(newName,sourceLevel, complianceLevel).getSeverity() != IStatus.ERROR; 285 break; 286 case IJavaElement.INITIALIZER : 287 isValid = false; break; 289 default : 290 isValid = JavaConventions.validateIdentifier(newName, sourceLevel, complianceLevel).getSeverity() != IStatus.ERROR; 291 break; 292 } 293 294 if (!isValid) { 295 throw new JavaModelException(new JavaModelStatus(IJavaModelStatusConstants.INVALID_NAME, element, newName)); 296 } 297 } 298 302 protected void verifySibling(IJavaElement element, IJavaElement destination) throws JavaModelException { 303 IJavaElement insertBeforeElement = (IJavaElement) this.insertBeforeElements.get(element); 304 if (insertBeforeElement != null) { 305 if (!insertBeforeElement.exists() || !insertBeforeElement.getParent().equals(destination)) { 306 error(IJavaModelStatusConstants.INVALID_SIBLING, insertBeforeElement); 307 } 308 } 309 } 310 } 311 | Popular Tags |