Search in sources :

Example 31 with IClassFile

use of org.eclipse.jdt.core.IClassFile in project flux by eclipse.

the class NavigationService method computeNavigation.

public JSONObject computeNavigation(String username, String requestorResourcePath, int offset, int length) {
    try {
        ICompilationUnit liveEditUnit = liveEditUnits.getLiveEditUnit(username, requestorResourcePath);
        if (liveEditUnit != null) {
            IJavaElement[] elements = liveEditUnit.codeSelect(offset, length);
            if (elements != null && elements.length > 0) {
                JSONObject result = new JSONObject();
                IJavaElement element = elements[0];
                IResource resource = element.getResource();
                // if the selected element corresponds to a resource in workspace, navigate to it
                if (resource != null && resource.getProject() != null) {
                    String projectName = resource.getProject().getName();
                    String resourcePath = resource.getProjectRelativePath().toString();
                    result.put("project", projectName);
                    result.put("resource", resourcePath);
                    if (element instanceof ISourceReference) {
                        ISourceRange nameRange = ((ISourceReference) element).getNameRange();
                        result.put("offset", nameRange.getOffset());
                        result.put("length", nameRange.getLength());
                    }
                    return result;
                }
                // walk up the java model until we reach a class file
                while (element != null && !(element instanceof IClassFile)) {
                    element = element.getParent();
                }
                if (element instanceof IClassFile) {
                    IClassFile classFile = (IClassFile) element;
                    ISourceRange sourceRange = classFile.getSourceRange();
                    if (sourceRange != null) {
                        String projectName = element.getJavaProject().getProject().getName();
                        String resourcePath = classFile.getParent().getElementName().replace('.', '/');
                        resourcePath = "classpath:/" + resourcePath + "/" + classFile.getElementName();
                        result.put("project", projectName);
                        result.put("resource", resourcePath);
                        return result;
                    }
                }
            // we don't know how to navigate to this element
            }
        }
    } catch (JavaModelException e) {
        e.printStackTrace();
    } catch (JSONException e) {
        e.printStackTrace();
    }
    return null;
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) IJavaElement(org.eclipse.jdt.core.IJavaElement) JavaModelException(org.eclipse.jdt.core.JavaModelException) IClassFile(org.eclipse.jdt.core.IClassFile) JSONObject(org.json.JSONObject) JSONException(org.json.JSONException) ISourceReference(org.eclipse.jdt.core.ISourceReference) IResource(org.eclipse.core.resources.IResource) ISourceRange(org.eclipse.jdt.core.ISourceRange)

Example 32 with IClassFile

use of org.eclipse.jdt.core.IClassFile in project xtext-eclipse by eclipse.

the class JavaBreakPointProvider method getHandleId.

private String getHandleId(final IJavaStratumLineBreakpoint breakpoint) throws CoreException {
    IClassFile classFile = getClassFile(breakpoint);
    if (classFile != null)
        return classFile.getType().getHandleIdentifier();
    ILocationInEclipseResource javaLocation = getJavaLocation(breakpoint);
    if (javaLocation == null)
        return null;
    IStorage javaResource = javaLocation.getPlatformResource();
    if (!(javaResource instanceof IFile))
        return null;
    ICompilationUnit compilationUnit = (ICompilationUnit) JavaCore.create((IFile) javaResource);
    IJavaElement element = compilationUnit.getElementAt(javaLocation.getTextRegion().getOffset());
    return element == null ? null : element.getHandleIdentifier();
}
Also used : ILocationInEclipseResource(org.eclipse.xtext.ui.generator.trace.ILocationInEclipseResource) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) IJavaElement(org.eclipse.jdt.core.IJavaElement) IClassFile(org.eclipse.jdt.core.IClassFile) IFile(org.eclipse.core.resources.IFile) IStorage(org.eclipse.core.resources.IStorage)

Example 33 with IClassFile

use of org.eclipse.jdt.core.IClassFile in project xtext-eclipse by eclipse.

the class StratumBreakpointAdapterFactory method toggleBreakpoints.

@Override
public void toggleBreakpoints(IWorkbenchPart part, ISelection selection) throws CoreException {
    if (!(part instanceof XtextEditor) || !(selection instanceof ITextSelection)) {
        return;
    }
    try {
        final XtextEditor xtextEditor = (XtextEditor) part;
        final IEditorInput editorInput = xtextEditor.getEditorInput();
        final IResource breakpointResource = breakpointUtil.getBreakpointResource(editorInput);
        final SourceRelativeURI breakpointUri = breakpointUtil.getBreakpointURI(editorInput);
        final int offset = ((ITextSelection) selection).getOffset();
        final int line = xtextEditor.getDocument().getLineOfOffset(offset) + 1;
        Data data = xtextEditor.getDocument().tryReadOnly(new IUnitOfWork<Data, XtextResource>() {

            @Override
            public Data exec(XtextResource state) throws Exception {
                IResourceServiceProvider provider = state.getResourceServiceProvider();
                IStratumBreakpointSupport breakpointSupport = provider.get(IStratumBreakpointSupport.class);
                Data result = new Data();
                result.name = state.getURI().lastSegment();
                result.valid = breakpointSupport != null && breakpointSupport.isValidLineForBreakPoint(state, line);
                result.types = getClassNamePattern(state);
                result.lang = provider.get(LanguageInfo.class);
                result.sourceUri = state.getURI();
                IClassFile classFile = Adapters.adapt(editorInput, IClassFile.class);
                if (classFile != null) {
                    result.classHandle = classFile.getHandleIdentifier();
                }
                return result;
            }
        });
        if (data == null)
            return;
        IJavaStratumLineBreakpoint existingBreakpoint = findExistingBreakpoint(breakpointResource, breakpointUri, line);
        if (existingBreakpoint != null) {
            existingBreakpoint.delete();
            return;
        }
        if (!data.valid || data.types == null)
            return;
        if (log.isDebugEnabled()) {
            log.debug("Types the breakpoint listens for : " + data.types);
        }
        final IRegion information = xtextEditor.getDocument().getLineInformation(line - 1);
        final int charStart = information.getOffset();
        final int charEnd = information.getOffset() + information.getLength();
        final String shortName = data.lang.getShortName();
        Map<String, Object> attributes = Maps.newHashMap();
        if (breakpointUri != null)
            attributes.put(JarFileMarkerAnnotationModel.MARKER_URI, breakpointUri.toString());
        attributes.put(ORG_ECLIPSE_JDT_DEBUG_CORE_SOURCE_NAME, data.name);
        attributes.put(ORG_ECLIPSE_XTEXT_XBASE_SOURCE_URI, data.sourceUri.toString());
        if (data.classHandle != null)
            attributes.put(ORG_ECLIPSE_XTEXT_XBASE_CLASS_HANDLE, data.classHandle);
        final IJavaStratumLineBreakpoint breakpoint = JDIDebugModel.createStratumBreakpoint(breakpointResource, shortName, null, null, data.types, line, charStart, charEnd, 0, true, attributes);
        // make sure the class name pattern gets updated on change
        final IMarker marker = breakpoint.getMarker();
        final IWorkspace ws = marker.getResource().getWorkspace();
        IResourceChangeListener listener = new IResourceChangeListener() {

            @Override
            public void resourceChanged(IResourceChangeEvent event) {
                if (!marker.exists())
                    ws.removeResourceChangeListener(this);
                IResourceDelta delta = event.getDelta();
                if (delta == null)
                    return;
                final IResourceDelta findMember = event.getDelta().findMember(marker.getResource().getFullPath());
                if (findMember == null)
                    return;
                IResource res = findMember.getResource();
                if (res == null || !res.exists())
                    return;
                if (event.getType() == IResourceChangeEvent.PRE_DELETE) {
                    ws.removeResourceChangeListener(this);
                } else if (event.getType() == IResourceChangeEvent.POST_CHANGE && (findMember.getFlags() & IResourceDelta.CONTENT) != 0) {
                    String classNamePattern = getClassNamePattern(event.getResource());
                    try {
                        breakpoint.getMarker().setAttribute("org.eclipse.jdt.debug.pattern", classNamePattern);
                    } catch (CoreException e) {
                        log.info(e.getMessage(), e);
                    }
                }
            }
        };
        ws.addResourceChangeListener(listener, IResourceChangeEvent.POST_CHANGE | IResourceChangeEvent.PRE_DELETE);
    } catch (BadLocationException e) {
        log.info(e.getMessage(), e);
    }
}
Also used : IClassFile(org.eclipse.jdt.core.IClassFile) XtextResource(org.eclipse.xtext.resource.XtextResource) IRegion(org.eclipse.jface.text.IRegion) IJavaStratumLineBreakpoint(org.eclipse.jdt.debug.core.IJavaStratumLineBreakpoint) IResourceServiceProvider(org.eclipse.xtext.resource.IResourceServiceProvider) IResourceChangeEvent(org.eclipse.core.resources.IResourceChangeEvent) IStratumBreakpointSupport(org.eclipse.xtext.debug.IStratumBreakpointSupport) XtextEditor(org.eclipse.xtext.ui.editor.XtextEditor) IResourceChangeListener(org.eclipse.core.resources.IResourceChangeListener) ITextSelection(org.eclipse.jface.text.ITextSelection) IJavaStratumLineBreakpoint(org.eclipse.jdt.debug.core.IJavaStratumLineBreakpoint) IBreakpoint(org.eclipse.debug.core.model.IBreakpoint) CoreException(org.eclipse.core.runtime.CoreException) BadLocationException(org.eclipse.jface.text.BadLocationException) CoreException(org.eclipse.core.runtime.CoreException) IWorkspace(org.eclipse.core.resources.IWorkspace) IMarker(org.eclipse.core.resources.IMarker) SourceRelativeURI(org.eclipse.xtext.generator.trace.SourceRelativeURI) IEditorInput(org.eclipse.ui.IEditorInput) IResource(org.eclipse.core.resources.IResource) BadLocationException(org.eclipse.jface.text.BadLocationException) IResourceDelta(org.eclipse.core.resources.IResourceDelta)

Example 34 with IClassFile

use of org.eclipse.jdt.core.IClassFile in project jbosstools-hibernate by jbosstools.

the class Utils method findCompilationUnits.

public static ICompilationUnit[] findCompilationUnits(IJavaProject javaProject, IPath path) {
    IJavaElement javaElement = null;
    try {
        javaElement = javaProject.findElement(path.makeRelative());
    } catch (JavaModelException e) {
    // just ignore it!
    // HibernateConsolePlugin.getDefault().logErrorMessage("JavaModelException: ", e); //$NON-NLS-1$
    }
    ICompilationUnit[] res = null;
    if (javaElement != null) {
        if (javaElement instanceof ICompilationUnit) {
            res = new ICompilationUnit[] { (ICompilationUnit) javaElement };
        } else if (javaElement instanceof IPackageFragment) {
            try {
                res = ((IPackageFragment) javaElement).getCompilationUnits();
            } catch (JavaModelException e) {
            // just ignore it!
            // HibernateConsolePlugin.getDefault().logErrorMessage("JavaModelException: ", e); //$NON-NLS-1$
            }
        } else if (javaElement instanceof IClassFile) {
        }
    }
    return res;
}
Also used : IJavaElement(org.eclipse.jdt.core.IJavaElement) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) JavaModelException(org.eclipse.jdt.core.JavaModelException) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) IClassFile(org.eclipse.jdt.core.IClassFile)

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