]> git.uio.no Git - ifi-stolz-refaktor.git/blame - software/no.uio.ifi.refaktor/src/no/uio/ifi/refaktor/utils/RefaktorHandleUtils.java
Adding some logging capabilities to RefaktorDebug.
[ifi-stolz-refaktor.git] / software / no.uio.ifi.refaktor / src / no / uio / ifi / refaktor / utils / RefaktorHandleUtils.java
CommitLineData
bea8957a
EK
1package no.uio.ifi.refaktor.utils;
2
f6c9fe53 3import static no.uio.ifi.refaktor.RefaktorAssert.assertThat;
4bf3bf42
EK
4import no.uio.ifi.refaktor.exceptions.RefaktorException;
5
fe4785f1 6import org.eclipse.core.resources.IFile;
bea8957a 7import org.eclipse.core.resources.IProject;
fe4785f1 8import org.eclipse.jdt.core.ICompilationUnit;
36a99b48 9import org.eclipse.jdt.core.IJavaProject;
bea8957a 10import org.eclipse.jdt.core.IMethod;
0f9013c4 11import org.eclipse.jdt.core.IType;
bea8957a
EK
12import org.eclipse.jdt.core.JavaCore;
13import org.eclipse.jdt.core.JavaModelException;
558315ef 14import org.eclipse.ui.IEditorPart;
bea8957a
EK
15
16public class RefaktorHandleUtils {
fe4785f1 17
18447975 18 public static ICompilationUnit getCompilationUnitFromEditor(IEditorPart editor) {
558315ef 19 return findCompilationUnitFromFile(DocumentUtils.getFileFromEditor(editor));
fe4785f1
EK
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 }
4bf3bf42 33
37b905cb 34 public static IType findType(IProject project, String packageName, String simpleTypeName) throws JavaModelException {
36a99b48 35 return createJavaProjectFrom(project).findType(packageName + "." + simpleTypeName);
37b905cb 36 }
bea8957a 37
4bf3bf42
EK
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);
02079d76 42 assertThat(method.exists());
4bf3bf42
EK
43 return method;
44 } catch (JavaModelException e) {
45 throw new RefaktorException("The requested method handle could not be found");
46 }
47 }
48
bea8957a
EK
49 public static IMethod findMethodHandle(IProject project, String packageName, String simpleTypeName,
50 String methodName, String[] methodSignature) throws JavaModelException {
0f9013c4 51 return findType(project, packageName, simpleTypeName).getMethod(methodName, methodSignature);
bea8957a
EK
52 }
53
37b905cb 54 public static IMethod findMethodHandle(IProject project,
03f05305
EK
55 String packageName, String simpleTypeName, String simpleSignature) throws JavaModelException {
56 IMethod[] methods = findMethodsForType(project, packageName, simpleTypeName);
57ec7e36 57 return findMethodHandleThatMatchesSimpleSignature(methods, simpleSignature);
03f05305
EK
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
ebc41ab6 65 private static IMethod findMethodHandleThatMatchesSimpleSignature(IMethod[] methods, String simpleSignature)
03f05305
EK
66 throws JavaModelException {
67 for (IMethod method: methods) {
57ec7e36
EK
68 String methodSource = method.getSource();
69 if (methodSource == null)
03f05305 70 continue;
57ec7e36 71 if (makeSimpleSignature(methodSource).equals(simpleSignature))
03f05305 72 return method;
37b905cb 73 }
03f05305
EK
74 return null;
75 }
76
57ec7e36
EK
77 private static String makeSimpleSignature(String methodSource) {
78 return methodSource.substring(0, methodSource.indexOf(')') + 1).trim();
0f9013c4 79 }
36a99b48
EK
80
81 public static IJavaProject createJavaProjectFrom(IProject project) {
02079d76 82 assertThat(project.exists());
36a99b48
EK
83 return JavaCore.create(project);
84 }
bea8957a 85}