]> git.uio.no Git - ifi-stolz-refaktor.git/blob - software/no.uio.ifi.refaktor/src/no/uio/ifi/refaktor/utils/RefaktorHandleUtils.java
Software: moving text selections to new package
[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 java.util.Arrays;
4 import java.util.LinkedList;
5 import java.util.List;
6 import java.util.regex.Matcher;
7 import java.util.regex.Pattern;
8
9 import no.uio.ifi.refaktor.exceptions.RefaktorException;
10 import no.uio.ifi.refaktor.utils.nullobjects.NullCompilationUnit;
11 import no.uio.ifi.refaktor.utils.nullobjects.NullPackageFragmentHandle;
12
13 import org.eclipse.core.resources.IFile;
14 import org.eclipse.core.resources.IProject;
15 import org.eclipse.core.runtime.CoreException;
16 import org.eclipse.core.runtime.NullProgressMonitor;
17 import org.eclipse.jdt.core.ICompilationUnit;
18 import org.eclipse.jdt.core.IJavaElement;
19 import org.eclipse.jdt.core.IJavaProject;
20 import org.eclipse.jdt.core.IMember;
21 import org.eclipse.jdt.core.IMethod;
22 import org.eclipse.jdt.core.IPackageFragment;
23 import org.eclipse.jdt.core.IPackageFragmentRoot;
24 import org.eclipse.jdt.core.IType;
25 import org.eclipse.jdt.core.JavaCore;
26 import org.eclipse.jdt.core.JavaModelException;
27 import org.eclipse.ui.IEditorPart;
28
29 // TODO: clean up
30 public class RefaktorHandleUtils {
31         private static final String SIMPLE_NAME_REGEX = "[a-zA-Z_$][\\w$]*";
32         private static final String SIMPLE_NAME_REGEX_RELUCTANT = SIMPLE_NAME_REGEX + "?";
33         private static final String QUALIFIED_NAME_REGEX = 
34                         SIMPLE_NAME_REGEX_RELUCTANT + "(?:\\." + SIMPLE_NAME_REGEX_RELUCTANT + ")*";
35
36         public static ICompilationUnit getCompilationUnitFromEditor(IEditorPart editor) {
37                 return findCompilationUnitFromFile(DocumentUtils.getFileFromEditor(editor));
38         }
39
40         private static ICompilationUnit findCompilationUnitFromFile(IFile file) {
41                 ICompilationUnit cu = JavaCore.createCompilationUnitFrom(file);
42                 return cu != null ? cu : new NullCompilationUnit();
43         }
44
45         public static ICompilationUnit findCompilationUnitFromProjectPackageAndTypeName(IProject project, String packageName, String simpleTypeName) {
46                 try {
47                         return findType(project, packageName, simpleTypeName).getCompilationUnit();
48                 } catch (JavaModelException e) {
49                         throw new RuntimeException(e);
50                 }
51         }
52
53         private static IType findType(IProject project, String packageName, String typeName) throws JavaModelException {
54                 return findType(project, combine(packageName, typeName));
55         }
56
57         private static IType findType(IProject project, String fullyQualifiedTypeName) throws JavaModelException {
58                 IJavaProject javaProject = createJavaProjectFrom(project);
59                 assert javaProject != null: project;
60                 return findType(javaProject, fullyQualifiedTypeName);
61         }
62
63         private static IType findType(IJavaProject javaProject, String fullyQualifiedTypeName) throws JavaModelException {
64                 IType type = tryToFindAnonymousOrLocalType(javaProject, fullyQualifiedTypeName);
65                 if (type == null || !type.exists())
66                         type = javaProject.findType(fullyQualifiedTypeName, new NullProgressMonitor());
67                 assert type != null;
68                 return type;
69         }
70
71         private static IType tryToFindAnonymousOrLocalType(IJavaProject javaProject, String fullyQualifiedTypeName) throws JavaModelException {
72                 Pattern pattern = Pattern.compile("^(" + QUALIFIED_NAME_REGEX + ")\\$(?:\\d+|" + SIMPLE_NAME_REGEX + ")$");
73                 Matcher matcher = pattern.matcher(fullyQualifiedTypeName);
74
75                 if (matcher.matches()) {
76                         IType parentType = findType(javaProject, matcher.group(1));
77                         if (parentType == null)
78                                 return null;
79                         return searchForAnonymousOrLocalType(parentType, fullyQualifiedTypeName);
80                 }
81                 return null;
82         }
83
84         private static IType searchForAnonymousOrLocalType(IMember parent, String fullyQualifiedTypeName) throws JavaModelException {
85                 if (parent instanceof IType && ((IType) parent).getFullyQualifiedName().equals(fullyQualifiedTypeName))
86                         return (IType) parent;
87
88                 for (IJavaElement child : parent.getChildren()) {
89                         if (child instanceof IMember) {
90                                 IType anonymousType = searchForAnonymousOrLocalType((IMember) child, fullyQualifiedTypeName);
91                                 if (anonymousType != null)
92                                         return anonymousType;
93                         }
94                 }
95
96                 return null;
97         }
98
99         private static String combine(String packageName, String typeName) {
100                 return packageName + "." + typeName;
101         }
102
103         public static IMethod findMethodHandleChecked(IProject project, String packageName, String typeName, 
104                         String methodName, String... methodSignature) {
105                 try {
106                         IMethod method = findMethodHandle(project, packageName, typeName, methodName, methodSignature);
107                         assert method.exists() : "method.exists()";
108                         return method;
109                 } catch (JavaModelException e) {
110                         throw new RefaktorException("The requested method handle could not be found");
111                 }
112         }
113
114         public static IMethod findMethodHandle(IProject project, String packageName, String simpleTypeName, 
115                         String methodName, String[] methodSignature) throws JavaModelException {
116                 IType type = findType(project, packageName, simpleTypeName);
117                 assert type.exists() : "type.exists()";
118                 return type.getMethod(methodName, methodSignature);
119         }
120
121         public static IMethod findMethodHandle(IProject project, String fullyQualifiedTypeName, String simpleMethodSignature) throws JavaModelException {
122                 assert !"".equals(fullyQualifiedTypeName);
123                 IType type = findType(project, fullyQualifiedTypeName);
124                 assert type != null: "Project: " + project + ", Type name: " + fullyQualifiedTypeName;
125                 MethodSignature methodSignature = MethodSignature.parseSignature(simpleMethodSignature);
126                 return type.getMethod(methodSignature.getName(), methodSignature.getParameterSignaturesArray());
127         }
128
129         public static IJavaProject createJavaProjectFrom(IProject project) {
130                 assert project.exists();
131                 assert project.isOpen();
132                 try {
133                         assert project.hasNature(JavaCore.NATURE_ID);
134                 } catch (CoreException e) {
135                         // Project does not exist or is not open
136                 }
137                 return JavaCore.create(project);
138         }
139
140         public static IPackageFragment findPackageChecked(String packageName, IProject project) {
141                 try {
142                         return findPackage(packageName, project);
143                 } catch (JavaModelException e) {
144                         RefaktorDebug.log(e);
145                         return new NullPackageFragmentHandle();
146                 }
147         }
148
149         private static IPackageFragment findPackage(String packageName, IProject project) throws JavaModelException {
150                 IJavaProject javaProject = createJavaProjectFrom(project);
151                 for (IPackageFragment packageFragment: javaProject.getPackageFragments()) {
152                         if (packageFragment.getElementName().equals(packageName))
153                                 return packageFragment;
154                 }
155                 return new NullPackageFragmentHandle();
156         }
157
158         public static List<IPackageFragment> getPackagesFromProject(IProject project) throws JavaModelException {
159                 IPackageFragment[] packageFragments = createJavaProjectFrom(project).getPackageFragments();
160                 return findPackagesAmongPackageFragments(Arrays.asList(packageFragments));
161         }
162
163         public static List<IPackageFragment> findPackagesAmongPackageFragments(List<IPackageFragment> packageFragments) throws JavaModelException {
164                 List<IPackageFragment> packages = new LinkedList<IPackageFragment>();
165                 for (IPackageFragment packageFragment: packageFragments) {
166                         if (containsSourceCode(packageFragment))
167                                 packages.add(packageFragment);
168                 }
169                 return packages;
170         }
171
172         private static boolean containsSourceCode(IPackageFragment packageFragment) throws JavaModelException {
173                 return packageFragment.getKind() == IPackageFragmentRoot.K_SOURCE && packageFragment.hasChildren();
174         }
175 }