Search in sources :

Example 41 with Javadoc

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

the class JavadocContentAccess2 method getHTMLContentFromAttachedSource.

private static String getHTMLContentFromAttachedSource(IPackageFragmentRoot root, IPackageFragment packageFragment) throws CoreException {
    String filePath = packageFragment.getElementName().replace('.', '/') + '/' + JavaModelUtil.PACKAGE_INFO_JAVA;
    String contents = getFileContentFromAttachedSource(root, filePath);
    if (contents != null) {
        Javadoc packageJavadocNode = getPackageJavadocNode(packageFragment, contents);
        if (packageJavadocNode != null) {
            return new JavadocContentAccess2(packageFragment, packageJavadocNode, contents).toHTML();
        }
    }
    filePath = packageFragment.getElementName().replace('.', '/') + '/' + JavaModelUtil.PACKAGE_HTML;
    return getFileContentFromAttachedSource(root, filePath);
}
Also used : Javadoc(org.eclipse.jdt.core.dom.Javadoc)

Example 42 with Javadoc

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

the class ChangeUtil method updatePackageStatement.

private static boolean updatePackageStatement(CompilationUnit astCU, String pkgName, ASTRewrite rewriter, ICompilationUnit cu) throws JavaModelException {
    boolean defaultPackage = pkgName.isEmpty();
    AST ast = astCU.getAST();
    if (defaultPackage) {
        // remove existing package statement
        PackageDeclaration pkg = astCU.getPackage();
        if (pkg == null) {
            return false;
        }
        int pkgStart;
        Javadoc javadoc = pkg.getJavadoc();
        if (javadoc != null) {
            pkgStart = javadoc.getStartPosition() + javadoc.getLength() + 1;
        } else {
            pkgStart = pkg.getStartPosition();
        }
        int extendedStart = astCU.getExtendedStartPosition(pkg);
        if (pkgStart != extendedStart) {
            String commentSource = cu.getSource().substring(extendedStart, pkgStart);
            ASTNode comment = rewriter.createStringPlaceholder(commentSource, ASTNode.PACKAGE_DECLARATION);
            rewriter.set(astCU, CompilationUnit.PACKAGE_PROPERTY, comment, null);
        } else {
            rewriter.set(astCU, CompilationUnit.PACKAGE_PROPERTY, null, null);
        }
    } else {
        org.eclipse.jdt.core.dom.PackageDeclaration pkg = astCU.getPackage();
        if (pkg != null) {
            // Skip if the new package name is same as the existing package name.
            if (Objects.equals(pkg.getName().getFullyQualifiedName(), pkgName)) {
                return false;
            }
            // rename package statement
            Name name = ast.newName(pkgName);
            rewriter.set(pkg, PackageDeclaration.NAME_PROPERTY, name, null);
        } else {
            // create new package statement
            pkg = ast.newPackageDeclaration();
            pkg.setName(ast.newName(pkgName));
            rewriter.set(astCU, CompilationUnit.PACKAGE_PROPERTY, pkg, null);
        }
    }
    return true;
}
Also used : PackageDeclaration(org.eclipse.jdt.core.dom.PackageDeclaration) AST(org.eclipse.jdt.core.dom.AST) ASTNode(org.eclipse.jdt.core.dom.ASTNode) Javadoc(org.eclipse.jdt.core.dom.Javadoc) IPackageDeclaration(org.eclipse.jdt.core.IPackageDeclaration) PackageDeclaration(org.eclipse.jdt.core.dom.PackageDeclaration) Name(org.eclipse.jdt.core.dom.Name)

Example 43 with Javadoc

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

the class SelfEncapsulateFieldRefactoring method createGetterMethod.

private MethodDeclaration createGetterMethod(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(fGetterName));
    result.modifiers().addAll(ASTNodeFactory.newModifiers(ast, createModifiers()));
    Type returnType = DimensionRewrite.copyTypeAndAddDimensions(type, fFieldDeclaration.extraDimensions(), rewriter);
    result.setReturnType2(returnType);
    Block block = ast.newBlock();
    result.setBody(block);
    String body = CodeGeneration.getGetterMethodBodyContent(fField.getCompilationUnit(), getTypeName(field.getParent()), fGetterName, fField.getElementName(), lineDelimiter);
    if (body != null) {
        body = body.substring(0, body.lastIndexOf(lineDelimiter));
        ASTNode getterNode = rewriter.createStringPlaceholder(body, ASTNode.BLOCK);
        block.statements().add(getterNode);
    } else {
        ReturnStatement rs = ast.newReturnStatement();
        rs.setExpression(ast.newSimpleName(fField.getElementName()));
        block.statements().add(rs);
    }
    if (fGenerateJavadoc) {
        String string = CodeGeneration.getGetterComment(fField.getCompilationUnit(), getTypeName(field.getParent()), fGetterName, fField.getElementName(), ASTNodes.asString(type), StubUtility.getBaseName(fField), lineDelimiter);
        if (string != null) {
            Javadoc javadoc = (Javadoc) fRewriter.createStringPlaceholder(string, ASTNode.JAVADOC);
            result.setJavadoc(javadoc);
        }
    }
    return result;
}
Also used : IType(org.eclipse.jdt.core.IType) Type(org.eclipse.jdt.core.dom.Type) MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) 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) FieldDeclaration(org.eclipse.jdt.core.dom.FieldDeclaration)

Example 44 with Javadoc

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

the class ExtractMethodRefactoring method createNewMethod.

private MethodDeclaration createNewMethod(ASTNode[] selectedNodes, String lineDelimiter, TextEditGroup substitute) throws CoreException {
    MethodDeclaration result = createNewMethodDeclaration();
    result.setBody(createMethodBody(selectedNodes, substitute, result.getModifiers()));
    if (fGenerateJavadoc) {
        AbstractTypeDeclaration enclosingType = (AbstractTypeDeclaration) ASTNodes.getParent(fAnalyzer.getEnclosingBodyDeclaration(), AbstractTypeDeclaration.class);
        String string = CodeGeneration.getMethodComment(fCUnit, enclosingType.getName().getIdentifier(), result, null, lineDelimiter);
        if (string != null) {
            Javadoc javadoc = (Javadoc) fRewriter.createStringPlaceholder(string, ASTNode.JAVADOC);
            result.setJavadoc(javadoc);
        }
    }
    return result;
}
Also used : MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) Javadoc(org.eclipse.jdt.core.dom.Javadoc) AbstractTypeDeclaration(org.eclipse.jdt.core.dom.AbstractTypeDeclaration)

Example 45 with Javadoc

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

the class TypeChangeCorrectionProposal method getRewrite.

@Override
protected ASTRewrite getRewrite() throws CoreException {
    ASTNode boundNode = fAstRoot.findDeclaringNode(fBinding);
    ASTNode declNode = null;
    CompilationUnit newRoot = fAstRoot;
    if (boundNode != null) {
        // is same CU
        declNode = boundNode;
    } else {
        newRoot = ASTResolving.createQuickFixAST(getCompilationUnit(), null);
        declNode = newRoot.findDeclaringNode(fBinding.getKey());
    }
    if (declNode != null) {
        AST ast = declNode.getAST();
        ASTRewrite rewrite = ASTRewrite.create(ast);
        ImportRewrite imports = createImportRewrite(newRoot);
        ImportRewriteContext context = new ContextSensitiveImportRewriteContext(newRoot, declNode.getStartPosition(), imports);
        Type type = imports.addImport(fNewType, ast, context);
        if (declNode instanceof MethodDeclaration) {
            MethodDeclaration methodDecl = (MethodDeclaration) declNode;
            Type origReturnType = methodDecl.getReturnType2();
            rewrite.set(methodDecl, MethodDeclaration.RETURN_TYPE2_PROPERTY, type, null);
            DimensionRewrite.removeAllChildren(methodDecl, MethodDeclaration.EXTRA_DIMENSIONS2_PROPERTY, rewrite, null);
            // add javadoc tag
            Javadoc javadoc = methodDecl.getJavadoc();
            if (javadoc != null && origReturnType != null && origReturnType.isPrimitiveType() && ((PrimitiveType) origReturnType).getPrimitiveTypeCode() == PrimitiveType.VOID) {
                TagElement returnTag = JavadocTagsSubProcessor.findTag(javadoc, TagElement.TAG_RETURN, null);
                if (returnTag == null) {
                    returnTag = ast.newTagElement();
                    returnTag.setTagName(TagElement.TAG_RETURN);
                    TextElement commentStart = ast.newTextElement();
                    returnTag.fragments().add(commentStart);
                    //$NON-NLS-1$
                    addLinkedPosition(rewrite.track(commentStart), false, "comment_start");
                    ListRewrite tagsRewriter = rewrite.getListRewrite(javadoc, Javadoc.TAGS_PROPERTY);
                    JavadocTagsSubProcessor.insertTag(tagsRewriter, returnTag, null);
                }
            }
        } else if (declNode instanceof AnnotationTypeMemberDeclaration) {
            AnnotationTypeMemberDeclaration methodDecl = (AnnotationTypeMemberDeclaration) declNode;
            rewrite.set(methodDecl, AnnotationTypeMemberDeclaration.TYPE_PROPERTY, type, null);
        } else if (declNode instanceof VariableDeclarationFragment) {
            ASTNode parent = declNode.getParent();
            if (parent instanceof FieldDeclaration) {
                FieldDeclaration fieldDecl = (FieldDeclaration) parent;
                if (fieldDecl.fragments().size() > 1 && (fieldDecl.getParent() instanceof AbstractTypeDeclaration)) {
                    // split
                    VariableDeclarationFragment placeholder = (VariableDeclarationFragment) rewrite.createMoveTarget(declNode);
                    FieldDeclaration newField = ast.newFieldDeclaration(placeholder);
                    newField.setType(type);
                    AbstractTypeDeclaration typeDecl = (AbstractTypeDeclaration) fieldDecl.getParent();
                    ListRewrite listRewrite = rewrite.getListRewrite(typeDecl, typeDecl.getBodyDeclarationsProperty());
                    if (fieldDecl.fragments().indexOf(declNode) == 0) {
                        // if it as the first in the list-> insert before
                        listRewrite.insertBefore(newField, parent, null);
                    } else {
                        listRewrite.insertAfter(newField, parent, null);
                    }
                } else {
                    rewrite.set(fieldDecl, FieldDeclaration.TYPE_PROPERTY, type, null);
                    DimensionRewrite.removeAllChildren(declNode, VariableDeclarationFragment.EXTRA_DIMENSIONS2_PROPERTY, rewrite, null);
                }
            } else if (parent instanceof VariableDeclarationStatement) {
                VariableDeclarationStatement varDecl = (VariableDeclarationStatement) parent;
                if (varDecl.fragments().size() > 1 && (varDecl.getParent() instanceof Block)) {
                    // split
                    VariableDeclarationFragment placeholder = (VariableDeclarationFragment) rewrite.createMoveTarget(declNode);
                    VariableDeclarationStatement newStat = ast.newVariableDeclarationStatement(placeholder);
                    newStat.setType(type);
                    ListRewrite listRewrite = rewrite.getListRewrite(varDecl.getParent(), Block.STATEMENTS_PROPERTY);
                    if (varDecl.fragments().indexOf(declNode) == 0) {
                        // if it as the first in the list-> insert before
                        listRewrite.insertBefore(newStat, parent, null);
                    } else {
                        listRewrite.insertAfter(newStat, parent, null);
                    }
                } else {
                    rewrite.set(varDecl, VariableDeclarationStatement.TYPE_PROPERTY, type, null);
                    DimensionRewrite.removeAllChildren(declNode, VariableDeclarationFragment.EXTRA_DIMENSIONS2_PROPERTY, rewrite, null);
                }
            } else if (parent instanceof VariableDeclarationExpression) {
                VariableDeclarationExpression varDecl = (VariableDeclarationExpression) parent;
                rewrite.set(varDecl, VariableDeclarationExpression.TYPE_PROPERTY, type, null);
                DimensionRewrite.removeAllChildren(declNode, VariableDeclarationFragment.EXTRA_DIMENSIONS2_PROPERTY, rewrite, null);
            }
        } else if (declNode instanceof SingleVariableDeclaration) {
            SingleVariableDeclaration variableDeclaration = (SingleVariableDeclaration) declNode;
            rewrite.set(variableDeclaration, SingleVariableDeclaration.TYPE_PROPERTY, type, null);
            DimensionRewrite.removeAllChildren(declNode, SingleVariableDeclaration.EXTRA_DIMENSIONS2_PROPERTY, rewrite, null);
        }
        // set up linked mode
        //$NON-NLS-1$
        final String KEY_TYPE = "type";
        addLinkedPosition(rewrite.track(type), true, KEY_TYPE);
        if (fTypeProposals != null) {
            for (int i = 0; i < fTypeProposals.length; i++) {
                addLinkedPositionProposal(KEY_TYPE, fTypeProposals[i]);
            }
        }
        return rewrite;
    }
    return null;
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) AST(org.eclipse.jdt.core.dom.AST) ImportRewrite(org.eclipse.jdt.core.dom.rewrite.ImportRewrite) MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) SingleVariableDeclaration(org.eclipse.jdt.core.dom.SingleVariableDeclaration) VariableDeclarationExpression(org.eclipse.jdt.core.dom.VariableDeclarationExpression) Javadoc(org.eclipse.jdt.core.dom.Javadoc) ListRewrite(org.eclipse.jdt.core.dom.rewrite.ListRewrite) FieldDeclaration(org.eclipse.jdt.core.dom.FieldDeclaration) ContextSensitiveImportRewriteContext(org.eclipse.jdt.internal.corext.codemanipulation.ContextSensitiveImportRewriteContext) Type(org.eclipse.jdt.core.dom.Type) PrimitiveType(org.eclipse.jdt.core.dom.PrimitiveType) TextElement(org.eclipse.jdt.core.dom.TextElement) ImportRewriteContext(org.eclipse.jdt.core.dom.rewrite.ImportRewrite.ImportRewriteContext) ContextSensitiveImportRewriteContext(org.eclipse.jdt.internal.corext.codemanipulation.ContextSensitiveImportRewriteContext) AnnotationTypeMemberDeclaration(org.eclipse.jdt.core.dom.AnnotationTypeMemberDeclaration) VariableDeclarationFragment(org.eclipse.jdt.core.dom.VariableDeclarationFragment) ASTNode(org.eclipse.jdt.core.dom.ASTNode) ASTRewrite(org.eclipse.jdt.core.dom.rewrite.ASTRewrite) TagElement(org.eclipse.jdt.core.dom.TagElement) VariableDeclarationStatement(org.eclipse.jdt.core.dom.VariableDeclarationStatement) Block(org.eclipse.jdt.core.dom.Block) PrimitiveType(org.eclipse.jdt.core.dom.PrimitiveType) AbstractTypeDeclaration(org.eclipse.jdt.core.dom.AbstractTypeDeclaration)

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