1 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 NO_SOURCE_FILE_NAME = "NO SOURCE FILE NAME"; 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 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 obj) { 54 if (this.compoundName == null) return super.equals(obj); 55 if (!(obj instanceof PossibleMatch)) return false; 56 57 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 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 80 public char[] getFileName() { 81 return this.openable.getElementName().toCharArray(); 82 } 83 public char[] getMainTypeName() { 84 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 96 private char[] getQualifiedName() { 97 if (this.openable instanceof CompilationUnit) { 98 String fileName = this.openable.getElementName(); char[] mainTypeName = Util.getNameWithoutJavaLikeExtension(fileName).toCharArray(); 102 CompilationUnit cu = (CompilationUnit) this.openable; 103 return cu.getType(new String (mainTypeName)).getFullyQualifiedName().toCharArray(); 104 } else if (this.openable instanceof ClassFile) { 105 String fileName = getSourceFileName(); 106 if (fileName == NO_SOURCE_FILE_NAME) 107 return ((ClassFile) this.openable).getType().getFullyQualifiedName('.').toCharArray(); 108 109 int index = Util.indexOfJavaLikeExtension(fileName); 111 String 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 121 private String 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 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 toString() { 144 return this.openable == null ? "Fake PossibleMatch" : this.openable.toString(); } 146 } 147 | Popular Tags |