Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,15 @@ public interface ISourceLookUpProvider extends IProvider {

String[] getFullyQualifiedName(String uri, int[] lines, int[] columns) throws DebugException;

/**
* Given a fully qualified class name and source file path, search the associated disk source file.
*
* @param fullyQualifiedName
* the fully qualified class name (e.g. com.microsoft.java.debug.core.adapter.ISourceLookUpProvider).
* @param sourcePath
* the qualified source file path (e.g. com\microsoft\java\debug\core\adapter\ISourceLookupProvider.java).
* @return the associated source file uri.
*/
String getSourceFileURI(String fullyQualifiedName, String sourcePath);

String getSourceContents(String uri);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

package com.microsoft.java.debug.core.adapter.handler;

import java.io.File;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.Arrays;
Expand Down Expand Up @@ -123,7 +124,7 @@ private Types.Source convertDebuggerSourceToClient(Location location, IDebugAdap
} catch (AbsentInformationException e) {
String enclosingType = AdapterUtils.parseEnclosingType(fullyQualifiedName);
sourceName = enclosingType.substring(enclosingType.lastIndexOf('.') + 1) + ".java";
relativeSourcePath = enclosingType.replace('.', '/') + ".java";
relativeSourcePath = enclosingType.replace('.', File.separatorChar) + ".java";
}

final String finalRelativeSourcePath = relativeSourcePath;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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>();

Expand All @@ -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
Expand Down Expand Up @@ -164,17 +160,32 @@ public String[] getFullyQualifiedName(String uri, int[] lines, int[] columns) th

@Override
public String getSourceFileURI(String fullyQualifiedName, String sourcePath) {
Copy link
Copy Markdown
Member

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?

Copy link
Copy Markdown
Contributor Author

@testforstephen testforstephen Dec 8, 2017

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).

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));
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please delete the obsolete method searchDeclarationFileByFqn and searchDeclarationFileByFqn. And even AdapterUtils.parseEnclosingType.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The 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
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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 =
Expand All @@ -314,4 +273,5 @@ private static String readFile(String filePath, Charset cs) {
}
return builder.toString();
}

}
Loading