]> git.uio.no Git - ifi-stolz-refaktor.git/blob - software/no.uio.ifi.refaktor/src/no/uio/ifi/refaktor/utils/RefaktorHandleUtils.java
Making the ExtractAndMoveMethodAnalysisResult lightweight enough for it
[ifi-stolz-refaktor.git] / software / no.uio.ifi.refaktor / src / no / uio / ifi / refaktor / utils / RefaktorHandleUtils.java
1 package no.uio.ifi.refaktor.utils;
2
3 import static no.uio.ifi.refaktor.RefaktorAssert.assertThat;
4 import no.uio.ifi.refaktor.exceptions.RefaktorException;
5
6 import org.eclipse.core.resources.IFile;
7 import org.eclipse.core.resources.IProject;
8 import org.eclipse.jdt.core.ICompilationUnit;
9 import org.eclipse.jdt.core.IJavaProject;
10 import org.eclipse.jdt.core.IMethod;
11 import org.eclipse.jdt.core.IType;
12 import org.eclipse.jdt.core.JavaCore;
13 import org.eclipse.jdt.core.JavaModelException;
14 import org.eclipse.ui.IEditorPart;
15
16 public class RefaktorHandleUtils {
17
18         public static ICompilationUnit getCompilationUnitFromEditor(IEditorPart editor) {
19                 return findCompilationUnitFromFile(DocumentUtils.getFileFromEditor(editor));
20         }
21
22         public static ICompilationUnit findCompilationUnitFromFile(IFile file) {
23                 return JavaCore.createCompilationUnitFrom(file);
24         }
25
26         public static ICompilationUnit findCompilationUnitFromProjectPackageAndTypeName(IProject project, String packageName, String simpleTypeName) {
27                 try {
28                         return findType(project, packageName, simpleTypeName).getCompilationUnit();
29                 } catch (JavaModelException e) {
30                         throw new RuntimeException(e);
31                 }
32         }
33
34         public static IType findType(IProject project, String packageName, String simpleTypeName) throws JavaModelException {
35                 return createJavaProjectFrom(project).findType(packageName + "." + simpleTypeName);
36         }
37
38         public static IMethod findMethodHandleChecked(IProject project, String packageName, String simpleTypeName, 
39                         String methodName, String[] methodSignature) {
40                 try {
41                         IMethod method = findMethodHandle(project, packageName, simpleTypeName, methodName, methodSignature);
42                         assertThat("method.exists()", method.exists());
43                         return method;
44                 } catch (JavaModelException e) {
45                         throw new RefaktorException("The requested method handle could not be found");
46                 }
47         }
48
49         public static IMethod findMethodHandle(IProject project, String packageName, String simpleTypeName, 
50                         String methodName, String[] methodSignature) throws JavaModelException {
51                 return findType(project, packageName, simpleTypeName).getMethod(methodName, methodSignature);
52         }
53
54         public static IMethod findMethodHandle(IProject project,
55                         String packageName, String simpleTypeName, String simpleSignature) throws JavaModelException {
56                 IMethod[] methods = findMethodsForType(project, packageName, simpleTypeName);
57                 return findMethodHandleThatMatchesSimpleSignature(methods, simpleSignature);
58         }
59
60         private static IMethod[] findMethodsForType(IProject project, String packageName, String simpleTypeName)
61                         throws JavaModelException {
62                 return findType(project, packageName, simpleTypeName).getMethods();
63         }
64
65         private static IMethod findMethodHandleThatMatchesSimpleSignature(IMethod[] methods, String simpleSignature)
66                         throws JavaModelException {
67                 for (IMethod method: methods) {
68                         String methodSource = method.getSource();
69                         if (methodSource == null)
70                                 continue;
71                         if (makeSimpleSignature(methodSource).equals(simpleSignature)) 
72                                 return method;
73                 }
74                 return null;
75         }
76
77         private static String makeSimpleSignature(String methodSource) {
78                 return methodSource.substring(0, methodSource.indexOf(')') + 1).trim();
79         }
80
81         public static IJavaProject createJavaProjectFrom(IProject project) {
82                 assertThat(project.exists());
83                 return JavaCore.create(project);
84         }
85 }