String dir = ""; //$NON-NLS-1$
if (javaElement == null) {
return dir;
}
IJavaProject project = javaElement.getJavaProject();
if (project == null) {
return dir;
}
// default bytecode location
IPath path = project.getOutputLocation();
IResource resource = javaElement.getUnderlyingResource();
if (resource == null) {
return dir;
}
// resolve multiple output locations here
if (project.exists() && project.getProject().isOpen()) {
IClasspathEntry entries[] = project.getRawClasspath();
for (int i = 0; i < entries.length; i++) {
IClasspathEntry classpathEntry = entries[i];
if (classpathEntry.getEntryKind() == IClasspathEntry.CPE_SOURCE) {
IPath outputPath = classpathEntry.getOutputLocation();
if (outputPath != null
&& classpathEntry.getPath().isPrefixOf(
resource.getFullPath())) {
path = outputPath;
break;
}
}
}
}
if (path == null) {
// check the default location if not already included
IPath def = project.getOutputLocation();
if (def != null && def.isPrefixOf(resource.getFullPath())){
path = def;
}
}
if(path == null){
return dir;
}
IWorkspace workspace = ResourcesPlugin.getWorkspace();
if (!project.getPath().equals(path)) {
IFolder outputFolder = workspace.getRoot().getFolder(path);
if (outputFolder != null) {
// linked resources will be resolved here!
IPath rawPath = outputFolder.getRawLocation();
if (rawPath != null) {
path = rawPath;
}
}
} else {
path = project.getProject().getLocation();
}
// here we should resolve path variables,
// probably existing at first place of path
IPathVariableManager pathManager = workspace.getPathVariableManager();