1 package no.uio.ifi.refaktor.utils;
3 import java.util.Arrays;
4 import java.util.LinkedList;
6 import java.util.regex.Matcher;
7 import java.util.regex.Pattern;
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;
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;
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 + ")*";
36 public static ICompilationUnit getCompilationUnitFromEditor(IEditorPart editor) {
37 return findCompilationUnitFromFile(DocumentUtils.getFileFromEditor(editor));
40 private static ICompilationUnit findCompilationUnitFromFile(IFile file) {
41 ICompilationUnit cu = JavaCore.createCompilationUnitFrom(file);
42 return cu != null ? cu : new NullCompilationUnit();
45 public static ICompilationUnit findCompilationUnitFromProjectPackageAndTypeName(IProject project, String packageName, String simpleTypeName) {
47 return findType(project, packageName, simpleTypeName).getCompilationUnit();
48 } catch (JavaModelException e) {
49 throw new RuntimeException(e);
53 private static IType findType(IProject project, String packageName, String typeName) throws JavaModelException {
54 return findType(project, combine(packageName, typeName));
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);
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());
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);
75 if (matcher.matches()) {
76 IType parentType = findType(javaProject, matcher.group(1));
77 if (parentType == null)
79 return searchForAnonymousOrLocalType(parentType, fullyQualifiedTypeName);
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;
88 for (IJavaElement child : parent.getChildren()) {
89 if (child instanceof IMember) {
90 IType anonymousType = searchForAnonymousOrLocalType((IMember) child, fullyQualifiedTypeName);
91 if (anonymousType != null)
99 private static String combine(String packageName, String typeName) {
100 return packageName + "." + typeName;
103 public static IMethod findMethodHandleChecked(IProject project, String packageName, String typeName,
104 String methodName, String... methodSignature) {
106 IMethod method = findMethodHandle(project, packageName, typeName, methodName, methodSignature);
107 assert method.exists() : "method.exists()";
109 } catch (JavaModelException e) {
110 throw new RefaktorException("The requested method handle could not be found");
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);
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());
129 public static IJavaProject createJavaProjectFrom(IProject project) {
130 assert project.exists();
131 assert project.isOpen();
133 assert project.hasNature(JavaCore.NATURE_ID);
134 } catch (CoreException e) {
135 // Project does not exist or is not open
137 return JavaCore.create(project);
140 public static IPackageFragment findPackageChecked(String packageName, IProject project) {
142 return findPackage(packageName, project);
143 } catch (JavaModelException e) {
144 RefaktorDebug.log(e);
145 return new NullPackageFragmentHandle();
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;
155 return new NullPackageFragmentHandle();
158 public static List<IPackageFragment> getPackagesFromProject(IProject project) throws JavaModelException {
159 IPackageFragment[] packageFragments = createJavaProjectFrom(project).getPackageFragments();
160 return findPackagesAmongPackageFragments(Arrays.asList(packageFragments));
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);
172 private static boolean containsSourceCode(IPackageFragment packageFragment) throws JavaModelException {
173 return packageFragment.getKind() == IPackageFragmentRoot.K_SOURCE && packageFragment.hasChildren();