package org.eclipse.jdt.internal.core.builder;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.jdt.core.compiler.CharOperation;
import org.eclipse.jdt.internal.compiler.env.ICompilationUnit;
import org.eclipse.jdt.internal.compiler.env.IModule;
import org.eclipse.jdt.internal.compiler.problem.AbortCompilation;
import org.eclipse.jdt.internal.core.util.Util;
public class SourceFile implements ICompilationUnit {
public IFile resource;
ClasspathMultiDirectory sourceLocation;
String initialTypeName;
boolean updateClassFile;
public SourceFile(IFile resource, ClasspathMultiDirectory sourceLocation) {
this.resource = resource;
this.sourceLocation = sourceLocation;
this.initialTypeName = extractTypeName();
this.updateClassFile = false;
}
public SourceFile(IFile resource, ClasspathMultiDirectory sourceLocation, boolean updateClassFile) {
this(resource, sourceLocation);
this.updateClassFile = updateClassFile;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof SourceFile)) return false;
SourceFile f = (SourceFile) o;
return this.sourceLocation == f.sourceLocation && this.resource.getFullPath().equals(f.resource.getFullPath());
}
String () {
IPath fullPath = this.resource.getFullPath();
int resourceSegmentCount = fullPath.segmentCount();
int sourceFolderSegmentCount = this.sourceLocation.sourceFolder.getFullPath().segmentCount();
int charCount = (resourceSegmentCount - sourceFolderSegmentCount - 1);
resourceSegmentCount--;
for (int i = sourceFolderSegmentCount; i < resourceSegmentCount; i++)
charCount += fullPath.segment(i).length();
String lastSegment = fullPath.segment(resourceSegmentCount);
int extensionIndex = Util.indexOfJavaLikeExtension(lastSegment);
charCount += extensionIndex;
char[] result = new char[charCount];
int offset = 0;
for (int i = sourceFolderSegmentCount; i < resourceSegmentCount; i++) {
String segment = fullPath.segment(i);
int size = segment.length();
segment.getChars(0, size, result, offset);
offset += size;
result[offset++] = '/';
}
lastSegment.getChars(0, extensionIndex, result, offset);
return new String(result);
}
@Override
public char[] getContents() {
try {
return Util.getResourceContentsAsCharArray(this.resource);
} catch (CoreException e) {
throw new AbortCompilation(true, new MissingSourceFileException(this.resource.getFullPath().toString()));
}
}
@Override
public char[] getFileName() {
return this.resource.getFullPath().toString().toCharArray();
}
@Override
public char[] getMainTypeName() {
char[] typeName = this.initialTypeName.toCharArray();
int lastIndex = CharOperation.lastIndexOf('/', typeName);
return CharOperation.subarray(typeName, lastIndex + 1, -1);
}
@Override
public char[][] getPackageName() {
char[] typeName = this.initialTypeName.toCharArray();
int lastIndex = CharOperation.lastIndexOf('/', typeName);
return CharOperation.splitOn('/', typeName, 0, lastIndex);
}
@Override
public int hashCode() {
return this.initialTypeName.hashCode();
}
@Override
public boolean ignoreOptionalProblems() {
return this.sourceLocation.ignoreOptionalProblems;
}
String typeLocator() {
return this.resource.getProjectRelativePath().toString();
}
@Override
public String toString() {
return "SourceFile["
+ this.resource.getFullPath() + "]";
}
@Override
public char[] getModuleName() {
IModule mod = this.sourceLocation.module();
return mod == null ? null : mod.name();
}
}