List of usage examples for org.eclipse.jdt.core ITypeRoot codeSelect
IJavaElement[] codeSelect(int offset, int length) throws JavaModelException;
From source file:com.microsoft.javapkgsrv.JavaParser.java
License:MIT License
public List<JavaElement> ProcessQuickInfoRequest(String fileParseContents, String typeRootId, int cursorPosition) throws Exception { if (ActiveTypeRoots.containsKey(typeRootId)) { ITypeRoot cu = ActiveTypeRoots.get(typeRootId); cu.getBuffer().setContents(fileParseContents.toCharArray()); IJavaElement[] elements = cu.codeSelect(cursorPosition, 0); List<JavaElement> ret = new ArrayList<JavaElement>(); long flags = JavaElementLabelComposer.ALL_FULLY_QUALIFIED | JavaElementLabelComposer.ALL_DEFAULT | JavaElementLabelComposer.M_PRE_RETURNTYPE | JavaElementLabelComposer.F_PRE_TYPE_SIGNATURE; for (IJavaElement element : elements) { StringBuffer buffer = new StringBuffer(); JavaElementLabelComposer composer = new JavaElementLabelComposer(buffer); composer.appendElementLabel(element, flags); System.out.println(element.getPath().toString()); JavaElement.Builder b = JavaElement.newBuilder().setDefinition(buffer.toString()); String javaDoc = null; try { javaDoc = element.getAttachedJavadoc(null); } catch (JavaModelException jme) { jme.printStackTrace();//from ww w .jav a2 s. co m } if (javaDoc != null) b.setJavaDoc(javaDoc); ret.add(b.build()); } return ret; } return null; }
From source file:com.microsoft.javapkgsrv.JavaParser.java
License:MIT License
public List<FindDefinitionResponse.JavaElement> ProcessFindDefinintionRequest(String fileParseContents, String typeRootId, int cursorPosition) throws Exception { if (ActiveTypeRoots.containsKey(typeRootId)) { ITypeRoot cu = ActiveTypeRoots.get(typeRootId); cu.getBuffer().setContents(fileParseContents.toCharArray()); IJavaElement[] elements = cu.codeSelect(cursorPosition, 0); List<FindDefinitionResponse.JavaElement> ret = new ArrayList<FindDefinitionResponse.JavaElement>(); for (IJavaElement element : elements) { String definition = element.toString(); String path = element.getResource() != null ? element.getResource().getLocation().toOSString() : element.getPath().toOSString(); //String path = element.getPath().makeAbsolute().toOSString(); // element.getPath().toString(); boolean isAvailable = false; int posStart = -1; int posLength = 0; String contents = null; String classFileName = null; IClassFile classFileObj = null; ISourceReference srcRef = (ISourceReference) element; if (srcRef != null) { ISourceRange range = srcRef.getSourceRange(); if (SourceRange.isAvailable(range)) { isAvailable = true;/*from ww w .j av a 2 s . co m*/ posStart = range.getOffset(); posLength = range.getLength(); //if (path.endsWith(".jar")) //{ IOpenable op = element.getOpenable(); if (op != null && op instanceof IClassFile) { IBuffer buff = op.getBuffer(); classFileObj = (IClassFile) op; classFileName = classFileObj.getElementName(); contents = buff.getContents(); } //} } } FindDefinitionResponse.JavaElement.Builder retItem = FindDefinitionResponse.JavaElement.newBuilder() .setDefinition(definition).setFilePath(path).setHasSource(isAvailable) .setPositionStart(posStart).setPositionLength(posLength); if (contents != null) { //int hashCode = classFileObj.hashCode(); String handle = classFileObj.getHandleIdentifier(); ActiveTypeRoots.put(handle, classFileObj); retItem.setFileName(classFileName); retItem.setTypeRootIdentifier(TypeRootIdentifier.newBuilder().setHandle(handle).build()); } System.out.println(retItem.toString()); if (contents != null) { retItem.setFileContents(contents); } ret.add(retItem.build()); } return ret; } return null; }
From source file:net.harawata.mybatipse.mybatis.JavaHyperlinkDetector.java
License:Open Source License
@Override public IHyperlink[] detectHyperlinks(ITextViewer textViewer, IRegion region, boolean canShowMultipleHyperlinks) { IHyperlink[] links = null;/* w ww . ja v a 2 s. c o m*/ ITextEditor editor = (ITextEditor) getAdapter(ITextEditor.class); IEditorInput input = editor.getEditorInput(); IJavaElement element = (IJavaElement) input.getAdapter(IJavaElement.class); ITypeRoot typeRoot = (ITypeRoot) element.getAdapter(ITypeRoot.class); try { IJavaElement[] srcElements = typeRoot.codeSelect(region.getOffset(), region.getLength()); if (srcElements.length == 1) { IJavaElement srcElement = srcElements[0]; switch (srcElement.getElementType()) { case IJavaElement.METHOD: links = getLinks(typeRoot, null, "//*[@id='" + srcElement.getElementName() + "']", region); break; case IJavaElement.TYPE: links = getLinks(typeRoot, null, "//mapper", region); break; default: break; } } } catch (JavaModelException e) { Activator.log(Status.ERROR, e.getMessage(), e); } return links; }
From source file:org.eclipse.recommenders.internal.rcp.JavaElementSelections.java
License:Open Source License
/** * Returns the {@link IJavaElement} at the given offset. If no {@link IJavaElement} is selected, the innermost * enclosing {@link IJavaElement} is returned (e.g., the declaring method or type). If both selection resolutions * fail, {@link Optional#absent()} is returned. *//*ww w . jav a 2 s . co m*/ public static Optional<IJavaElement> resolveJavaElementFromTypeRootInEditor(final ITypeRoot root, final int offset) { ensureIsNotNull(root); try { if (isInvalidSelection(root, offset)) { return absent(); } // try resolve elements at current offset final IJavaElement[] elements = root.codeSelect(offset, 0); if (elements.length > 0) { // return java element under cursor/selection start return of(elements[0]); } else { // XXX MB: decided against selection changes because these // frequent changes were too disturbing return absent(); // ignore that for a while: // // if no java element has been selected, return the innermost // Java element enclosing a given offset. // // This might evaluate to null. // IJavaElement enclosingElement = root.getElementAt(offset); // if (enclosingElement == null) { // // selection occurred in empty space somewhere before the // type declaration. // // return type-root then. // enclosingElement = root; // } // return of(enclosingElement); } } catch (final Exception e) { // actually, these can happen when using snipmatch's in-editor completion. // fractions of seconds seem potentially to lead to this exception, thus, we swallow them here. if (!isInvalidSelection(root, offset)) { log(ERROR_FAILED_TO_RESOLVE_SELECTION, root.getHandleIdentifier(), offset, e); } return absent(); } }
From source file:org.jboss.tools.cdi.deltaspike.text.ext.AuthorizerHyperlinkDetector.java
License:Open Source License
public IHyperlink[] detectHyperlinks(ITextViewer textViewer, IRegion region, boolean canShowMultipleHyperlinks) { this.region = region; this.viewer = textViewer; ITextEditor textEditor = (ITextEditor) getAdapter(ITextEditor.class); if (region == null || !canShowMultipleHyperlinks || !(textEditor instanceof JavaEditor)) return null; int offset = region.getOffset(); ITypeRoot input = EditorUtility.getEditorInputJavaElement(textEditor, false); if (input == null) return null; document = textEditor.getDocumentProvider().getDocument(textEditor.getEditorInput()); IRegion wordRegion = JavaWordFinder.findWord(document, offset); if (wordRegion == null) return null; IProject project = null;//from w w w .j ava2s . c o m project = input.getJavaProject().getProject(); if (project == null) return null; CDICoreNature cdiNature = CDIUtil.getCDINatureWithProgress(project); if (cdiNature == null) return null; IJavaElement[] elements = null; IType annotationType = null; try { elements = input.codeSelect(wordRegion.getOffset(), wordRegion.getLength()); if (elements == null) return null; if (elements.length != 1) return null; ArrayList<IHyperlink> hyperlinks = new ArrayList<IHyperlink>(); int position = 0; if (elements[0] instanceof IType) { annotationType = (IType) elements[0]; if (!annotationType.isAnnotation()) { annotationType = null; } elements[0] = input.getElementAt(wordRegion.getOffset()); if (elements[0] == null) return null; if (elements[0] instanceof IMethod) { position = offset; } } findAuthorizerMethods(cdiNature, elements[0], annotationType, position, input.getPath(), hyperlinks); if (hyperlinks != null && !hyperlinks.isEmpty()) { return (IHyperlink[]) hyperlinks.toArray(new IHyperlink[hyperlinks.size()]); } } catch (JavaModelException jme) { DeltaspikeCorePlugin.getDefault().logError(jme); } return null; }
From source file:org.jboss.tools.cdi.seam.text.ext.hyperlink.GenericInjectedPointHyperlinkDetector.java
License:Open Source License
public IHyperlink[] detectHyperlinks(ITextViewer textViewer, IRegion region, boolean canShowMultipleHyperlinks) { this.region = region; this.viewer = textViewer; ITextEditor textEditor = (ITextEditor) getAdapter(ITextEditor.class); if (region == null || !canShowMultipleHyperlinks || !(textEditor instanceof JavaEditor)) return null; int offset = region.getOffset(); ITypeRoot input = EditorUtility.getEditorInputJavaElement(textEditor, false); if (input == null) return null; document = textEditor.getDocumentProvider().getDocument(textEditor.getEditorInput()); IRegion wordRegion = JavaWordFinder.findWord(document, offset); if (wordRegion == null) return null; IProject project = null;/*w w w. j a v a 2 s . c o m*/ project = input.getJavaProject().getProject(); if (project == null) return null; CDICoreNature cdiNature = CDIUtil.getCDINatureWithProgress(project); if (cdiNature == null) return null; IJavaElement[] elements = null; try { elements = input.codeSelect(wordRegion.getOffset(), wordRegion.getLength()); if (elements == null) return null; if (elements.length != 1) return null; ArrayList<IHyperlink> hyperlinks = new ArrayList<IHyperlink>(); int position = 0; if (elements[0] instanceof IType) { elements[0] = input.getElementAt(wordRegion.getOffset()); if (elements[0] == null) return null; if (elements[0] instanceof IMethod) { position = offset; } } findInjectedBeans(cdiNature, elements[0], position, input.getPath(), hyperlinks); if (hyperlinks != null && !hyperlinks.isEmpty()) { return (IHyperlink[]) hyperlinks.toArray(new IHyperlink[hyperlinks.size()]); } } catch (JavaModelException jme) { CDISeamExtPlugin.getDefault().logError(jme); } return null; }
From source file:org.jboss.tools.cdi.text.ext.hyperlink.EventAndObserverMethodHyperlinkDetector.java
License:Open Source License
public IHyperlink[] detectHyperlinks(ITextViewer textViewer, IRegion region, boolean canShowMultipleHyperlinks) { ITextEditor textEditor = (ITextEditor) getAdapter(ITextEditor.class); if (region == null || !(textEditor instanceof JavaEditor)) return null; if (textEditor.getEditorInput() instanceof IFileEditorInput) { file = ((IFileEditorInput) textEditor.getEditorInput()).getFile(); }//w w w . j av a2 s .c o m int offset = region.getOffset(); ITypeRoot input = EditorUtility.getEditorInputJavaElement(textEditor, true); if (input == null) return null; IDocument document = textEditor.getDocumentProvider().getDocument(textEditor.getEditorInput()); IRegion wordRegion = JavaWordFinder.findWord(document, offset); if (wordRegion == null) return null; IProject project = null; project = input.getJavaProject().getProject(); if (project == null) return null; CDICoreNature cdiNature = CDIUtil.getCDINatureWithProgress(project); if (cdiNature == null) return null; IJavaElement[] elements = null; try { elements = input.codeSelect(wordRegion.getOffset(), wordRegion.getLength()); if (elements == null) return null; if (elements.length != 1) return null; ArrayList<IHyperlink> hyperlinks = new ArrayList<IHyperlink>(); int position = 0; if (elements[0] instanceof IType) { elements[0] = input.getElementAt(wordRegion.getOffset()); if (elements[0] == null) return null; if (elements[0] instanceof IMethod) { position = offset; } } ICDIProject cdiProject = CDIUtil.getCDIProject(file, cdiNature, textEditor.isDirty()); if (cdiProject != null) { IInjectionPoint injectionPoint = findInjectedPoint(cdiProject, elements[0], position, input.getPath()); Set<IParameter> param = findObserverParameter(cdiProject, elements[0], offset, input.getPath()); if (injectionPoint != null) { Collection<IObserverMethod> observerMethods = cdiProject.resolveObserverMethods(injectionPoint); if (observerMethods.size() == 1) { hyperlinks.add( new ObserverMethodHyperlink(region, observerMethods.iterator().next(), document)); } else if (observerMethods.size() > 0) { hyperlinks.add( new ObserverMethodListHyperlink(textViewer, region, observerMethods, document)); } } else if (param != null) { Set<IInjectionPoint> events = new HashSet<IInjectionPoint>(); for (IParameter p : param) events.addAll(cdiProject.findObservedEvents(p)); if (events.size() == 1) { hyperlinks.add(new EventHyperlink(region, events.iterator().next(), document)); } else if (events.size() > 0) { hyperlinks.add(new EventListHyperlink(textViewer, region, events, document)); } } } if (hyperlinks != null && !hyperlinks.isEmpty()) { return (IHyperlink[]) hyperlinks.toArray(new IHyperlink[hyperlinks.size()]); } } catch (JavaModelException jme) { CDIExtensionsPlugin.getDefault().logError(jme); } return null; }
From source file:org.jboss.tools.cdi.text.ext.hyperlink.InjectedPointHyperlinkDetector.java
License:Open Source License
public IHyperlink[] detectHyperlinks(ITextViewer textViewer, IRegion region, boolean canShowMultipleHyperlinks) { this.region = region; this.viewer = textViewer; ITextEditor textEditor = (ITextEditor) getAdapter(ITextEditor.class); if (region == null || !(textEditor instanceof JavaEditor)) return null; if (textEditor.getEditorInput() instanceof IFileEditorInput) { file = ((IFileEditorInput) textEditor.getEditorInput()).getFile(); }//from w w w. j a v a 2 s .c om int offset = region.getOffset(); ITypeRoot input = EditorUtility.getEditorInputJavaElement(textEditor, true); if (input == null) return null; document = textEditor.getDocumentProvider().getDocument(textEditor.getEditorInput()); IRegion wordRegion = JavaWordFinder.findWord(document, offset); if (wordRegion == null) return null; IProject project = null; project = input.getJavaProject().getProject(); if (project == null) return null; CDICoreNature cdiNature = CDIUtil.getCDINatureWithProgress(project); if (cdiNature == null) return null; IJavaElement[] elements = null; try { elements = input.codeSelect(wordRegion.getOffset(), wordRegion.getLength()); if (elements == null) return null; if (elements.length != 1) return null; ArrayList<IHyperlink> hyperlinks = new ArrayList<IHyperlink>(); int position = 0; if (elements[0] instanceof IType) { elements[0] = input.getElementAt(wordRegion.getOffset()); if (elements[0] == null) return null; if (elements[0] instanceof IMethod) { position = offset; } } findInjectedBeans(cdiNature, elements[0], position, input.getPath(), hyperlinks, textEditor.isDirty()); if (hyperlinks != null && !hyperlinks.isEmpty()) { return (IHyperlink[]) hyperlinks.toArray(new IHyperlink[hyperlinks.size()]); } } catch (JavaModelException jme) { CDIExtensionsPlugin.getDefault().logError(jme); } return null; }
From source file:org.jboss.tools.cdi.text.ext.hyperlink.ProducerDisposerHyperlinkDetector.java
License:Open Source License
public IHyperlink[] detectHyperlinks(ITextViewer textViewer, IRegion region, boolean canShowMultipleHyperlinks) { ITextEditor textEditor = (ITextEditor) getAdapter(ITextEditor.class); if (region == null || !(textEditor instanceof JavaEditor)) return null; if (textEditor.getEditorInput() instanceof IFileEditorInput) { file = ((IFileEditorInput) textEditor.getEditorInput()).getFile(); }/*from w w w.j a v a2s.c om*/ int offset = region.getOffset(); ITypeRoot input = EditorUtility.getEditorInputJavaElement(textEditor, true); if (input == null) return null; IDocument document = textEditor.getDocumentProvider().getDocument(textEditor.getEditorInput()); IRegion wordRegion = JavaWordFinder.findWord(document, offset); if (wordRegion == null) return null; IProject project = null; project = input.getJavaProject().getProject(); if (project == null) return null; Collection<IBean> beans = getBeans(project, input.getPath(), textEditor.isDirty()); if (beans == null) return null; IJavaElement[] elements = null; try { elements = input.codeSelect(wordRegion.getOffset(), wordRegion.getLength()); if (elements == null) return null; if (elements.length != 1) return null; ArrayList<IHyperlink> hyperlinks = new ArrayList<IHyperlink>(); if (elements[0] instanceof IType) { if (CDIConstants.PRODUCES_ANNOTATION_TYPE_NAME .equals(((IType) elements[0]).getFullyQualifiedName())) { elements[0] = input.getElementAt(wordRegion.getOffset()); if (elements[0] == null) return null; } } if (elements[0] instanceof IMethod) { for (IBean bean : beans) { if (bean instanceof IClassBean) { IProducerMethod producer = getProducer((IClassBean) bean, (IMethod) elements[0]); if (producer != null) { List<IMethod> disposers = findDisposerMethods(producer); for (IMethod method : disposers) { hyperlinks.add(new DisposerHyperlink(region, method, document)); } } else { IBeanMethod disposer = getDisposer((IClassBean) bean, (IMethod) elements[0]); if (disposer != null) { List<IMethod> producers = findProducerMethods((IClassBean) bean, disposer); for (IMethod method : producers) { hyperlinks.add(new ProducerHyperlink(region, method, document)); } } } } } } if (hyperlinks != null && !hyperlinks.isEmpty()) { return (IHyperlink[]) hyperlinks.toArray(new IHyperlink[hyperlinks.size()]); } } catch (JavaModelException jme) { CDIExtensionsPlugin.getDefault().logError(jme); } return null; }
From source file:org.jboss.tools.vscode.java.internal.handlers.NavigateToDefinitionHandler.java
License:Open Source License
private Location computeDefinitonNavigation(ITypeRoot unit, int line, int column) { try {/*from w w w .j a va 2 s.c o m*/ IJavaElement[] elements = unit.codeSelect(JsonRpcHelpers.toOffset(unit.getBuffer(), line, column), 0); if (elements == null || elements.length != 1) return null; IJavaElement element = elements[0]; ICompilationUnit compilationUnit = (ICompilationUnit) element .getAncestor(IJavaElement.COMPILATION_UNIT); IClassFile cf = (IClassFile) element.getAncestor(IJavaElement.CLASS_FILE); if (compilationUnit != null || (cf != null && cf.getSourceRange() != null)) { return JDTUtils.toLocation(element); } return null; } catch (JavaModelException e) { JavaLanguageServerPlugin.logException("Problem with codeSelect for" + unit.getElementName(), e); } return null; }