KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > corext > util > SearchUtils


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 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.util;
12
13 import org.eclipse.jdt.core.ICompilationUnit;
14 import org.eclipse.jdt.core.IJavaElement;
15 import org.eclipse.jdt.core.JavaCore;
16 import org.eclipse.jdt.core.search.SearchEngine;
17 import org.eclipse.jdt.core.search.SearchMatch;
18 import org.eclipse.jdt.core.search.SearchParticipant;
19 import org.eclipse.jdt.core.search.SearchPattern;
20
21 public class SearchUtils {
22
23     /**
24      * @param match
25      * @return the enclosing {@link IJavaElement}, or null iff none
26      */

27     public static IJavaElement getEnclosingJavaElement(SearchMatch match) {
28         Object JavaDoc element = match.getElement();
29         if (element instanceof IJavaElement)
30             return (IJavaElement) element;
31         else
32             return null;
33     }
34     
35     /**
36      * @param match
37      * @return the enclosing {@link ICompilationUnit} of the given match, or null iff none
38      */

39     public static ICompilationUnit getCompilationUnit(SearchMatch match) {
40         IJavaElement enclosingElement = getEnclosingJavaElement(match);
41         if (enclosingElement != null){
42             if (enclosingElement instanceof ICompilationUnit)
43                 return (ICompilationUnit) enclosingElement;
44             ICompilationUnit cu= (ICompilationUnit) enclosingElement.getAncestor(IJavaElement.COMPILATION_UNIT);
45             if (cu != null)
46                 return cu;
47         }
48         
49         IJavaElement jElement= JavaCore.create(match.getResource());
50         if (jElement != null && jElement.exists() && jElement.getElementType() == IJavaElement.COMPILATION_UNIT)
51             return (ICompilationUnit) jElement;
52         return null;
53     }
54     
55     public static SearchParticipant[] getDefaultSearchParticipants() {
56         return new SearchParticipant[] { SearchEngine.getDefaultSearchParticipant() };
57     }
58     
59     /**
60      * Constant for use as matchRule in {@link SearchPattern#createPattern(IJavaElement, int, int)}
61      * to get search behavior as of 3.1M3 (all generic instantiations are found).
62      */

63     public final static int GENERICS_AGNOSTIC_MATCH_RULE= SearchPattern.R_EXACT_MATCH | SearchPattern.R_CASE_SENSITIVE | SearchPattern.R_ERASURE_MATCH;
64
65     /**
66      * Returns whether the given pattern is a camel case pattern or not.
67      *
68      * @param pattern the pattern to inspect
69      * @return whether it is a camel case pattern or not
70      */

71     public static boolean isCamelCasePattern(String JavaDoc pattern) {
72         return SearchPattern.validateMatchRule(
73             pattern,
74             SearchPattern.R_CAMELCASE_MATCH) == SearchPattern.R_CAMELCASE_MATCH;
75     }
76 }
Popular Tags