クラスファイルのパスからソースファイルのパスへの変換が正しく動作していなかったのを修正。 #3

Merged r-isitani merged 1 commit into nitta-lab:master from nitta-lab:findSourceFile on 21 Aug 2020
Showing 2 changed files
View
32
src/org/ntlab/traceDebugger/JavaElementFinder.java
package org.ntlab.traceDebugger;
 
import java.net.URI;
import java.util.ArrayList;
import java.util.List;
 
import org.eclipse.core.resources.IFile;
IWorkspace workspace = ResourcesPlugin.getWorkspace();
IProject[] projects = workspace.getRoot().getProjects();
String projectName = "";
String srcForderName = "/src";
String outputClassPath = null;
IPath projectOutputLocation = null;
boolean hasFoundSrcForderName = false;
for (IProject project : projects) {
projectName = project.getFullPath().toString();
if (!(tmp.contains(projectName + "/"))) continue;
IJavaProject javaProject = JavaCore.create(project);
try {
projectOutputLocation = javaProject.getOutputLocation(); // プロジェクト全体の出力フォルダ
for (IClasspathEntry entry : javaProject.getResolvedClasspath(true)) {
if (entry.getEntryKind() != IClasspathEntry.CPE_SOURCE) continue;
IPath srcForderPath = entry.getPath();
srcForderName = srcForderPath.toString();
srcForderName = srcForderPath.toString();
IPath outputLocation = entry.getOutputLocation(); // ソース毎の出力フォルダ
if (outputLocation != null) {
URI path = PathUtility.workspaceRelativePathToAbsoluteURI(outputLocation, workspace);
outputClassPath = PathUtility.URIPathToPath(path.getPath());
}
hasFoundSrcForderName = true;
break;
}
} catch (JavaModelException e) {
e.printStackTrace();
}
if (hasFoundSrcForderName) break;
}
if (outputClassPath == null && projectOutputLocation != null) {
URI path = PathUtility.workspaceRelativePathToAbsoluteURI(projectOutputLocation, workspace);
outputClassPath = PathUtility.URIPathToPath(path.getPath());
}
// tmp = tmp.replace(tmp.substring(0, tmp.lastIndexOf(projectName)), "");
tmp = tmp.replace(tmp.substring(0, tmp.indexOf(projectName)), "");
tmp = tmp.replace("/bin", srcForderName.substring(srcForderName.lastIndexOf("/")));
if (outputClassPath != null && tmp.startsWith(outputClassPath)) {
tmp = srcForderName + tmp.substring(outputClassPath.length());
} else {
tmp = tmp.replace(tmp.substring(0, tmp.indexOf(projectName)), "");
tmp = tmp.replace("/bin", srcForderName.substring(srcForderName.lastIndexOf("/")));
}
tmp = tmp.replace(".class", ".java");
String filePath = tmp;
IPath path = new Path(filePath);
return ResourcesPlugin.getWorkspace().getRoot().getFile(path);
View
32
src/org/ntlab/traceDebugger/PathUtility.java 0 → 100644
package org.ntlab.traceDebugger;
 
import java.net.URI;
 
import org.eclipse.core.internal.localstore.FileSystemResourceManager;
import org.eclipse.core.internal.resources.Workspace;
import org.eclipse.core.resources.IWorkspace;
import org.eclipse.core.runtime.IPath;
 
@SuppressWarnings("restriction")
public class PathUtility {
 
public static URI workspaceRelativePathToAbsoluteURI(IPath relativePath, IWorkspace iworkspace) {
if (iworkspace instanceof Workspace) {
Workspace workspace = (Workspace) iworkspace;
FileSystemResourceManager fsm = workspace.getFileSystemManager();
return fsm.locationURIFor(workspace.getRoot().getFolder(relativePath));
} else {
return null;
}
}
 
public static String URIPathToPath(String uriPath) {
if (uriPath.indexOf('/') >= 0) {
return uriPath.substring(uriPath.indexOf('/') + 1).split("!/")[0];
} else {
return uriPath.split("!/")[0];
}
}
 
}