01: /*******************************************************************************
02: * Copyright (c) 2000, 2007 IBM Corporation and others.
03: * All rights reserved. This program and the accompanying materials
04: * are made available under the terms of the Eclipse Public License v1.0
05: * which accompanies this distribution, and is available at
06: * http://www.eclipse.org/legal/epl-v10.html
07: *
08: * Contributors:
09: * IBM Corporation - initial API and implementation
10: *******************************************************************************/package org.eclipse.jdt.internal.core.search.indexing;
11:
12: import org.eclipse.core.resources.IProject;
13: import org.eclipse.core.resources.ResourcesPlugin;
14: import org.eclipse.core.runtime.IPath;
15: import org.eclipse.core.runtime.Path;
16: import org.eclipse.jdt.core.JavaCore;
17: import org.eclipse.jdt.core.search.SearchDocument;
18: import org.eclipse.jdt.internal.compiler.SourceElementParser;
19: import org.eclipse.jdt.internal.compiler.util.SuffixConstants;
20: import org.eclipse.jdt.internal.core.JavaModelManager;
21: import org.eclipse.jdt.internal.core.jdom.CompilationUnit;
22: import org.eclipse.jdt.internal.core.search.processing.JobManager;
23:
24: /**
25: * A SourceIndexer indexes java files using a java parser. The following items are indexed:
26: * Declarations of:
27: * - Classes<br>
28: * - Interfaces; <br>
29: * - Methods;<br>
30: * - Fields;<br>
31: * References to:
32: * - Methods (with number of arguments); <br>
33: * - Fields;<br>
34: * - Types;<br>
35: * - Constructors.
36: */
37: public class SourceIndexer extends AbstractIndexer implements
38: SuffixConstants {
39:
40: public SourceIndexer(SearchDocument document) {
41: super (document);
42: }
43:
44: public void indexDocument() {
45: // Create a new Parser
46: SourceIndexerRequestor requestor = new SourceIndexerRequestor(
47: this );
48: String documentPath = this .document.getPath();
49: SourceElementParser parser = ((InternalSearchDocument) this .document).parser;
50: if (parser == null) {
51: IPath path = new Path(documentPath);
52: IProject project = ResourcesPlugin.getWorkspace().getRoot()
53: .getProject(path.segment(0));
54: parser = JavaModelManager.getJavaModelManager().indexManager
55: .getSourceElementParser(JavaCore.create(project),
56: requestor);
57: } else {
58: parser.requestor = requestor;
59: }
60:
61: // Launch the parser
62: char[] source = null;
63: char[] name = null;
64: try {
65: source = document.getCharContents();
66: name = documentPath.toCharArray();
67: } catch (Exception e) {
68: // ignore
69: }
70: if (source == null || name == null)
71: return; // could not retrieve document info (e.g. resource was discarded)
72: CompilationUnit compilationUnit = new CompilationUnit(source,
73: name);
74: try {
75: parser
76: .parseCompilationUnit(compilationUnit, true/*full parse*/);
77: } catch (Exception e) {
78: if (JobManager.VERBOSE) {
79: e.printStackTrace();
80: }
81: }
82: }
83: }
|