Search in sources :

Example 16 with MethodDeclaration

use of org.eclipse.jdt.core.dom.MethodDeclaration in project che by eclipse.

the class VarargsWarningsSubProcessor method addRemoveSafeVarargsProposals.

public static void addRemoveSafeVarargsProposals(IInvocationContext context, IProblemLocation problem, Collection<ICommandAccess> proposals) {
    ASTNode coveringNode = problem.getCoveringNode(context.getASTRoot());
    if (!(coveringNode instanceof MethodDeclaration))
        return;
    MethodDeclaration methodDeclaration = (MethodDeclaration) coveringNode;
    MarkerAnnotation annotation = null;
    List<? extends ASTNode> modifiers = methodDeclaration.modifiers();
    for (Iterator<? extends ASTNode> iterator = modifiers.iterator(); iterator.hasNext(); ) {
        ASTNode node = iterator.next();
        if (node instanceof MarkerAnnotation) {
            annotation = (MarkerAnnotation) node;
            if ("SafeVarargs".equals(annotation.resolveAnnotationBinding().getName())) {
                //$NON-NLS-1$
                break;
            }
        }
    }
    if (annotation == null)
        return;
    ASTRewrite rewrite = ASTRewrite.create(coveringNode.getAST());
    rewrite.remove(annotation, null);
    String label = CorrectionMessages.VarargsWarningsSubProcessor_remove_safevarargs_label;
    //JavaPlugin.getDefault().getWorkbench().getSharedImages().getImage(ISharedImages.IMG_TOOL_DELETE);
    Image image = JavaPluginImages.get(JavaPluginImages.IMG_TOOL_DELETE);
    ASTRewriteCorrectionProposal proposal = new ASTRewriteCorrectionProposal(label, context.getCompilationUnit(), rewrite, IProposalRelevance.REMOVE_SAFEVARARGS, image);
    proposals.add(proposal);
}
Also used : ASTRewriteCorrectionProposal(org.eclipse.jdt.ui.text.java.correction.ASTRewriteCorrectionProposal) MarkerAnnotation(org.eclipse.jdt.core.dom.MarkerAnnotation) MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) ASTNode(org.eclipse.jdt.core.dom.ASTNode) ASTRewrite(org.eclipse.jdt.core.dom.rewrite.ASTRewrite) Image(org.eclipse.swt.graphics.Image)

Example 17 with MethodDeclaration

use of org.eclipse.jdt.core.dom.MethodDeclaration in project che by eclipse.

the class AbstractMethodCorrectionProposal method findConstructorInsertIndex.

private int findConstructorInsertIndex(List<BodyDeclaration> decls) {
    int nDecls = decls.size();
    int lastMethod = 0;
    for (int i = nDecls - 1; i >= 0; i--) {
        BodyDeclaration curr = decls.get(i);
        if (curr instanceof MethodDeclaration) {
            if (((MethodDeclaration) curr).isConstructor()) {
                return i + 1;
            }
            lastMethod = i;
        }
    }
    return lastMethod;
}
Also used : MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) BodyDeclaration(org.eclipse.jdt.core.dom.BodyDeclaration)

Example 18 with MethodDeclaration

use of org.eclipse.jdt.core.dom.MethodDeclaration in project che by eclipse.

the class AddTypeParameterProposal method getRewrite.

@Override
protected ASTRewrite getRewrite() throws CoreException {
    ASTNode boundNode = fAstRoot.findDeclaringNode(fBinding);
    ASTNode declNode = null;
    if (boundNode != null) {
        // is same CU
        declNode = boundNode;
        createImportRewrite(fAstRoot);
    } else {
        CompilationUnit newRoot = ASTResolving.createQuickFixAST(getCompilationUnit(), null);
        declNode = newRoot.findDeclaringNode(fBinding.getKey());
        createImportRewrite(newRoot);
    }
    AST ast = declNode.getAST();
    TypeParameter newTypeParam = ast.newTypeParameter();
    newTypeParam.setName(ast.newSimpleName(fTypeParamName));
    if (fBounds != null && fBounds.length > 0) {
        List<Type> typeBounds = newTypeParam.typeBounds();
        ImportRewriteContext importRewriteContext = new ContextSensitiveImportRewriteContext(declNode, getImportRewrite());
        for (int i = 0; i < fBounds.length; i++) {
            Type newBound = getImportRewrite().addImport(fBounds[i], ast, importRewriteContext);
            typeBounds.add(newBound);
        }
    }
    ASTRewrite rewrite = ASTRewrite.create(ast);
    ListRewrite listRewrite;
    Javadoc javadoc;
    List<TypeParameter> otherTypeParams;
    if (declNode instanceof TypeDeclaration) {
        TypeDeclaration declaration = (TypeDeclaration) declNode;
        listRewrite = rewrite.getListRewrite(declaration, TypeDeclaration.TYPE_PARAMETERS_PROPERTY);
        otherTypeParams = declaration.typeParameters();
        javadoc = declaration.getJavadoc();
    } else {
        MethodDeclaration declaration = (MethodDeclaration) declNode;
        listRewrite = rewrite.getListRewrite(declNode, MethodDeclaration.TYPE_PARAMETERS_PROPERTY);
        otherTypeParams = declaration.typeParameters();
        javadoc = declaration.getJavadoc();
    }
    listRewrite.insertLast(newTypeParam, null);
    if (javadoc != null && otherTypeParams != null) {
        ListRewrite tagsRewriter = rewrite.getListRewrite(javadoc, Javadoc.TAGS_PROPERTY);
        Set<String> previousNames = JavadocTagsSubProcessor.getPreviousTypeParamNames(otherTypeParams, null);
        String name = '<' + fTypeParamName + '>';
        TagElement newTag = ast.newTagElement();
        newTag.setTagName(TagElement.TAG_PARAM);
        TextElement text = ast.newTextElement();
        text.setText(name);
        newTag.fragments().add(text);
        JavadocTagsSubProcessor.insertTag(tagsRewriter, newTag, previousNames);
    }
    return rewrite;
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) AST(org.eclipse.jdt.core.dom.AST) TypeParameter(org.eclipse.jdt.core.dom.TypeParameter) MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) Javadoc(org.eclipse.jdt.core.dom.Javadoc) ListRewrite(org.eclipse.jdt.core.dom.rewrite.ListRewrite) Type(org.eclipse.jdt.core.dom.Type) ContextSensitiveImportRewriteContext(org.eclipse.jdt.internal.corext.codemanipulation.ContextSensitiveImportRewriteContext) TextElement(org.eclipse.jdt.core.dom.TextElement) ImportRewriteContext(org.eclipse.jdt.core.dom.rewrite.ImportRewrite.ImportRewriteContext) ContextSensitiveImportRewriteContext(org.eclipse.jdt.internal.corext.codemanipulation.ContextSensitiveImportRewriteContext) ASTNode(org.eclipse.jdt.core.dom.ASTNode) ASTRewrite(org.eclipse.jdt.core.dom.rewrite.ASTRewrite) TagElement(org.eclipse.jdt.core.dom.TagElement) TypeDeclaration(org.eclipse.jdt.core.dom.TypeDeclaration)

Example 19 with MethodDeclaration

use of org.eclipse.jdt.core.dom.MethodDeclaration in project che by eclipse.

the class IntroduceFactoryRefactoring method getTargetNode.

/**
	 * Finds and returns the <code>ASTNode</code> for the given source text
	 * selection, if it is an entire constructor call or the class name portion
	 * of a constructor call or constructor declaration, or null otherwise.
	 * @param unit The compilation unit in which the selection was made
	 * @param offset The textual offset of the start of the selection
	 * @param length The length of the selection in characters
	 * @return ClassInstanceCreation or MethodDeclaration
	 */
private ASTNode getTargetNode(ICompilationUnit unit, int offset, int length) {
    ASTNode node = ASTNodes.getNormalizedNode(NodeFinder.perform(fCU, offset, length));
    if (node.getNodeType() == ASTNode.CLASS_INSTANCE_CREATION)
        return node;
    if (node.getNodeType() == ASTNode.METHOD_DECLARATION && ((MethodDeclaration) node).isConstructor())
        return node;
    // we have some sub node. Make sure its the right child of the parent
    StructuralPropertyDescriptor location = node.getLocationInParent();
    ASTNode parent = node.getParent();
    if (location == ClassInstanceCreation.TYPE_PROPERTY) {
        return parent;
    } else if (location == MethodDeclaration.NAME_PROPERTY && ((MethodDeclaration) parent).isConstructor()) {
        return parent;
    }
    return null;
}
Also used : MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) ASTNode(org.eclipse.jdt.core.dom.ASTNode) StructuralPropertyDescriptor(org.eclipse.jdt.core.dom.StructuralPropertyDescriptor)

Example 20 with MethodDeclaration

use of org.eclipse.jdt.core.dom.MethodDeclaration in project che by eclipse.

the class IntroduceFactoryRefactoring method getCtorCallAt.

/**
	 * Look "in the vicinity" of the given range to find the <code>ClassInstanceCreation</code>
	 * node that this search hit identified. Necessary because the <code>SearchEngine</code>
	 * doesn't always cough up text extents that <code>NodeFinder.perform()</code> agrees with.
	 * @param start
	 * @param length
	 * @param unitAST
	 * @return return a {@link ClassInstanceCreation} or a {@link MethodRef} or <code>null</code> if this is really a constructor->constructor call (e.g. "this(...)")
	 * @throws CoreException
	 */
private ASTNode getCtorCallAt(int start, int length, CompilationUnit unitAST) throws CoreException {
    ICompilationUnit unitHandle = ASTCreator.getCu(unitAST);
    ASTNode node = NodeFinder.perform(unitAST, start, length);
    if (node == null)
        throw new CoreException(JavaUIStatus.createError(IStatus.ERROR, Messages.format(RefactoringCoreMessages.IntroduceFactory_noASTNodeForConstructorSearchHit, new Object[] { Integer.toString(start), Integer.toString(start + length), BasicElementLabels.getJavaCodeString(unitHandle.getSource().substring(start, start + length)), BasicElementLabels.getFileName(unitHandle) }), null));
    if (node instanceof ClassInstanceCreation) {
        if (((ClassInstanceCreation) node).getAnonymousClassDeclaration() != null) {
            // Cannot replace anonymous inner class, see https://bugs.eclipse.org/bugs/show_bug.cgi?id=250660
            fConstructorVisibility = Modifier.PROTECTED;
            return null;
        }
        return node;
    } else if (node instanceof VariableDeclaration) {
        Expression init = ((VariableDeclaration) node).getInitializer();
        if (init instanceof ClassInstanceCreation) {
            return init;
        } else if (init != null)
            throw new CoreException(JavaUIStatus.createError(IStatus.ERROR, Messages.format(RefactoringCoreMessages.IntroduceFactory_unexpectedInitializerNodeType, new Object[] { BasicElementLabels.getJavaCodeString(init.toString()), BasicElementLabels.getFileName(unitHandle) }), null));
        else
            throw new CoreException(JavaUIStatus.createError(IStatus.ERROR, Messages.format(RefactoringCoreMessages.IntroduceFactory_noConstructorCallNodeInsideFoundVarbleDecl, BasicElementLabels.getJavaCodeString(node.toString())), null));
    } else if (node instanceof ConstructorInvocation) {
        // to another flavor on the same class.
        return null;
    } else if (node instanceof SuperConstructorInvocation) {
        // This is a call we can bypass; it's from one constructor flavor
        // to another flavor on the same class.
        fConstructorVisibility = Modifier.PROTECTED;
        return null;
    } else if (node instanceof ExpressionStatement) {
        Expression expr = ((ExpressionStatement) node).getExpression();
        if (expr instanceof ClassInstanceCreation)
            return expr;
        else
            throw new CoreException(JavaUIStatus.createError(IStatus.ERROR, Messages.format(RefactoringCoreMessages.IntroduceFactory_unexpectedASTNodeTypeForConstructorSearchHit, new Object[] { BasicElementLabels.getJavaCodeString(expr.toString()), BasicElementLabels.getFileName(unitHandle) }), null));
    } else if (node instanceof SimpleName && (node.getParent() instanceof MethodDeclaration || node.getParent() instanceof AbstractTypeDeclaration)) {
        // We seem to have been given a hit for an implicit call to the base-class constructor.
        // Do nothing with this (implicit) call, but have to make sure we make the derived class
        // doesn't lose access to the base-class constructor (so make it 'protected', not 'private').
        fConstructorVisibility = Modifier.PROTECTED;
        return null;
    } else if (node instanceof MethodRef) {
        return node;
    } else
        throw new CoreException(JavaUIStatus.createError(IStatus.ERROR, Messages.format(RefactoringCoreMessages.IntroduceFactory_unexpectedASTNodeTypeForConstructorSearchHit, new Object[] { BasicElementLabels.getJavaElementName(node.getClass().getName() + "('" + node.toString() + "')"), BasicElementLabels.getFileName(unitHandle) }), //$NON-NLS-1$ //$NON-NLS-2$
        null));
}
Also used : ClassInstanceCreation(org.eclipse.jdt.core.dom.ClassInstanceCreation) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) SimpleName(org.eclipse.jdt.core.dom.SimpleName) MethodRef(org.eclipse.jdt.core.dom.MethodRef) CoreException(org.eclipse.core.runtime.CoreException) SuperConstructorInvocation(org.eclipse.jdt.core.dom.SuperConstructorInvocation) ConstructorInvocation(org.eclipse.jdt.core.dom.ConstructorInvocation) Expression(org.eclipse.jdt.core.dom.Expression) ExpressionStatement(org.eclipse.jdt.core.dom.ExpressionStatement) ASTNode(org.eclipse.jdt.core.dom.ASTNode) VariableDeclaration(org.eclipse.jdt.core.dom.VariableDeclaration) SingleVariableDeclaration(org.eclipse.jdt.core.dom.SingleVariableDeclaration) SuperConstructorInvocation(org.eclipse.jdt.core.dom.SuperConstructorInvocation) AbstractTypeDeclaration(org.eclipse.jdt.core.dom.AbstractTypeDeclaration)

Aggregations

MethodDeclaration (org.eclipse.jdt.core.dom.MethodDeclaration)131 ASTNode (org.eclipse.jdt.core.dom.ASTNode)80 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)48 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)40 AST (org.eclipse.jdt.core.dom.AST)37 CompilationUnit (org.eclipse.jdt.core.dom.CompilationUnit)35 Type (org.eclipse.jdt.core.dom.Type)35 ASTRewrite (org.eclipse.jdt.core.dom.rewrite.ASTRewrite)35 Block (org.eclipse.jdt.core.dom.Block)33 IMethodBinding (org.eclipse.jdt.core.dom.IMethodBinding)33 Expression (org.eclipse.jdt.core.dom.Expression)30 BodyDeclaration (org.eclipse.jdt.core.dom.BodyDeclaration)29 SimpleName (org.eclipse.jdt.core.dom.SimpleName)28 AbstractTypeDeclaration (org.eclipse.jdt.core.dom.AbstractTypeDeclaration)26 ReturnStatement (org.eclipse.jdt.core.dom.ReturnStatement)23 SingleVariableDeclaration (org.eclipse.jdt.core.dom.SingleVariableDeclaration)23 TypeDeclaration (org.eclipse.jdt.core.dom.TypeDeclaration)20 Javadoc (org.eclipse.jdt.core.dom.Javadoc)19 ASTRewriteCorrectionProposal (org.eclipse.jdt.ui.text.java.correction.ASTRewriteCorrectionProposal)18 ArrayList (java.util.ArrayList)17