1 11 package org.eclipse.jdt.internal.corext.refactoring.rename; 12 13 import org.eclipse.core.runtime.Assert; 14 import org.eclipse.core.runtime.CoreException; 15 import org.eclipse.core.runtime.IProgressMonitor; 16 import org.eclipse.core.runtime.SubProgressMonitor; 17 18 import org.eclipse.ltk.core.refactoring.RefactoringStatus; 19 import org.eclipse.ltk.core.refactoring.RefactoringStatusContext; 20 21 import org.eclipse.jdt.core.IMethod; 22 import org.eclipse.jdt.core.IType; 23 import org.eclipse.jdt.core.ITypeHierarchy; 24 import org.eclipse.jdt.core.JavaModelException; 25 import org.eclipse.jdt.core.dom.IMethodBinding; 26 import org.eclipse.jdt.core.dom.Modifier; 27 28 import org.eclipse.jdt.internal.corext.Corext; 29 import org.eclipse.jdt.internal.corext.refactoring.Checks; 30 import org.eclipse.jdt.internal.corext.refactoring.RefactoringCoreMessages; 31 import org.eclipse.jdt.internal.corext.refactoring.base.JavaStatusContext; 32 import org.eclipse.jdt.internal.corext.refactoring.base.RefactoringStatusCodes; 33 import org.eclipse.jdt.internal.corext.refactoring.util.JavaElementUtil; 34 import org.eclipse.jdt.internal.corext.util.JavaModelUtil; 35 import org.eclipse.jdt.internal.corext.util.JdtFlags; 36 import org.eclipse.jdt.internal.corext.util.Messages; 37 import org.eclipse.jdt.internal.corext.util.MethodOverrideTester; 38 39 public class MethodChecks { 40 41 private MethodChecks(){ 43 } 44 45 public static boolean isVirtual(IMethod method) throws JavaModelException { 46 if (method.isConstructor()) 47 return false; 48 if (JdtFlags.isPrivate(method)) 49 return false; 50 if (JdtFlags.isStatic(method)) 51 return false; 52 return true; 53 } 54 55 public static boolean isVirtual(IMethodBinding methodBinding){ 56 if (methodBinding.isConstructor()) 57 return false; 58 if (Modifier.isPrivate(methodBinding.getModifiers())) return false; 60 if (Modifier.isStatic(methodBinding.getModifiers())) return false; 62 return true; 63 } 64 65 public static RefactoringStatus checkIfOverridesAnother(IMethod method, ITypeHierarchy hierarchy) throws JavaModelException { 66 IMethod overrides= MethodChecks.overridesAnotherMethod(method, hierarchy); 67 if (overrides == null) 68 return null; 69 70 RefactoringStatusContext context= JavaStatusContext.create(overrides); 71 String message= Messages.format(RefactoringCoreMessages.MethodChecks_overrides, 72 new String []{JavaElementUtil.createMethodSignature(overrides), JavaModelUtil.getFullyQualifiedName(overrides.getDeclaringType())}); 73 return RefactoringStatus.createStatus(RefactoringStatus.FATAL, message, context, Corext.getPluginId(), RefactoringStatusCodes.OVERRIDES_ANOTHER_METHOD, overrides); 74 } 75 76 81 public static RefactoringStatus checkIfComesFromInterface(IMethod method, ITypeHierarchy hierarchy, IProgressMonitor monitor) throws JavaModelException { 82 IMethod inInterface= MethodChecks.isDeclaredInInterface(method, hierarchy, monitor); 83 84 if (inInterface == null) 85 return null; 86 87 RefactoringStatusContext context= JavaStatusContext.create(inInterface); 88 String message= Messages.format(RefactoringCoreMessages.MethodChecks_implements, 89 new String []{JavaElementUtil.createMethodSignature(inInterface), JavaModelUtil.getFullyQualifiedName(inInterface.getDeclaringType())}); 90 return RefactoringStatus.createStatus(RefactoringStatus.FATAL, message, context, Corext.getPluginId(), RefactoringStatusCodes.METHOD_DECLARED_IN_INTERFACE, inInterface); 91 } 92 93 98 public static IMethod isDeclaredInInterface(IMethod method, ITypeHierarchy hierarchy, IProgressMonitor monitor) throws JavaModelException { 99 Assert.isTrue(isVirtual(method)); 100 IProgressMonitor subMonitor= new SubProgressMonitor(monitor, 1); 101 try { 102 IType[] classes= hierarchy.getAllClasses(); 103 subMonitor.beginTask("", classes.length); for (int i= 0; i < classes.length; i++) { 105 final IType clazz= classes[i]; 106 IType[] superinterfaces= null; 107 if (clazz.equals(hierarchy.getType())) 108 superinterfaces= hierarchy.getAllSuperInterfaces(clazz); 109 else 110 superinterfaces= clazz.newSupertypeHierarchy(new SubProgressMonitor(subMonitor, 1)).getAllSuperInterfaces(clazz); 111 for (int j= 0; j < superinterfaces.length; j++) { 112 IMethod found= Checks.findSimilarMethod(method, superinterfaces[j]); 113 if (found != null && !found.equals(method)) 114 return found; 115 } 116 subMonitor.worked(1); 117 } 118 return null; 119 } finally { 120 subMonitor.done(); 121 } 122 } 123 124 public static IMethod overridesAnotherMethod(IMethod method, ITypeHierarchy hierarchy) throws JavaModelException { 125 MethodOverrideTester tester= new MethodOverrideTester(method.getDeclaringType(), hierarchy); 126 IMethod found= tester.findDeclaringMethod(method, true); 127 boolean overrides= (found != null && !found.equals(method) && (!JdtFlags.isStatic(found)) && (!JdtFlags.isPrivate(found))); 128 if (overrides) 129 return found; 130 else 131 return null; 132 } 133 134 144 public static IMethod getTopmostMethod(IMethod method, ITypeHierarchy typeHierarchy, IProgressMonitor monitor) throws JavaModelException { 145 146 Assert.isNotNull(method); 147 148 ITypeHierarchy hierarchy= typeHierarchy; 149 IMethod topmostMethod= null; 150 final IType declaringType= method.getDeclaringType(); 151 if (!declaringType.isInterface()) { 152 if ((hierarchy == null) || !declaringType.equals(hierarchy.getType())) 153 hierarchy= declaringType.newTypeHierarchy(monitor); 154 155 IMethod inInterface= isDeclaredInInterface(method, hierarchy, monitor); 156 if (inInterface != null && !inInterface.equals(method)) 157 topmostMethod= inInterface; 158 } 159 if (topmostMethod == null) { 160 if (hierarchy == null) 161 hierarchy= declaringType.newSupertypeHierarchy(monitor); 162 IMethod overrides= overridesAnotherMethod(method, hierarchy); 163 if (overrides != null && !overrides.equals(method)) 164 topmostMethod= overrides; 165 } 166 return topmostMethod; 167 } 168 169 173 public static IMethod[] getOverriddenMethods(IMethod method, IProgressMonitor monitor) throws CoreException { 174 175 Assert.isNotNull(method); 176 return RippleMethodFinder2.getRelatedMethods(method, monitor, null); 177 } 178 } 179 | Popular Tags |