use of org.eclipse.jdt.core.IClassFile in project eclipse.jdt.ls by eclipse.
the class JDTUtils method findElementsAtSelection.
public static IJavaElement[] findElementsAtSelection(ITypeRoot unit, int line, int column, PreferenceManager preferenceManager, IProgressMonitor monitor) throws JavaModelException {
if (unit == null) {
return null;
}
int offset = JsonRpcHelpers.toOffset(unit.getBuffer(), line, column);
if (offset > -1) {
return unit.codeSelect(offset, 0);
}
if (unit instanceof IClassFile) {
IClassFile classFile = (IClassFile) unit;
ContentProviderManager contentProvider = JavaLanguageServerPlugin.getContentProviderManager();
String contents = contentProvider.getSource(classFile, monitor);
if (contents != null) {
IDocument document = new Document(contents);
try {
offset = document.getLineOffset(line) + column;
if (offset > -1) {
String name = parse(contents, offset);
if (name == null) {
return null;
}
SearchPattern pattern = SearchPattern.createPattern(name, IJavaSearchConstants.TYPE, IJavaSearchConstants.DECLARATIONS, SearchPattern.R_FULL_MATCH);
IJavaSearchScope scope = createSearchScope(unit.getJavaProject());
List<IJavaElement> elements = new ArrayList<>();
SearchRequestor requestor = new SearchRequestor() {
@Override
public void acceptSearchMatch(SearchMatch match) {
if (match.getElement() instanceof IJavaElement) {
elements.add((IJavaElement) match.getElement());
}
}
};
SearchEngine searchEngine = new SearchEngine();
searchEngine.search(pattern, new SearchParticipant[] { SearchEngine.getDefaultSearchParticipant() }, scope, requestor, null);
return elements.toArray(new IJavaElement[0]);
}
} catch (BadLocationException | CoreException e) {
JavaLanguageServerPlugin.logException(e.getMessage(), e);
}
}
}
return null;
}
use of org.eclipse.jdt.core.IClassFile in project eclipse.jdt.ls by eclipse.
the class CodeLensHandler method getCodeLensSymbols.
public List<CodeLens> getCodeLensSymbols(String uri, IProgressMonitor monitor) {
if (!preferenceManager.getPreferences().isCodeLensEnabled()) {
return Collections.emptyList();
}
final ICompilationUnit unit = JDTUtils.resolveCompilationUnit(uri);
IClassFile classFile = null;
if (unit == null) {
classFile = JDTUtils.resolveClassFile(uri);
if (classFile == null) {
return Collections.emptyList();
}
} else {
if (!unit.getResource().exists() || monitor.isCanceled()) {
return Collections.emptyList();
}
}
try {
ITypeRoot typeRoot = unit != null ? unit : classFile;
IJavaElement[] elements = typeRoot.getChildren();
ArrayList<CodeLens> lenses = new ArrayList<>(elements.length);
collectCodeLenses(typeRoot, elements, lenses, monitor);
if (monitor.isCanceled()) {
lenses.clear();
}
return lenses;
} catch (JavaModelException e) {
JavaLanguageServerPlugin.logException("Problem getting code lenses for" + unit.getElementName(), e);
}
return Collections.emptyList();
}
use of org.eclipse.jdt.core.IClassFile in project eclipse.jdt.ls by eclipse.
the class HoverHandlerTest method testHoverThrowable.
@Test
public void testHoverThrowable() throws Exception {
String uriString = ClassFileUtil.getURI(project, "java.lang.Exception");
IClassFile classFile = JDTUtils.resolveClassFile(uriString);
String contents = JavaLanguageServerPlugin.getContentProviderManager().getSource(classFile, monitor);
IDocument document = new Document(contents);
IRegion region = new FindReplaceDocumentAdapter(document).find(0, "Throwable", true, false, false, false);
int offset = region.getOffset();
int line = document.getLineOfOffset(offset);
int character = offset - document.getLineOffset(line);
TextDocumentIdentifier textDocument = new TextDocumentIdentifier(uriString);
Position position = new Position(line, character);
TextDocumentPositionParams params = new TextDocumentPositionParams(textDocument, position);
Hover hover = handler.hover(params, monitor);
assertNotNull(hover);
assertTrue("Unexpected hover ", !hover.getContents().isEmpty());
}
use of org.eclipse.jdt.core.IClassFile in project whole by wholeplatform.
the class BrowseListChooser method populateClassList.
public void populateClassList(IPackageFragment packageFragment) {
classList.clear();
try {
ClassLoader projectURLClassLoader = JDTUtils.createClassLoader(packageFragment.getJavaProject(), true);
for (IJavaElement javaElement : packageFragment.getChildren()) {
if (javaElement instanceof IClassFile) {
IClassFile classFile = ((IClassFile) javaElement);
Class<?> clazz = projectURLClassLoader.loadClass(classFile.getType().getFullyQualifiedName());
if (clazz != null && !clazz.isAnonymousClass() && !clazz.isMemberClass())
classList.add(clazz);
}
if (javaElement instanceof ICompilationUnit) {
ICompilationUnit compilationUnit = ((ICompilationUnit) javaElement);
IType[] types = compilationUnit.getTypes();
for (IType type : types) {
Class<?> clazz = projectURLClassLoader.loadClass(type.getFullyQualifiedName());
if (clazz != null && !clazz.isAnonymousClass() && !clazz.isMemberClass())
classList.add(clazz);
}
}
}
} catch (JavaModelException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
listChooser.refresh();
// TODO uncomment when checked state handling fixed
// listChooser.setAllChecked(true);
}
use of org.eclipse.jdt.core.IClassFile in project xtext-eclipse by eclipse.
the class XbaseBreakpointUtil method getBreakpointURI.
// this URI is only used for breakpoints on JARed files
public SourceRelativeURI getBreakpointURI(IEditorInput input) {
IResource resource = Adapters.adapt(input, IResource.class);
if (resource != null)
return null;
if (input instanceof IStorageEditorInput) {
IStorage storage;
try {
storage = ((IStorageEditorInput) input).getStorage();
if (storage instanceof IResource)
return null;
if (storage instanceof IJarEntryResource) {
IJarEntryResource jarEntryResource = (IJarEntryResource) storage;
if (!jarEntryResource.getPackageFragmentRoot().isArchive())
return null;
Object parent = jarEntryResource.getParent();
if (parent instanceof IPackageFragment) {
String path = ((IPackageFragment) parent).getElementName().replace('.', '/');
return new SourceRelativeURI(path + "/" + storage.getName());
} else if (parent instanceof IPackageFragmentRoot) {
return new SourceRelativeURI(storage.getName());
}
}
} catch (CoreException e) {
logger.error("Error finding breakpoint URI", e);
return null;
}
} else {
IClassFile classFile = Adapters.adapt(input, IClassFile.class);
if (classFile != null) {
ITrace traceToSource = traceForTypeRootProvider.getTraceToSource(classFile);
if (traceToSource == null)
return null;
for (ILocationInResource loc : traceToSource.getAllAssociatedLocations()) return loc.getSrcRelativeResourceURI();
return null;
}
}
return null;
}
Aggregations