KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > corext > refactoring > rename > MethodChecks


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

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     //no instances
42
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())) //TODO is this enough?
59
return false;
60         if (Modifier.isStatic(methodBinding.getModifiers())) //TODO is this enough?
61
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 JavaDoc message= Messages.format(RefactoringCoreMessages.MethodChecks_overrides,
72                 new String JavaDoc[]{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     /**
77      * Checks if the given method is declared in an interface. If the method's declaring type
78      * is an interface the method returns <code>false</code> if it is only declared in that
79      * interface.
80      */

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 JavaDoc message= Messages.format(RefactoringCoreMessages.MethodChecks_implements,
89                 new String JavaDoc[]{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     /**
94      * Checks if the given method is declared in an interface. If the method's declaring type
95      * is an interface the method returns <code>false</code> if it is only declared in that
96      * interface.
97      */

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); //$NON-NLS-1$
104
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     /**
135      * Locates the topmost method of an override ripple and returns it. If none
136      * is found, null is returned.
137      *
138      * @param method the IMethod which may be part of a ripple
139      * @param typeHierarchy a ITypeHierarchy of the declaring type of the method. May be null
140      * @param monitor an IProgressMonitor
141      * @return the topmost method of the ripple, or null if none
142      * @throws JavaModelException
143      */

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     /**
170      * Finds all overridden methods of a certain method.
171      *
172      */

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