-
Notifications
You must be signed in to change notification settings - Fork 189
Looking up the stack frame's associated source file from source containers directly to improve searching perf and bugs #127
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
52ff6fd
Source lookup from project classpath entries
testforstephen 79886dd
Loop source containers to find the associated source file for the sta…
testforstephen 4ffd1b8
Remove unused class
testforstephen 22f8881
Add a workaround to fix jdt sourcelookup bug for java 9
testforstephen 5df54b6
Merge branch 'master' of github.com:Microsoft/java-debug into jinbo_s…
testforstephen 23006da
Fix source lookup support for java 9
testforstephen 939ff22
fix review comments
testforstephen adb3895
Refine javadoc
testforstephen d086833
Use stream api to optimize code
testforstephen d5fc90a
Merge branch 'master' into jinbo_sourcelookup
testforstephen a2b7a25
Use distinct api to remove duplicated projects
testforstephen 6e81a6a
Merge branch 'jinbo_sourcelookup' of github.com:Microsoft/java-debug …
testforstephen File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -20,31 +20,22 @@ | |
| import java.nio.charset.Charset; | ||
| import java.nio.file.Files; | ||
| import java.nio.file.Paths; | ||
| import java.util.ArrayList; | ||
| import java.util.HashMap; | ||
| import java.util.Map; | ||
| import java.util.logging.Level; | ||
| import java.util.logging.Logger; | ||
|
|
||
| import org.eclipse.core.resources.IResource; | ||
| import org.eclipse.debug.core.sourcelookup.ISourceContainer; | ||
| import org.eclipse.jdt.core.IBuffer; | ||
| import org.eclipse.jdt.core.IClassFile; | ||
| import org.eclipse.jdt.core.IJavaElement; | ||
| import org.eclipse.jdt.core.IJavaProject; | ||
| import org.eclipse.jdt.core.IType; | ||
| import org.eclipse.jdt.core.ITypeRoot; | ||
| import org.eclipse.jdt.core.JavaCore; | ||
| import org.eclipse.jdt.core.JavaModelException; | ||
| import org.eclipse.jdt.core.dom.AST; | ||
| import org.eclipse.jdt.core.dom.ASTParser; | ||
| import org.eclipse.jdt.core.dom.CompilationUnit; | ||
| import org.eclipse.jdt.core.search.IJavaSearchConstants; | ||
| import org.eclipse.jdt.core.search.IJavaSearchScope; | ||
| import org.eclipse.jdt.core.search.SearchEngine; | ||
| import org.eclipse.jdt.core.search.SearchMatch; | ||
| import org.eclipse.jdt.core.search.SearchParticipant; | ||
| import org.eclipse.jdt.core.search.SearchPattern; | ||
| import org.eclipse.jdt.core.search.SearchRequestor; | ||
| import org.eclipse.jdt.internal.debug.core.breakpoints.ValidBreakpointLocationLocator; | ||
|
|
||
| import com.microsoft.java.debug.core.Configuration; | ||
|
|
@@ -58,6 +49,7 @@ public class JdtSourceLookUpProvider implements ISourceLookUpProvider { | |
| private static final Logger logger = Logger.getLogger(Configuration.LOGGER_NAME); | ||
| private static final String JDT_SCHEME = "jdt"; | ||
| private static final String PATH_SEPARATOR = "/"; | ||
| private ISourceContainer[] sourceContainers = null; | ||
|
|
||
| private HashMap<String, Object> options = new HashMap<String, Object>(); | ||
|
|
||
|
|
@@ -67,6 +59,10 @@ public void initialize(IDebugSession debugSession, Map<String, Object> props) { | |
| throw new IllegalArgumentException("argument is null"); | ||
| } | ||
| options.putAll(props); | ||
| // During initialization, trigger a background job to load the source containers to improve the perf. | ||
| new Thread(() -> { | ||
| getSourceContainers(); | ||
| }).start(); | ||
| } | ||
|
|
||
| @Override | ||
|
|
@@ -164,17 +160,32 @@ public String[] getFullyQualifiedName(String uri, int[] lines, int[] columns) th | |
|
|
||
| @Override | ||
| public String getSourceFileURI(String fullyQualifiedName, String sourcePath) { | ||
| if (fullyQualifiedName == null) { | ||
| throw new IllegalArgumentException("fullyQualifiedName is null"); | ||
| if (sourcePath == null) { | ||
| return null; | ||
| } | ||
| // Jdt Search Engine doesn't support searching anonymous class or local type directly. | ||
| // But because the inner class and anonymous class have the same java file as the enclosing type, | ||
| // search their enclosing type instead. | ||
| if (fullyQualifiedName.indexOf("$") >= 0) { | ||
| return searchDeclarationFileByFqn(AdapterUtils.parseEnclosingType(fullyQualifiedName)); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please delete the obsolete method searchDeclarationFileByFqn and searchDeclarationFileByFqn. And even AdapterUtils.parseEnclosingType.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nice. |
||
| } else { | ||
| return searchDeclarationFileByFqn(fullyQualifiedName); | ||
|
|
||
| Object sourceElement = JdtUtils.findSourceElement(sourcePath, getSourceContainers()); | ||
| if (sourceElement instanceof IResource) { | ||
| return getFileURI((IResource) sourceElement); | ||
| } else if (sourceElement instanceof IClassFile) { | ||
| try { | ||
| IClassFile file = (IClassFile) sourceElement; | ||
| if (file.getBuffer() != null) { | ||
| return getFileURI(file); | ||
| } | ||
| } catch (JavaModelException e) { | ||
| // do nothing. | ||
| } | ||
| } | ||
| return null; | ||
| } | ||
|
|
||
| private synchronized ISourceContainer[] getSourceContainers() { | ||
| if (sourceContainers == null) { | ||
| sourceContainers = JdtUtils.getSourceContainers((String) options.get(Constants.PROJECTNAME)); | ||
| } | ||
|
|
||
| return sourceContainers; | ||
| } | ||
|
|
||
| @Override | ||
|
|
@@ -204,50 +215,6 @@ private String getContents(IClassFile cf) { | |
| return source; | ||
| } | ||
|
|
||
| private String searchDeclarationFileByFqn(String fullyQualifiedName) { | ||
| String projectName = (String) options.get(Constants.PROJECTNAME); | ||
| IJavaProject project = JdtUtils.getJavaProject(projectName); | ||
| IJavaSearchScope searchScope = createSearchScope(project); | ||
| SearchPattern pattern = SearchPattern.createPattern( | ||
| fullyQualifiedName, | ||
| IJavaSearchConstants.TYPE, | ||
| IJavaSearchConstants.DECLARATIONS, | ||
| SearchPattern.R_EXACT_MATCH); | ||
|
|
||
| ArrayList<String> uris = new ArrayList<String>(); | ||
|
|
||
| SearchRequestor requestor = new SearchRequestor() { | ||
| @Override | ||
| public void acceptSearchMatch(SearchMatch match) { | ||
| Object element = match.getElement(); | ||
| if (element instanceof IType) { | ||
| IType type = (IType) element; | ||
| if (type.isBinary()) { | ||
| try { | ||
| // let the search engine to ignore those class files without attached source. | ||
| if (type.getSource() != null) { | ||
| uris.add(getFileURI(type.getClassFile())); | ||
| } | ||
| } catch (JavaModelException e) { | ||
| // ignore | ||
| } | ||
| } else { | ||
| uris.add(getFileURI(type.getResource())); | ||
| } | ||
| } | ||
| } | ||
| }; | ||
| SearchEngine searchEngine = new SearchEngine(); | ||
| try { | ||
| searchEngine.search(pattern, new SearchParticipant[] { | ||
| SearchEngine.getDefaultSearchParticipant() | ||
| }, searchScope, requestor, null /* progress monitor */); | ||
| } catch (Exception e) { | ||
| logger.log(Level.SEVERE, String.format("Search engine failed: %s", e.toString()), e); | ||
| } | ||
| return uris.size() == 0 ? null : uris.get(0); | ||
| } | ||
|
|
||
| private static String getFileURI(IClassFile classFile) { | ||
| String packageName = classFile.getParent().getElementName(); | ||
| String jarName = classFile.getParent().getParent().getElementName(); | ||
|
|
@@ -288,14 +255,6 @@ private static IClassFile resolveClassFile(String uriString) { | |
| return null; | ||
| } | ||
|
|
||
| private static IJavaSearchScope createSearchScope(IJavaProject project) { | ||
| if (project == null) { | ||
| return SearchEngine.createWorkspaceScope(); | ||
| } | ||
| return SearchEngine.createJavaSearchScope(new IJavaProject[] {project}, | ||
| IJavaSearchScope.SOURCES | IJavaSearchScope.APPLICATION_LIBRARIES | IJavaSearchScope.SYSTEM_LIBRARIES); | ||
| } | ||
|
|
||
| private static String readFile(String filePath, Charset cs) { | ||
| StringBuilder builder = new StringBuilder(); | ||
| try (BufferedReader bufferReader = | ||
|
|
@@ -314,4 +273,5 @@ private static String readFile(String filePath, Charset cs) { | |
| } | ||
| return builder.toString(); | ||
| } | ||
|
|
||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seems like the fullyQualifiedName is never used. Shall we just delete it?
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Different provider implementation may have different source lookup mechanism. I prefer to keep current interface design.
If there is user compliant, i'd like to change the interface to
public String getSourceFileURI(StackFrame stackFrame).From the stack frame, the provider could get more info for accurate searching, such as class name, source name, source path, even source info for the specified stratum (like scala).