Search in sources :

Example 6 with IClassFile

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;
}
Also used : IJavaElement(org.eclipse.jdt.core.IJavaElement) SearchMatch(org.eclipse.jdt.core.search.SearchMatch) IClassFile(org.eclipse.jdt.core.IClassFile) ContentProviderManager(org.eclipse.jdt.ls.core.internal.managers.ContentProviderManager) ArrayList(java.util.ArrayList) Document(org.eclipse.jface.text.Document) IDocument(org.eclipse.jface.text.IDocument) SearchRequestor(org.eclipse.jdt.core.search.SearchRequestor) SearchEngine(org.eclipse.jdt.core.search.SearchEngine) CoreException(org.eclipse.core.runtime.CoreException) IJavaSearchScope(org.eclipse.jdt.core.search.IJavaSearchScope) SearchPattern(org.eclipse.jdt.core.search.SearchPattern) IDocument(org.eclipse.jface.text.IDocument) BadLocationException(org.eclipse.jface.text.BadLocationException)

Example 7 with IClassFile

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();
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) IJavaElement(org.eclipse.jdt.core.IJavaElement) CodeLens(org.eclipse.lsp4j.CodeLens) JavaModelException(org.eclipse.jdt.core.JavaModelException) IClassFile(org.eclipse.jdt.core.IClassFile) ITypeRoot(org.eclipse.jdt.core.ITypeRoot) ArrayList(java.util.ArrayList)

Example 8 with IClassFile

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());
}
Also used : TextDocumentIdentifier(org.eclipse.lsp4j.TextDocumentIdentifier) IClassFile(org.eclipse.jdt.core.IClassFile) Position(org.eclipse.lsp4j.Position) Hover(org.eclipse.lsp4j.Hover) MarkedString(org.eclipse.lsp4j.MarkedString) TextDocumentPositionParams(org.eclipse.lsp4j.TextDocumentPositionParams) Document(org.eclipse.jface.text.Document) IDocument(org.eclipse.jface.text.IDocument) IDocument(org.eclipse.jface.text.IDocument) IRegion(org.eclipse.jface.text.IRegion) FindReplaceDocumentAdapter(org.eclipse.jface.text.FindReplaceDocumentAdapter) AbstractProjectsManagerBasedTest(org.eclipse.jdt.ls.core.internal.managers.AbstractProjectsManagerBasedTest) Test(org.junit.Test)

Example 9 with IClassFile

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);
}
Also used : IJavaElement(org.eclipse.jdt.core.IJavaElement) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) JavaModelException(org.eclipse.jdt.core.JavaModelException) IClassFile(org.eclipse.jdt.core.IClassFile) IType(org.eclipse.jdt.core.IType)

Example 10 with IClassFile

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;
}
Also used : IStorageEditorInput(org.eclipse.ui.IStorageEditorInput) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) IClassFile(org.eclipse.jdt.core.IClassFile) IStorage(org.eclipse.core.resources.IStorage) ILocationInResource(org.eclipse.xtext.generator.trace.ILocationInResource) IPackageFragmentRoot(org.eclipse.jdt.core.IPackageFragmentRoot) CoreException(org.eclipse.core.runtime.CoreException) IJarEntryResource(org.eclipse.jdt.core.IJarEntryResource) ITrace(org.eclipse.xtext.generator.trace.ITrace) SourceRelativeURI(org.eclipse.xtext.generator.trace.SourceRelativeURI) IResource(org.eclipse.core.resources.IResource)

Aggregations

IClassFile (org.eclipse.jdt.core.IClassFile)34 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)20 IJavaElement (org.eclipse.jdt.core.IJavaElement)13 IType (org.eclipse.jdt.core.IType)9 JavaModelException (org.eclipse.jdt.core.JavaModelException)8 CoreException (org.eclipse.core.runtime.CoreException)7 ArrayList (java.util.ArrayList)6 ISourceRange (org.eclipse.jdt.core.ISourceRange)5 IPackageFragment (org.eclipse.jdt.core.IPackageFragment)4 ITypeRoot (org.eclipse.jdt.core.ITypeRoot)4 BadLocationException (org.eclipse.jface.text.BadLocationException)4 IResource (org.eclipse.core.resources.IResource)3 IStorage (org.eclipse.core.resources.IStorage)3 IMethod (org.eclipse.jdt.core.IMethod)3 IPackageFragmentRoot (org.eclipse.jdt.core.IPackageFragmentRoot)3 ISourceReference (org.eclipse.jdt.core.ISourceReference)3 Document (org.eclipse.jface.text.Document)3 IOException (java.io.IOException)2 DebuggerException (org.eclipse.che.api.debugger.server.exceptions.DebuggerException)2 JarEntry (org.eclipse.che.ide.ext.java.shared.JarEntry)2