1 package no.uio.ifi.refaktor.utils;
3 import static no.uio.ifi.refaktor.assertion.RefaktorAssert.assertThat;
5 import java.util.Arrays;
6 import java.util.LinkedList;
9 import no.uio.ifi.refaktor.exceptions.RefaktorException;
11 import org.eclipse.core.resources.IFile;
12 import org.eclipse.core.resources.IProject;
13 import org.eclipse.core.runtime.CoreException;
14 import org.eclipse.jdt.core.ICompilationUnit;
15 import org.eclipse.jdt.core.IJavaProject;
16 import org.eclipse.jdt.core.IMethod;
17 import org.eclipse.jdt.core.IPackageFragment;
18 import org.eclipse.jdt.core.IPackageFragmentRoot;
19 import org.eclipse.jdt.core.IType;
20 import org.eclipse.jdt.core.JavaCore;
21 import org.eclipse.jdt.core.JavaModelException;
22 import org.eclipse.ui.IEditorPart;
25 public class RefaktorHandleUtils {
27 public static ICompilationUnit getCompilationUnitFromEditor(IEditorPart editor) {
28 return findCompilationUnitFromFile(DocumentUtils.getFileFromEditor(editor));
31 public static ICompilationUnit findCompilationUnitFromFile(IFile file) {
32 return JavaCore.createCompilationUnitFrom(file);
35 public static ICompilationUnit findCompilationUnitFromProjectPackageAndTypeName(IProject project, String packageName, String simpleTypeName) {
37 return findType(project, packageName, simpleTypeName).getCompilationUnit();
38 } catch (JavaModelException e) {
39 throw new RuntimeException(e);
43 public static IType findType(IProject project, String packageName, String simpleTypeName) throws JavaModelException {
44 return findType(project, packageName + "." + simpleTypeName);
47 private static IType findType(IProject project, String fullyQualifiedTypeName) throws JavaModelException {
48 return createJavaProjectFrom(project).findType(fullyQualifiedTypeName);
51 public static IMethod findMethodHandleChecked(IProject project, String packageName, String simpleTypeName,
52 String methodName, String... methodSignature) {
54 IMethod method = findMethodHandle(project, packageName, simpleTypeName, methodName, methodSignature);
55 assertThat("method.exists()", method.exists());
57 } catch (JavaModelException e) {
58 throw new RefaktorException("The requested method handle could not be found");
62 public static IMethod findMethodHandle(IProject project, String packageName, String simpleTypeName,
63 String methodName, String[] methodSignature) throws JavaModelException {
64 return findType(project, packageName, simpleTypeName).getMethod(methodName, methodSignature);
67 public static IMethod findMethodHandle(IProject project, String fullyQualifiedTypeName, String simpleSignature) throws JavaModelException {
68 IType type = findType(project, fullyQualifiedTypeName);
69 MethodSignature methodSignature = MethodSignature.parseSignature(simpleSignature);
70 return type.getMethod(methodSignature.getName(), methodSignature.getParameterSignaturesArray());
73 public static IJavaProject createJavaProjectFrom(IProject project) {
74 assertThat(project.exists());
75 assertThat(project.isOpen());
77 assertThat(project.hasNature(JavaCore.NATURE_ID));
78 } catch (CoreException e) {
79 // Project does not exist or is not open
81 return JavaCore.create(project);
84 public static IPackageFragment findPackageChecked(String packageName, IProject project) {
86 return findPackage(packageName, project);
87 } catch (JavaModelException e) {
89 return new NullPackageFragmentHandle();
93 private static IPackageFragment findPackage(String packageName, IProject project) throws JavaModelException {
94 IJavaProject javaProject = createJavaProjectFrom(project);
95 for (IPackageFragment packageFragment: javaProject.getPackageFragments()) {
96 if (packageFragment.getElementName().equals(packageName))
97 return packageFragment;
99 return new NullPackageFragmentHandle();
102 public static List<IPackageFragment> getPackagesFromProject(IProject project) throws JavaModelException {
103 IPackageFragment[] packageFragments = createJavaProjectFrom(project).getPackageFragments();
104 return findPackagesAmongPackageFragments(Arrays.asList(packageFragments));
107 public static List<IPackageFragment> findPackagesAmongPackageFragments(List<IPackageFragment> packageFragments) throws JavaModelException {
108 List<IPackageFragment> packages = new LinkedList<IPackageFragment>();
109 for (IPackageFragment packageFragment: packageFragments) {
110 if (containsSourceCode(packageFragment))
111 packages.add(packageFragment);
116 private static boolean containsSourceCode(IPackageFragment packageFragment) throws JavaModelException {
117 return packageFragment.getKind() == IPackageFragmentRoot.K_SOURCE && packageFragment.hasChildren();