1 11 package org.eclipse.jdt.internal.core.builder; 12 13 import org.eclipse.core.resources.IFile; 14 import org.eclipse.core.runtime.IPath; 15 import org.eclipse.core.runtime.CoreException; 16 17 import org.eclipse.jdt.core.compiler.CharOperation; 18 import org.eclipse.jdt.internal.compiler.env.ICompilationUnit; 19 import org.eclipse.jdt.internal.compiler.problem.AbortCompilation; 20 import org.eclipse.jdt.internal.core.util.Util; 21 22 public class SourceFile implements ICompilationUnit { 23 24 public IFile resource; 25 ClasspathMultiDirectory sourceLocation; 26 String initialTypeName; 27 boolean updateClassFile; 28 29 public SourceFile(IFile resource, ClasspathMultiDirectory sourceLocation) { 30 this.resource = resource; 31 this.sourceLocation = sourceLocation; 32 this.initialTypeName = extractTypeName(); 33 this.updateClassFile = false; 34 } 35 36 public SourceFile(IFile resource, ClasspathMultiDirectory sourceLocation, boolean updateClassFile) { 37 this(resource, sourceLocation); 38 39 this.updateClassFile = updateClassFile; 40 } 41 42 public boolean equals(Object o) { 43 if (this == o) return true; 44 if (!(o instanceof SourceFile)) return false; 45 46 SourceFile f = (SourceFile) o; 47 return this.sourceLocation == f.sourceLocation && this.resource.getFullPath().equals(f.resource.getFullPath()); 48 } 49 50 String extractTypeName() { 51 IPath fullPath = this.resource.getFullPath(); 53 int resourceSegmentCount = fullPath.segmentCount(); 54 int sourceFolderSegmentCount = this.sourceLocation.sourceFolder.getFullPath().segmentCount(); 55 int charCount = (resourceSegmentCount - sourceFolderSegmentCount - 1); 56 resourceSegmentCount--; for (int i = sourceFolderSegmentCount; i < resourceSegmentCount; i++) 58 charCount += fullPath.segment(i).length(); 59 String lastSegment = fullPath.segment(resourceSegmentCount); 60 int extensionIndex = Util.indexOfJavaLikeExtension(lastSegment); 61 charCount += extensionIndex; 62 63 char[] result = new char[charCount]; 64 int offset = 0; 65 for (int i = sourceFolderSegmentCount; i < resourceSegmentCount; i++) { 66 String segment = fullPath.segment(i); 67 int size = segment.length(); 68 segment.getChars(0, size, result, offset); 69 offset += size; 70 result[offset++] = '/'; 71 } 72 lastSegment.getChars(0, extensionIndex, result, offset); 73 return new String (result); 74 } 75 76 public char[] getContents() { 77 78 try { 79 return Util.getResourceContentsAsCharArray(this.resource); 80 } catch (CoreException e) { 81 throw new AbortCompilation(true, new MissingSourceFileException(this.resource.getFullPath().toString())); 82 } 83 } 84 85 88 public char[] getFileName() { 89 return this.resource.getFullPath().toString().toCharArray(); } 91 92 public char[] getMainTypeName() { 93 char[] typeName = this.initialTypeName.toCharArray(); 94 int lastIndex = CharOperation.lastIndexOf('/', typeName); 95 return CharOperation.subarray(typeName, lastIndex + 1, -1); 96 } 97 98 public char[][] getPackageName() { 99 char[] typeName = this.initialTypeName.toCharArray(); 100 int lastIndex = CharOperation.lastIndexOf('/', typeName); 101 return CharOperation.splitOn('/', typeName, 0, lastIndex); 102 } 103 public int hashCode() { 104 return this.initialTypeName.hashCode(); 105 } 106 String typeLocator() { 107 return this.resource.getProjectRelativePath().toString(); 108 } 109 110 public String toString() { 111 return "SourceFile[" + this.resource.getFullPath() + "]"; } 114 } 115 | Popular Tags |