]>
Commit | Line | Data |
---|---|---|
1b2798f6 EK |
1 | /******************************************************************************* |
2 | * Copyright (c) 2000, 2011 IBM Corporation and others. | |
3 | * All rights reserved. This program and the accompanying materials | |
4 | * are made available under the terms of the Eclipse Public License v1.0 | |
5 | * which accompanies this distribution, and is available at | |
6 | * http://www.eclipse.org/legal/epl-v10.html | |
7 | * | |
8 | * Contributors: | |
9 | * IBM Corporation - initial API and implementation | |
10 | *******************************************************************************/ | |
11 | package org.eclipse.jdt.internal.corext.refactoring.reorg; | |
12 | ||
13 | import java.util.ArrayList; | |
14 | import java.util.Arrays; | |
15 | import java.util.Comparator; | |
16 | import java.util.HashSet; | |
17 | import java.util.List; | |
18 | import java.util.Set; | |
19 | ||
20 | import org.eclipse.core.runtime.Assert; | |
21 | ||
22 | import org.eclipse.core.resources.IFile; | |
23 | ||
24 | import org.eclipse.jdt.core.IClassFile; | |
25 | import org.eclipse.jdt.core.ICompilationUnit; | |
26 | import org.eclipse.jdt.core.IJavaElement; | |
27 | import org.eclipse.jdt.core.ISourceReference; | |
28 | import org.eclipse.jdt.core.JavaModelException; | |
29 | ||
30 | ||
31 | public class SourceReferenceUtil { | |
32 | ||
33 | //no instances | |
34 | private SourceReferenceUtil(){} | |
35 | ||
36 | public static IFile getFile(ISourceReference ref) { | |
37 | ICompilationUnit unit= getCompilationUnit(ref); | |
38 | return (IFile) unit.getPrimary().getResource(); | |
39 | } | |
40 | ||
41 | public static ICompilationUnit getCompilationUnit(ISourceReference o){ | |
42 | Assert.isTrue(! (o instanceof IClassFile)); | |
43 | ||
44 | if (o instanceof ICompilationUnit) | |
45 | return (ICompilationUnit)o; | |
46 | if (o instanceof IJavaElement) | |
47 | return (ICompilationUnit) ((IJavaElement)o).getAncestor(IJavaElement.COMPILATION_UNIT); | |
48 | return null; | |
49 | } | |
50 | ||
51 | private static boolean hasParentInSet(IJavaElement elem, Set<ISourceReference> set){ | |
52 | IJavaElement parent= elem.getParent(); | |
53 | while (parent != null) { | |
54 | if (set.contains(parent)) | |
55 | return true; | |
56 | parent= parent.getParent(); | |
57 | } | |
58 | return false; | |
59 | } | |
60 | ||
61 | public static ISourceReference[] removeAllWithParentsSelected(ISourceReference[] elems){ | |
62 | Set<ISourceReference> set= new HashSet<ISourceReference>(Arrays.asList(elems)); | |
63 | List<ISourceReference> result= new ArrayList<ISourceReference>(elems.length); | |
64 | for (int i= 0; i < elems.length; i++) { | |
65 | ISourceReference elem= elems[i]; | |
66 | if (! (elem instanceof IJavaElement)) | |
67 | result.add(elem); | |
68 | else{ | |
69 | if (! hasParentInSet(((IJavaElement)elem), set)) | |
70 | result.add(elem); | |
71 | } | |
72 | } | |
73 | return result.toArray(new ISourceReference[result.size()]); | |
74 | } | |
75 | ||
76 | public static ISourceReference[] sortByOffset(ISourceReference[] methods){ | |
77 | Arrays.sort(methods, new Comparator<ISourceReference>(){ | |
78 | public int compare(ISourceReference o1, ISourceReference o2){ | |
79 | try{ | |
80 | return o2.getSourceRange().getOffset() - o1.getSourceRange().getOffset(); | |
81 | } catch (JavaModelException e){ | |
82 | return o2.hashCode() - o1.hashCode(); | |
83 | } | |
84 | } | |
85 | }); | |
86 | return methods; | |
87 | } | |
88 | } | |
89 |