1 14 15 package org.eclipse.jdt.internal.corext.refactoring.code; 16 17 import java.util.Arrays ; 18 import java.util.Collections ; 19 import java.util.HashMap ; 20 import java.util.LinkedHashSet ; 21 import java.util.List ; 22 import java.util.Map ; 23 import java.util.StringTokenizer ; 24 25 import org.eclipse.core.runtime.Assert; 26 import org.eclipse.core.runtime.CoreException; 27 import org.eclipse.core.runtime.IProgressMonitor; 28 import org.eclipse.core.runtime.SubProgressMonitor; 29 30 import org.eclipse.jface.text.TextSelection; 31 32 import org.eclipse.ltk.core.refactoring.Change; 33 import org.eclipse.ltk.core.refactoring.ChangeDescriptor; 34 import org.eclipse.ltk.core.refactoring.RefactoringChangeDescriptor; 35 import org.eclipse.ltk.core.refactoring.RefactoringDescriptor; 36 import org.eclipse.ltk.core.refactoring.RefactoringStatus; 37 import org.eclipse.ltk.core.refactoring.RefactoringStatusEntry; 38 import org.eclipse.ltk.core.refactoring.participants.RefactoringArguments; 39 40 import org.eclipse.jdt.core.ICompilationUnit; 41 import org.eclipse.jdt.core.IJavaElement; 42 import org.eclipse.jdt.core.IMethod; 43 import org.eclipse.jdt.core.JavaModelException; 44 import org.eclipse.jdt.core.dom.ASTNode; 45 import org.eclipse.jdt.core.dom.ArrayInitializer; 46 import org.eclipse.jdt.core.dom.Assignment; 47 import org.eclipse.jdt.core.dom.ClassInstanceCreation; 48 import org.eclipse.jdt.core.dom.Expression; 49 import org.eclipse.jdt.core.dom.FieldAccess; 50 import org.eclipse.jdt.core.dom.IBinding; 51 import org.eclipse.jdt.core.dom.ITypeBinding; 52 import org.eclipse.jdt.core.dom.MethodDeclaration; 53 import org.eclipse.jdt.core.dom.MethodInvocation; 54 import org.eclipse.jdt.core.dom.Name; 55 import org.eclipse.jdt.core.dom.NullLiteral; 56 import org.eclipse.jdt.core.dom.QualifiedName; 57 import org.eclipse.jdt.core.dom.SimpleName; 58 import org.eclipse.jdt.core.refactoring.IJavaRefactorings; 59 60 import org.eclipse.jdt.internal.corext.Corext; 61 import org.eclipse.jdt.internal.corext.SourceRange; 62 import org.eclipse.jdt.internal.corext.codemanipulation.StubUtility; 63 import org.eclipse.jdt.internal.corext.dom.ASTNodes; 64 import org.eclipse.jdt.internal.corext.dom.Bindings; 65 import org.eclipse.jdt.internal.corext.dom.NodeFinder; 66 import org.eclipse.jdt.internal.corext.dom.ScopeAnalyzer; 67 import org.eclipse.jdt.internal.corext.dom.fragments.ASTFragmentFactory; 68 import org.eclipse.jdt.internal.corext.dom.fragments.IASTFragment; 69 import org.eclipse.jdt.internal.corext.dom.fragments.IExpressionFragment; 70 import org.eclipse.jdt.internal.corext.refactoring.Checks; 71 import org.eclipse.jdt.internal.corext.refactoring.JavaRefactoringArguments; 72 import org.eclipse.jdt.internal.corext.refactoring.JDTRefactoringDescriptor; 73 import org.eclipse.jdt.internal.corext.refactoring.JDTRefactoringDescriptorComment; 74 import org.eclipse.jdt.internal.corext.refactoring.ParameterInfo; 75 import org.eclipse.jdt.internal.corext.refactoring.RefactoringAvailabilityTester; 76 import org.eclipse.jdt.internal.corext.refactoring.RefactoringCoreMessages; 77 import org.eclipse.jdt.internal.corext.refactoring.base.RefactoringStatusCodes; 78 import org.eclipse.jdt.internal.corext.refactoring.changes.RefactoringDescriptorChange; 79 import org.eclipse.jdt.internal.corext.refactoring.structure.BodyUpdater; 80 import org.eclipse.jdt.internal.corext.refactoring.structure.ChangeSignatureRefactoring; 81 import org.eclipse.jdt.internal.corext.refactoring.structure.CompilationUnitRewrite; 82 import org.eclipse.jdt.internal.corext.refactoring.tagging.IDelegateUpdating; 83 import org.eclipse.jdt.internal.corext.util.Messages; 84 85 import org.eclipse.jdt.ui.JavaElementLabels; 86 87 import org.eclipse.jdt.internal.ui.JavaPlugin; 88 import org.eclipse.jdt.internal.ui.actions.SelectionConverter; 89 90 public class IntroduceParameterRefactoring extends ScriptableRefactoring implements IDelegateUpdating { 91 92 private static final String ATTRIBUTE_ARGUMENT= "argument"; 94 private static final String [] KNOWN_METHOD_NAME_PREFIXES= {"get", "is"}; 96 private ICompilationUnit fSourceCU; 97 private int fSelectionStart; 98 private int fSelectionLength; 99 100 private IMethod fMethod; 101 private ChangeSignatureRefactoring fChangeSignatureRefactoring; 102 private ParameterInfo fParameter; 103 private String fParameterName; 104 private RefactoringArguments fArguments; 105 106 private Expression fSelectedExpression; 107 private String [] fExcludedParameterNames; 108 109 115 public IntroduceParameterRefactoring(ICompilationUnit unit, int selectionStart, int selectionLength) { 116 Assert.isTrue(selectionStart >= 0); 117 Assert.isTrue(selectionLength >= 0); 118 fSourceCU= unit; 119 fSelectionStart= selectionStart; 120 fSelectionLength= selectionLength; 121 } 122 123 125 public boolean canEnableDelegateUpdating() { 126 return true; 127 } 128 129 public boolean getDelegateUpdating() { 130 return (fChangeSignatureRefactoring != null) ? fChangeSignatureRefactoring.getDelegateUpdating() : false; 131 } 132 133 public void setDelegateUpdating(boolean updating) { 134 if (fChangeSignatureRefactoring != null) 135 fChangeSignatureRefactoring.setDelegateUpdating(updating); 136 } 137 138 public void setDeprecateDelegates(boolean deprecate) { 139 if (fChangeSignatureRefactoring != null) 140 fChangeSignatureRefactoring.setDeprecateDelegates(deprecate); 141 } 142 143 public boolean getDeprecateDelegates() { 144 return (fChangeSignatureRefactoring != null) ? fChangeSignatureRefactoring.getDeprecateDelegates() : false; 145 } 146 147 149 public String getName() { 150 return RefactoringCoreMessages.IntroduceParameterRefactoring_name; 151 } 152 153 155 public RefactoringStatus checkInitialConditions(IProgressMonitor pm) throws CoreException { 156 try { 157 pm.beginTask("", 7); 159 if (! fSourceCU.isStructureKnown()) 160 return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.IntroduceParameterRefactoring_syntax_error); 161 162 IJavaElement enclosingElement= SelectionConverter.resolveEnclosingElement(fSourceCU, new TextSelection(fSelectionStart, fSelectionLength)); 163 if (! (enclosingElement instanceof IMethod)) 164 return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.IntroduceParameterRefactoring_expression_in_method); 165 166 fMethod= (IMethod) enclosingElement; 167 pm.worked(1); 168 169 RefactoringStatus result= new RefactoringStatus(); 170 if (fArguments != null) { 171 fChangeSignatureRefactoring= new ChangeSignatureRefactoring(null); 173 result= fChangeSignatureRefactoring.initialize(fArguments); 174 if (!result.hasFatalError()) { 175 fChangeSignatureRefactoring.setValidationContext(getValidationContext()); 176 result.merge(fChangeSignatureRefactoring.checkInitialConditions(new SubProgressMonitor(pm, 2))); 177 if (result.hasFatalError()) 178 return result; 179 } else { 180 pm.worked(2); 181 return result; 182 } 183 } else { 184 fChangeSignatureRefactoring= RefactoringAvailabilityTester.isChangeSignatureAvailable(fMethod) ? new ChangeSignatureRefactoring(fMethod) : null; 186 if (fChangeSignatureRefactoring == null) 187 return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.IntroduceParameterRefactoring_expression_in_method); 188 fChangeSignatureRefactoring.setValidationContext(getValidationContext()); 189 result.merge(fChangeSignatureRefactoring.checkInitialConditions(new SubProgressMonitor(pm, 1))); 190 if (result.hasFatalError()) { 191 RefactoringStatusEntry entry= result.getEntryMatchingSeverity(RefactoringStatus.FATAL); 192 if (entry.getCode() == RefactoringStatusCodes.OVERRIDES_ANOTHER_METHOD || entry.getCode() == RefactoringStatusCodes.METHOD_DECLARED_IN_INTERFACE) { 193 IMethod method= (IMethod) entry.getData(); 195 fChangeSignatureRefactoring= RefactoringAvailabilityTester.isChangeSignatureAvailable(method) ? new ChangeSignatureRefactoring(method) : null; 196 if (fChangeSignatureRefactoring == null) { 197 String msg= Messages.format(RefactoringCoreMessages.IntroduceParameterRefactoring_cannot_introduce, entry.getMessage()); 198 return RefactoringStatus.createFatalErrorStatus(msg); 199 } 200 result= fChangeSignatureRefactoring.checkInitialConditions(new SubProgressMonitor(pm, 1)); 201 if (result.hasFatalError()) 202 return result; 203 } else { 204 return result; 205 } 206 } else { 207 pm.worked(1); 208 } 209 } 210 211 CompilationUnitRewrite cuRewrite= fChangeSignatureRefactoring.getBaseCuRewrite(); 212 if (! cuRewrite.getCu().equals(fSourceCU)) 213 cuRewrite= new CompilationUnitRewrite(fSourceCU); 215 initializeSelectedExpression(cuRewrite); 216 pm.worked(1); 217 218 result.merge(checkSelection(cuRewrite, new SubProgressMonitor(pm, 3))); 219 if (result.hasFatalError()) 220 return result; 221 222 initializeExcludedParameterNames(cuRewrite); 223 224 addParameterInfo(cuRewrite); 225 226 fChangeSignatureRefactoring.setBodyUpdater(new BodyUpdater() { 227 public void updateBody(MethodDeclaration methodDeclaration, CompilationUnitRewrite rewrite, RefactoringStatus updaterResult) { 228 replaceSelectedExpression(rewrite); 229 } 230 }); 231 232 return result; 233 } finally { 234 pm.done(); 235 if (fChangeSignatureRefactoring != null) 236 fChangeSignatureRefactoring.setValidationContext(null); 237 } 238 } 239 240 private void addParameterInfo(CompilationUnitRewrite cuRewrite) throws JavaModelException { 241 ITypeBinding typeBinding= Bindings.normalizeForDeclarationUse(fSelectedExpression.resolveTypeBinding(), fSelectedExpression.getAST()); 242 String typeName= cuRewrite.getImportRewrite().addImport(typeBinding); 243 String name= fParameterName != null ? fParameterName : guessedParameterName(); 244 String defaultValue= fSourceCU.getBuffer().getText(fSelectedExpression.getStartPosition(), fSelectedExpression.getLength()); 245 fParameter= ParameterInfo.createInfoForAddedParameter(typeBinding, typeName, name, defaultValue); 246 if (fArguments == null) { 247 List parameterInfos= fChangeSignatureRefactoring.getParameterInfos(); 248 int parametersCount= parameterInfos.size(); 249 if (parametersCount > 0 && ((ParameterInfo) parameterInfos.get(parametersCount - 1)).isOldVarargs()) 250 parameterInfos.add(parametersCount - 1, fParameter); 251 else 252 parameterInfos.add(fParameter); 253 } 254 } 255 256 private void replaceSelectedExpression(CompilationUnitRewrite cuRewrite) { 257 if (! fSourceCU.equals(cuRewrite.getCu())) 258 return; 259 261 Expression expression= (Expression) NodeFinder.perform(cuRewrite.getRoot(), fSelectedExpression.getStartPosition(), fSelectedExpression.getLength()); 263 264 ASTNode newExpression= cuRewrite.getRoot().getAST().newSimpleName(fParameter.getNewName()); 265 String description= RefactoringCoreMessages.IntroduceParameterRefactoring_replace; 266 cuRewrite.getASTRewrite().replace(expression, newExpression, cuRewrite.createGroupDescription(description)); 267 } 268 269 private void initializeSelectedExpression(CompilationUnitRewrite cuRewrite) throws JavaModelException { 270 IASTFragment fragment= ASTFragmentFactory.createFragmentForSourceRange( 271 new SourceRange(fSelectionStart, fSelectionLength), cuRewrite.getRoot(), cuRewrite.getCu()); 272 273 if (! (fragment instanceof IExpressionFragment)) 274 return; 275 276 Expression expression= ((IExpressionFragment) fragment).getAssociatedExpression(); 278 if (fragment.getStartPosition() != expression.getStartPosition() 279 || fragment.getLength() != expression.getLength()) 280 return; 281 282 if (Checks.isInsideJavadoc(expression)) 283 return; 284 if (Checks.isEnumCase(expression.getParent())) 286 return; 287 288 fSelectedExpression= expression; 289 } 290 291 private RefactoringStatus checkSelection(CompilationUnitRewrite cuRewrite, IProgressMonitor pm) { 292 if (fSelectedExpression == null){ 293 String message= RefactoringCoreMessages.IntroduceParameterRefactoring_select; 294 return CodeRefactoringUtil.checkMethodSyntaxErrors(fSelectionStart, fSelectionLength, cuRewrite.getRoot(), message); 295 } 296 297 MethodDeclaration methodDeclaration= (MethodDeclaration) ASTNodes.getParent(fSelectedExpression, MethodDeclaration.class); 298 if (methodDeclaration == null) 299 return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.IntroduceParameterRefactoring_expression_in_method); 300 if (methodDeclaration.resolveBinding() == null) 301 return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.IntroduceParameterRefactoring_no_binding); 302 304 RefactoringStatus result= new RefactoringStatus(); 305 result.merge(checkExpression()); 306 if (result.hasFatalError()) 307 return result; 308 309 result.merge(checkExpressionBinding()); 310 if (result.hasFatalError()) 311 return result; 312 313 321 return result; 322 } 323 324 private RefactoringStatus checkExpression() { 325 Expression selectedExpression= fSelectedExpression; 327 328 if (selectedExpression instanceof Name && selectedExpression.getParent() instanceof ClassInstanceCreation) 329 return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.ExtractTempRefactoring_name_in_new); 330 332 if (selectedExpression instanceof NullLiteral) { 333 return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.ExtractTempRefactoring_null_literals); 334 } else if (selectedExpression instanceof ArrayInitializer) { 335 return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.ExtractTempRefactoring_array_initializer); 336 } else if (selectedExpression instanceof Assignment) { 337 if (selectedExpression.getParent() instanceof Expression) 338 return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.ExtractTempRefactoring_assignment); 339 else 340 return null; 341 342 } else if (selectedExpression instanceof SimpleName){ 343 if ((((SimpleName)selectedExpression)).isDeclaration()) 344 return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.ExtractTempRefactoring_names_in_declarations); 345 if (selectedExpression.getParent() instanceof QualifiedName && selectedExpression.getLocationInParent() == QualifiedName.NAME_PROPERTY 346 || selectedExpression.getParent() instanceof FieldAccess && selectedExpression.getLocationInParent() == FieldAccess.NAME_PROPERTY) 347 return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.ExtractTempRefactoring_select_expression); 348 } 349 350 return null; 351 } 352 353 private RefactoringStatus checkExpressionBinding() { 354 return checkExpressionFragmentIsRValue(); 355 } 356 357 private RefactoringStatus checkExpressionFragmentIsRValue() { 359 switch(Checks.checkExpressionIsRValue(fSelectedExpression)) { 360 case Checks.NOT_RVALUE_MISC: 361 return RefactoringStatus.createStatus(RefactoringStatus.FATAL, RefactoringCoreMessages.IntroduceParameterRefactoring_select, null, Corext.getPluginId(), RefactoringStatusCodes.EXPRESSION_NOT_RVALUE, null); 362 case Checks.NOT_RVALUE_VOID: 363 return RefactoringStatus.createStatus(RefactoringStatus.FATAL, RefactoringCoreMessages.IntroduceParameterRefactoring_no_void, null, Corext.getPluginId(), RefactoringStatusCodes.EXPRESSION_NOT_RVALUE_VOID, null); 364 case Checks.IS_RVALUE: 365 return new RefactoringStatus(); 366 default: 367 Assert.isTrue(false); return null; 368 } 369 } 370 371 public List getParameterInfos() { 372 return fChangeSignatureRefactoring.getParameterInfos(); 373 } 374 375 public ParameterInfo getAddedParameterInfo() { 376 return fParameter; 377 } 378 379 public String getMethodSignaturePreview() throws JavaModelException { 380 return fChangeSignatureRefactoring.getNewMethodSignature(); 381 } 382 383 385 public void setParameterName(String name) { 386 Assert.isNotNull(name); 387 fParameter.setNewName(name); 388 } 389 390 394 public String guessedParameterName() { 395 String [] proposals= guessParameterNames(); 396 if (proposals.length == 0) 397 return ""; else 399 return proposals[0]; 400 } 401 402 404 409 public String [] guessParameterNames() { 410 LinkedHashSet proposals= new LinkedHashSet (); if (fSelectedExpression instanceof MethodInvocation){ 412 proposals.addAll(guessTempNamesFromMethodInvocation((MethodInvocation) fSelectedExpression, fExcludedParameterNames)); 413 } 414 proposals.addAll(guessTempNamesFromExpression(fSelectedExpression, fExcludedParameterNames)); 415 return (String []) proposals.toArray(new String [proposals.size()]); 416 } 417 418 private List guessTempNamesFromMethodInvocation(MethodInvocation selectedMethodInvocation, String [] excludedVariableNames) { 419 String methodName= selectedMethodInvocation.getName().getIdentifier(); 420 for (int i= 0; i < KNOWN_METHOD_NAME_PREFIXES.length; i++) { 421 String prefix= KNOWN_METHOD_NAME_PREFIXES[i]; 422 if (! methodName.startsWith(prefix)) 423 continue; if (methodName.length() == prefix.length()) 425 return Collections.EMPTY_LIST; char firstAfterPrefix= methodName.charAt(prefix.length()); 427 if (! Character.isUpperCase(firstAfterPrefix)) 428 continue; String proposal= Character.toLowerCase(firstAfterPrefix) + methodName.substring(prefix.length() + 1); 431 methodName= proposal; 432 break; 433 } 434 String [] proposals= StubUtility.getLocalNameSuggestions(fSourceCU.getJavaProject(), methodName, 0, excludedVariableNames); 435 return Arrays.asList(proposals); 436 } 437 438 private List guessTempNamesFromExpression(Expression selectedExpression, String [] excluded) { 439 ITypeBinding expressionBinding= Bindings.normalizeForDeclarationUse( 440 selectedExpression.resolveTypeBinding(), 441 selectedExpression.getAST()); 442 String typeName= getQualifiedName(expressionBinding); 443 if (typeName.length() == 0) 444 typeName= expressionBinding.getName(); 445 if (typeName.length() == 0) 446 return Collections.EMPTY_LIST; 447 int typeParamStart= typeName.indexOf("<"); if (typeParamStart != -1) 449 typeName= typeName.substring(0, typeParamStart); 450 String [] proposals= StubUtility.getLocalNameSuggestions(fSourceCU.getJavaProject(), typeName, expressionBinding.getDimensions(), excluded); 451 return Arrays.asList(proposals); 452 } 453 454 456 private static String getQualifiedName(ITypeBinding typeBinding) { 457 if (typeBinding.isAnonymous()) 458 return getQualifiedName(typeBinding.getSuperclass()); 459 if (! typeBinding.isArray()) 460 return typeBinding.getQualifiedName(); 461 else 462 return typeBinding.getElementType().getQualifiedName(); 463 } 464 465 private void initializeExcludedParameterNames(CompilationUnitRewrite cuRewrite) { 466 IBinding[] bindings= new ScopeAnalyzer(cuRewrite.getRoot()).getDeclarationsInScope( 467 fSelectedExpression.getStartPosition(), ScopeAnalyzer.VARIABLES); 468 fExcludedParameterNames= new String [bindings.length]; 469 for (int i= 0; i < fExcludedParameterNames.length; i++) { 470 fExcludedParameterNames[i]= bindings[i].getName(); 471 } 472 } 473 474 public RefactoringStatus validateInput() { 475 return fChangeSignatureRefactoring.checkSignature(); 476 } 477 478 480 public RefactoringStatus checkFinalConditions(IProgressMonitor pm) throws CoreException { 481 fChangeSignatureRefactoring.setValidationContext(getValidationContext()); 482 RefactoringStatus result; 483 try { 484 result= fChangeSignatureRefactoring.checkFinalConditions(pm); 485 } finally { 486 fChangeSignatureRefactoring.setValidationContext(null); 487 } 488 return result; 489 } 490 491 public Change createChange(IProgressMonitor pm) throws CoreException { 492 fChangeSignatureRefactoring.setValidationContext(getValidationContext()); 493 Change result; 494 try { 495 result= fChangeSignatureRefactoring.createChange(pm); 496 } finally { 497 fChangeSignatureRefactoring.setValidationContext(null); 498 } 499 if (result != null) { 500 final ChangeDescriptor descriptor= result.getDescriptor(); 501 if (descriptor instanceof RefactoringChangeDescriptor) { 502 final RefactoringDescriptor refactoringDescriptor= ((RefactoringChangeDescriptor) descriptor).getRefactoringDescriptor(); 503 if (refactoringDescriptor instanceof JDTRefactoringDescriptor) { 504 final JDTRefactoringDescriptor extended= (JDTRefactoringDescriptor) refactoringDescriptor; 505 final Map arguments= new HashMap (); 506 arguments.put(ATTRIBUTE_ARGUMENT, fParameter.getNewName()); 507 arguments.put(JDTRefactoringDescriptor.ATTRIBUTE_SELECTION, new Integer (fSelectionStart).toString() + " " + new Integer (fSelectionLength).toString()); arguments.putAll(extended.getArguments()); 509 String signature= fChangeSignatureRefactoring.getMethodName(); 510 try { 511 signature= fChangeSignatureRefactoring.getOldMethodSignature(); 512 } catch (JavaModelException exception) { 513 JavaPlugin.log(exception); 514 } 515 final String description= Messages.format(RefactoringCoreMessages.IntroduceParameterRefactoring_descriptor_description_short, fChangeSignatureRefactoring.getMethod().getElementName()); 516 final String header= Messages.format(RefactoringCoreMessages.IntroduceParameterRefactoring_descriptor_description, new String [] { fParameter.getNewName(), signature, ASTNodes.asString(fSelectedExpression)}); 517 final JDTRefactoringDescriptorComment comment= new JDTRefactoringDescriptorComment(extended.getProject(), this, header); 518 comment.addSetting(Messages.format(RefactoringCoreMessages.IntroduceParameterRefactoring_original_pattern, JavaElementLabels.getTextLabel(fChangeSignatureRefactoring.getMethod(), JavaElementLabels.ALL_FULLY_QUALIFIED))); 519 comment.addSetting(Messages.format(RefactoringCoreMessages.IntroduceParameterRefactoring_expression_pattern, ASTNodes.asString(fSelectedExpression))); 520 comment.addSetting(Messages.format(RefactoringCoreMessages.IntroduceParameterRefactoring_parameter_pattern, getAddedParameterInfo().getNewName())); 521 result= new RefactoringDescriptorChange(new JDTRefactoringDescriptor(IJavaRefactorings.INTRODUCE_PARAMETER, extended.getProject(), description, comment.asString(), arguments, extended.getFlags()), RefactoringCoreMessages.IntroduceParameterRefactoring_name, new Change[] { result}); 522 } 523 } 524 } 525 return result; 526 } 527 528 public RefactoringStatus initialize(final RefactoringArguments arguments) { 529 fArguments= arguments; 530 if (arguments instanceof JavaRefactoringArguments) { 531 final JavaRefactoringArguments extended= (JavaRefactoringArguments) arguments; 532 final String selection= extended.getAttribute(JDTRefactoringDescriptor.ATTRIBUTE_SELECTION); 533 if (selection != null) { 534 int offset= -1; 535 int length= -1; 536 final StringTokenizer tokenizer= new StringTokenizer (selection); 537 if (tokenizer.hasMoreTokens()) 538 offset= Integer.valueOf(tokenizer.nextToken()).intValue(); 539 if (tokenizer.hasMoreTokens()) 540 length= Integer.valueOf(tokenizer.nextToken()).intValue(); 541 if (offset >= 0 && length >= 0) { 542 fSelectionStart= offset; 543 fSelectionLength= length; 544 } else 545 return RefactoringStatus.createFatalErrorStatus(Messages.format(RefactoringCoreMessages.InitializableRefactoring_illegal_argument, new Object [] { selection, JDTRefactoringDescriptor.ATTRIBUTE_SELECTION})); 546 } else 547 return RefactoringStatus.createFatalErrorStatus(Messages.format(RefactoringCoreMessages.InitializableRefactoring_argument_not_exist, JDTRefactoringDescriptor.ATTRIBUTE_SELECTION)); 548 final String handle= extended.getAttribute(JDTRefactoringDescriptor.ATTRIBUTE_INPUT); 549 if (handle != null) { 550 final IJavaElement element= JDTRefactoringDescriptor.handleToElement(extended.getProject(), handle, false); 551 if (element == null || !element.exists() || element.getElementType() != IJavaElement.COMPILATION_UNIT) 552 return createInputFatalStatus(element, IJavaRefactorings.INTRODUCE_PARAMETER); 553 else 554 fSourceCU= ((IMethod) element).getCompilationUnit(); 555 } else 556 return RefactoringStatus.createFatalErrorStatus(Messages.format(RefactoringCoreMessages.InitializableRefactoring_argument_not_exist, JDTRefactoringDescriptor.ATTRIBUTE_INPUT)); 557 final String name= extended.getAttribute(ATTRIBUTE_ARGUMENT); 558 if (name != null && !"".equals(name)) fParameterName= name; 560 else 561 return RefactoringStatus.createFatalErrorStatus(Messages.format(RefactoringCoreMessages.InitializableRefactoring_argument_not_exist, ATTRIBUTE_ARGUMENT)); 562 } else 563 return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.InitializableRefactoring_inacceptable_arguments); 564 return new RefactoringStatus(); 565 } 566 567 570 public String getDelegateUpdatingTitle(boolean plural) { 571 if (plural) 572 return RefactoringCoreMessages.DelegateCreator_keep_original_changed_plural; 573 else 574 return RefactoringCoreMessages.DelegateCreator_keep_original_changed_singular; 575 } 576 } 577 | Popular Tags |