Search in sources :

Example 26 with IClassFile

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;
}
Also used : IJavaElement(org.eclipse.jdt.core.IJavaElement) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) SearchMatch(org.eclipse.jdt.core.search.SearchMatch) IClassFile(org.eclipse.jdt.core.IClassFile) ArrayList(java.util.ArrayList) SearchRequestor(org.eclipse.jdt.core.search.SearchRequestor) SearchEngine(org.eclipse.jdt.core.search.SearchEngine) CoreException(org.eclipse.core.runtime.CoreException) SearchPattern(org.eclipse.jdt.core.search.SearchPattern) Location(org.eclipse.lsp4j.Location)

Example 27 with IClassFile

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;
}
Also used : IClassFile(org.eclipse.jdt.core.IClassFile) IContentProvider(org.eclipse.jdt.ls.core.internal.IContentProvider) IDecompiler(org.eclipse.jdt.ls.core.internal.IDecompiler) URI(java.net.URI) CoreException(org.eclipse.core.runtime.CoreException)

Example 28 with IClassFile

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

Example 29 with IClassFile

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

Example 30 with IClassFile

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();
    }
}
Also used : IJavaProject(org.eclipse.jdt.core.IJavaProject) IClassFile(org.eclipse.jdt.core.IClassFile) JSONObject(org.json.JSONObject) CoreException(org.eclipse.core.runtime.CoreException) JSONException(org.json.JSONException) IOException(java.io.IOException) IType(org.eclipse.jdt.core.IType)

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