List of usage examples for org.eclipse.jdt.core IJavaElement getAncestor
IJavaElement getAncestor(int ancestorType);
From source file:org.eclipse.pde.api.tools.internal.builder.AbstractProblemDetector.java
License:Open Source License
/** * Performs a quick look-up using the offset into the the * {@link ICompilationUnit}/* w w w.j ava2s .c o m*/ * * @param jtype * @param document * @param reference * @param offset * @return * @throws JavaModelException */ protected IMethod quickLookup(final IType jtype, IDocument document, IReference reference, int offset) throws JavaModelException { if (offset > -1) { IJavaElement element = jtype.getCompilationUnit().getElementAt(offset); if (element != null) { IJavaElement ancestor = element.getAncestor(IJavaElement.METHOD); if (ancestor != null) { return (IMethod) ancestor; } } } return null; }
From source file:org.eclipse.pde.api.tools.internal.builder.IllegalExtendsProblemDetector.java
License:Open Source License
private IMethod quickLookup(final IType jtype, IDocument document, IReference reference, int offset) throws JavaModelException { if (offset > -1) { IJavaElement element = jtype.getCompilationUnit().getElementAt(offset); if (element != null) { IJavaElement ancestor = element.getAncestor(IJavaElement.METHOD); if (ancestor != null) { return (IMethod) ancestor; }//from w ww . j av a2s .c o m } } return null; }
From source file:org.eclipse.pde.api.tools.ui.internal.markers.UpdateSinceTagOperation.java
License:Open Source License
public void run(IProgressMonitor monitor) { if (monitor != null && monitor.isCanceled()) { return;//www.j a va2 s .co m } if (monitor != null) { monitor.beginTask(MarkerMessages.UpdateSinceTagOperation_title, 3); } // retrieve the AST node compilation unit try { Integer charStartAttribute = (Integer) this.fMarker.getAttribute(IMarker.CHAR_START); int intValue = charStartAttribute.intValue(); IJavaElement javaElement = null; IJavaElement handleElement = null; if (intValue > 0) { IResource resource = this.fMarker.getResource(); javaElement = JavaCore.create(resource); } else { // this is a case where the marker is reported against the // MANIFEST.MF file String handle = (String) fMarker.getAttribute(IApiMarkerConstants.MARKER_ATTR_HANDLE_ID); if (handle != null) { handleElement = JavaCore.create(handle); } if (handleElement != null && handleElement.exists()) { javaElement = handleElement.getAncestor(IJavaElement.COMPILATION_UNIT); } } if (javaElement != null && javaElement.getElementType() == IJavaElement.COMPILATION_UNIT) { ICompilationUnit compilationUnit = (ICompilationUnit) javaElement; if (!compilationUnit.isWorkingCopy()) { // open an editor of the corresponding unit to "show" the // quickfix change JavaUI.openInEditor(compilationUnit); } ASTParser parser = ASTParser.newParser(AST.JLS8); parser.setSource(compilationUnit); if (intValue <= 0) { // try to use the name range of the corresponding element if (handleElement instanceof IMember) { IMember member = (IMember) handleElement; ISourceRange range = member.getNameRange(); if (range != null) { intValue = range.getOffset(); } else { range = member.getSourceRange(); if (range != null && range.getOffset() > 0) { intValue = range.getOffset(); } else { return; } } } else { return; } } parser.setFocalPosition(intValue); parser.setResolveBindings(true); Map<String, String> options = compilationUnit.getJavaProject().getOptions(true); options.put(JavaCore.COMPILER_DOC_COMMENT_SUPPORT, JavaCore.ENABLED); parser.setCompilerOptions(options); final CompilationUnit unit = (CompilationUnit) parser.createAST(new NullProgressMonitor()); BodyDeclaration node = null; NodeFinder nodeFinder = new NodeFinder(intValue); unit.accept(nodeFinder); if (monitor != null) { monitor.worked(1); } node = nodeFinder.getNode(); if (node != null) { unit.recordModifications(); AST ast = unit.getAST(); ASTRewrite rewrite = ASTRewrite.create(ast); if (IApiProblem.SINCE_TAG_MISSING == this.sinceTagType) { Javadoc docnode = node.getJavadoc(); if (docnode == null) { docnode = ast.newJavadoc(); // we do not want to create a new empty Javadoc node // in // the AST if there are no missing tags rewrite.set(node, node.getJavadocProperty(), docnode, null); } else { List<TagElement> tags = docnode.tags(); boolean found = false; loop: for (Iterator<TagElement> iterator = tags.iterator(); iterator.hasNext();) { TagElement element = iterator.next(); String tagName = element.getTagName(); if (TagElement.TAG_SINCE.equals(tagName)) { found = true; break loop; } } if (found) { return; } } ListRewrite lrewrite = rewrite.getListRewrite(docnode, Javadoc.TAGS_PROPERTY); // check the existing tags list TagElement newtag = ast.newTagElement(); newtag.setTagName(TagElement.TAG_SINCE); TextElement textElement = ast.newTextElement(); textElement.setText(this.sinceTagVersion); newtag.fragments().add(textElement); lrewrite.insertLast(newtag, null); } else { Javadoc docnode = node.getJavadoc(); List<TagElement> tags = docnode.tags(); TagElement sinceTag = null; for (Iterator<TagElement> iterator = tags.iterator(); iterator.hasNext();) { TagElement tagElement = iterator.next(); if (TagElement.TAG_SINCE.equals(tagElement.getTagName())) { sinceTag = tagElement; break; } } if (sinceTag != null) { List<TextElement> fragments = sinceTag.fragments(); if (fragments.size() >= 1) { TextElement textElement = fragments.get(0); StringBuffer buffer = new StringBuffer(); buffer.append(' ').append(this.sinceTagVersion); rewrite.set(textElement, TextElement.TEXT_PROPERTY, String.valueOf(buffer), null); } else { ListRewrite lrewrite = rewrite.getListRewrite(docnode, Javadoc.TAGS_PROPERTY); // check the existing tags list TagElement newtag = ast.newTagElement(); newtag.setTagName(TagElement.TAG_SINCE); TextElement textElement = ast.newTextElement(); textElement.setText(this.sinceTagVersion); newtag.fragments().add(textElement); lrewrite.replace(sinceTag, newtag, null); } } } try { if (monitor != null) { monitor.worked(1); } TextEdit edit = rewrite.rewriteAST(); compilationUnit.applyTextEdit(edit, monitor); if (monitor != null) { monitor.worked(1); } } finally { compilationUnit.reconcile(ICompilationUnit.NO_AST, false /* * don * 't * force * problem * detection */, null /* * use * primary * owner */, null /* * no * progress * monitor */); } } } } catch (CoreException e) { ApiUIPlugin.log(e); } finally { if (monitor != null) { monitor.done(); } } }
From source file:org.eclipse.pde.internal.core.JavaElementChangeListener.java
License:Open Source License
private void updateTable(IJavaElement element) { IJavaProject jProject = (IJavaProject) element.getAncestor(IJavaElement.JAVA_PROJECT); if (jProject != null) { IProject project = jProject.getProject(); IPluginModelBase model = PluginRegistry.findModel(project); if (model != null) { String id = model.getPluginBase().getId(); if (id != null) fTable.put(id, Long.toString(System.currentTimeMillis())); }/*from ww w . ja va2 s . c o m*/ } }
From source file:org.eclipse.recommenders.completion.rcp.RecommendersCompletionContext.java
License:Open Source License
@Override public Optional<IType> getClosestEnclosingType() { IJavaElement enclosing = get(ENCLOSING_ELEMENT, null); if (enclosing == null) { return absent(); }/* w w w . ja va 2 s. c o m*/ if (enclosing instanceof IType) { return of((IType) enclosing); } else { final IType type = (IType) enclosing.getAncestor(IJavaElement.TYPE); return fromNullable(type); } }
From source file:org.eclipse.recommenders.internal.apidocs.rcp.StaticHooksProvider.java
License:Open Source License
@JavaSelectionSubscriber public void onJavaElementSelection(final IJavaElement e, final JavaElementSelectionEvent event, final Composite parent) throws ExecutionException { IPackageFragment pkg = (IPackageFragment) e.getAncestor(IJavaElement.PACKAGE_FRAGMENT); if (pkg != null) { onPackageSelection(pkg, event, parent); }//w ww . j a va 2s . c o m }
From source file:org.eclipse.recommenders.internal.models.rcp.ProjectCoordinateProvider.java
License:Open Source License
private Result<ProjectCoordinate> tryToProjectCoordinate(IJavaElement element) { IPackageFragmentRoot root = cast(element.getAncestor(PACKAGE_FRAGMENT_ROOT)); if (root == null) { return Result.absent(); }//from ww w . jav a 2 s. c o m try { DependencyInfo info = dependencyInfoCache.get(root).orNull(); if (info == null) { return Result.absent(); } return pcAdvisorService.trySuggest(info); // Pass-through REASON_NOT_IN_CACHE results } catch (Exception e) { return Result.absent(e); } }
From source file:org.eclipse.recommenders.jdt.JavaElementsFinder.java
License:Open Source License
/** * * @param typeSignature//from w ww . j a v a 2 s.c o m * e.g., QList; * @param enclosing * @return */ public static Optional<ITypeName> resolveType(char[] typeSignature, @Nullable IJavaElement enclosing) { typeSignature = CharOperation.replaceOnCopy(typeSignature, '.', '/'); VmTypeName res = null; try { int dimensions = Signature.getArrayCount(typeSignature); outer: switch (typeSignature[dimensions]) { case Signature.C_BOOLEAN: case Signature.C_BYTE: case Signature.C_CHAR: case Signature.C_DOUBLE: case Signature.C_FLOAT: case Signature.C_INT: case Signature.C_LONG: case Signature.C_SHORT: case Signature.C_VOID: // take the whole string including any arrays res = VmTypeName.get(new String(typeSignature, 0, typeSignature.length)); break; case Signature.C_RESOLVED: // take the whole string including any arrays but remove the trailing ';' res = VmTypeName.get(new String(typeSignature, 0, typeSignature.length - 1 /* ';' */)); break; case Signature.C_UNRESOLVED: if (enclosing == null) { break; } // take the whole string (e.g. QList; or [QList;) String unresolved = new String(typeSignature, dimensions + 1, typeSignature.length - (dimensions + 2 /* 'Q' + ';' */)); IType ancestor = (IType) enclosing.getAncestor(IJavaElement.TYPE); if (ancestor == null) { break; } final String[][] resolvedNames = ancestor.resolveType(unresolved); if (isEmpty(resolvedNames)) { break; } String array = repeat('[', dimensions); final String pkg = resolvedNames[0][0].replace('.', '/'); final String name = resolvedNames[0][1].replace('.', '$'); res = VmTypeName.get(array + 'L' + pkg + '/' + name); break; case Signature.C_TYPE_VARIABLE: String varName = new String(typeSignature, dimensions + 1, typeSignature.length - (dimensions + 2 /* 'Q' + ';' */)); array = repeat('[', dimensions); for (IJavaElement cur = enclosing; cur instanceof IType || cur instanceof IMethod; cur = cur.getParent()) { switch (cur.getElementType()) { case TYPE: { IType type = (IType) cur; ITypeParameter param = type.getTypeParameter(varName); if (param.exists()) { String[] signatures = getBoundSignatures(param); if (isEmpty(signatures)) { res = VmTypeName.OBJECT; break outer; } // XXX we only consider the first type. char[] append = array.concat(signatures[0]).toCharArray(); return resolveType(append, type); } } case METHOD: { IMethod method = (IMethod) cur; ITypeParameter param = method.getTypeParameter(varName); if (param.exists()) { String[] signatures = getBoundSignatures(param); if (isEmpty(signatures)) { res = dimensions == 0 ? OBJECT : VmTypeName.get(repeat('[', dimensions) + OBJECT.getIdentifier()); break outer; } // XXX we only consider the first type. char[] append = array.concat(signatures[0]).toCharArray(); return resolveType(append, method); } } } } break; default: break; } } catch (Exception e) { Logs.log(LogMessages.ERROR_FAILED_TO_CREATE_TYPENAME, e, charToString(typeSignature) + (enclosing != null ? " in " + enclosing.getElementName() : "")); } return Optional.<ITypeName>fromNullable(res); }
From source file:org.eclipse.recommenders.rcp.utils.JdtUtils.java
License:Open Source License
private static IType findClosestTypeOrThis(final IJavaElement parent) { return (IType) (parent instanceof IType ? parent : parent.getAncestor(IJavaElement.TYPE)); }
From source file:org.eclipse.viatra.cep.tooling.ui.wizards.NewVeplFileWizardContainerConfigurationPage.java
License:Open Source License
public void init(IStructuredSelection selection) { IJavaElement jElement = getInitialJavaElement(selection); initContainerPage(jElement);/*from w w w .java2 s . co m*/ if (jElement != null) { IPackageFragment pack = (IPackageFragment) jElement.getAncestor(IJavaElement.PACKAGE_FRAGMENT); setPackageFragment(pack, true); } packageChanged(); }