KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > core > search > matching > PossibleMatch


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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.core.search.matching;
12
13 import org.eclipse.core.resources.IResource;
14 import org.eclipse.jdt.core.*;
15 import org.eclipse.jdt.core.compiler.CharOperation;
16 import org.eclipse.jdt.core.search.SearchDocument;
17 import org.eclipse.jdt.internal.compiler.ast.CompilationUnitDeclaration;
18 import org.eclipse.jdt.internal.compiler.classfmt.ClassFileReader;
19 import org.eclipse.jdt.internal.compiler.env.ICompilationUnit;
20 import org.eclipse.jdt.internal.core.*;
21 import org.eclipse.jdt.internal.core.util.Util;
22
23 public class PossibleMatch implements ICompilationUnit {
24
25 public static final String JavaDoc NO_SOURCE_FILE_NAME = "NO SOURCE FILE NAME"; //$NON-NLS-1$
26

27 public IResource resource;
28 public Openable openable;
29 public MatchingNodeSet nodeSet;
30 public char[][] compoundName;
31 CompilationUnitDeclaration parsedUnit;
32 public SearchDocument document;
33 private String JavaDoc sourceFileName;
34 private char[] source;
35
36 public PossibleMatch(MatchLocator locator, IResource resource, Openable openable, SearchDocument document, boolean mustResolve) {
37     this.resource = resource;
38     this.openable = openable;
39     this.document = document;
40     this.nodeSet = new MatchingNodeSet(mustResolve);
41     char[] qualifiedName = getQualifiedName();
42     if (qualifiedName != null)
43         this.compoundName = CharOperation.splitOn('.', qualifiedName);
44 }
45 public void cleanUp() {
46     this.source = null;
47     if (this.parsedUnit != null) {
48         this.parsedUnit.cleanUp();
49         this.parsedUnit = null;
50     }
51     this.nodeSet = null;
52 }
53 public boolean equals(Object JavaDoc obj) {
54     if (this.compoundName == null) return super.equals(obj);
55     if (!(obj instanceof PossibleMatch)) return false;
56
57     // By using the compoundName of the source file, multiple .class files (A, A$M...) are considered equal
58
// Even .class files for secondary types and their nested types
59
return CharOperation.equals(this.compoundName, ((PossibleMatch) obj).compoundName);
60 }
61 public char[] getContents() {
62     if (this.source != null) return this.source;
63
64     if (this.openable instanceof ClassFile) {
65         String JavaDoc fileName = getSourceFileName();
66         if (fileName == NO_SOURCE_FILE_NAME) return CharOperation.NO_CHAR;
67
68         SourceMapper sourceMapper = this.openable.getSourceMapper();
69         IType type = ((ClassFile) this.openable).getType();
70         return this.source = sourceMapper.findSource(type, fileName);
71     }
72     return this.source = this.document.getCharContents();
73 }
74 /**
75  * The exact openable file name. In particular, will be the originating .class file for binary openable with attached
76  * source.
77  * @see org.eclipse.jdt.internal.compiler.env.IDependent#getFileName()
78  * @see PackageReferenceLocator#isDeclaringPackageFragment(IPackageFragment, org.eclipse.jdt.internal.compiler.lookup.ReferenceBinding)
79  */

80 public char[] getFileName() {
81     return this.openable.getElementName().toCharArray();
82 }
83 public char[] getMainTypeName() {
84     // The file is no longer opened to get its name => remove fix for bug 32182
85
return this.compoundName[this.compoundName.length-1];
86 }
87 public char[][] getPackageName() {
88     int length = this.compoundName.length;
89     if (length <= 1) return CharOperation.NO_CHAR_CHAR;
90     return CharOperation.subarray(this.compoundName, 0, length - 1);
91 }
92 /*
93  * Returns the fully qualified name of the main type of the compilation unit
94  * or the main type of the .java file that defined the class file.
95  */

96 private char[] getQualifiedName() {
97     if (this.openable instanceof CompilationUnit) {
98         // get file name
99
String JavaDoc fileName = this.openable.getElementName(); // working copy on a .class file may not have a resource, so use the element name
100
// get main type name
101
char[] mainTypeName = Util.getNameWithoutJavaLikeExtension(fileName).toCharArray();
102         CompilationUnit cu = (CompilationUnit) this.openable;
103         return cu.getType(new String JavaDoc(mainTypeName)).getFullyQualifiedName().toCharArray();
104     } else if (this.openable instanceof ClassFile) {
105         String JavaDoc fileName = getSourceFileName();
106         if (fileName == NO_SOURCE_FILE_NAME)
107             return ((ClassFile) this.openable).getType().getFullyQualifiedName('.').toCharArray();
108
109         // Class file may have a source file name with ".java" extension (see bug 73784)
110
int index = Util.indexOfJavaLikeExtension(fileName);
111         String JavaDoc simpleName = index==-1 ? fileName : fileName.substring(0, index);
112         PackageFragment pkg = (PackageFragment) this.openable.getParent();
113         return Util.concatWith(pkg.names, simpleName, '.').toCharArray();
114     }
115     return null;
116 }
117 /*
118  * Returns the source file name of the class file.
119  * Returns NO_SOURCE_FILE_NAME if not found.
120  */

121 private String JavaDoc getSourceFileName() {
122     if (this.sourceFileName != null) return this.sourceFileName;
123
124     this.sourceFileName = NO_SOURCE_FILE_NAME;
125     if (this.openable.getSourceMapper() != null) {
126         BinaryType type = (BinaryType) ((ClassFile) this.openable).getType();
127         ClassFileReader reader = MatchLocator.classFileReader(type);
128         if (reader != null) {
129             String JavaDoc fileName = type.sourceFileName(reader);
130             this.sourceFileName = fileName == null ? NO_SOURCE_FILE_NAME : fileName;
131         }
132     }
133     return this.sourceFileName;
134 }
135 public int hashCode() {
136     if (this.compoundName == null) return super.hashCode();
137
138     int hashCode = 0;
139     for (int i = 0, length = this.compoundName.length; i < length; i++)
140         hashCode += CharOperation.hashCode(this.compoundName[i]);
141     return hashCode;
142 }
143 public String JavaDoc toString() {
144     return this.openable == null ? "Fake PossibleMatch" : this.openable.toString(); //$NON-NLS-1$
145
}
146 }
147
Popular Tags