Search in sources :

Example 76 with SimpleName

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

the class XbaseHoverDocumentationProvider method handleParamTag.

protected void handleParamTag(TagElement tag) {
    @SuppressWarnings("unchecked") List<? extends ASTNode> fragments = tag.fragments();
    int i = 0;
    int size = fragments.size();
    if (size > 0) {
        Object first = fragments.get(0);
        buffer.append(PARAM_NAME_START);
        if (first instanceof SimpleName) {
            String name = ((SimpleName) first).getIdentifier();
            buffer.append(name);
            i++;
        } else if (first instanceof TextElement) {
            String firstText = ((TextElement) first).getText();
            if ("<".equals(firstText)) {
                // $NON-NLS-1$
                // $NON-NLS-1$
                buffer.append("&lt;");
                i++;
                if (size > 1) {
                    Object second = fragments.get(1);
                    if (second instanceof SimpleName) {
                        String name = ((SimpleName) second).getIdentifier();
                        buffer.append(name);
                        i++;
                        if (size > 2) {
                            Object third = fragments.get(2);
                            String thirdText = ((TextElement) third).getText();
                            if (">".equals(thirdText)) {
                                // $NON-NLS-1$
                                // $NON-NLS-1$
                                buffer.append("&gt;");
                                i++;
                            }
                        }
                    }
                }
            }
        }
        buffer.append(PARAM_NAME_END);
        handleContentElements(fragments.subList(i, fragments.size()));
    }
}
Also used : TextElement(org.eclipse.jdt.core.dom.TextElement) SimpleName(org.eclipse.jdt.core.dom.SimpleName) EObject(org.eclipse.emf.ecore.EObject)

Example 77 with SimpleName

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

the class JvmImplementationOpener method openImplementations.

/**
 * Main parts of the logic is taken from {@link org.eclipse.jdt.internal.ui.javaeditor.JavaElementImplementationHyperlink}
 *
 * @param element - Element to show implementations for
 * @param textviewer - Viewer to show hierarchy view on
 * @param region - Region where to show hierarchy view
 */
public void openImplementations(final IJavaElement element, ITextViewer textviewer, IRegion region) {
    if (element instanceof IMethod) {
        ITypeRoot typeRoot = ((IMethod) element).getTypeRoot();
        CompilationUnit ast = SharedASTProvider.getAST(typeRoot, SharedASTProvider.WAIT_YES, null);
        if (ast == null) {
            openQuickHierarchy(textviewer, element, region);
            return;
        }
        try {
            ISourceRange nameRange = ((IMethod) element).getNameRange();
            ASTNode node = NodeFinder.perform(ast, nameRange);
            ITypeBinding parentTypeBinding = null;
            if (node instanceof SimpleName) {
                ASTNode parent = node.getParent();
                if (parent instanceof MethodInvocation) {
                    Expression expression = ((MethodInvocation) parent).getExpression();
                    if (expression == null) {
                        parentTypeBinding = Bindings.getBindingOfParentType(node);
                    } else {
                        parentTypeBinding = expression.resolveTypeBinding();
                    }
                } else if (parent instanceof SuperMethodInvocation) {
                    // Directly go to the super method definition
                    openEditor(element);
                    return;
                } else if (parent instanceof MethodDeclaration) {
                    parentTypeBinding = Bindings.getBindingOfParentType(node);
                }
            }
            final IType type = parentTypeBinding != null ? (IType) parentTypeBinding.getJavaElement() : null;
            if (type == null) {
                openQuickHierarchy(textviewer, element, region);
                return;
            }
            final String earlyExitIndicator = "EarlyExitIndicator";
            final ArrayList<IJavaElement> links = Lists.newArrayList();
            IRunnableWithProgress runnable = new IRunnableWithProgress() {

                @Override
                public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
                    if (monitor == null) {
                        monitor = new NullProgressMonitor();
                    }
                    try {
                        String methodLabel = JavaElementLabels.getElementLabel(element, JavaElementLabels.DEFAULT_QUALIFIED);
                        monitor.beginTask(Messages.format("Searching for implementors of  ''{0}''", methodLabel), 100);
                        SearchRequestor requestor = new SearchRequestor() {

                            @Override
                            public void acceptSearchMatch(SearchMatch match) throws CoreException {
                                if (match.getAccuracy() == SearchMatch.A_ACCURATE) {
                                    IJavaElement element = (IJavaElement) match.getElement();
                                    if (element instanceof IMethod && !JdtFlags.isAbstract((IMethod) element)) {
                                        links.add(element);
                                        if (links.size() > 1) {
                                            throw new OperationCanceledException(earlyExitIndicator);
                                        }
                                    }
                                }
                            }
                        };
                        int limitTo = IJavaSearchConstants.DECLARATIONS | IJavaSearchConstants.IGNORE_DECLARING_TYPE | IJavaSearchConstants.IGNORE_RETURN_TYPE;
                        SearchPattern pattern = SearchPattern.createPattern(element, limitTo);
                        Assert.isNotNull(pattern);
                        SearchParticipant[] participants = new SearchParticipant[] { SearchEngine.getDefaultSearchParticipant() };
                        SearchEngine engine = new SearchEngine();
                        engine.search(pattern, participants, SearchEngine.createHierarchyScope(type), requestor, SubMonitor.convert(monitor, 100));
                        if (monitor.isCanceled()) {
                            throw new InterruptedException();
                        }
                    } catch (OperationCanceledException e) {
                        throw new InterruptedException(e.getMessage());
                    } catch (CoreException e) {
                        throw new InvocationTargetException(e);
                    } finally {
                        monitor.done();
                    }
                }
            };
            try {
                PlatformUI.getWorkbench().getProgressService().busyCursorWhile(runnable);
            } catch (InvocationTargetException e) {
                IStatus status = new Status(IStatus.ERROR, JavaPlugin.getPluginId(), IStatus.OK, Messages.format("An error occurred while searching for implementations of method ''{0}''. See error log for details.", element.getElementName()), e.getCause());
                JavaPlugin.log(status);
                ErrorDialog.openError(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(), "Open Implementation", "Problems finding implementations.", status);
            } catch (InterruptedException e) {
                if (e.getMessage() != earlyExitIndicator) {
                    return;
                }
            }
            if (links.size() == 1) {
                openEditor(links.get(0));
            } else {
                openQuickHierarchy(textviewer, element, region);
            }
        } catch (JavaModelException e) {
            log.error("An error occurred while searching for implementations", e.getCause());
        } catch (PartInitException e) {
            log.error("An error occurred while searching for implementations", e.getCause());
        }
    }
}
Also used : NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) IStatus(org.eclipse.core.runtime.IStatus) JavaModelException(org.eclipse.jdt.core.JavaModelException) SimpleName(org.eclipse.jdt.core.dom.SimpleName) OperationCanceledException(org.eclipse.core.runtime.OperationCanceledException) MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation) SuperMethodInvocation(org.eclipse.jdt.core.dom.SuperMethodInvocation) SuperMethodInvocation(org.eclipse.jdt.core.dom.SuperMethodInvocation) IType(org.eclipse.jdt.core.IType) IRunnableWithProgress(org.eclipse.jface.operation.IRunnableWithProgress) SearchParticipant(org.eclipse.jdt.core.search.SearchParticipant) SearchEngine(org.eclipse.jdt.core.search.SearchEngine) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) ASTNode(org.eclipse.jdt.core.dom.ASTNode) SearchPattern(org.eclipse.jdt.core.search.SearchPattern) IMethod(org.eclipse.jdt.core.IMethod) PartInitException(org.eclipse.ui.PartInitException) ISourceRange(org.eclipse.jdt.core.ISourceRange) CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) IStatus(org.eclipse.core.runtime.IStatus) Status(org.eclipse.core.runtime.Status) IJavaElement(org.eclipse.jdt.core.IJavaElement) SearchMatch(org.eclipse.jdt.core.search.SearchMatch) MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) ITypeRoot(org.eclipse.jdt.core.ITypeRoot) InvocationTargetException(java.lang.reflect.InvocationTargetException) SearchRequestor(org.eclipse.jdt.core.search.SearchRequestor) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) CoreException(org.eclipse.core.runtime.CoreException) Expression(org.eclipse.jdt.core.dom.Expression)

Example 78 with SimpleName

use of org.eclipse.jdt.core.dom.SimpleName in project AutoRefactor by JnRouvignac.

the class PrimitiveWrapperCreationRefactoring method replaceMethodName.

private boolean replaceMethodName(MethodInvocation node, String methodName) {
    final SimpleName name = this.ctx.getASTBuilder().simpleName(methodName);
    this.ctx.getRefactorings().set(node, MethodInvocation.NAME_PROPERTY, name);
    return DO_NOT_VISIT_SUBTREE;
}
Also used : SimpleName(org.eclipse.jdt.core.dom.SimpleName)

Example 79 with SimpleName

use of org.eclipse.jdt.core.dom.SimpleName in project AutoRefactor by JnRouvignac.

the class RemoveUnneededThisExpressionRefactoring method thisExpressionRefersToEnclosingType.

private static boolean thisExpressionRefersToEnclosingType(Name thisQualifierName, ASTNode node) {
    if (thisQualifierName == null) {
        return true;
    }
    final ASTNode enclosingType = getEnclosingType(node);
    if (enclosingType instanceof AnonymousClassDeclaration) {
        return false;
    }
    final AbstractTypeDeclaration ancestor = (AbstractTypeDeclaration) enclosingType;
    if (thisQualifierName instanceof SimpleName) {
        return isEqual((SimpleName) thisQualifierName, ancestor.getName());
    } else if (thisQualifierName instanceof QualifiedName) {
        final QualifiedName qn = (QualifiedName) thisQualifierName;
        return isEqual(qn.getName(), ancestor.getName()) && thisExpressionRefersToEnclosingType(qn.getQualifier(), ancestor);
    }
    throw new NotImplementedException(thisQualifierName);
}
Also used : SimpleName(org.eclipse.jdt.core.dom.SimpleName) QualifiedName(org.eclipse.jdt.core.dom.QualifiedName) NotImplementedException(org.autorefactor.util.NotImplementedException) ASTNode(org.eclipse.jdt.core.dom.ASTNode) AnonymousClassDeclaration(org.eclipse.jdt.core.dom.AnonymousClassDeclaration) AbstractTypeDeclaration(org.eclipse.jdt.core.dom.AbstractTypeDeclaration)

Example 80 with SimpleName

use of org.eclipse.jdt.core.dom.SimpleName in project AutoRefactor by JnRouvignac.

the class AbstractClassSubstituteRefactoring method canVarOccurrenceBeRefactored0.

private boolean canVarOccurrenceBeRefactored0(final Block node, final List<VariableDeclaration> varDecls, final List<MethodInvocation> methodCallsToRefactor, final List<VariableDeclaration> otherVarDecls) {
    for (final VariableDeclaration varDecl : varDecls) {
        final VarOccurrenceVisitor varOccurrenceVisitor = new VarOccurrenceVisitor(varDecl);
        final Statement parent = getAncestorOrNull(varDecl, Statement.class);
        Statement nextSibling = getNextSibling(parent);
        while (nextSibling != null) {
            varOccurrenceVisitor.visitNode(nextSibling);
            nextSibling = getNextSibling(nextSibling);
        }
        if (varOccurrenceVisitor.isUsedInAnnonymousClass()) {
            return false;
        }
        for (final SimpleName varOccurrence : varOccurrenceVisitor.getVarOccurrences()) {
            final List<VariableDeclaration> subVarDecls = new ArrayList<VariableDeclaration>();
            if (!canBeRefactored(node, varOccurrence, varOccurrence.resolveTypeBinding(), subVarDecls, methodCallsToRefactor)) {
                return false;
            }
            otherVarDecls.addAll(subVarDecls);
        }
    }
    return true;
}
Also used : Statement(org.eclipse.jdt.core.dom.Statement) VariableDeclarationStatement(org.eclipse.jdt.core.dom.VariableDeclarationStatement) SimpleName(org.eclipse.jdt.core.dom.SimpleName) ArrayList(java.util.ArrayList) VariableDeclaration(org.eclipse.jdt.core.dom.VariableDeclaration)

Aggregations

SimpleName (org.eclipse.jdt.core.dom.SimpleName)291 ASTNode (org.eclipse.jdt.core.dom.ASTNode)122 IBinding (org.eclipse.jdt.core.dom.IBinding)70 Expression (org.eclipse.jdt.core.dom.Expression)67 AST (org.eclipse.jdt.core.dom.AST)63 ArrayList (java.util.ArrayList)60 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)57 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)55 IVariableBinding (org.eclipse.jdt.core.dom.IVariableBinding)53 MethodInvocation (org.eclipse.jdt.core.dom.MethodInvocation)47 CompilationUnit (org.eclipse.jdt.core.dom.CompilationUnit)46 VariableDeclarationFragment (org.eclipse.jdt.core.dom.VariableDeclarationFragment)44 QualifiedName (org.eclipse.jdt.core.dom.QualifiedName)43 ASTRewrite (org.eclipse.jdt.core.dom.rewrite.ASTRewrite)41 Name (org.eclipse.jdt.core.dom.Name)40 MethodDeclaration (org.eclipse.jdt.core.dom.MethodDeclaration)35 Type (org.eclipse.jdt.core.dom.Type)35 Block (org.eclipse.jdt.core.dom.Block)34 ThisExpression (org.eclipse.jdt.core.dom.ThisExpression)33 Assignment (org.eclipse.jdt.core.dom.Assignment)32