Search in sources :

Example 76 with Javadoc

use of org.eclipse.jdt.core.dom.Javadoc in project eclipse.jdt.ls by eclipse.

the class SelfEncapsulateFieldRefactoring method createSetterMethod.

private MethodDeclaration createSetterMethod(AST ast, ASTRewrite rewriter, String lineDelimiter) throws CoreException {
    FieldDeclaration field = ASTNodes.getParent(fFieldDeclaration, FieldDeclaration.class);
    Type type = field.getType();
    MethodDeclaration result = ast.newMethodDeclaration();
    result.setName(ast.newSimpleName(fSetterName));
    result.modifiers().addAll(ASTNodeFactory.newModifiers(ast, createModifiers()));
    if (fSetterMustReturnValue) {
        result.setReturnType2((Type) rewriter.createCopyTarget(type));
    }
    SingleVariableDeclaration param = ast.newSingleVariableDeclaration();
    result.parameters().add(param);
    param.setName(ast.newSimpleName(fArgName));
    param.setType((Type) rewriter.createCopyTarget(type));
    List<Dimension> extraDimensions = DimensionRewrite.copyDimensions(fFieldDeclaration.extraDimensions(), rewriter);
    param.extraDimensions().addAll(extraDimensions);
    Block block = ast.newBlock();
    result.setBody(block);
    String fieldAccess = createFieldAccess();
    String body = CodeGeneration.getSetterMethodBodyContent(fField.getCompilationUnit(), getTypeName(field.getParent()), fSetterName, fieldAccess, fArgName, lineDelimiter);
    if (body != null) {
        body = body.substring(0, body.lastIndexOf(lineDelimiter));
        ASTNode setterNode = rewriter.createStringPlaceholder(body, ASTNode.BLOCK);
        block.statements().add(setterNode);
    } else {
        Assignment ass = ast.newAssignment();
        ass.setLeftHandSide((Expression) rewriter.createStringPlaceholder(fieldAccess, ASTNode.QUALIFIED_NAME));
        ass.setRightHandSide(ast.newSimpleName(fArgName));
        block.statements().add(ass);
    }
    if (fSetterMustReturnValue) {
        ReturnStatement rs = ast.newReturnStatement();
        rs.setExpression(ast.newSimpleName(fArgName));
        block.statements().add(rs);
    }
    if (fGenerateJavadoc) {
        String string = CodeGeneration.getSetterComment(fField.getCompilationUnit(), getTypeName(field.getParent()), fSetterName, fField.getElementName(), ASTNodes.asString(type), fArgName, StubUtility.getBaseName(fField), lineDelimiter);
        if (string != null) {
            Javadoc javadoc = (Javadoc) fRewriter.createStringPlaceholder(string, ASTNode.JAVADOC);
            result.setJavadoc(javadoc);
        }
    }
    return result;
}
Also used : Assignment(org.eclipse.jdt.core.dom.Assignment) IType(org.eclipse.jdt.core.IType) Type(org.eclipse.jdt.core.dom.Type) MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) SingleVariableDeclaration(org.eclipse.jdt.core.dom.SingleVariableDeclaration) ASTNode(org.eclipse.jdt.core.dom.ASTNode) ReturnStatement(org.eclipse.jdt.core.dom.ReturnStatement) Block(org.eclipse.jdt.core.dom.Block) Javadoc(org.eclipse.jdt.core.dom.Javadoc) Dimension(org.eclipse.jdt.core.dom.Dimension) FieldDeclaration(org.eclipse.jdt.core.dom.FieldDeclaration)

Example 77 with Javadoc

use of org.eclipse.jdt.core.dom.Javadoc in project eclipse.jdt.ls by eclipse.

the class ConstructorFromSuperclassProposal method createNewMethodDeclaration.

private MethodDeclaration createNewMethodDeclaration(AST ast, IMethodBinding binding, ASTRewrite rewrite, ImportRewriteContext importRewriteContext, CodeGenerationSettings commentSettings) throws CoreException {
    String name = fTypeNode.getName().getIdentifier();
    MethodDeclaration decl = ast.newMethodDeclaration();
    decl.setConstructor(true);
    decl.setName(ast.newSimpleName(name));
    Block body = ast.newBlock();
    decl.setBody(body);
    SuperConstructorInvocation invocation = null;
    List<SingleVariableDeclaration> parameters = decl.parameters();
    String[] paramNames = getArgumentNames(binding);
    ITypeBinding enclosingInstance = getEnclosingInstance();
    if (enclosingInstance != null) {
        invocation = addEnclosingInstanceAccess(rewrite, importRewriteContext, parameters, paramNames, enclosingInstance);
    }
    if (binding == null) {
        decl.modifiers().add(ast.newModifier(Modifier.ModifierKeyword.PUBLIC_KEYWORD));
    } else {
        decl.modifiers().addAll(ASTNodeFactory.newModifiers(ast, binding.getModifiers()));
        ITypeBinding[] params = binding.getParameterTypes();
        for (int i = 0; i < params.length; i++) {
            SingleVariableDeclaration var = ast.newSingleVariableDeclaration();
            var.setType(getImportRewrite().addImport(params[i], ast, importRewriteContext, TypeLocation.LOCAL_VARIABLE));
            var.setName(ast.newSimpleName(paramNames[i]));
            parameters.add(var);
        }
        List<Type> thrownExceptions = decl.thrownExceptionTypes();
        ITypeBinding[] excTypes = binding.getExceptionTypes();
        for (int i = 0; i < excTypes.length; i++) {
            Type excType = getImportRewrite().addImport(excTypes[i], ast, importRewriteContext, TypeLocation.EXCEPTION);
            thrownExceptions.add(excType);
        }
        if (invocation == null) {
            invocation = ast.newSuperConstructorInvocation();
        }
        List<Expression> arguments = invocation.arguments();
        for (int i = 0; i < paramNames.length; i++) {
            Name argument = ast.newSimpleName(paramNames[i]);
            arguments.add(argument);
            // $NON-NLS-1$
            addLinkedPosition(rewrite.track(argument), false, "arg_name_" + paramNames[i]);
        }
    }
    // $NON-NLS-1$
    String bodyStatement = (invocation == null) ? "" : ASTNodes.asFormattedString(invocation, 0, String.valueOf('\n'), getCompilationUnit().getOptions(true));
    String placeHolder = CodeGeneration.getMethodBodyContent(getCompilationUnit(), name, name, true, bodyStatement, String.valueOf('\n'));
    if (placeHolder != null) {
        ASTNode todoNode = rewrite.createStringPlaceholder(placeHolder, ASTNode.RETURN_STATEMENT);
        body.statements().add(todoNode);
    }
    if (commentSettings != null) {
        String string = CodeGeneration.getMethodComment(getCompilationUnit(), name, decl, null, String.valueOf('\n'));
        if (string != null) {
            Javadoc javadoc = (Javadoc) rewrite.createStringPlaceholder(string, ASTNode.JAVADOC);
            decl.setJavadoc(javadoc);
        }
    }
    return decl;
}
Also used : MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) SingleVariableDeclaration(org.eclipse.jdt.core.dom.SingleVariableDeclaration) Javadoc(org.eclipse.jdt.core.dom.Javadoc) Name(org.eclipse.jdt.core.dom.Name) Type(org.eclipse.jdt.core.dom.Type) Expression(org.eclipse.jdt.core.dom.Expression) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) ASTNode(org.eclipse.jdt.core.dom.ASTNode) Block(org.eclipse.jdt.core.dom.Block) SuperConstructorInvocation(org.eclipse.jdt.core.dom.SuperConstructorInvocation)

Example 78 with Javadoc

use of org.eclipse.jdt.core.dom.Javadoc in project eclipse.jdt.ls by eclipse.

the class SimilarElementsRequestor method findSimilarElement.

public static SimilarElement[] findSimilarElement(ICompilationUnit cu, Name name, int kind) throws JavaModelException {
    int pos = name.getStartPosition();
    int nArguments = -1;
    String identifier = ASTNodes.getSimpleNameIdentifier(name);
    String returnType = null;
    ICompilationUnit preparedCU = null;
    try {
        if (name.isQualifiedName()) {
            pos = ((QualifiedName) name).getName().getStartPosition();
        } else {
            // first letter must be included, other
            pos = name.getStartPosition() + 1;
        }
        Javadoc javadoc = (Javadoc) ASTNodes.getParent(name, ASTNode.JAVADOC);
        if (javadoc != null) {
            preparedCU = createPreparedCU(cu, javadoc, name.getStartPosition());
            cu = preparedCU;
        }
        SimilarElementsRequestor requestor = new SimilarElementsRequestor(identifier, kind, nArguments, returnType, !isTestSource(cu));
        requestor.setIgnored(CompletionProposal.ANONYMOUS_CLASS_DECLARATION, true);
        requestor.setIgnored(CompletionProposal.ANONYMOUS_CLASS_CONSTRUCTOR_INVOCATION, true);
        requestor.setIgnored(CompletionProposal.KEYWORD, true);
        requestor.setIgnored(CompletionProposal.LABEL_REF, true);
        requestor.setIgnored(CompletionProposal.METHOD_DECLARATION, true);
        requestor.setIgnored(CompletionProposal.PACKAGE_REF, true);
        requestor.setIgnored(CompletionProposal.MODULE_REF, true);
        requestor.setIgnored(CompletionProposal.MODULE_DECLARATION, true);
        requestor.setIgnored(CompletionProposal.VARIABLE_DECLARATION, true);
        requestor.setIgnored(CompletionProposal.METHOD_REF, true);
        requestor.setIgnored(CompletionProposal.CONSTRUCTOR_INVOCATION, true);
        requestor.setIgnored(CompletionProposal.METHOD_REF_WITH_CASTED_RECEIVER, true);
        requestor.setIgnored(CompletionProposal.FIELD_REF, true);
        requestor.setIgnored(CompletionProposal.FIELD_REF_WITH_CASTED_RECEIVER, true);
        requestor.setIgnored(CompletionProposal.LOCAL_VARIABLE_REF, true);
        requestor.setIgnored(CompletionProposal.VARIABLE_DECLARATION, true);
        requestor.setIgnored(CompletionProposal.VARIABLE_DECLARATION, true);
        requestor.setIgnored(CompletionProposal.POTENTIAL_METHOD_DECLARATION, true);
        requestor.setIgnored(CompletionProposal.METHOD_NAME_REFERENCE, true);
        return requestor.process(cu, pos);
    } finally {
        if (preparedCU != null) {
            preparedCU.discardWorkingCopy();
        }
    }
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) QualifiedName(org.eclipse.jdt.core.dom.QualifiedName) Javadoc(org.eclipse.jdt.core.dom.Javadoc)

Example 79 with Javadoc

use of org.eclipse.jdt.core.dom.Javadoc in project eclipse.jdt.ls by eclipse.

the class ReturnTypeSubProcessor method addMissingReturnStatementProposals.

public static void addMissingReturnStatementProposals(IInvocationContext context, IProblemLocationCore problem, Collection<ChangeCorrectionProposal> proposals) {
    ICompilationUnit cu = context.getCompilationUnit();
    ASTNode selectedNode = problem.getCoveringNode(context.getASTRoot());
    if (selectedNode == null) {
        return;
    }
    ReturnStatement existingStatement = (selectedNode instanceof ReturnStatement) ? (ReturnStatement) selectedNode : null;
    // Lambda Expression can be in a MethodDeclaration or a Field Declaration
    if (selectedNode instanceof LambdaExpression) {
        MissingReturnTypeInLambdaCorrectionProposal proposal = new MissingReturnTypeInLambdaCorrectionProposal(cu, (LambdaExpression) selectedNode, existingStatement, IProposalRelevance.MISSING_RETURN_TYPE);
        proposals.add(proposal);
    } else {
        BodyDeclaration decl = ASTResolving.findParentBodyDeclaration(selectedNode);
        if (decl instanceof MethodDeclaration) {
            MethodDeclaration methodDecl = (MethodDeclaration) decl;
            Block block = methodDecl.getBody();
            if (block == null) {
                return;
            }
            proposals.add(new MissingReturnTypeCorrectionProposal(cu, methodDecl, existingStatement, IProposalRelevance.MISSING_RETURN_TYPE));
            Type returnType = methodDecl.getReturnType2();
            if (returnType != null && !"void".equals(ASTNodes.asString(returnType))) {
                // $NON-NLS-1$
                AST ast = methodDecl.getAST();
                ASTRewrite rewrite = ASTRewrite.create(ast);
                rewrite.replace(returnType, ast.newPrimitiveType(PrimitiveType.VOID), null);
                Javadoc javadoc = methodDecl.getJavadoc();
                if (javadoc != null) {
                    TagElement tagElement = JavadocTagsSubProcessor.findTag(javadoc, TagElement.TAG_RETURN, null);
                    if (tagElement != null) {
                        rewrite.remove(tagElement, null);
                    }
                }
                String label = CorrectionMessages.ReturnTypeSubProcessor_changetovoid_description;
                ASTRewriteCorrectionProposal proposal = new ASTRewriteCorrectionProposal(label, CodeActionKind.QuickFix, cu, rewrite, IProposalRelevance.CHANGE_RETURN_TYPE_TO_VOID);
                proposals.add(proposal);
            }
        }
    }
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) AST(org.eclipse.jdt.core.dom.AST) MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) MissingReturnTypeInLambdaCorrectionProposal(org.eclipse.jdt.ls.core.internal.corrections.proposals.MissingReturnTypeInLambdaCorrectionProposal) Javadoc(org.eclipse.jdt.core.dom.Javadoc) ASTRewriteCorrectionProposal(org.eclipse.jdt.ls.core.internal.corrections.proposals.ASTRewriteCorrectionProposal) Type(org.eclipse.jdt.core.dom.Type) PrimitiveType(org.eclipse.jdt.core.dom.PrimitiveType) MissingReturnTypeCorrectionProposal(org.eclipse.jdt.ls.core.internal.corrections.proposals.MissingReturnTypeCorrectionProposal) ASTNode(org.eclipse.jdt.core.dom.ASTNode) ReturnStatement(org.eclipse.jdt.core.dom.ReturnStatement) Block(org.eclipse.jdt.core.dom.Block) ASTRewrite(org.eclipse.jdt.core.dom.rewrite.ASTRewrite) TagElement(org.eclipse.jdt.core.dom.TagElement) BodyDeclaration(org.eclipse.jdt.core.dom.BodyDeclaration) LambdaExpression(org.eclipse.jdt.core.dom.LambdaExpression)

Aggregations

Javadoc (org.eclipse.jdt.core.dom.Javadoc)79 ASTNode (org.eclipse.jdt.core.dom.ASTNode)39 AST (org.eclipse.jdt.core.dom.AST)37 MethodDeclaration (org.eclipse.jdt.core.dom.MethodDeclaration)37 Type (org.eclipse.jdt.core.dom.Type)31 TagElement (org.eclipse.jdt.core.dom.TagElement)28 ASTRewrite (org.eclipse.jdt.core.dom.rewrite.ASTRewrite)23 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)20 Block (org.eclipse.jdt.core.dom.Block)19 SingleVariableDeclaration (org.eclipse.jdt.core.dom.SingleVariableDeclaration)18 ImportRewriteContext (org.eclipse.jdt.core.dom.rewrite.ImportRewrite.ImportRewriteContext)18 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)17 PrimitiveType (org.eclipse.jdt.core.dom.PrimitiveType)17 AbstractTypeDeclaration (org.eclipse.jdt.core.dom.AbstractTypeDeclaration)16 BodyDeclaration (org.eclipse.jdt.core.dom.BodyDeclaration)16 ReturnStatement (org.eclipse.jdt.core.dom.ReturnStatement)16 ImportRewrite (org.eclipse.jdt.core.dom.rewrite.ImportRewrite)16 ContextSensitiveImportRewriteContext (org.eclipse.jdt.internal.corext.codemanipulation.ContextSensitiveImportRewriteContext)16 CompilationUnit (org.eclipse.jdt.core.dom.CompilationUnit)15 ListRewrite (org.eclipse.jdt.core.dom.rewrite.ListRewrite)14