1 package no.uio.ifi.refaktor.utils;
3 import static no.uio.ifi.refaktor.RefaktorAssert.assertThat;
4 import no.uio.ifi.refaktor.exceptions.RefaktorException;
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;
16 public class RefaktorHandleUtils {
18 public static ICompilationUnit getCompilationUnitFromEditor(IEditorPart editor) {
19 return findCompilationUnitFromFile(DocumentUtils.getFileFromEditor(editor));
22 public static ICompilationUnit findCompilationUnitFromFile(IFile file) {
23 return JavaCore.createCompilationUnitFrom(file);
26 public static ICompilationUnit findCompilationUnitFromProjectPackageAndTypeName(IProject project, String packageName, String simpleTypeName) {
28 return findType(project, packageName, simpleTypeName).getCompilationUnit();
29 } catch (JavaModelException e) {
30 throw new RuntimeException(e);
34 public static IType findType(IProject project, String packageName, String simpleTypeName) throws JavaModelException {
35 return createJavaProjectFrom(project).findType(packageName + "." + simpleTypeName);
38 public static IMethod findMethodHandleChecked(IProject project, String packageName, String simpleTypeName,
39 String methodName, String[] methodSignature) {
41 IMethod method = findMethodHandle(project, packageName, simpleTypeName, methodName, methodSignature);
42 assertThat("method.exists()", method.exists());
44 } catch (JavaModelException e) {
45 throw new RefaktorException("The requested method handle could not be found");
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);
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);
60 private static IMethod[] findMethodsForType(IProject project, String packageName, String simpleTypeName)
61 throws JavaModelException {
62 return findType(project, packageName, simpleTypeName).getMethods();
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)
71 if (makeSimpleSignature(methodSource).equals(simpleSignature))
77 private static String makeSimpleSignature(String methodSource) {
78 return methodSource.substring(0, methodSource.indexOf(')') + 1).trim();
81 public static IJavaProject createJavaProjectFrom(IProject project) {
82 assertThat(project.exists());
83 return JavaCore.create(project);