List of usage examples for org.eclipse.jdt.core IJavaElement getAncestor
IJavaElement getAncestor(int ancestorType);
From source file:org.codehaus.groovy.eclipse.astviews.ASTView.java
License:Apache License
private void hookGroovy() { partListener = new IPartListener() { public void partActivated(IWorkbenchPart part) { }/*from ww w . j a va2 s. co m*/ public void partBroughtToTop(IWorkbenchPart part) { try { if (part instanceof IEditorPart) { IFile file = (IFile) ((IEditorPart) part).getEditorInput().getAdapter(IFile.class); if (file != null && ContentTypeUtils.isGroovyLikeFileName(file.getName())) { ICompilationUnit unit = JavaCore.createCompilationUnitFrom(file); if (unit instanceof GroovyCompilationUnit) { if (editor != part) { editor = (IEditorPart) part; Object[] treePaths = viewer.getExpandedElements(); viewer.setInput(((GroovyCompilationUnit) unit).getModuleNode()); viewer.setExpandedElements(treePaths); } else { // nothing to do! } return; } } } } catch (Exception e) { GroovyCore.logException("Error updating AST Viewer", e); //$NON-NLS-1$ } editor = null; // This is a guard - the content provider should not be null, // but sometimes this happens when the // part is disposed of for various reasons (unhandled exceptions // AFAIK). Without this guard, // error message popups continue until Eclipse if forcefully // killed. if (viewer.getContentProvider() != null) { viewer.setInput(null); } } public void partClosed(IWorkbenchPart part) { } public void partDeactivated(IWorkbenchPart part) { } public void partOpened(IWorkbenchPart part) { } }; getSite().getPage().addPartListener(partListener); // Warm the listener up. if (getSite().getPage().getActiveEditor() instanceof GroovyEditor) { partListener.partBroughtToTop(getSite().getPage().getActiveEditor()); } listener = new IElementChangedListener() { public void elementChanged(ElementChangedEvent event) { // The editor is currently not a GroovyEditor, so // there is not // ASTView to refresh. if (editor == null) { return; } IJavaElementDelta delta = event.getDelta(); IFile file = (IFile) editor.getEditorInput().getAdapter(IFile.class); final GroovyCompilationUnit unit = (GroovyCompilationUnit) JavaCore.createCompilationUnitFrom(file); // determine if the delta contains the ICompUnit under question if (isUnitInDelta(delta, unit)) { Display.getDefault().asyncExec(new Runnable() { public void run() { Object[] treePaths = viewer.getExpandedElements(); viewer.setInput(unit.getModuleNode()); viewer.setExpandedElements(treePaths); } }); } } private boolean isUnitInDelta(IJavaElementDelta delta, GroovyCompilationUnit unit) { IJavaElement elt = delta.getElement(); if (elt.getElementType() == IJavaElement.COMPILATION_UNIT) { // comparing with a compilation unit // if test fails, no need to go further if (elt.getElementName().equals(unit.getElementName())) { return true; } else { return false; } } ICompilationUnit candidateUnit = (ICompilationUnit) elt.getAncestor(IJavaElement.COMPILATION_UNIT); if (candidateUnit != null) { // now if test fails, no need to go further if (candidateUnit.getElementName().equals(unit.getElementName())) { return true; } else { return false; } } // delta is a potential ancestor of this compilationUnit IJavaElementDelta[] deltas = delta.getAffectedChildren(); if (deltas != null) { for (IJavaElementDelta delta2 : deltas) { if (isUnitInDelta(delta2, unit)) { return true; } } } return false; } }; JavaCore.addElementChangedListener(listener, ElementChangedEvent.POST_RECONCILE); }
From source file:org.codehaus.groovy.eclipse.codeassist.requestor.ContentAssistContext.java
License:Apache License
public IType getEnclosingType() { try {/*from w w w . jav a 2 s . c o m*/ IJavaElement element = unit.getElementAt(completionLocation); if (element != null) { return (IType) element.getAncestor(IJavaElement.TYPE); } } catch (JavaModelException e) { GroovyCore.logException("Exception finding completion for " + unit, e); } return null; }
From source file:org.dawnsci.common.widgets.breadcrumb.ResourceToItemsMapper.java
License:Open Source License
/** * Method that decides which elements can have error markers * Returns null if an element can not have error markers. * @param element The input element// w ww . j a va 2 s. c o m * @return Returns the corresponding resource or null */ private static IResource getCorrespondingResource(Object element) { if (element instanceof IJavaElement) { IJavaElement elem = (IJavaElement) element; IResource res = elem.getResource(); if (res == null) { ICompilationUnit cu = (ICompilationUnit) elem.getAncestor(IJavaElement.COMPILATION_UNIT); if (cu != null) { // elements in compilation units are mapped to the underlying resource of the original cu res = cu.getResource(); } } return res; } else if (element instanceof IResource) { return (IResource) element; } return null; }
From source file:org.eclipse.ajdt.core.parserbridge.AJCompilationUnitProblemFinder.java
License:Open Source License
private static String getTypeNameAtPosition(CategorizedProblem categorizedProblem, CompilationUnit unit) throws JavaModelException { IJavaElement elt = unit.getElementAt(categorizedProblem.getSourceStart()); IType type = elt != null ? (IType) elt.getAncestor(IJavaElement.TYPE) : null; if (type == null) { // just return the name of the CU int dotIndex = unit.getElementName().indexOf('.'); if (dotIndex > 0) { return unit.getElementName().substring(0, dotIndex); } else {// www . j a v a 2 s .c o m return unit.getElementName(); } } return type.getElementName(); }
From source file:org.eclipse.ajdt.core.parserbridge.AJCompilationUnitProblemFinder.java
License:Open Source License
private static boolean isAspect(CategorizedProblem problem, CompilationUnit unit, boolean isJavaFileInAJEditor) { if (isJavaFileInAJEditor) { // we don't know...be safe and // let compiler do the errors return true; }//from ww w .j a v a2s . c om if (unit instanceof AJCompilationUnit) { try { IJavaElement elt = unit.getElementAt(problem.getSourceStart()); if (elt != null) { IType type = (IType) elt.getAncestor(IJavaElement.TYPE); return type != null && type instanceof AspectElement; } } catch (JavaModelException e) { } } return false; }
From source file:org.eclipse.ajdt.core.parserbridge.AJCompilationUnitProblemFinder.java
License:Open Source License
private static boolean isPrivilegedAspect(CategorizedProblem problem, CompilationUnit unit, boolean isJavaFileInAJEditor) { if (isJavaFileInAJEditor) { // we don't know...be safe and // let compiler do the errors return true; }// w w w . j a va 2 s . c o m if (unit instanceof AJCompilationUnit) { try { IJavaElement elt = unit.getElementAt(problem.getSourceStart()); if (elt != null) { IType type = (IType) elt.getAncestor(IJavaElement.TYPE); if (type != null && type instanceof AspectElement) { AspectElement aspectType = (AspectElement) type; return aspectType.isPrivileged(); } } } catch (JavaModelException e) { } } return false; }
From source file:org.eclipse.ajdt.internal.core.search.AJDTSearchProvider.java
License:Open Source License
/** * convert ITD matches into the target types * Or ignore otherwise//from ww w . ja v a 2 s . c om * @throws JavaModelException */ public IJavaElement filterJUnit4TestMatch(IJavaElement possibleTest) throws JavaModelException { // the results are returned as ResolvedSourceMethod, not an ITD // so must do a little work to get to the real ITD if (!(possibleTest instanceof IMethod)) { return possibleTest; } IJavaElement parent = possibleTest.getAncestor(IJavaElement.TYPE); if (parent instanceof AspectElement) { String itdName = possibleTest.getElementName().replace('$', '.'); IntertypeElement matchingITD = findMatchingITD((AspectElement) parent, (IMethod) possibleTest, itdName); if (matchingITD != null) { return matchingITD.createMockDeclaration(); } } return possibleTest; }
From source file:org.eclipse.ajdt.internal.ui.ajdocexport.AJdocOptionsManager.java
License:Open Source License
private IJavaElement getSelectableJavaElement(Object obj) throws JavaModelException { IJavaElement je = null; if (obj instanceof IAdaptable) { je = (IJavaElement) ((IAdaptable) obj).getAdapter(IJavaElement.class); }/* ww w . j a v a 2 s . co m*/ if (je != null) { switch (je.getElementType()) { case IJavaElement.JAVA_MODEL: case IJavaElement.JAVA_PROJECT: case IJavaElement.CLASS_FILE: break; case IJavaElement.PACKAGE_FRAGMENT_ROOT: if (containsCompilationUnits((IPackageFragmentRoot) je)) { return je; } break; case IJavaElement.PACKAGE_FRAGMENT: if (containsCompilationUnits((IPackageFragment) je)) { return je; } break; default: ICompilationUnit cu = (ICompilationUnit) je.getAncestor(IJavaElement.COMPILATION_UNIT); if (cu != null) { return cu; } } IJavaProject project = je.getJavaProject(); if (isValidProject(project)) return project; } return null; }
From source file:org.eclipse.ajdt.internal.ui.editor.actions.AJOrganizeImportsAction.java
License:Open Source License
private boolean isEnabled(IStructuredSelection selection) { Object[] selected = selection.toArray(); for (int i = 0; i < selected.length; i++) { try {/*from w w w . j a v a 2 s . c o m*/ if (selected[i] instanceof IJavaElement) { IJavaElement elem = (IJavaElement) selected[i]; if (elem.exists()) { switch (elem.getElementType()) { case IJavaElement.TYPE: return elem.getParent().getElementType() == IJavaElement.COMPILATION_UNIT; // for browsing perspective case IJavaElement.COMPILATION_UNIT: return true; case IJavaElement.IMPORT_CONTAINER: return true; case IJavaElement.PACKAGE_FRAGMENT: case IJavaElement.PACKAGE_FRAGMENT_ROOT: IPackageFragmentRoot root = (IPackageFragmentRoot) elem .getAncestor(IJavaElement.PACKAGE_FRAGMENT_ROOT); return (root.getKind() == IPackageFragmentRoot.K_SOURCE); case IJavaElement.JAVA_PROJECT: // https://bugs.eclipse.org/bugs/show_bug.cgi?id=65638 return true; } } } else if (selected[i] instanceof LogicalPackage) { return true; } } catch (JavaModelException e) { if (!e.isDoesNotExist()) { JavaPlugin.log(e); } } } return false; }
From source file:org.eclipse.ajdt.internal.ui.editor.outline.AJOutlineInformationControl.java
License:Open Source License
/** * {@inheritDoc}/*from ww w .j a va 2 s . com*/ */ public void setInput(Object information) { if (information == null || information instanceof String) { inputChanged(null, null); return; } IJavaElement je = (IJavaElement) information; ICompilationUnit cu = (ICompilationUnit) je.getAncestor(IJavaElement.COMPILATION_UNIT); if (cu != null) fInput = cu; else fInput = je.getAncestor(IJavaElement.CLASS_FILE); inputChanged(fInput, information); }