1 12 package org.eclipse.jdt.internal.corext.callhierarchy; 13 14 import java.util.ArrayList ; 15 import java.util.Arrays ; 16 import java.util.Collection ; 17 import java.util.List ; 18 import java.util.StringTokenizer ; 19 20 import org.eclipse.core.runtime.NullProgressMonitor; 21 22 import org.eclipse.jface.preference.IPreferenceStore; 23 24 import org.eclipse.jdt.core.IJavaElement; 25 import org.eclipse.jdt.core.IMember; 26 import org.eclipse.jdt.core.IMethod; 27 import org.eclipse.jdt.core.ITypeRoot; 28 import org.eclipse.jdt.core.JavaModelException; 29 import org.eclipse.jdt.core.dom.AST; 30 import org.eclipse.jdt.core.dom.ASTParser; 31 import org.eclipse.jdt.core.dom.CompilationUnit; 32 import org.eclipse.jdt.core.search.IJavaSearchScope; 33 import org.eclipse.jdt.core.search.SearchEngine; 34 35 import org.eclipse.jdt.internal.ui.JavaPlugin; 36 import org.eclipse.jdt.internal.ui.util.StringMatcher; 37 38 public class CallHierarchy { 39 private static final String PREF_USE_IMPLEMENTORS= "PREF_USE_IMPLEMENTORS"; private static final String PREF_USE_FILTERS = "PREF_USE_FILTERS"; private static final String PREF_FILTERS_LIST = "PREF_FILTERS_LIST"; 43 private static final String DEFAULT_IGNORE_FILTERS = "java.*,javax.*"; private static CallHierarchy fgInstance; 45 private IJavaSearchScope fSearchScope; 46 private StringMatcher[] fFilters; 47 48 public static CallHierarchy getDefault() { 49 if (fgInstance == null) { 50 fgInstance = new CallHierarchy(); 51 } 52 53 return fgInstance; 54 } 55 56 public boolean isSearchUsingImplementorsEnabled() { 57 IPreferenceStore settings = JavaPlugin.getDefault().getPreferenceStore(); 58 59 return settings.getBoolean(PREF_USE_IMPLEMENTORS); 60 } 61 62 public void setSearchUsingImplementorsEnabled(boolean enabled) { 63 IPreferenceStore settings = JavaPlugin.getDefault().getPreferenceStore(); 64 65 settings.setValue(PREF_USE_IMPLEMENTORS, enabled); 66 } 67 68 public Collection getImplementingMethods(IMethod method) { 69 if (isSearchUsingImplementorsEnabled()) { 70 IJavaElement[] result = Implementors.getInstance().searchForImplementors(new IJavaElement[] { 71 method 72 }, new NullProgressMonitor()); 73 74 if ((result != null) && (result.length > 0)) { 75 return Arrays.asList(result); 76 } 77 } 78 79 return new ArrayList (0); 80 } 81 82 public Collection getInterfaceMethods(IMethod method) { 83 if (isSearchUsingImplementorsEnabled()) { 84 IJavaElement[] result = Implementors.getInstance().searchForInterfaces(new IJavaElement[] { 85 method 86 }, new NullProgressMonitor()); 87 88 if ((result != null) && (result.length > 0)) { 89 return Arrays.asList(result); 90 } 91 } 92 93 return new ArrayList (0); 94 } 95 96 public MethodWrapper getCallerRoot(IMethod method) { 97 return new CallerMethodWrapper(null, new MethodCall(method)); 98 } 99 100 public MethodWrapper getCalleeRoot(IMethod method) { 101 return new CalleeMethodWrapper(null, new MethodCall(method)); 102 } 103 104 public static CallLocation getCallLocation(Object element) { 105 CallLocation callLocation = null; 106 107 if (element instanceof MethodWrapper) { 108 MethodWrapper methodWrapper = (MethodWrapper) element; 109 MethodCall methodCall = methodWrapper.getMethodCall(); 110 111 if (methodCall != null) { 112 callLocation = methodCall.getFirstCallLocation(); 113 } 114 } else if (element instanceof CallLocation) { 115 callLocation = (CallLocation) element; 116 } 117 118 return callLocation; 119 } 120 121 public IJavaSearchScope getSearchScope() { 122 if (fSearchScope == null) { 123 fSearchScope= SearchEngine.createWorkspaceScope(); 124 } 125 126 return fSearchScope; 127 } 128 129 public void setSearchScope(IJavaSearchScope searchScope) { 130 this.fSearchScope = searchScope; 131 } 132 133 140 public boolean isIgnored(String fullyQualifiedName) { 141 if ((getIgnoreFilters() != null) && (getIgnoreFilters().length > 0)) { 142 for (int i = 0; i < getIgnoreFilters().length; i++) { 143 String fullyQualifiedName1 = fullyQualifiedName; 144 145 if (getIgnoreFilters()[i].match(fullyQualifiedName1)) { 146 return true; 147 } 148 } 149 } 150 151 return false; 152 } 153 154 public boolean isFilterEnabled() { 155 IPreferenceStore settings = JavaPlugin.getDefault().getPreferenceStore(); 156 return settings.getBoolean(PREF_USE_FILTERS); 157 } 158 159 public void setFilterEnabled(boolean filterEnabled) { 160 IPreferenceStore settings = JavaPlugin.getDefault().getPreferenceStore(); 161 settings.setValue(PREF_USE_FILTERS, filterEnabled); 162 } 163 164 168 public String getFilters() { 169 IPreferenceStore settings = JavaPlugin.getDefault().getPreferenceStore(); 170 171 return settings.getString(PREF_FILTERS_LIST); 172 } 173 174 public void setFilters(String filters) { 175 fFilters = null; 176 177 IPreferenceStore settings = JavaPlugin.getDefault().getPreferenceStore(); 178 settings.setValue(PREF_FILTERS_LIST, filters); 179 } 180 181 186 private StringMatcher[] getIgnoreFilters() { 187 if (fFilters == null) { 188 String filterString = null; 189 190 if (isFilterEnabled()) { 191 filterString = getFilters(); 192 193 if (filterString == null) { 194 filterString = DEFAULT_IGNORE_FILTERS; 195 } 196 } 197 198 if (filterString != null) { 199 fFilters = parseList(filterString); 200 } else { 201 fFilters = null; 202 } 203 } 204 205 return fFilters; 206 } 207 208 214 private static StringMatcher[] parseList(String listString) { 215 List list = new ArrayList (10); 216 StringTokenizer tokenizer = new StringTokenizer (listString, ","); 218 while (tokenizer.hasMoreTokens()) { 219 String textFilter = tokenizer.nextToken().trim(); 220 list.add(new StringMatcher(textFilter, false, false)); 221 } 222 223 return (StringMatcher[]) list.toArray(new StringMatcher[list.size()]); 224 } 225 226 static CompilationUnit getCompilationUnitNode(IMember member, boolean resolveBindings) { 227 ITypeRoot typeRoot= member.getTypeRoot(); 228 try { 229 if (typeRoot.exists() && typeRoot.getBuffer() != null) { 230 ASTParser parser= ASTParser.newParser(AST.JLS3); 231 parser.setSource(typeRoot); 232 parser.setResolveBindings(resolveBindings); 233 return (CompilationUnit) parser.createAST(null); 234 } 235 } catch (JavaModelException e) { 236 JavaPlugin.log(e); 237 } 238 return null; 239 } 240 } 241 | Popular Tags |