use of org.eclipse.jdt.core.IClassFile in project eclipse.jdt.ls by eclipse.
the class ReferencesHandler method findReferences.
public List<Location> findReferences(ReferenceParams param, IProgressMonitor monitor) {
final List<Location> locations = new ArrayList<>();
try {
IJavaElement elementToSearch = JDTUtils.findElementAtSelection(JDTUtils.resolveTypeRoot(param.getTextDocument().getUri()), param.getPosition().getLine(), param.getPosition().getCharacter(), this.preferenceManager, monitor);
if (elementToSearch == null) {
return locations;
}
SearchEngine engine = new SearchEngine();
SearchPattern pattern = SearchPattern.createPattern(elementToSearch, IJavaSearchConstants.REFERENCES);
engine.search(pattern, new SearchParticipant[] { SearchEngine.getDefaultSearchParticipant() }, createSearchScope(), new SearchRequestor() {
@Override
public void acceptSearchMatch(SearchMatch match) throws CoreException {
Object o = match.getElement();
if (o instanceof IJavaElement) {
IJavaElement element = (IJavaElement) o;
ICompilationUnit compilationUnit = (ICompilationUnit) element.getAncestor(IJavaElement.COMPILATION_UNIT);
Location location = null;
if (compilationUnit != null) {
location = JDTUtils.toLocation(compilationUnit, match.getOffset(), match.getLength());
} else {
IClassFile cf = (IClassFile) element.getAncestor(IJavaElement.CLASS_FILE);
if (cf != null && cf.getSourceRange() != null) {
location = JDTUtils.toLocation(cf, match.getOffset(), match.getLength());
}
}
if (location != null) {
locations.add(location);
}
}
}
}, monitor);
} catch (CoreException e) {
JavaLanguageServerPlugin.logException("Find references failure ", e);
}
return locations;
}
use of org.eclipse.jdt.core.IClassFile in project eclipse.jdt.ls by eclipse.
the class ContentProviderManager method getContent.
private String getContent(Object source, String cacheKey, Class<? extends IContentProvider> providerType, IProgressMonitor monitor) {
URI uri = source instanceof URI ? (URI) source : null;
List<ContentProviderDescriptor> matches = findMatchingProviders(uri);
if (monitor.isCanceled()) {
return EMPTY_CONTENT;
}
int previousPriority = -1;
for (ContentProviderDescriptor match : matches) {
IContentProvider contentProvider = match.getContentProvider();
if (!providerType.isInstance(contentProvider)) {
JavaLanguageServerPlugin.logError("Unable to load " + providerType.getSimpleName() + " class for " + match.id);
continue;
}
if (monitor.isCanceled()) {
return EMPTY_CONTENT;
}
if (previousPriority == match.priority) {
requestPreferredProvider(match.priority, matches);
}
try {
contentProvider.setPreferences(preferenceManager.getPreferences());
String content = null;
if (uri != null) {
content = contentProvider.getContent(uri, monitor);
} else if (source instanceof IClassFile) {
content = ((IDecompiler) contentProvider).getSource((IClassFile) source, monitor);
}
if (monitor.isCanceled()) {
return EMPTY_CONTENT;
} else if (content != null) {
return content;
}
} catch (Exception e) {
JavaLanguageServerPlugin.logException("Error getting content via " + match.id, e);
}
previousPriority = match.priority;
}
return EMPTY_CONTENT;
}
use of org.eclipse.jdt.core.IClassFile in project eclipse.jdt.ls by eclipse.
the class JDTUtils method toLocation.
/**
* Creates a location for a given java element.
* Element can be a {@link ICompilationUnit} or {@link IClassFile}
*
* @param element
* @return location or null
* @throws JavaModelException
*/
public static Location toLocation(IJavaElement element) throws JavaModelException {
ICompilationUnit unit = (ICompilationUnit) element.getAncestor(IJavaElement.COMPILATION_UNIT);
IClassFile cf = (IClassFile) element.getAncestor(IJavaElement.CLASS_FILE);
if (unit == null && cf == null) {
return null;
}
if (element instanceof ISourceReference) {
ISourceRange nameRange = getNameRange(element);
if (SourceRange.isAvailable(nameRange)) {
if (cf == null) {
return toLocation(unit, nameRange.getOffset(), nameRange.getLength());
} else {
return toLocation(cf, nameRange.getOffset(), nameRange.getLength());
}
}
}
return null;
}
use of org.eclipse.jdt.core.IClassFile in project eclipse.jdt.ls by eclipse.
the class JDTUtils method resolveClassFile.
/**
* Given the uri returns a {@link IClassFile}.
* May return null if it can not resolve the uri to a
* library.
*
* @see #toLocation(IClassFile, int, int)
* @param uri with 'jdt' scheme
* @return class file
*/
public static IClassFile resolveClassFile(URI uri) {
if (uri != null && JDT_SCHEME.equals(uri.getScheme()) && "contents".equals(uri.getAuthority())) {
String handleId = uri.getQuery();
IJavaElement element = JavaCore.create(handleId);
IClassFile cf = (IClassFile) element.getAncestor(IJavaElement.CLASS_FILE);
return cf;
}
return null;
}
use of org.eclipse.jdt.core.IClassFile in project flux by eclipse.
the class Repository method getClasspathResource.
public void getClasspathResource(JSONObject request) {
try {
final int callbackID = request.getInt("callback_id");
final String sender = request.getString("requestSenderID");
final String projectName = request.getString("project");
final String resourcePath = request.getString("resource");
final String username = request.getString("username");
ConnectedProject connectedProject = this.syncedProjects.get(projectName);
if (this.username.equals(username) && connectedProject != null) {
String typeName = resourcePath.substring("classpath:/".length());
if (typeName.endsWith(".class")) {
typeName = typeName.substring(0, typeName.length() - ".class".length());
}
typeName = typeName.replace('/', '.');
IJavaProject javaProject = JavaCore.create(connectedProject.getProject());
if (javaProject != null) {
IType type = javaProject.findType(typeName);
IClassFile classFile = type.getClassFile();
if (classFile != null && classFile.getSourceRange() != null) {
JSONObject message = new JSONObject();
message.put("callback_id", callbackID);
message.put("requestSenderID", sender);
message.put("username", this.username);
message.put("project", projectName);
message.put("resource", resourcePath);
message.put("readonly", true);
String content = classFile.getSource();
message.put("content", content);
message.put("type", "file");
messagingConnector.send("getResourceResponse", message);
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
Aggregations