001: /*******************************************************************************
002: * Copyright (c) 2000, 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.jdt.ui.tests.search;
011:
012: import org.eclipse.search.ui.NewSearchUI;
013:
014: import org.eclipse.jdt.core.IJavaProject;
015: import org.eclipse.jdt.core.IMethod;
016: import org.eclipse.jdt.core.IType;
017: import org.eclipse.jdt.core.JavaModelException;
018: import org.eclipse.jdt.core.search.IJavaSearchConstants;
019:
020: import org.eclipse.jdt.ui.search.ElementQuerySpecification;
021: import org.eclipse.jdt.ui.search.PatternQuerySpecification;
022:
023: import org.eclipse.jdt.internal.ui.search.JavaSearchQuery;
024: import org.eclipse.jdt.internal.ui.search.JavaSearchResult;
025: import org.eclipse.jdt.internal.ui.search.JavaSearchScopeFactory;
026:
027: public class SearchTestHelper {
028: static int countMethodRefs(String TypeName, String methodName,
029: String[] parameterTypes) throws JavaModelException {
030: JavaSearchQuery query = runMethodRefQuery(TypeName, methodName,
031: parameterTypes);
032: JavaSearchResult result = (JavaSearchResult) query
033: .getSearchResult();
034: return result.getMatchCount();
035: }
036:
037: static int countMethodRefs(String methodName,
038: String[] parameterTypes) throws JavaModelException {
039: JavaSearchQuery query = runMethodRefQuery(methodName,
040: parameterTypes);
041: JavaSearchResult result = (JavaSearchResult) query
042: .getSearchResult();
043: return result.getMatchCount();
044: }
045:
046: static JavaSearchQuery runMethodRefQuery(String TypeName,
047: String methodName, String[] parameterTypes)
048: throws JavaModelException {
049: IMethod method = getMethod(TypeName, methodName, parameterTypes);
050: JavaSearchQuery query = new JavaSearchQuery(
051: new ElementQuerySpecification(method,
052: IJavaSearchConstants.REFERENCES,
053: JavaSearchScopeFactory.getInstance()
054: .createWorkspaceScope(true),
055: "workspace scope"));
056: NewSearchUI.runQueryInForeground(null, query);
057: return query;
058: }
059:
060: static JavaSearchQuery runTypeRefQuery(String typeName)
061: throws JavaModelException {
062: IType type = getType(typeName);
063: JavaSearchQuery query = new JavaSearchQuery(
064: new ElementQuerySpecification(type,
065: IJavaSearchConstants.REFERENCES,
066: JavaSearchScopeFactory.getInstance()
067: .createWorkspaceScope(true),
068: "workspace scope"));
069: NewSearchUI.runQueryInForeground(null, query);
070: return query;
071: }
072:
073: static JavaSearchQuery runMethodRefQuery(String methodName,
074: String[] parameterTypes) throws JavaModelException {
075: JavaSearchQuery query = new JavaSearchQuery(
076: new PatternQuerySpecification(methodName,
077: IJavaSearchConstants.METHOD, true,
078: IJavaSearchConstants.REFERENCES,
079: JavaSearchScopeFactory.getInstance()
080: .createWorkspaceScope(true),
081: "workspace scope"));
082: NewSearchUI.runQueryInForeground(null, query);
083: return query;
084: }
085:
086: static IMethod getMethod(String TypeName, String methodName,
087: String[] parameterTypes) throws JavaModelException {
088: IType type = getType(TypeName);
089: if (type == null)
090: return null;
091: IMethod method = type.getMethod(methodName, parameterTypes);
092: return method;
093: }
094:
095: static IType getType(String TypeName) throws JavaModelException {
096: IJavaProject p = JUnitSourceSetup.getProject();
097: IType type = p.findType(TypeName);
098: return type;
099: }
100: }
|