]> git.uio.no Git - ifi-stolz-refaktor.git/blob - software/no.uio.ifi.refaktor/src/no/uio/ifi/refaktor/utils/RefaktorHandleUtils.java
MethodSignature: fixing api mismatch between Java 6 and 7
[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.assertion.RefaktorAssert.assertThat;
4
5 import java.util.Arrays;
6 import java.util.LinkedList;
7 import java.util.List;
8
9 import no.uio.ifi.refaktor.exceptions.RefaktorException;
10
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;
23
24 // TODO: clean up
25 public class RefaktorHandleUtils {
26
27         public static ICompilationUnit getCompilationUnitFromEditor(IEditorPart editor) {
28                 return findCompilationUnitFromFile(DocumentUtils.getFileFromEditor(editor));
29         }
30
31         public static ICompilationUnit findCompilationUnitFromFile(IFile file) {
32                 return JavaCore.createCompilationUnitFrom(file);
33         }
34
35         public static ICompilationUnit findCompilationUnitFromProjectPackageAndTypeName(IProject project, String packageName, String simpleTypeName) {
36                 try {
37                         return findType(project, packageName, simpleTypeName).getCompilationUnit();
38                 } catch (JavaModelException e) {
39                         throw new RuntimeException(e);
40                 }
41         }
42
43         public static IType findType(IProject project, String packageName, String simpleTypeName) throws JavaModelException {
44                 return findType(project, packageName + "." + simpleTypeName);
45         }
46
47         private static IType findType(IProject project, String fullyQualifiedTypeName) throws JavaModelException {
48                 return createJavaProjectFrom(project).findType(fullyQualifiedTypeName);
49         }
50
51         public static IMethod findMethodHandleChecked(IProject project, String packageName, String simpleTypeName, 
52                         String methodName, String... methodSignature) {
53                 try {
54                         IMethod method = findMethodHandle(project, packageName, simpleTypeName, methodName, methodSignature);
55                         assertThat("method.exists()", method.exists());
56                         return method;
57                 } catch (JavaModelException e) {
58                         throw new RefaktorException("The requested method handle could not be found");
59                 }
60         }
61
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);
65         }
66
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());
71         }
72
73         public static IJavaProject createJavaProjectFrom(IProject project) {
74                 assertThat(project.exists());
75                 assertThat(project.isOpen());
76                 try {
77                         assertThat(project.hasNature(JavaCore.NATURE_ID));
78                 } catch (CoreException e) {
79                         // Project does not exist or is not open
80                 }
81                 return JavaCore.create(project);
82         }
83
84         public static IPackageFragment findPackageChecked(String packageName, IProject project) {
85                 try {
86                         return findPackage(packageName, project);
87                 } catch (JavaModelException e) {
88                         RefaktorDebug.log(e);
89                         return new NullPackageFragmentHandle();
90                 }
91         }
92
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;
98                 }
99                 return new NullPackageFragmentHandle();
100         }
101
102         public static List<IPackageFragment> getPackagesFromProject(IProject project) throws JavaModelException {
103                 IPackageFragment[] packageFragments = createJavaProjectFrom(project).getPackageFragments();
104                 return findPackagesAmongPackageFragments(Arrays.asList(packageFragments));
105         }
106
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);
112                 }
113                 return packages;
114         }
115
116         private static boolean containsSourceCode(IPackageFragment packageFragment) throws JavaModelException {
117                 return packageFragment.getKind() == IPackageFragmentRoot.K_SOURCE && packageFragment.hasChildren();
118         }
119 }