1 11 package org.eclipse.jdt.internal.corext.refactoring; 12 13 import java.util.ArrayList ; 14 import java.util.Arrays ; 15 import java.util.HashSet ; 16 import java.util.Iterator ; 17 import java.util.List ; 18 import java.util.Set ; 19 20 import org.eclipse.core.runtime.CoreException; 21 22 import org.eclipse.core.resources.IProject; 23 import org.eclipse.core.resources.IResource; 24 25 import org.eclipse.jface.viewers.IStructuredSelection; 26 27 import org.eclipse.jdt.core.Flags; 28 import org.eclipse.jdt.core.IClasspathEntry; 29 import org.eclipse.jdt.core.ICompilationUnit; 30 import org.eclipse.jdt.core.IField; 31 import org.eclipse.jdt.core.IJavaElement; 32 import org.eclipse.jdt.core.IJavaModel; 33 import org.eclipse.jdt.core.IJavaProject; 34 import org.eclipse.jdt.core.ILocalVariable; 35 import org.eclipse.jdt.core.IMember; 36 import org.eclipse.jdt.core.IMethod; 37 import org.eclipse.jdt.core.IPackageDeclaration; 38 import org.eclipse.jdt.core.IPackageFragment; 39 import org.eclipse.jdt.core.IPackageFragmentRoot; 40 import org.eclipse.jdt.core.IType; 41 import org.eclipse.jdt.core.ITypeParameter; 42 import org.eclipse.jdt.core.JavaModelException; 43 import org.eclipse.jdt.core.Signature; 44 import org.eclipse.jdt.core.dom.ASTNode; 45 import org.eclipse.jdt.core.dom.ClassInstanceCreation; 46 import org.eclipse.jdt.core.dom.ExpressionStatement; 47 import org.eclipse.jdt.core.dom.PrimitiveType; 48 import org.eclipse.jdt.core.dom.Statement; 49 50 import org.eclipse.jdt.internal.corext.SourceRange; 51 import org.eclipse.jdt.internal.corext.dom.ASTNodes; 52 import org.eclipse.jdt.internal.corext.refactoring.rename.MethodChecks; 53 import org.eclipse.jdt.internal.corext.refactoring.reorg.ReorgPolicyFactory; 54 import org.eclipse.jdt.internal.corext.refactoring.reorg.ReorgUtils; 55 import org.eclipse.jdt.internal.corext.refactoring.util.JavaElementUtil; 56 import org.eclipse.jdt.internal.corext.util.JavaModelUtil; 57 import org.eclipse.jdt.internal.corext.util.JdtFlags; 58 59 import org.eclipse.jdt.internal.ui.javaeditor.JavaTextSelection; 60 import org.eclipse.jdt.internal.ui.refactoring.actions.RefactoringActions; 61 62 73 public final class RefactoringAvailabilityTester { 74 75 public static IType getDeclaringType(IJavaElement element) { 76 if (element == null) 77 return null; 78 if (!(element instanceof IType)) 79 element= element.getAncestor(IJavaElement.TYPE); 80 return (IType) element; 81 } 82 83 public static IJavaElement[] getJavaElements(final Object [] elements) { 84 List result= new ArrayList (); 85 for (int index= 0; index < elements.length; index++) { 86 if (elements[index] instanceof IJavaElement) 87 result.add(elements[index]); 88 } 89 return (IJavaElement[]) result.toArray(new IJavaElement[result.size()]); 90 } 91 92 public static IMember[] getPullUpMembers(final IType type) throws JavaModelException { 93 final List list= new ArrayList (3); 94 if (type.exists()) { 95 IMember[] members= type.getFields(); 96 for (int index= 0; index < members.length; index++) { 97 if (isPullUpAvailable(members[index])) 98 list.add(members[index]); 99 } 100 members= type.getMethods(); 101 for (int index= 0; index < members.length; index++) { 102 if (isPullUpAvailable(members[index])) 103 list.add(members[index]); 104 } 105 members= type.getTypes(); 106 for (int index= 0; index < members.length; index++) { 107 if (isPullUpAvailable(members[index])) 108 list.add(members[index]); 109 } 110 } 111 return (IMember[]) list.toArray(new IMember[list.size()]); 112 } 113 114 public static IMember[] getPushDownMembers(final IType type) throws JavaModelException { 115 final List list= new ArrayList (3); 116 if (type.exists()) { 117 IMember[] members= type.getFields(); 118 for (int index= 0; index < members.length; index++) { 119 if (isPushDownAvailable(members[index])) 120 list.add(members[index]); 121 } 122 members= type.getMethods(); 123 for (int index= 0; index < members.length; index++) { 124 if (isPushDownAvailable(members[index])) 125 list.add(members[index]); 126 } 127 } 128 return (IMember[]) list.toArray(new IMember[list.size()]); 129 } 130 131 public static IResource[] getResources(final Object [] elements) { 132 List result= new ArrayList (); 133 for (int index= 0; index < elements.length; index++) { 134 if (elements[index] instanceof IResource) 135 result.add(elements[index]); 136 } 137 return (IResource[]) result.toArray(new IResource[result.size()]); 138 } 139 140 public static IType getSingleSelectedType(IStructuredSelection selection) throws JavaModelException { 141 Object first= selection.getFirstElement(); 142 if (first instanceof IType) 143 return (IType) first; 144 if (first instanceof ICompilationUnit) { 145 final ICompilationUnit unit= (ICompilationUnit) first; 146 if (unit.exists()) 147 return JavaElementUtil.getMainType(unit); 148 } 149 return null; 150 } 151 152 public static IType getTopLevelType(final IMember[] members) { 153 if (members != null && members.length == 1 && Checks.isTopLevelType(members[0])) 154 return (IType) members[0]; 155 return null; 156 } 157 158 public static boolean isChangeSignatureAvailable(final IMethod method) throws JavaModelException { 159 return Checks.isAvailable(method) && !Flags.isAnnotation(method.getDeclaringType().getFlags()); 160 } 161 162 public static boolean isChangeSignatureAvailable(final IStructuredSelection selection) throws JavaModelException { 163 if (selection.size() == 1) { 164 if (selection.getFirstElement() instanceof IMethod) { 165 final IMethod method= (IMethod) selection.getFirstElement(); 166 return isChangeSignatureAvailable(method); 167 } 168 } 169 return false; 170 } 171 172 public static boolean isChangeSignatureAvailable(final JavaTextSelection selection) throws JavaModelException { 173 final IJavaElement[] elements= selection.resolveElementAtOffset(); 174 if (elements.length == 1 && (elements[0] instanceof IMethod)) 175 return isChangeSignatureAvailable((IMethod) elements[0]); 176 final IJavaElement element= selection.resolveEnclosingElement(); 177 return (element instanceof IMethod) && isChangeSignatureAvailable((IMethod) element); 178 } 179 180 public static boolean isCommonDeclaringType(final IMember[] members) { 181 if (members.length == 0) 182 return false; 183 final IType type= members[0].getDeclaringType(); 184 if (type == null) 185 return false; 186 for (int index= 0; index < members.length; index++) { 187 if (!type.equals(members[index].getDeclaringType())) 188 return false; 189 } 190 return true; 191 } 192 193 public static boolean isConvertAnonymousAvailable(final IStructuredSelection selection) throws JavaModelException { 194 if (selection.size() == 1) { 195 if (selection.getFirstElement() instanceof IType) { 196 return isConvertAnonymousAvailable((IType) selection.getFirstElement()); 197 } 198 } 199 return false; 200 } 201 202 public static boolean isConvertAnonymousAvailable(final IType type) throws JavaModelException { 203 if (Checks.isAvailable(type)) { 204 final IJavaElement element= type.getParent(); 205 if (element instanceof IField && JdtFlags.isEnum((IMember) element)) 206 return false; 207 return type.isAnonymous(); 208 } 209 return false; 210 } 211 212 public static boolean isConvertAnonymousAvailable(final JavaTextSelection selection) throws JavaModelException { 213 final IType type= RefactoringActions.getEnclosingType(selection); 214 if (type != null) 215 return RefactoringAvailabilityTester.isConvertAnonymousAvailable(type); 216 return false; 217 } 218 219 public static boolean isCopyAvailable(final IResource[] resources, final IJavaElement[] elements) throws JavaModelException { 220 return ReorgPolicyFactory.createCopyPolicy(resources, elements).canEnable(); 221 } 222 223 public static boolean isDelegateCreationAvailable(final IField field) throws JavaModelException { 224 return field.exists() && (Flags.isStatic(field.getFlags()) && Flags.isFinal(field.getFlags()) ); 228 } 229 230 public static boolean isDeleteAvailable(final IJavaElement element) throws JavaModelException { 231 if (!element.exists()) 232 return false; 233 if (element instanceof IJavaModel || element instanceof IJavaProject) 234 return false; 235 if (element.getParent() != null && element.getParent().isReadOnly()) 236 return false; 237 if (element instanceof IPackageFragmentRoot) { 238 IPackageFragmentRoot root= (IPackageFragmentRoot) element; 239 if (root.isExternal() || Checks.isClasspathDelete(root)) return false; 243 244 if (root.getResource().equals(root.getJavaProject().getProject())) 245 return false; 246 } 247 if (element.getResource() == null && !RefactoringAvailabilityTester.isWorkingCopyElement(element)) 248 return false; 249 if (element instanceof IMember && ((IMember) element).isBinary()) 250 return false; 251 return true; 252 } 253 254 public static boolean isDeleteAvailable(final IResource resource) { 255 if (!resource.exists() || resource.isPhantom()) 256 return false; 257 if (resource.getType() == IResource.ROOT || resource.getType() == IResource.PROJECT) 258 return false; 259 return true; 260 } 261 262 public static boolean isDeleteAvailable(final IStructuredSelection selection) throws JavaModelException { 263 if (!selection.isEmpty()) 264 return isDeleteAvailable(selection.toArray()); 265 return false; 266 } 267 268 public static boolean isDeleteAvailable(final Object [] objects) throws JavaModelException { 269 if (objects.length != 0) { 270 final IResource[] resources= RefactoringAvailabilityTester.getResources(objects); 271 final IJavaElement[] elements= RefactoringAvailabilityTester.getJavaElements(objects); 272 if (objects.length != resources.length + elements.length) 273 return false; 274 for (int index= 0; index < resources.length; index++) { 275 if (!isDeleteAvailable(resources[index])) 276 return false; 277 } 278 for (int index= 0; index < elements.length; index++) { 279 if (!isDeleteAvailable(elements[index])) 280 return false; 281 } 282 return true; 283 } 284 return false; 285 } 286 287 public static boolean isExternalizeStringsAvailable(final IStructuredSelection selection) throws JavaModelException { 288 for (Iterator iter= selection.iterator(); iter.hasNext();) { 289 Object element= iter.next(); 290 if (element instanceof IJavaElement) { 291 IJavaElement javaElement= (IJavaElement)element; 292 if (javaElement.exists() && !javaElement.isReadOnly()) { 293 int elementType= javaElement.getElementType(); 294 if (elementType == IJavaElement.PACKAGE_FRAGMENT) { 295 return true; 296 } else if (elementType == IJavaElement.PACKAGE_FRAGMENT_ROOT) { 297 IPackageFragmentRoot root= (IPackageFragmentRoot)javaElement; 298 if (!root.isExternal() && !ReorgUtils.isClassFolder(root)) 299 return true; 300 } else if (elementType == IJavaElement.JAVA_PROJECT) { 301 return true; 302 } else if (elementType == IJavaElement.COMPILATION_UNIT) { 303 ICompilationUnit cu= (ICompilationUnit)javaElement; 304 if (cu.exists()) 305 return true; 306 } else if (elementType == IJavaElement.TYPE) { 307 IType type= (IType)element; 308 ICompilationUnit cu= type.getCompilationUnit(); 309 if (cu != null && cu.exists()) 310 return true; 311 } 312 } 313 } 314 } 315 return false; 316 } 317 318 public static boolean isExtractConstantAvailable(final JavaTextSelection selection) { 319 return (selection.resolveInClassInitializer() || selection.resolveInMethodBody() || selection.resolveInVariableInitializer()) && Checks.isExtractableExpression(selection.resolveSelectedNodes(), selection.resolveCoveringNode()); 320 } 321 322 public static boolean isExtractInterfaceAvailable(final IStructuredSelection selection) throws JavaModelException { 323 if (selection.size() == 1) { 324 Object first= selection.getFirstElement(); 325 if (first instanceof IType) { 326 return isExtractInterfaceAvailable((IType) first); 327 } else if (first instanceof ICompilationUnit) { 328 ICompilationUnit unit= (ICompilationUnit) first; 329 if (!unit.exists() || unit.isReadOnly()) 330 return false; 331 332 return true; 333 } 334 } 335 return false; 336 } 337 338 public static boolean isExtractInterfaceAvailable(final IType type) throws JavaModelException { 339 return Checks.isAvailable(type) && !type.isBinary() && !type.isReadOnly() && !type.isAnnotation() && !type.isAnonymous(); 340 } 341 342 public static boolean isExtractInterfaceAvailable(final JavaTextSelection selection) throws JavaModelException { 343 return isExtractInterfaceAvailable(RefactoringActions.getEnclosingOrPrimaryType(selection)); 344 } 345 346 public static boolean isExtractMethodAvailable(final ASTNode[] nodes) { 347 if (nodes != null && nodes.length != 0) { 348 if (nodes.length == 1) 349 return nodes[0] instanceof Statement || Checks.isExtractableExpression(nodes[0]); 350 else { 351 for (int index= 0; index < nodes.length; index++) { 352 if (!(nodes[index] instanceof Statement)) 353 return false; 354 } 355 return true; 356 } 357 } 358 return false; 359 } 360 361 public static boolean isExtractMethodAvailable(final JavaTextSelection selection) { 362 return (selection.resolveInMethodBody() || selection.resolveInClassInitializer()) && RefactoringAvailabilityTester.isExtractMethodAvailable(selection.resolveSelectedNodes()); 363 } 364 365 public static boolean isExtractSupertypeAvailable(IMember member) throws JavaModelException { 366 if (!member.exists()) 367 return false; 368 final int type= member.getElementType(); 369 if (type != IJavaElement.METHOD && type != IJavaElement.FIELD && type != IJavaElement.TYPE) 370 return false; 371 if (JdtFlags.isEnum(member) && type != IJavaElement.TYPE) 372 return false; 373 if (!Checks.isAvailable(member)) 374 return false; 375 if (member instanceof IType) { 376 if (!JdtFlags.isStatic(member) && !JdtFlags.isEnum(member) && !JdtFlags.isAnnotation(member)) 377 return false; 378 } 379 if (member instanceof IMethod) { 380 final IMethod method= (IMethod) member; 381 if (method.isConstructor()) 382 return false; 383 if (JdtFlags.isNative(method)) 384 return false; 385 final IType declaring= method.getDeclaringType(); 386 if (declaring != null && declaring.isAnnotation()) 387 return false; 388 } 389 return true; 390 } 391 392 public static boolean isExtractSupertypeAvailable(final IMember[] members) throws JavaModelException { 393 if (members != null && members.length != 0) { 394 final IType type= getTopLevelType(members); 395 if (type != null && !type.isInterface()) 396 return true; 397 for (int index= 0; index < members.length; index++) { 398 if (!isExtractSupertypeAvailable(members[index])) 399 return false; 400 } 401 return isCommonDeclaringType(members); 402 } 403 return false; 404 } 405 406 public static boolean isExtractSupertypeAvailable(final IStructuredSelection selection) throws JavaModelException { 407 if (!selection.isEmpty()) { 408 if (selection.size() == 1) { 409 if (selection.getFirstElement() instanceof ICompilationUnit) 410 return true; final IType type= getSingleSelectedType(selection); 412 if (type != null) 413 return Checks.isAvailable(type) && isExtractSupertypeAvailable(new IType[] { type}); 414 } 415 for (final Iterator iterator= selection.iterator(); iterator.hasNext();) { 416 if (!(iterator.next() instanceof IMember)) 417 return false; 418 } 419 final Set members= new HashSet (); 420 members.addAll(Arrays.asList(selection.toArray())); 421 return isExtractSupertypeAvailable((IMember[]) members.toArray(new IMember[members.size()])); 422 } 423 return false; 424 } 425 426 public static boolean isExtractSupertypeAvailable(final JavaTextSelection selection) throws JavaModelException { 427 IJavaElement element= selection.resolveEnclosingElement(); 428 if (!(element instanceof IMember)) 429 return false; 430 return isExtractSupertypeAvailable(new IMember[] { (IMember) element}); 431 } 432 433 public static boolean isExtractTempAvailable(final JavaTextSelection selection) { 434 final ASTNode[] nodes= selection.resolveSelectedNodes(); 435 return (selection.resolveInMethodBody() || selection.resolveInClassInitializer()) && (Checks.isExtractableExpression(nodes, selection.resolveCoveringNode()) || (nodes != null && nodes.length == 1 && nodes[0] instanceof ExpressionStatement)); 436 } 437 438 public static boolean isGeneralizeTypeAvailable(final IJavaElement element) throws JavaModelException { 439 if (element != null && element.exists()) { 440 String type= null; 441 if (element instanceof IMethod) 442 type= ((IMethod) element).getReturnType(); 443 else if (element instanceof IField) { 444 final IField field= (IField) element; 445 if (JdtFlags.isEnum(field)) 446 return false; 447 type= field.getTypeSignature(); 448 } else if (element instanceof ILocalVariable) 449 return true; 450 else if (element instanceof IType) { 451 final IType clazz= (IType) element; 452 if (JdtFlags.isEnum(clazz)) 453 return false; 454 return true; 455 } 456 if (type == null || PrimitiveType.toCode(Signature.toString(type)) != null) 457 return false; 458 return true; 459 } 460 return false; 461 } 462 463 public static boolean isGeneralizeTypeAvailable(final IStructuredSelection selection) throws JavaModelException { 464 if (selection.size() == 1) { 465 final Object element= selection.getFirstElement(); 466 if (element instanceof IMethod) { 467 final IMethod method= (IMethod) element; 468 if (!method.exists()) 469 return false; 470 final String type= method.getReturnType(); 471 if (PrimitiveType.toCode(Signature.toString(type)) == null) 472 return Checks.isAvailable(method); 473 } else if (element instanceof IField) { 474 final IField field= (IField) element; 475 if (!field.exists()) 476 return false; 477 if (!JdtFlags.isEnum(field)) 478 return Checks.isAvailable(field); 479 } 480 } 481 return false; 482 } 483 484 public static boolean isGeneralizeTypeAvailable(final JavaTextSelection selection) throws JavaModelException { 485 final IJavaElement[] elements= selection.resolveElementAtOffset(); 486 if (elements.length != 1) 487 return false; 488 return isGeneralizeTypeAvailable(elements[0]); 489 } 490 491 public static boolean isInferTypeArgumentsAvailable(final IJavaElement element) throws JavaModelException { 492 if (!Checks.isAvailable(element)) { 493 return false; 494 } else if (element instanceof IJavaProject) { 495 IJavaProject project= (IJavaProject) element; 496 IClasspathEntry[] classpathEntries= project.getRawClasspath(); 497 for (int i= 0; i < classpathEntries.length; i++) { 498 if (classpathEntries[i].getEntryKind() == IClasspathEntry.CPE_SOURCE) 499 return true; 500 } 501 return false; 502 } else if (element instanceof IPackageFragmentRoot) { 503 return ((IPackageFragmentRoot) element).getKind() == IPackageFragmentRoot.K_SOURCE; 504 } else if (element instanceof IPackageFragment) { 505 return ((IPackageFragment) element).getKind() == IPackageFragmentRoot.K_SOURCE; 506 } else if (element instanceof ICompilationUnit) { 507 return true; 508 } else if (element.getAncestor(IJavaElement.COMPILATION_UNIT) != null) { 509 return true; 510 } else { 511 return false; 512 } 513 } 514 515 public static boolean isInferTypeArgumentsAvailable(final IJavaElement[] elements) throws JavaModelException { 516 if (elements.length == 0) 517 return false; 518 519 for (int i= 0; i < elements.length; i++) { 520 if (!(isInferTypeArgumentsAvailable(elements[i]))) 521 return false; 522 } 523 return true; 524 } 525 526 public static boolean isInferTypeArgumentsAvailable(final IStructuredSelection selection) throws JavaModelException { 527 if (selection.isEmpty()) 528 return false; 529 530 for (Iterator iter= selection.iterator(); iter.hasNext();) { 531 Object element= iter.next(); 532 if (!(element instanceof IJavaElement)) 533 return false; 534 if (element instanceof ICompilationUnit) { 535 ICompilationUnit unit= (ICompilationUnit) element; 536 if (!unit.exists() || unit.isReadOnly()) 537 return false; 538 539 return true; 540 } 541 if (!isInferTypeArgumentsAvailable((IJavaElement) element)) 542 return false; 543 } 544 return true; 545 } 546 547 public static boolean isInlineConstantAvailable(final IField field) throws JavaModelException { 548 return Checks.isAvailable(field) && JdtFlags.isStatic(field) && JdtFlags.isFinal(field) && !JdtFlags.isEnum(field); 549 } 550 551 public static boolean isInlineConstantAvailable(final IStructuredSelection selection) throws JavaModelException { 552 if (selection.isEmpty() || selection.size() != 1) 553 return false; 554 final Object first= selection.getFirstElement(); 555 return (first instanceof IField) && isInlineConstantAvailable(((IField) first)); 556 } 557 558 public static boolean isInlineConstantAvailable(final JavaTextSelection selection) throws JavaModelException { 559 final IJavaElement[] elements= selection.resolveElementAtOffset(); 560 if (elements.length != 1) 561 return false; 562 return (elements[0] instanceof IField) && isInlineConstantAvailable(((IField) elements[0])); 563 } 564 565 public static boolean isInlineMethodAvailable(IMethod method) throws JavaModelException { 566 if (method == null) 567 return false; 568 if (!method.exists()) 569 return false; 570 if (!method.isStructureKnown()) 571 return false; 572 if (!method.isBinary()) 573 return true; 574 if (method.isConstructor()) 575 return false; 576 return SourceRange.isAvailable(method.getNameRange()); 577 } 578 579 public static boolean isInlineMethodAvailable(final IStructuredSelection selection) throws JavaModelException { 580 if (selection.isEmpty() || selection.size() != 1) 581 return false; 582 final Object first= selection.getFirstElement(); 583 return (first instanceof IMethod) && isInlineMethodAvailable(((IMethod) first)); 584 } 585 586 public static boolean isInlineMethodAvailable(final JavaTextSelection selection) throws JavaModelException { 587 final IJavaElement[] elements= selection.resolveElementAtOffset(); 588 if (elements.length != 1) 589 return false; 590 IJavaElement element= elements[0]; 591 if (!(element instanceof IMethod)) 592 return false; 593 IMethod method= (IMethod) element; 594 if (!isInlineMethodAvailable((method))) 595 return false; 596 597 IJavaElement enclosingElement= selection.resolveEnclosingElement(); 599 if (enclosingElement == null || enclosingElement.getAncestor(IJavaElement.CLASS_FILE) == null) 600 return true; 601 if (!(enclosingElement instanceof IMethod)) 602 return false; 603 IMethod enclosingMethod= (IMethod) enclosingElement; 604 if (enclosingMethod.isConstructor()) 605 return false; 606 int nameOffset= enclosingMethod.getNameRange().getOffset(); 607 int nameLength= enclosingMethod.getNameRange().getLength(); 608 return (nameOffset <= selection.getOffset()) && (selection.getOffset() + selection.getLength() <= nameOffset + nameLength); 609 } 610 611 public static boolean isInlineTempAvailable(final ILocalVariable variable) throws JavaModelException { 612 return Checks.isAvailable(variable); 613 } 614 615 public static boolean isInlineTempAvailable(final JavaTextSelection selection) throws JavaModelException { 616 final IJavaElement[] elements= selection.resolveElementAtOffset(); 617 if (elements.length != 1) 618 return false; 619 return (elements[0] instanceof ILocalVariable) && isInlineTempAvailable((ILocalVariable) elements[0]); 620 } 621 622 public static boolean isIntroduceFactoryAvailable(final IMethod method) throws JavaModelException { 623 return Checks.isAvailable(method) && method.isConstructor(); 624 } 625 626 public static boolean isIntroduceFactoryAvailable(final IStructuredSelection selection) throws JavaModelException { 627 if (selection.size() == 1 && selection.getFirstElement() instanceof IMethod) 628 return isIntroduceFactoryAvailable((IMethod) selection.getFirstElement()); 629 return false; 630 } 631 632 public static boolean isIntroduceFactoryAvailable(final JavaTextSelection selection) throws JavaModelException { 633 final IJavaElement[] elements= selection.resolveElementAtOffset(); 634 if (elements.length == 1 && elements[0] instanceof IMethod) 635 return isIntroduceFactoryAvailable((IMethod) elements[0]); 636 637 if (!Checks.isAvailable(selection.resolveEnclosingElement())) 639 return false; 640 ASTNode node= selection.resolveCoveringNode(); 641 if (node == null) { 642 ASTNode[] selectedNodes= selection.resolveSelectedNodes(); 643 if (selectedNodes != null && selectedNodes.length == 1) { 644 node= selectedNodes[0]; 645 if (node == null) 646 return false; 647 } else { 648 return false; 649 } 650 } 651 652 if (node.getNodeType() == ASTNode.CLASS_INSTANCE_CREATION) 653 return true; 654 655 node= ASTNodes.getNormalizedNode(node); 656 if (node.getLocationInParent() == ClassInstanceCreation.TYPE_PROPERTY) 657 return true; 658 659 return false; 660 } 661 662 public static boolean isIntroduceIndirectionAvailable(IMethod method) throws JavaModelException { 663 if (method == null) 664 return false; 665 if (!method.exists()) 666 return false; 667 if (!method.isStructureKnown()) 668 return false; 669 if (method.isConstructor()) 670 return false; 671 if (method.getDeclaringType().isAnnotation()) 672 return false; 673 674 return true; 675 } 676 677 public static boolean isIntroduceIndirectionAvailable(final IStructuredSelection selection) throws JavaModelException { 678 if (selection.isEmpty() || selection.size() != 1) 679 return false; 680 final Object first= selection.getFirstElement(); 681 return (first instanceof IMethod) && isIntroduceIndirectionAvailable(((IMethod) first)); 682 } 683 684 public static boolean isIntroduceIndirectionAvailable(final JavaTextSelection selection) throws JavaModelException { 685 final IJavaElement[] elements= selection.resolveElementAtOffset(); 686 if (elements.length == 1) 687 return (elements[0] instanceof IMethod) && isIntroduceIndirectionAvailable(((IMethod) elements[0])); 688 ASTNode[] selectedNodes= selection.resolveSelectedNodes(); 689 if (selectedNodes == null || selectedNodes.length != 1) 690 return false; 691 switch (selectedNodes[0].getNodeType()) { 692 case ASTNode.METHOD_DECLARATION: 693 case ASTNode.METHOD_INVOCATION: 694 case ASTNode.SUPER_METHOD_INVOCATION: 695 return true; 696 default: 697 return false; 698 } 699 } 700 701 public static boolean isIntroduceParameterAvailable(final ASTNode[] selectedNodes, ASTNode coveringNode) { 702 return Checks.isExtractableExpression(selectedNodes, coveringNode); 703 } 704 705 public static boolean isIntroduceParameterAvailable(final JavaTextSelection selection) { 706 return selection.resolveInMethodBody() && isIntroduceParameterAvailable(selection.resolveSelectedNodes(), selection.resolveCoveringNode()); 707 } 708 709 public static boolean isMoveAvailable(final IResource[] resources, final IJavaElement[] elements) throws JavaModelException { 710 if (elements != null) { 711 for (int index= 0; index < elements.length; index++) { 712 IJavaElement element= elements[index]; 713 if (element == null || !element.exists()) 714 return false; 715 if ((element instanceof IType) && ((IType) element).isLocal()) 716 return false; 717 if ((element instanceof IPackageDeclaration)) 718 return false; 719 if (element instanceof IField && JdtFlags.isEnum((IMember) element)) 720 return false; 721 } 722 } 723 return ReorgPolicyFactory.createMovePolicy(resources, elements).canEnable(); 724 } 725 726 public static boolean isMoveAvailable(final JavaTextSelection selection) throws JavaModelException { 727 final IJavaElement element= selection.resolveEnclosingElement(); 728 if (element == null) 729 return false; 730 return isMoveAvailable(new IResource[0], new IJavaElement[] { element}); 731 } 732 733 public static boolean isMoveInnerAvailable(final IStructuredSelection selection) throws JavaModelException { 734 if (selection.size() == 1) { 735 Object first= selection.getFirstElement(); 736 if (first instanceof IType) { 737 return isMoveInnerAvailable((IType) first); 738 } 739 } 740 return false; 741 } 742 743 public static boolean isMoveInnerAvailable(final IType type) throws JavaModelException { 744 return Checks.isAvailable(type) && !Checks.isAnonymous(type) && !Checks.isTopLevel(type) && !Checks.isInsideLocalType(type); 745 } 746 747 public static boolean isMoveInnerAvailable(final JavaTextSelection selection) throws JavaModelException { 748 IType type= RefactoringAvailabilityTester.getDeclaringType(selection.resolveEnclosingElement()); 749 if (type == null) 750 return false; 751 return isMoveInnerAvailable(type); 752 } 753 754 public static boolean isMoveMethodAvailable(final IMethod method) throws JavaModelException { 755 return method.exists() && !method.isConstructor() && !method.isBinary() && !method.getDeclaringType().isAnnotation() && !method.isReadOnly() && !JdtFlags.isStatic(method); 756 } 757 758 public static boolean isMoveMethodAvailable(final IStructuredSelection selection) throws JavaModelException { 759 if (selection.size() == 1) { 760 final Object first= selection.getFirstElement(); 761 return first instanceof IMethod && isMoveMethodAvailable((IMethod) first); 762 } 763 return false; 764 } 765 766 public static boolean isMoveMethodAvailable(final JavaTextSelection selection) throws JavaModelException { 767 final IJavaElement method= selection.resolveEnclosingElement(); 768 if (!(method instanceof IMethod)) 769 return false; 770 return isMoveMethodAvailable((IMethod) method); 771 } 772 773 public static boolean isMoveStaticAvailable(final IMember member) throws JavaModelException { 774 if (!member.exists()) 775 return false; 776 final int type= member.getElementType(); 777 if (type != IJavaElement.METHOD && type != IJavaElement.FIELD && type != IJavaElement.TYPE) 778 return false; 779 if (JdtFlags.isEnum(member) && type != IJavaElement.TYPE) 780 return false; 781 final IType declaring= member.getDeclaringType(); 782 if (declaring == null) 783 return false; 784 if (!Checks.isAvailable(member)) 785 return false; 786 if (type == IJavaElement.METHOD && declaring.isInterface()) 787 return false; 788 if (type == IJavaElement.METHOD && !JdtFlags.isStatic(member)) 789 return false; 790 if (type == IJavaElement.METHOD && ((IMethod) member).isConstructor()) 791 return false; 792 if (type == IJavaElement.TYPE && !JdtFlags.isStatic(member)) 793 return false; 794 if (!declaring.isInterface() && !JdtFlags.isStatic(member)) 795 return false; 796 return true; 797 } 798 799 public static boolean isMoveStaticAvailable(final IMember[] members) throws JavaModelException { 800 for (int index= 0; index < members.length; index++) { 801 if (!isMoveStaticAvailable(members[index])) 802 return false; 803 } 804 return true; 805 } 806 807 public static boolean isMoveStaticAvailable(final JavaTextSelection selection) throws JavaModelException { 808 final IJavaElement element= selection.resolveEnclosingElement(); 809 if (!(element instanceof IMember)) 810 return false; 811 return RefactoringAvailabilityTester.isMoveStaticMembersAvailable(new IMember[] { (IMember) element}); 812 } 813 814 public static boolean isMoveStaticMembersAvailable(final IMember[] members) throws JavaModelException { 815 if (members == null) 816 return false; 817 if (members.length == 0) 818 return false; 819 if (!isMoveStaticAvailable(members)) 820 return false; 821 if (!isCommonDeclaringType(members)) 822 return false; 823 return true; 824 } 825 826 public static boolean isPromoteTempAvailable(final ILocalVariable variable) throws JavaModelException { 827 return Checks.isAvailable(variable); 828 } 829 830 public static boolean isPromoteTempAvailable(final JavaTextSelection selection) throws JavaModelException { 831 final IJavaElement[] elements= selection.resolveElementAtOffset(); 832 if (elements.length != 1) 833 return false; 834 return (elements[0] instanceof ILocalVariable) && isPromoteTempAvailable((ILocalVariable) elements[0]); 835 } 836 837 public static boolean isPullUpAvailable(IMember member) throws JavaModelException { 838 if (!member.exists()) 839 return false; 840 final int type= member.getElementType(); 841 if (type != IJavaElement.METHOD && type != IJavaElement.FIELD && type != IJavaElement.TYPE) 842 return false; 843 if (JdtFlags.isEnum(member) && type != IJavaElement.TYPE) 844 return false; 845 if (!Checks.isAvailable(member)) 846 return false; 847 if (member instanceof IType) { 848 if (!JdtFlags.isStatic(member) && !JdtFlags.isEnum(member) && !JdtFlags.isAnnotation(member)) 849 return false; 850 } 851 if (member instanceof IMethod) { 852 final IMethod method= (IMethod) member; 853 if (method.isConstructor()) 854 return false; 855 if (JdtFlags.isNative(method)) 856 return false; 857 final IType declaring= method.getDeclaringType(); 858 if (declaring != null && declaring.isAnnotation()) 859 return false; 860 } 861 return true; 862 } 863 864 public static boolean isPullUpAvailable(final IMember[] members) throws JavaModelException { 865 if (members != null && members.length != 0) { 866 final IType type= getTopLevelType(members); 867 if (type != null && getPullUpMembers(type).length != 0) 868 return true; 869 for (int index= 0; index < members.length; index++) { 870 if (!isPullUpAvailable(members[index])) 871 return false; 872 } 873 return isCommonDeclaringType(members); 874 } 875 return false; 876 } 877 878 public static boolean isPullUpAvailable(final IStructuredSelection selection) throws JavaModelException { 879 if (!selection.isEmpty()) { 880 if (selection.size() == 1) { 881 if (selection.getFirstElement() instanceof ICompilationUnit) 882 return true; final IType type= getSingleSelectedType(selection); 884 if (type != null) 885 return Checks.isAvailable(type) && isPullUpAvailable(new IType[] { type}); 886 } 887 for (final Iterator iterator= selection.iterator(); iterator.hasNext();) { 888 if (!(iterator.next() instanceof IMember)) 889 return false; 890 } 891 final Set members= new HashSet (); 892 members.addAll(Arrays.asList(selection.toArray())); 893 return isPullUpAvailable((IMember[]) members.toArray(new IMember[members.size()])); 894 } 895 return false; 896 } 897 898 public static boolean isPullUpAvailable(final JavaTextSelection selection) throws JavaModelException { 899 IJavaElement element= selection.resolveEnclosingElement(); 900 if (!(element instanceof IMember)) 901 return false; 902 return isPullUpAvailable(new IMember[] { (IMember) element}); 903 } 904 905 public static boolean isPushDownAvailable(final IMember member) throws JavaModelException { 906 if (!member.exists()) 907 return false; 908 final int type= member.getElementType(); 909 if (type != IJavaElement.METHOD && type != IJavaElement.FIELD) 910 return false; 911 if (JdtFlags.isEnum(member)) 912 return false; 913 if (!Checks.isAvailable(member)) 914 return false; 915 if (JdtFlags.isStatic(member)) 916 return false; 917 if (type == IJavaElement.METHOD) { 918 final IMethod method= (IMethod) member; 919 if (method.isConstructor()) 920 return false; 921 if (JdtFlags.isNative(method)) 922 return false; 923 final IType declaring= method.getDeclaringType(); 924 if (declaring != null && declaring.isAnnotation()) 925 return false; 926 } 927 return true; 928 } 929 930 public static boolean isPushDownAvailable(final IMember[] members) throws JavaModelException { 931 if (members != null && members.length != 0) { 932 final IType type= getTopLevelType(members); 933 if (type != null && RefactoringAvailabilityTester.getPushDownMembers(type).length != 0) 934 return true; 935 if (type != null && JdtFlags.isEnum(type)) 936 return false; 937 for (int index= 0; index < members.length; index++) { 938 if (!isPushDownAvailable(members[index])) 939 return false; 940 } 941 return isCommonDeclaringType(members); 942 } 943 return false; 944 } 945 946 public static boolean isPushDownAvailable(final IStructuredSelection selection) throws JavaModelException { 947 if (!selection.isEmpty()) { 948 if (selection.size() == 1) { 949 if (selection.getFirstElement() instanceof ICompilationUnit) 950 return true; final IType type= getSingleSelectedType(selection); 952 if (type != null) 953 return isPushDownAvailable(new IType[] { type}); 954 } 955 for (final Iterator iterator= selection.iterator(); iterator.hasNext();) { 956 if (!(iterator.next() instanceof IMember)) 957 return false; 958 } 959 final Set members= new HashSet (); 960 members.addAll(Arrays.asList(selection.toArray())); 961 return isPushDownAvailable((IMember[]) members.toArray(new IMember[members.size()])); 962 } 963 return false; 964 } 965 966 public static boolean isPushDownAvailable(final JavaTextSelection selection) throws JavaModelException { 967 IJavaElement element= selection.resolveEnclosingElement(); 968 if (!(element instanceof IMember)) 969 return false; 970 return isPullUpAvailable(new IMember[] { (IMember) element}); 971 } 972 973 public static boolean isRenameAvailable(final ICompilationUnit unit) { 974 if (unit == null) 975 return false; 976 if (!unit.exists()) 977 return false; 978 if (!JavaModelUtil.isPrimary(unit)) 979 return false; 980 if (unit.isReadOnly()) 981 return false; 982 return true; 983 } 984 985 public static boolean isRenameAvailable(final IJavaProject project) throws JavaModelException { 986 if (project == null) 987 return false; 988 if (!Checks.isAvailable(project)) 989 return false; 990 if (!project.isConsistent()) 991 return false; 992 return true; 993 } 994 995 public static boolean isRenameAvailable(final ILocalVariable variable) throws JavaModelException { 996 return Checks.isAvailable(variable); 997 } 998 999 public static boolean isRenameAvailable(final IMethod method) throws CoreException { 1000 if (method == null) 1001 return false; 1002 if (!Checks.isAvailable(method)) 1003 return false; 1004 if (method.isConstructor()) 1005 return false; 1006 if (isRenameProhibited(method)) 1007 return false; 1008 return true; 1009 } 1010 1011 public static boolean isRenameAvailable(final IPackageFragment fragment) throws JavaModelException { 1012 if (fragment == null) 1013 return false; 1014 if (!Checks.isAvailable(fragment)) 1015 return false; 1016 if (fragment.isDefaultPackage()) 1017 return false; 1018 return true; 1019 } 1020 1021 public static boolean isRenameAvailable(final IPackageFragmentRoot root) throws JavaModelException { 1022 if (root == null) 1023 return false; 1024 if (!Checks.isAvailable(root)) 1025 return false; 1026 if (root.isArchive()) 1027 return false; 1028 if (root.isExternal()) 1029 return false; 1030 if (!root.isConsistent()) 1031 return false; 1032 if (root.getResource() instanceof IProject) 1033 return false; 1034 return true; 1035 } 1036 1037 public static boolean isRenameAvailable(final IResource resource) { 1038 if (resource == null) 1039 return false; 1040 if (!resource.exists()) 1041 return false; 1042 if (!resource.isAccessible()) 1043 return false; 1044 return true; 1045 } 1046 1047 public static boolean isRenameAvailable(final IType type) throws JavaModelException { 1048 if (type == null) 1049 return false; 1050 if (type.isAnonymous()) 1051 return false; 1052 if (!Checks.isAvailable(type)) 1053 return false; 1054 if (isRenameProhibited(type)) 1055 return false; 1056 return true; 1057 } 1058 1059 public static boolean isRenameAvailable(final ITypeParameter parameter) throws JavaModelException { 1060 return Checks.isAvailable(parameter); 1061 } 1062 1063 public static boolean isRenameEnumConstAvailable(final IField field) throws JavaModelException { 1064 return Checks.isAvailable(field) && field.getDeclaringType().isEnum(); 1065 } 1066 1067 public static boolean isRenameFieldAvailable(final IField field) throws JavaModelException { 1068 return Checks.isAvailable(field) && !JdtFlags.isEnum(field); 1069 } 1070 1071 public static boolean isRenameNonVirtualMethodAvailable(final IMethod method) throws JavaModelException, CoreException { 1072 return isRenameAvailable(method) && !MethodChecks.isVirtual(method); 1073 } 1074 1075 public static boolean isRenameProhibited(final IMethod method) throws CoreException { 1076 if (method.getElementName().equals("toString") && (method.getNumberOfParameters() == 0) && (method.getReturnType().equals("Ljava.lang.String;") || method.getReturnType().equals("QString;") || method.getReturnType().equals("Qjava.lang.String;"))) return true; 1081 else 1082 return false; 1083 } 1084 1085 public static boolean isRenameProhibited(final IType type) { 1086 return type.getPackageFragment().getElementName().equals("java.lang"); } 1088 1089 public static boolean isRenameVirtualMethodAvailable(final IMethod method) throws CoreException { 1090 return isRenameAvailable(method) && MethodChecks.isVirtual(method); 1091 } 1092 1093 public static boolean isReplaceInvocationsAvailable(IMethod method) throws JavaModelException { 1094 if (method == null) 1095 return false; 1096 if (!method.exists()) 1097 return false; 1098 if (method.isConstructor()) 1099 return false; 1100 return true; 1101 } 1102 1103 public static boolean isReplaceInvocationsAvailable(final IStructuredSelection selection) throws JavaModelException { 1104 if (selection.isEmpty() || selection.size() != 1) 1105 return false; 1106 final Object first= selection.getFirstElement(); 1107 return (first instanceof IMethod) && isReplaceInvocationsAvailable(((IMethod) first)); 1108 } 1109 1110 public static boolean isReplaceInvocationsAvailable(final JavaTextSelection selection) throws JavaModelException { 1111 final IJavaElement[] elements= selection.resolveElementAtOffset(); 1112 if (elements.length != 1) 1113 return false; 1114 IJavaElement element= elements[0]; 1115 return (element instanceof IMethod) && isReplaceInvocationsAvailable(((IMethod) element)); 1116 } 1117 1118 public static boolean isSelfEncapsulateAvailable(IField field) throws JavaModelException { 1119 return Checks.isAvailable(field) && !JdtFlags.isEnum(field) && !field.getDeclaringType().isAnnotation(); 1120 } 1121 1122 public static boolean isSelfEncapsulateAvailable(final IStructuredSelection selection) throws JavaModelException { 1123 if (selection.size() == 1) { 1124 if (selection.getFirstElement() instanceof IField) { 1125 final IField field= (IField) selection.getFirstElement(); 1126 return Checks.isAvailable(field) && !JdtFlags.isEnum(field); 1127 } 1128 } 1129 return false; 1130 } 1131 1132 public static boolean isSelfEncapsulateAvailable(final JavaTextSelection selection) throws JavaModelException { 1133 final IJavaElement[] elements= selection.resolveElementAtOffset(); 1134 if (elements.length != 1) 1135 return false; 1136 return (elements[0] instanceof IField) && isSelfEncapsulateAvailable((IField) elements[0]); 1137 } 1138 1139 public static boolean isUseSuperTypeAvailable(final IStructuredSelection selection) throws JavaModelException { 1140 if (selection.size() == 1) { 1141 final Object first= selection.getFirstElement(); 1142 if (first instanceof IType) { 1143 return isUseSuperTypeAvailable((IType) first); 1144 } else if (first instanceof ICompilationUnit) { 1145 ICompilationUnit unit= (ICompilationUnit) first; 1146 if (!unit.exists() || unit.isReadOnly()) 1147 return false; 1148 1149 return true; 1150 } 1151 } 1152 return false; 1153 } 1154 1155 public static boolean isUseSuperTypeAvailable(final IType type) throws JavaModelException { 1156 return type != null && type.exists() && !type.isAnnotation() && !type.isAnonymous(); 1157 } 1158 1159 public static boolean isUseSuperTypeAvailable(final JavaTextSelection selection) throws JavaModelException { 1160 return isUseSuperTypeAvailable(RefactoringActions.getEnclosingOrPrimaryType(selection)); 1161 } 1162 1163 public static boolean isWorkingCopyElement(final IJavaElement element) { 1164 if (element instanceof ICompilationUnit) 1165 return ((ICompilationUnit) element).isWorkingCopy(); 1166 if (ReorgUtils.isInsideCompilationUnit(element)) 1167 return ReorgUtils.getCompilationUnit(element).isWorkingCopy(); 1168 return false; 1169 } 1170 1171 private RefactoringAvailabilityTester() { 1172 } 1174 1175 public static boolean isIntroduceParameterObjectAvailable(IStructuredSelection selection) throws JavaModelException{ 1176 return isChangeSignatureAvailable(selection); } 1178 1179 public static boolean isIntroduceParameterObjectAvailable(JavaTextSelection selection) throws JavaModelException{ 1180 return isChangeSignatureAvailable(selection); } 1182} 1183 | Popular Tags |