0001: /*******************************************************************************
0002: * Copyright (c) 2005, 2007 IBM Corporation and others.
0003: * All rights reserved. This program and the accompanying materials
0004: * are made available under the terms of the Eclipse Public License v1.0
0005: * which accompanies this distribution, and is available at
0006: * http://www.eclipse.org/legal/epl-v10.html
0007: *
0008: * Contributors:
0009: * IBM Corporation - initial API and implementation
0010: *******************************************************************************/package org.eclipse.jdt.internal.corext.refactoring;
0011:
0012: import java.util.ArrayList;
0013: import java.util.Arrays;
0014: import java.util.HashSet;
0015: import java.util.Iterator;
0016: import java.util.List;
0017: import java.util.Set;
0018:
0019: import org.eclipse.core.runtime.CoreException;
0020:
0021: import org.eclipse.core.resources.IProject;
0022: import org.eclipse.core.resources.IResource;
0023:
0024: import org.eclipse.jface.viewers.IStructuredSelection;
0025:
0026: import org.eclipse.ui.IWorkingSet;
0027:
0028: import org.eclipse.jdt.core.Flags;
0029: import org.eclipse.jdt.core.IClasspathEntry;
0030: import org.eclipse.jdt.core.ICompilationUnit;
0031: import org.eclipse.jdt.core.IField;
0032: import org.eclipse.jdt.core.IJavaElement;
0033: import org.eclipse.jdt.core.IJavaModel;
0034: import org.eclipse.jdt.core.IJavaProject;
0035: import org.eclipse.jdt.core.ILocalVariable;
0036: import org.eclipse.jdt.core.IMember;
0037: import org.eclipse.jdt.core.IMethod;
0038: import org.eclipse.jdt.core.IPackageDeclaration;
0039: import org.eclipse.jdt.core.IPackageFragment;
0040: import org.eclipse.jdt.core.IPackageFragmentRoot;
0041: import org.eclipse.jdt.core.IType;
0042: import org.eclipse.jdt.core.ITypeParameter;
0043: import org.eclipse.jdt.core.JavaModelException;
0044: import org.eclipse.jdt.core.Signature;
0045: import org.eclipse.jdt.core.dom.ASTNode;
0046: import org.eclipse.jdt.core.dom.ClassInstanceCreation;
0047: import org.eclipse.jdt.core.dom.ExpressionStatement;
0048: import org.eclipse.jdt.core.dom.PrimitiveType;
0049: import org.eclipse.jdt.core.dom.Statement;
0050:
0051: import org.eclipse.jdt.internal.corext.SourceRange;
0052: import org.eclipse.jdt.internal.corext.dom.ASTNodes;
0053: import org.eclipse.jdt.internal.corext.refactoring.rename.MethodChecks;
0054: import org.eclipse.jdt.internal.corext.refactoring.reorg.ReorgPolicyFactory;
0055: import org.eclipse.jdt.internal.corext.refactoring.reorg.ReorgUtils;
0056: import org.eclipse.jdt.internal.corext.refactoring.util.JavaElementUtil;
0057: import org.eclipse.jdt.internal.corext.util.JavaModelUtil;
0058: import org.eclipse.jdt.internal.corext.util.JdtFlags;
0059:
0060: import org.eclipse.jdt.internal.ui.javaeditor.JavaTextSelection;
0061: import org.eclipse.jdt.internal.ui.refactoring.actions.RefactoringActions;
0062: import org.eclipse.jdt.internal.ui.workingsets.JavaWorkingSetUpdater;
0063:
0064: /**
0065: * Helper class to detect whether a certain refactoring can be enabled on a
0066: * selection.
0067: * <p>
0068: * This class has been introduced to decouple actions from the refactoring code,
0069: * in order not to eagerly load refactoring classes during action
0070: * initialization.
0071: * </p>
0072: *
0073: * @since 3.1
0074: */
0075: public final class RefactoringAvailabilityTester {
0076:
0077: public static IType getDeclaringType(IJavaElement element) {
0078: if (element == null)
0079: return null;
0080: if (!(element instanceof IType))
0081: element = element.getAncestor(IJavaElement.TYPE);
0082: return (IType) element;
0083: }
0084:
0085: public static IJavaElement[] getJavaElements(final Object[] elements) {
0086: List result = new ArrayList();
0087: for (int index = 0; index < elements.length; index++) {
0088: if (elements[index] instanceof IJavaElement)
0089: result.add(elements[index]);
0090: }
0091: return (IJavaElement[]) result.toArray(new IJavaElement[result
0092: .size()]);
0093: }
0094:
0095: public static IMember[] getPullUpMembers(final IType type)
0096: throws JavaModelException {
0097: final List list = new ArrayList(3);
0098: if (type.exists()) {
0099: IMember[] members = type.getFields();
0100: for (int index = 0; index < members.length; index++) {
0101: if (isPullUpAvailable(members[index]))
0102: list.add(members[index]);
0103: }
0104: members = type.getMethods();
0105: for (int index = 0; index < members.length; index++) {
0106: if (isPullUpAvailable(members[index]))
0107: list.add(members[index]);
0108: }
0109: members = type.getTypes();
0110: for (int index = 0; index < members.length; index++) {
0111: if (isPullUpAvailable(members[index]))
0112: list.add(members[index]);
0113: }
0114: }
0115: return (IMember[]) list.toArray(new IMember[list.size()]);
0116: }
0117:
0118: public static IMember[] getPushDownMembers(final IType type)
0119: throws JavaModelException {
0120: final List list = new ArrayList(3);
0121: if (type.exists()) {
0122: IMember[] members = type.getFields();
0123: for (int index = 0; index < members.length; index++) {
0124: if (isPushDownAvailable(members[index]))
0125: list.add(members[index]);
0126: }
0127: members = type.getMethods();
0128: for (int index = 0; index < members.length; index++) {
0129: if (isPushDownAvailable(members[index]))
0130: list.add(members[index]);
0131: }
0132: }
0133: return (IMember[]) list.toArray(new IMember[list.size()]);
0134: }
0135:
0136: public static IResource[] getResources(final Object[] elements) {
0137: List result = new ArrayList();
0138: for (int index = 0; index < elements.length; index++) {
0139: if (elements[index] instanceof IResource)
0140: result.add(elements[index]);
0141: }
0142: return (IResource[]) result
0143: .toArray(new IResource[result.size()]);
0144: }
0145:
0146: public static IType getSingleSelectedType(
0147: IStructuredSelection selection) throws JavaModelException {
0148: Object first = selection.getFirstElement();
0149: if (first instanceof IType)
0150: return (IType) first;
0151: if (first instanceof ICompilationUnit) {
0152: final ICompilationUnit unit = (ICompilationUnit) first;
0153: if (unit.exists())
0154: return JavaElementUtil.getMainType(unit);
0155: }
0156: return null;
0157: }
0158:
0159: public static IType getTopLevelType(final IMember[] members) {
0160: if (members != null && members.length == 1
0161: && Checks.isTopLevelType(members[0]))
0162: return (IType) members[0];
0163: return null;
0164: }
0165:
0166: public static boolean isChangeSignatureAvailable(
0167: final IMethod method) throws JavaModelException {
0168: return Checks.isAvailable(method)
0169: && !Flags.isAnnotation(method.getDeclaringType()
0170: .getFlags());
0171: }
0172:
0173: public static boolean isChangeSignatureAvailable(
0174: final IStructuredSelection selection)
0175: throws JavaModelException {
0176: if (selection.size() == 1) {
0177: if (selection.getFirstElement() instanceof IMethod) {
0178: final IMethod method = (IMethod) selection
0179: .getFirstElement();
0180: return isChangeSignatureAvailable(method);
0181: }
0182: }
0183: return false;
0184: }
0185:
0186: public static boolean isChangeSignatureAvailable(
0187: final JavaTextSelection selection)
0188: throws JavaModelException {
0189: final IJavaElement[] elements = selection
0190: .resolveElementAtOffset();
0191: if (elements.length == 1 && (elements[0] instanceof IMethod))
0192: return isChangeSignatureAvailable((IMethod) elements[0]);
0193: final IJavaElement element = selection
0194: .resolveEnclosingElement();
0195: return (element instanceof IMethod)
0196: && isChangeSignatureAvailable((IMethod) element);
0197: }
0198:
0199: public static boolean isCommonDeclaringType(final IMember[] members) {
0200: if (members.length == 0)
0201: return false;
0202: final IType type = members[0].getDeclaringType();
0203: if (type == null)
0204: return false;
0205: for (int index = 0; index < members.length; index++) {
0206: if (!type.equals(members[index].getDeclaringType()))
0207: return false;
0208: }
0209: return true;
0210: }
0211:
0212: public static boolean isConvertAnonymousAvailable(
0213: final IStructuredSelection selection)
0214: throws JavaModelException {
0215: if (selection.size() == 1) {
0216: if (selection.getFirstElement() instanceof IType) {
0217: return isConvertAnonymousAvailable((IType) selection
0218: .getFirstElement());
0219: }
0220: }
0221: return false;
0222: }
0223:
0224: public static boolean isConvertAnonymousAvailable(final IType type)
0225: throws JavaModelException {
0226: if (Checks.isAvailable(type)) {
0227: final IJavaElement element = type.getParent();
0228: if (element instanceof IField
0229: && JdtFlags.isEnum((IMember) element))
0230: return false;
0231: return type.isAnonymous();
0232: }
0233: return false;
0234: }
0235:
0236: public static boolean isConvertAnonymousAvailable(
0237: final JavaTextSelection selection)
0238: throws JavaModelException {
0239: final IType type = RefactoringActions
0240: .getEnclosingType(selection);
0241: if (type != null)
0242: return RefactoringAvailabilityTester
0243: .isConvertAnonymousAvailable(type);
0244: return false;
0245: }
0246:
0247: public static boolean isCopyAvailable(final IResource[] resources,
0248: final IJavaElement[] elements) throws JavaModelException {
0249: return ReorgPolicyFactory.createCopyPolicy(resources, elements)
0250: .canEnable();
0251: }
0252:
0253: public static boolean isDelegateCreationAvailable(final IField field)
0254: throws JavaModelException {
0255: return field.exists()
0256: && (Flags.isStatic(field.getFlags()) && Flags
0257: .isFinal(field.getFlags()) /*
0258: * &&
0259: * hasInitializer(field)
0260: */);
0261: }
0262:
0263: public static boolean isDeleteAvailable(final IJavaElement element)
0264: throws JavaModelException {
0265: if (!element.exists())
0266: return false;
0267: if (element instanceof IJavaModel
0268: || element instanceof IJavaProject)
0269: return false;
0270: if (element.getParent() != null
0271: && element.getParent().isReadOnly())
0272: return false;
0273: if (element instanceof IPackageFragmentRoot) {
0274: IPackageFragmentRoot root = (IPackageFragmentRoot) element;
0275: if (root.isExternal() || Checks.isClasspathDelete(root)) // TODO
0276: // rename
0277: // isClasspathDelete
0278: return false;
0279:
0280: if (root.getResource().equals(
0281: root.getJavaProject().getProject()))
0282: return false;
0283: }
0284: if (element.getResource() == null
0285: && !RefactoringAvailabilityTester
0286: .isWorkingCopyElement(element))
0287: return false;
0288: if (element instanceof IMember
0289: && ((IMember) element).isBinary())
0290: return false;
0291: return true;
0292: }
0293:
0294: public static boolean isDeleteAvailable(final IResource resource) {
0295: if (!resource.exists() || resource.isPhantom())
0296: return false;
0297: if (resource.getType() == IResource.ROOT
0298: || resource.getType() == IResource.PROJECT)
0299: return false;
0300: return true;
0301: }
0302:
0303: public static boolean isDeleteAvailable(
0304: final IStructuredSelection selection)
0305: throws JavaModelException {
0306: if (!selection.isEmpty())
0307: return isDeleteAvailable(selection.toArray());
0308: return false;
0309: }
0310:
0311: public static boolean isDeleteAvailable(final Object[] objects)
0312: throws JavaModelException {
0313: if (objects.length != 0) {
0314: final IResource[] resources = RefactoringAvailabilityTester
0315: .getResources(objects);
0316: final IJavaElement[] elements = RefactoringAvailabilityTester
0317: .getJavaElements(objects);
0318: if (objects.length != resources.length + elements.length)
0319: return false;
0320: for (int index = 0; index < resources.length; index++) {
0321: if (!isDeleteAvailable(resources[index]))
0322: return false;
0323: }
0324: for (int index = 0; index < elements.length; index++) {
0325: if (!isDeleteAvailable(elements[index]))
0326: return false;
0327: }
0328: return true;
0329: }
0330: return false;
0331: }
0332:
0333: public static boolean isExternalizeStringsAvailable(
0334: final IStructuredSelection selection)
0335: throws JavaModelException {
0336: for (Iterator iter = selection.iterator(); iter.hasNext();) {
0337: Object element = iter.next();
0338: if (element instanceof IJavaElement) {
0339: IJavaElement javaElement = (IJavaElement) element;
0340: if (javaElement.exists() && !javaElement.isReadOnly()) {
0341: int elementType = javaElement.getElementType();
0342: if (elementType == IJavaElement.PACKAGE_FRAGMENT) {
0343: return true;
0344: } else if (elementType == IJavaElement.PACKAGE_FRAGMENT_ROOT) {
0345: IPackageFragmentRoot root = (IPackageFragmentRoot) javaElement;
0346: if (!root.isExternal()
0347: && !ReorgUtils.isClassFolder(root))
0348: return true;
0349: } else if (elementType == IJavaElement.JAVA_PROJECT) {
0350: return true;
0351: } else if (elementType == IJavaElement.COMPILATION_UNIT) {
0352: ICompilationUnit cu = (ICompilationUnit) javaElement;
0353: if (cu.exists())
0354: return true;
0355: } else if (elementType == IJavaElement.TYPE) {
0356: IJavaElement parent = ((IType) element)
0357: .getParent();
0358: if (parent instanceof ICompilationUnit
0359: && parent.exists())
0360: return true;
0361: }
0362: }
0363: } else if (element instanceof IWorkingSet) {
0364: IWorkingSet workingSet = (IWorkingSet) element;
0365: return JavaWorkingSetUpdater.ID.equals(workingSet
0366: .getId());
0367: }
0368: }
0369: return false;
0370: }
0371:
0372: public static boolean isExtractConstantAvailable(
0373: final JavaTextSelection selection) {
0374: return (selection.resolveInClassInitializer()
0375: || selection.resolveInMethodBody() || selection
0376: .resolveInVariableInitializer())
0377: && Checks.isExtractableExpression(selection
0378: .resolveSelectedNodes(), selection
0379: .resolveCoveringNode());
0380: }
0381:
0382: public static boolean isExtractInterfaceAvailable(
0383: final IStructuredSelection selection)
0384: throws JavaModelException {
0385: if (selection.size() == 1) {
0386: Object first = selection.getFirstElement();
0387: if (first instanceof IType) {
0388: return isExtractInterfaceAvailable((IType) first);
0389: } else if (first instanceof ICompilationUnit) {
0390: ICompilationUnit unit = (ICompilationUnit) first;
0391: if (!unit.exists() || unit.isReadOnly())
0392: return false;
0393:
0394: return true;
0395: }
0396: }
0397: return false;
0398: }
0399:
0400: public static boolean isExtractInterfaceAvailable(final IType type)
0401: throws JavaModelException {
0402: return Checks.isAvailable(type) && !type.isBinary()
0403: && !type.isReadOnly() && !type.isAnnotation()
0404: && !type.isAnonymous();
0405: }
0406:
0407: public static boolean isExtractInterfaceAvailable(
0408: final JavaTextSelection selection)
0409: throws JavaModelException {
0410: return isExtractInterfaceAvailable(RefactoringActions
0411: .getEnclosingOrPrimaryType(selection));
0412: }
0413:
0414: public static boolean isExtractMethodAvailable(final ASTNode[] nodes) {
0415: if (nodes != null && nodes.length != 0) {
0416: if (nodes.length == 1)
0417: return nodes[0] instanceof Statement
0418: || Checks.isExtractableExpression(nodes[0]);
0419: else {
0420: for (int index = 0; index < nodes.length; index++) {
0421: if (!(nodes[index] instanceof Statement))
0422: return false;
0423: }
0424: return true;
0425: }
0426: }
0427: return false;
0428: }
0429:
0430: public static boolean isExtractMethodAvailable(
0431: final JavaTextSelection selection) {
0432: return (selection.resolveInMethodBody() || selection
0433: .resolveInClassInitializer())
0434: && RefactoringAvailabilityTester
0435: .isExtractMethodAvailable(selection
0436: .resolveSelectedNodes());
0437: }
0438:
0439: public static boolean isExtractSupertypeAvailable(IMember member)
0440: throws JavaModelException {
0441: if (!member.exists())
0442: return false;
0443: final int type = member.getElementType();
0444: if (type != IJavaElement.METHOD && type != IJavaElement.FIELD
0445: && type != IJavaElement.TYPE)
0446: return false;
0447: if (JdtFlags.isEnum(member) && type != IJavaElement.TYPE)
0448: return false;
0449: if (!Checks.isAvailable(member))
0450: return false;
0451: if (member instanceof IType) {
0452: if (!JdtFlags.isStatic(member) && !JdtFlags.isEnum(member)
0453: && !JdtFlags.isAnnotation(member))
0454: return false;
0455: }
0456: if (member instanceof IMethod) {
0457: final IMethod method = (IMethod) member;
0458: if (method.isConstructor())
0459: return false;
0460: if (JdtFlags.isNative(method))
0461: return false;
0462: final IType declaring = method.getDeclaringType();
0463: if (declaring != null && declaring.isAnnotation())
0464: return false;
0465: }
0466: return true;
0467: }
0468:
0469: public static boolean isExtractSupertypeAvailable(
0470: final IMember[] members) throws JavaModelException {
0471: if (members != null && members.length != 0) {
0472: final IType type = getTopLevelType(members);
0473: if (type != null && !type.isInterface())
0474: return true;
0475: for (int index = 0; index < members.length; index++) {
0476: if (!isExtractSupertypeAvailable(members[index]))
0477: return false;
0478: }
0479: return isCommonDeclaringType(members);
0480: }
0481: return false;
0482: }
0483:
0484: public static boolean isExtractSupertypeAvailable(
0485: final IStructuredSelection selection)
0486: throws JavaModelException {
0487: if (!selection.isEmpty()) {
0488: if (selection.size() == 1) {
0489: if (selection.getFirstElement() instanceof ICompilationUnit)
0490: return true; // Do not force opening
0491: final IType type = getSingleSelectedType(selection);
0492: if (type != null)
0493: return Checks.isAvailable(type)
0494: && isExtractSupertypeAvailable(new IType[] { type });
0495: }
0496: for (final Iterator iterator = selection.iterator(); iterator
0497: .hasNext();) {
0498: if (!(iterator.next() instanceof IMember))
0499: return false;
0500: }
0501: final Set members = new HashSet();
0502: members.addAll(Arrays.asList(selection.toArray()));
0503: return isExtractSupertypeAvailable((IMember[]) members
0504: .toArray(new IMember[members.size()]));
0505: }
0506: return false;
0507: }
0508:
0509: public static boolean isExtractSupertypeAvailable(
0510: final JavaTextSelection selection)
0511: throws JavaModelException {
0512: IJavaElement element = selection.resolveEnclosingElement();
0513: if (!(element instanceof IMember))
0514: return false;
0515: return isExtractSupertypeAvailable(new IMember[] { (IMember) element });
0516: }
0517:
0518: public static boolean isExtractTempAvailable(
0519: final JavaTextSelection selection) {
0520: final ASTNode[] nodes = selection.resolveSelectedNodes();
0521: return (selection.resolveInMethodBody() || selection
0522: .resolveInClassInitializer())
0523: && (Checks.isExtractableExpression(nodes, selection
0524: .resolveCoveringNode()) || (nodes != null
0525: && nodes.length == 1 && nodes[0] instanceof ExpressionStatement));
0526: }
0527:
0528: public static boolean isGeneralizeTypeAvailable(
0529: final IJavaElement element) throws JavaModelException {
0530: if (element != null && element.exists()) {
0531: String type = null;
0532: if (element instanceof IMethod)
0533: type = ((IMethod) element).getReturnType();
0534: else if (element instanceof IField) {
0535: final IField field = (IField) element;
0536: if (JdtFlags.isEnum(field))
0537: return false;
0538: type = field.getTypeSignature();
0539: } else if (element instanceof ILocalVariable)
0540: return true;
0541: else if (element instanceof IType) {
0542: final IType clazz = (IType) element;
0543: if (JdtFlags.isEnum(clazz))
0544: return false;
0545: return true;
0546: }
0547: if (type == null
0548: || PrimitiveType.toCode(Signature.toString(type)) != null)
0549: return false;
0550: return true;
0551: }
0552: return false;
0553: }
0554:
0555: public static boolean isGeneralizeTypeAvailable(
0556: final IStructuredSelection selection)
0557: throws JavaModelException {
0558: if (selection.size() == 1) {
0559: final Object element = selection.getFirstElement();
0560: if (element instanceof IMethod) {
0561: final IMethod method = (IMethod) element;
0562: if (!method.exists())
0563: return false;
0564: final String type = method.getReturnType();
0565: if (PrimitiveType.toCode(Signature.toString(type)) == null)
0566: return Checks.isAvailable(method);
0567: } else if (element instanceof IField) {
0568: final IField field = (IField) element;
0569: if (!field.exists())
0570: return false;
0571: if (!JdtFlags.isEnum(field))
0572: return Checks.isAvailable(field);
0573: }
0574: }
0575: return false;
0576: }
0577:
0578: public static boolean isGeneralizeTypeAvailable(
0579: final JavaTextSelection selection)
0580: throws JavaModelException {
0581: final IJavaElement[] elements = selection
0582: .resolveElementAtOffset();
0583: if (elements.length != 1)
0584: return false;
0585: return isGeneralizeTypeAvailable(elements[0]);
0586: }
0587:
0588: public static boolean isInferTypeArgumentsAvailable(
0589: final IJavaElement element) throws JavaModelException {
0590: if (!Checks.isAvailable(element)) {
0591: return false;
0592: } else if (element instanceof IJavaProject) {
0593: IJavaProject project = (IJavaProject) element;
0594: IClasspathEntry[] classpathEntries = project
0595: .getRawClasspath();
0596: for (int i = 0; i < classpathEntries.length; i++) {
0597: if (classpathEntries[i].getEntryKind() == IClasspathEntry.CPE_SOURCE)
0598: return true;
0599: }
0600: return false;
0601: } else if (element instanceof IPackageFragmentRoot) {
0602: return ((IPackageFragmentRoot) element).getKind() == IPackageFragmentRoot.K_SOURCE;
0603: } else if (element instanceof IPackageFragment) {
0604: return ((IPackageFragment) element).getKind() == IPackageFragmentRoot.K_SOURCE;
0605: } else if (element instanceof ICompilationUnit) {
0606: return true;
0607: } else if (element.getAncestor(IJavaElement.COMPILATION_UNIT) != null) {
0608: return true;
0609: } else {
0610: return false;
0611: }
0612: }
0613:
0614: public static boolean isInferTypeArgumentsAvailable(
0615: final IJavaElement[] elements) throws JavaModelException {
0616: if (elements.length == 0)
0617: return false;
0618:
0619: for (int i = 0; i < elements.length; i++) {
0620: if (!(isInferTypeArgumentsAvailable(elements[i])))
0621: return false;
0622: }
0623: return true;
0624: }
0625:
0626: public static boolean isInferTypeArgumentsAvailable(
0627: final IStructuredSelection selection)
0628: throws JavaModelException {
0629: if (selection.isEmpty())
0630: return false;
0631:
0632: for (Iterator iter = selection.iterator(); iter.hasNext();) {
0633: Object element = iter.next();
0634: if (!(element instanceof IJavaElement))
0635: return false;
0636: if (element instanceof ICompilationUnit) {
0637: ICompilationUnit unit = (ICompilationUnit) element;
0638: if (!unit.exists() || unit.isReadOnly())
0639: return false;
0640:
0641: return true;
0642: }
0643: if (!isInferTypeArgumentsAvailable((IJavaElement) element))
0644: return false;
0645: }
0646: return true;
0647: }
0648:
0649: public static boolean isInlineConstantAvailable(final IField field)
0650: throws JavaModelException {
0651: return Checks.isAvailable(field) && JdtFlags.isStatic(field)
0652: && JdtFlags.isFinal(field) && !JdtFlags.isEnum(field);
0653: }
0654:
0655: public static boolean isInlineConstantAvailable(
0656: final IStructuredSelection selection)
0657: throws JavaModelException {
0658: if (selection.isEmpty() || selection.size() != 1)
0659: return false;
0660: final Object first = selection.getFirstElement();
0661: return (first instanceof IField)
0662: && isInlineConstantAvailable(((IField) first));
0663: }
0664:
0665: public static boolean isInlineConstantAvailable(
0666: final JavaTextSelection selection)
0667: throws JavaModelException {
0668: final IJavaElement[] elements = selection
0669: .resolveElementAtOffset();
0670: if (elements.length != 1)
0671: return false;
0672: return (elements[0] instanceof IField)
0673: && isInlineConstantAvailable(((IField) elements[0]));
0674: }
0675:
0676: public static boolean isInlineMethodAvailable(IMethod method)
0677: throws JavaModelException {
0678: if (method == null)
0679: return false;
0680: if (!method.exists())
0681: return false;
0682: if (!method.isStructureKnown())
0683: return false;
0684: if (!method.isBinary())
0685: return true;
0686: if (method.isConstructor())
0687: return false;
0688: return SourceRange.isAvailable(method.getNameRange());
0689: }
0690:
0691: public static boolean isInlineMethodAvailable(
0692: final IStructuredSelection selection)
0693: throws JavaModelException {
0694: if (selection.isEmpty() || selection.size() != 1)
0695: return false;
0696: final Object first = selection.getFirstElement();
0697: return (first instanceof IMethod)
0698: && isInlineMethodAvailable(((IMethod) first));
0699: }
0700:
0701: public static boolean isInlineMethodAvailable(
0702: final JavaTextSelection selection)
0703: throws JavaModelException {
0704: final IJavaElement[] elements = selection
0705: .resolveElementAtOffset();
0706: if (elements.length != 1)
0707: return false;
0708: IJavaElement element = elements[0];
0709: if (!(element instanceof IMethod))
0710: return false;
0711: IMethod method = (IMethod) element;
0712: if (!isInlineMethodAvailable((method)))
0713: return false;
0714:
0715: // in binary class, only activate for method declarations
0716: IJavaElement enclosingElement = selection
0717: .resolveEnclosingElement();
0718: if (enclosingElement == null
0719: || enclosingElement
0720: .getAncestor(IJavaElement.CLASS_FILE) == null)
0721: return true;
0722: if (!(enclosingElement instanceof IMethod))
0723: return false;
0724: IMethod enclosingMethod = (IMethod) enclosingElement;
0725: if (enclosingMethod.isConstructor())
0726: return false;
0727: int nameOffset = enclosingMethod.getNameRange().getOffset();
0728: int nameLength = enclosingMethod.getNameRange().getLength();
0729: return (nameOffset <= selection.getOffset())
0730: && (selection.getOffset() + selection.getLength() <= nameOffset
0731: + nameLength);
0732: }
0733:
0734: public static boolean isInlineTempAvailable(
0735: final ILocalVariable variable) throws JavaModelException {
0736: return Checks.isAvailable(variable);
0737: }
0738:
0739: public static boolean isInlineTempAvailable(
0740: final JavaTextSelection selection)
0741: throws JavaModelException {
0742: final IJavaElement[] elements = selection
0743: .resolveElementAtOffset();
0744: if (elements.length != 1)
0745: return false;
0746: return (elements[0] instanceof ILocalVariable)
0747: && isInlineTempAvailable((ILocalVariable) elements[0]);
0748: }
0749:
0750: public static boolean isIntroduceFactoryAvailable(
0751: final IMethod method) throws JavaModelException {
0752: return Checks.isAvailable(method) && method.isConstructor();
0753: }
0754:
0755: public static boolean isIntroduceFactoryAvailable(
0756: final IStructuredSelection selection)
0757: throws JavaModelException {
0758: if (selection.size() == 1
0759: && selection.getFirstElement() instanceof IMethod)
0760: return isIntroduceFactoryAvailable((IMethod) selection
0761: .getFirstElement());
0762: return false;
0763: }
0764:
0765: public static boolean isIntroduceFactoryAvailable(
0766: final JavaTextSelection selection)
0767: throws JavaModelException {
0768: final IJavaElement[] elements = selection
0769: .resolveElementAtOffset();
0770: if (elements.length == 1 && elements[0] instanceof IMethod)
0771: return isIntroduceFactoryAvailable((IMethod) elements[0]);
0772:
0773: // there's no IMethod for the default constructor
0774: if (!Checks.isAvailable(selection.resolveEnclosingElement()))
0775: return false;
0776: ASTNode node = selection.resolveCoveringNode();
0777: if (node == null) {
0778: ASTNode[] selectedNodes = selection.resolveSelectedNodes();
0779: if (selectedNodes != null && selectedNodes.length == 1) {
0780: node = selectedNodes[0];
0781: if (node == null)
0782: return false;
0783: } else {
0784: return false;
0785: }
0786: }
0787:
0788: if (node.getNodeType() == ASTNode.CLASS_INSTANCE_CREATION)
0789: return true;
0790:
0791: node = ASTNodes.getNormalizedNode(node);
0792: if (node.getLocationInParent() == ClassInstanceCreation.TYPE_PROPERTY)
0793: return true;
0794:
0795: return false;
0796: }
0797:
0798: public static boolean isIntroduceIndirectionAvailable(IMethod method)
0799: throws JavaModelException {
0800: if (method == null)
0801: return false;
0802: if (!method.exists())
0803: return false;
0804: if (!method.isStructureKnown())
0805: return false;
0806: if (method.isConstructor())
0807: return false;
0808: if (method.getDeclaringType().isAnnotation())
0809: return false;
0810:
0811: return true;
0812: }
0813:
0814: public static boolean isIntroduceIndirectionAvailable(
0815: final IStructuredSelection selection)
0816: throws JavaModelException {
0817: if (selection.isEmpty() || selection.size() != 1)
0818: return false;
0819: final Object first = selection.getFirstElement();
0820: return (first instanceof IMethod)
0821: && isIntroduceIndirectionAvailable(((IMethod) first));
0822: }
0823:
0824: public static boolean isIntroduceIndirectionAvailable(
0825: final JavaTextSelection selection)
0826: throws JavaModelException {
0827: final IJavaElement[] elements = selection
0828: .resolveElementAtOffset();
0829: if (elements.length == 1)
0830: return (elements[0] instanceof IMethod)
0831: && isIntroduceIndirectionAvailable(((IMethod) elements[0]));
0832: ASTNode[] selectedNodes = selection.resolveSelectedNodes();
0833: if (selectedNodes == null || selectedNodes.length != 1)
0834: return false;
0835: switch (selectedNodes[0].getNodeType()) {
0836: case ASTNode.METHOD_DECLARATION:
0837: case ASTNode.METHOD_INVOCATION:
0838: case ASTNode.SUPER_METHOD_INVOCATION:
0839: return true;
0840: default:
0841: return false;
0842: }
0843: }
0844:
0845: public static boolean isIntroduceParameterAvailable(
0846: final ASTNode[] selectedNodes, ASTNode coveringNode) {
0847: return Checks.isExtractableExpression(selectedNodes,
0848: coveringNode);
0849: }
0850:
0851: public static boolean isIntroduceParameterAvailable(
0852: final JavaTextSelection selection) {
0853: return selection.resolveInMethodBody()
0854: && isIntroduceParameterAvailable(selection
0855: .resolveSelectedNodes(), selection
0856: .resolveCoveringNode());
0857: }
0858:
0859: public static boolean isMoveAvailable(final IResource[] resources,
0860: final IJavaElement[] elements) throws JavaModelException {
0861: if (elements != null) {
0862: for (int index = 0; index < elements.length; index++) {
0863: IJavaElement element = elements[index];
0864: if (element == null || !element.exists())
0865: return false;
0866: if ((element instanceof IType)
0867: && ((IType) element).isLocal())
0868: return false;
0869: if ((element instanceof IPackageDeclaration))
0870: return false;
0871: if (element instanceof IField
0872: && JdtFlags.isEnum((IMember) element))
0873: return false;
0874: }
0875: }
0876: return ReorgPolicyFactory.createMovePolicy(resources, elements)
0877: .canEnable();
0878: }
0879:
0880: public static boolean isMoveAvailable(
0881: final JavaTextSelection selection)
0882: throws JavaModelException {
0883: final IJavaElement element = selection
0884: .resolveEnclosingElement();
0885: if (element == null)
0886: return false;
0887: return isMoveAvailable(new IResource[0],
0888: new IJavaElement[] { element });
0889: }
0890:
0891: public static boolean isMoveInnerAvailable(
0892: final IStructuredSelection selection)
0893: throws JavaModelException {
0894: if (selection.size() == 1) {
0895: Object first = selection.getFirstElement();
0896: if (first instanceof IType) {
0897: return isMoveInnerAvailable((IType) first);
0898: }
0899: }
0900: return false;
0901: }
0902:
0903: public static boolean isMoveInnerAvailable(final IType type)
0904: throws JavaModelException {
0905: return Checks.isAvailable(type) && !Checks.isAnonymous(type)
0906: && !Checks.isTopLevel(type)
0907: && !Checks.isInsideLocalType(type);
0908: }
0909:
0910: public static boolean isMoveInnerAvailable(
0911: final JavaTextSelection selection)
0912: throws JavaModelException {
0913: IType type = RefactoringAvailabilityTester
0914: .getDeclaringType(selection.resolveEnclosingElement());
0915: if (type == null)
0916: return false;
0917: return isMoveInnerAvailable(type);
0918: }
0919:
0920: public static boolean isMoveMethodAvailable(final IMethod method)
0921: throws JavaModelException {
0922: return method.exists() && !method.isConstructor()
0923: && !method.isBinary()
0924: && !method.getDeclaringType().isAnnotation()
0925: && !method.isReadOnly() && !JdtFlags.isStatic(method);
0926: }
0927:
0928: public static boolean isMoveMethodAvailable(
0929: final IStructuredSelection selection)
0930: throws JavaModelException {
0931: if (selection.size() == 1) {
0932: final Object first = selection.getFirstElement();
0933: return first instanceof IMethod
0934: && isMoveMethodAvailable((IMethod) first);
0935: }
0936: return false;
0937: }
0938:
0939: public static boolean isMoveMethodAvailable(
0940: final JavaTextSelection selection)
0941: throws JavaModelException {
0942: final IJavaElement method = selection.resolveEnclosingElement();
0943: if (!(method instanceof IMethod))
0944: return false;
0945: return isMoveMethodAvailable((IMethod) method);
0946: }
0947:
0948: public static boolean isMoveStaticAvailable(final IMember member)
0949: throws JavaModelException {
0950: if (!member.exists())
0951: return false;
0952: final int type = member.getElementType();
0953: if (type != IJavaElement.METHOD && type != IJavaElement.FIELD
0954: && type != IJavaElement.TYPE)
0955: return false;
0956: if (JdtFlags.isEnum(member) && type != IJavaElement.TYPE)
0957: return false;
0958: final IType declaring = member.getDeclaringType();
0959: if (declaring == null)
0960: return false;
0961: if (!Checks.isAvailable(member))
0962: return false;
0963: if (type == IJavaElement.METHOD && declaring.isInterface())
0964: return false;
0965: if (type == IJavaElement.METHOD && !JdtFlags.isStatic(member))
0966: return false;
0967: if (type == IJavaElement.METHOD
0968: && ((IMethod) member).isConstructor())
0969: return false;
0970: if (type == IJavaElement.TYPE && !JdtFlags.isStatic(member))
0971: return false;
0972: if (!declaring.isInterface() && !JdtFlags.isStatic(member))
0973: return false;
0974: return true;
0975: }
0976:
0977: public static boolean isMoveStaticAvailable(final IMember[] members)
0978: throws JavaModelException {
0979: for (int index = 0; index < members.length; index++) {
0980: if (!isMoveStaticAvailable(members[index]))
0981: return false;
0982: }
0983: return true;
0984: }
0985:
0986: public static boolean isMoveStaticAvailable(
0987: final JavaTextSelection selection)
0988: throws JavaModelException {
0989: final IJavaElement element = selection
0990: .resolveEnclosingElement();
0991: if (!(element instanceof IMember))
0992: return false;
0993: return RefactoringAvailabilityTester
0994: .isMoveStaticMembersAvailable(new IMember[] { (IMember) element });
0995: }
0996:
0997: public static boolean isMoveStaticMembersAvailable(
0998: final IMember[] members) throws JavaModelException {
0999: if (members == null)
1000: return false;
1001: if (members.length == 0)
1002: return false;
1003: if (!isMoveStaticAvailable(members))
1004: return false;
1005: if (!isCommonDeclaringType(members))
1006: return false;
1007: return true;
1008: }
1009:
1010: public static boolean isPromoteTempAvailable(
1011: final ILocalVariable variable) throws JavaModelException {
1012: return Checks.isAvailable(variable);
1013: }
1014:
1015: public static boolean isPromoteTempAvailable(
1016: final JavaTextSelection selection)
1017: throws JavaModelException {
1018: final IJavaElement[] elements = selection
1019: .resolveElementAtOffset();
1020: if (elements.length != 1)
1021: return false;
1022: return (elements[0] instanceof ILocalVariable)
1023: && isPromoteTempAvailable((ILocalVariable) elements[0]);
1024: }
1025:
1026: public static boolean isPullUpAvailable(IMember member)
1027: throws JavaModelException {
1028: if (!member.exists())
1029: return false;
1030: final int type = member.getElementType();
1031: if (type != IJavaElement.METHOD && type != IJavaElement.FIELD
1032: && type != IJavaElement.TYPE)
1033: return false;
1034: if (JdtFlags.isEnum(member) && type != IJavaElement.TYPE)
1035: return false;
1036: if (!Checks.isAvailable(member))
1037: return false;
1038: if (member instanceof IType) {
1039: if (!JdtFlags.isStatic(member) && !JdtFlags.isEnum(member)
1040: && !JdtFlags.isAnnotation(member))
1041: return false;
1042: }
1043: if (member instanceof IMethod) {
1044: final IMethod method = (IMethod) member;
1045: if (method.isConstructor())
1046: return false;
1047: if (JdtFlags.isNative(method))
1048: return false;
1049: final IType declaring = method.getDeclaringType();
1050: if (declaring != null && declaring.isAnnotation())
1051: return false;
1052: }
1053: return true;
1054: }
1055:
1056: public static boolean isPullUpAvailable(final IMember[] members)
1057: throws JavaModelException {
1058: if (members != null && members.length != 0) {
1059: final IType type = getTopLevelType(members);
1060: if (type != null && getPullUpMembers(type).length != 0)
1061: return true;
1062: for (int index = 0; index < members.length; index++) {
1063: if (!isPullUpAvailable(members[index]))
1064: return false;
1065: }
1066: return isCommonDeclaringType(members);
1067: }
1068: return false;
1069: }
1070:
1071: public static boolean isPullUpAvailable(
1072: final IStructuredSelection selection)
1073: throws JavaModelException {
1074: if (!selection.isEmpty()) {
1075: if (selection.size() == 1) {
1076: if (selection.getFirstElement() instanceof ICompilationUnit)
1077: return true; // Do not force opening
1078: final IType type = getSingleSelectedType(selection);
1079: if (type != null)
1080: return Checks.isAvailable(type)
1081: && isPullUpAvailable(new IType[] { type });
1082: }
1083: for (final Iterator iterator = selection.iterator(); iterator
1084: .hasNext();) {
1085: if (!(iterator.next() instanceof IMember))
1086: return false;
1087: }
1088: final Set members = new HashSet();
1089: members.addAll(Arrays.asList(selection.toArray()));
1090: return isPullUpAvailable((IMember[]) members
1091: .toArray(new IMember[members.size()]));
1092: }
1093: return false;
1094: }
1095:
1096: public static boolean isPullUpAvailable(
1097: final JavaTextSelection selection)
1098: throws JavaModelException {
1099: IJavaElement element = selection.resolveEnclosingElement();
1100: if (!(element instanceof IMember))
1101: return false;
1102: return isPullUpAvailable(new IMember[] { (IMember) element });
1103: }
1104:
1105: public static boolean isPushDownAvailable(final IMember member)
1106: throws JavaModelException {
1107: if (!member.exists())
1108: return false;
1109: final int type = member.getElementType();
1110: if (type != IJavaElement.METHOD && type != IJavaElement.FIELD)
1111: return false;
1112: if (JdtFlags.isEnum(member))
1113: return false;
1114: if (!Checks.isAvailable(member))
1115: return false;
1116: if (JdtFlags.isStatic(member))
1117: return false;
1118: if (type == IJavaElement.METHOD) {
1119: final IMethod method = (IMethod) member;
1120: if (method.isConstructor())
1121: return false;
1122: if (JdtFlags.isNative(method))
1123: return false;
1124: final IType declaring = method.getDeclaringType();
1125: if (declaring != null && declaring.isAnnotation())
1126: return false;
1127: }
1128: return true;
1129: }
1130:
1131: public static boolean isPushDownAvailable(final IMember[] members)
1132: throws JavaModelException {
1133: if (members != null && members.length != 0) {
1134: final IType type = getTopLevelType(members);
1135: if (type != null
1136: && RefactoringAvailabilityTester
1137: .getPushDownMembers(type).length != 0)
1138: return true;
1139: if (type != null && JdtFlags.isEnum(type))
1140: return false;
1141: for (int index = 0; index < members.length; index++) {
1142: if (!isPushDownAvailable(members[index]))
1143: return false;
1144: }
1145: return isCommonDeclaringType(members);
1146: }
1147: return false;
1148: }
1149:
1150: public static boolean isPushDownAvailable(
1151: final IStructuredSelection selection)
1152: throws JavaModelException {
1153: if (!selection.isEmpty()) {
1154: if (selection.size() == 1) {
1155: if (selection.getFirstElement() instanceof ICompilationUnit)
1156: return true; // Do not force opening
1157: final IType type = getSingleSelectedType(selection);
1158: if (type != null)
1159: return isPushDownAvailable(new IType[] { type });
1160: }
1161: for (final Iterator iterator = selection.iterator(); iterator
1162: .hasNext();) {
1163: if (!(iterator.next() instanceof IMember))
1164: return false;
1165: }
1166: final Set members = new HashSet();
1167: members.addAll(Arrays.asList(selection.toArray()));
1168: return isPushDownAvailable((IMember[]) members
1169: .toArray(new IMember[members.size()]));
1170: }
1171: return false;
1172: }
1173:
1174: public static boolean isPushDownAvailable(
1175: final JavaTextSelection selection)
1176: throws JavaModelException {
1177: IJavaElement element = selection.resolveEnclosingElement();
1178: if (!(element instanceof IMember))
1179: return false;
1180: return isPullUpAvailable(new IMember[] { (IMember) element });
1181: }
1182:
1183: public static boolean isRenameAvailable(final ICompilationUnit unit) {
1184: if (unit == null)
1185: return false;
1186: if (!unit.exists())
1187: return false;
1188: if (!JavaModelUtil.isPrimary(unit))
1189: return false;
1190: if (unit.isReadOnly())
1191: return false;
1192: return true;
1193: }
1194:
1195: public static boolean isRenameAvailable(final IJavaProject project)
1196: throws JavaModelException {
1197: if (project == null)
1198: return false;
1199: if (!Checks.isAvailable(project))
1200: return false;
1201: if (!project.isConsistent())
1202: return false;
1203: return true;
1204: }
1205:
1206: public static boolean isRenameAvailable(
1207: final ILocalVariable variable) throws JavaModelException {
1208: return Checks.isAvailable(variable);
1209: }
1210:
1211: public static boolean isRenameAvailable(final IMethod method)
1212: throws CoreException {
1213: if (method == null)
1214: return false;
1215: if (!Checks.isAvailable(method))
1216: return false;
1217: if (method.isConstructor())
1218: return false;
1219: if (isRenameProhibited(method))
1220: return false;
1221: return true;
1222: }
1223:
1224: public static boolean isRenameAvailable(
1225: final IPackageFragment fragment) throws JavaModelException {
1226: if (fragment == null)
1227: return false;
1228: if (!Checks.isAvailable(fragment))
1229: return false;
1230: if (fragment.isDefaultPackage())
1231: return false;
1232: return true;
1233: }
1234:
1235: public static boolean isRenameAvailable(
1236: final IPackageFragmentRoot root) throws JavaModelException {
1237: if (root == null)
1238: return false;
1239: if (!Checks.isAvailable(root))
1240: return false;
1241: if (root.isArchive())
1242: return false;
1243: if (root.isExternal())
1244: return false;
1245: if (!root.isConsistent())
1246: return false;
1247: if (root.getResource() instanceof IProject)
1248: return false;
1249: return true;
1250: }
1251:
1252: public static boolean isRenameAvailable(final IResource resource) {
1253: if (resource == null)
1254: return false;
1255: if (!resource.exists())
1256: return false;
1257: if (!resource.isAccessible())
1258: return false;
1259: return true;
1260: }
1261:
1262: public static boolean isRenameAvailable(final IType type)
1263: throws JavaModelException {
1264: if (type == null)
1265: return false;
1266: if (type.isAnonymous())
1267: return false;
1268: if (!Checks.isAvailable(type))
1269: return false;
1270: if (isRenameProhibited(type))
1271: return false;
1272: return true;
1273: }
1274:
1275: public static boolean isRenameAvailable(
1276: final ITypeParameter parameter) throws JavaModelException {
1277: return Checks.isAvailable(parameter);
1278: }
1279:
1280: public static boolean isRenameEnumConstAvailable(final IField field)
1281: throws JavaModelException {
1282: return Checks.isAvailable(field)
1283: && field.getDeclaringType().isEnum();
1284: }
1285:
1286: public static boolean isRenameFieldAvailable(final IField field)
1287: throws JavaModelException {
1288: return Checks.isAvailable(field) && !JdtFlags.isEnum(field);
1289: }
1290:
1291: public static boolean isRenameNonVirtualMethodAvailable(
1292: final IMethod method) throws JavaModelException,
1293: CoreException {
1294: return isRenameAvailable(method)
1295: && !MethodChecks.isVirtual(method);
1296: }
1297:
1298: public static boolean isRenameProhibited(final IMethod method)
1299: throws CoreException {
1300: if (method.getElementName().equals("toString") //$NON-NLS-1$
1301: && (method.getNumberOfParameters() == 0)
1302: && (method.getReturnType().equals("Ljava.lang.String;") //$NON-NLS-1$
1303: || method.getReturnType().equals("QString;") //$NON-NLS-1$
1304: || method.getReturnType().equals("Qjava.lang.String;"))) //$NON-NLS-1$
1305: return true;
1306: else
1307: return false;
1308: }
1309:
1310: public static boolean isRenameProhibited(final IType type) {
1311: return type.getPackageFragment().getElementName().equals(
1312: "java.lang"); //$NON-NLS-1$
1313: }
1314:
1315: public static boolean isRenameVirtualMethodAvailable(
1316: final IMethod method) throws CoreException {
1317: return isRenameAvailable(method)
1318: && MethodChecks.isVirtual(method);
1319: }
1320:
1321: public static boolean isReplaceInvocationsAvailable(IMethod method)
1322: throws JavaModelException {
1323: if (method == null)
1324: return false;
1325: if (!method.exists())
1326: return false;
1327: if (method.isConstructor())
1328: return false;
1329: return true;
1330: }
1331:
1332: public static boolean isReplaceInvocationsAvailable(
1333: final IStructuredSelection selection)
1334: throws JavaModelException {
1335: if (selection.isEmpty() || selection.size() != 1)
1336: return false;
1337: final Object first = selection.getFirstElement();
1338: return (first instanceof IMethod)
1339: && isReplaceInvocationsAvailable(((IMethod) first));
1340: }
1341:
1342: public static boolean isReplaceInvocationsAvailable(
1343: final JavaTextSelection selection)
1344: throws JavaModelException {
1345: final IJavaElement[] elements = selection
1346: .resolveElementAtOffset();
1347: if (elements.length != 1)
1348: return false;
1349: IJavaElement element = elements[0];
1350: return (element instanceof IMethod)
1351: && isReplaceInvocationsAvailable(((IMethod) element));
1352: }
1353:
1354: public static boolean isSelfEncapsulateAvailable(IField field)
1355: throws JavaModelException {
1356: return Checks.isAvailable(field) && !JdtFlags.isEnum(field)
1357: && !field.getDeclaringType().isAnnotation();
1358: }
1359:
1360: public static boolean isSelfEncapsulateAvailable(
1361: final IStructuredSelection selection)
1362: throws JavaModelException {
1363: if (selection.size() == 1) {
1364: if (selection.getFirstElement() instanceof IField) {
1365: final IField field = (IField) selection
1366: .getFirstElement();
1367: return Checks.isAvailable(field)
1368: && !JdtFlags.isEnum(field);
1369: }
1370: }
1371: return false;
1372: }
1373:
1374: public static boolean isSelfEncapsulateAvailable(
1375: final JavaTextSelection selection)
1376: throws JavaModelException {
1377: final IJavaElement[] elements = selection
1378: .resolveElementAtOffset();
1379: if (elements.length != 1)
1380: return false;
1381: return (elements[0] instanceof IField)
1382: && isSelfEncapsulateAvailable((IField) elements[0]);
1383: }
1384:
1385: public static boolean isUseSuperTypeAvailable(
1386: final IStructuredSelection selection)
1387: throws JavaModelException {
1388: if (selection.size() == 1) {
1389: final Object first = selection.getFirstElement();
1390: if (first instanceof IType) {
1391: return isUseSuperTypeAvailable((IType) first);
1392: } else if (first instanceof ICompilationUnit) {
1393: ICompilationUnit unit = (ICompilationUnit) first;
1394: if (!unit.exists() || unit.isReadOnly())
1395: return false;
1396:
1397: return true;
1398: }
1399: }
1400: return false;
1401: }
1402:
1403: public static boolean isUseSuperTypeAvailable(final IType type)
1404: throws JavaModelException {
1405: return type != null && type.exists() && !type.isAnnotation()
1406: && !type.isAnonymous();
1407: }
1408:
1409: public static boolean isUseSuperTypeAvailable(
1410: final JavaTextSelection selection)
1411: throws JavaModelException {
1412: return isUseSuperTypeAvailable(RefactoringActions
1413: .getEnclosingOrPrimaryType(selection));
1414: }
1415:
1416: public static boolean isWorkingCopyElement(
1417: final IJavaElement element) {
1418: if (element instanceof ICompilationUnit)
1419: return ((ICompilationUnit) element).isWorkingCopy();
1420: if (ReorgUtils.isInsideCompilationUnit(element))
1421: return ReorgUtils.getCompilationUnit(element)
1422: .isWorkingCopy();
1423: return false;
1424: }
1425:
1426: private RefactoringAvailabilityTester() {
1427: // Not for instantiation
1428: }
1429:
1430: public static boolean isIntroduceParameterObjectAvailable(
1431: IStructuredSelection selection) throws JavaModelException {
1432: return isChangeSignatureAvailable(selection); //TODO test selected element for more than 1 parameter?
1433: }
1434:
1435: public static boolean isIntroduceParameterObjectAvailable(
1436: JavaTextSelection selection) throws JavaModelException {
1437: return isChangeSignatureAvailable(selection); //TODO test selected element for more than 1 parameter?
1438: }
1439:
1440: public static boolean isExtractClassAvailable(IType type)
1441: throws JavaModelException {
1442: if (type == null)
1443: return false;
1444: if (!type.exists())
1445: return false;
1446: return ReorgUtils.hasSourceAvailable(type);
1447: }
1448: }
|