Search in sources :

Example 31 with ImportRewrite

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

the class CoreTests method newImportsRewrite.

protected ImportRewrite newImportsRewrite(ICompilationUnit cu, String[] order, int normalThreshold, int staticThreshold, boolean restoreExistingImports) throws CoreException {
    ImportRewrite rewrite = StubUtility.createImportRewrite(cu, restoreExistingImports);
    rewrite.setImportOrder(order);
    rewrite.setOnDemandImportThreshold(normalThreshold);
    rewrite.setStaticOnDemandImportThreshold(staticThreshold);
    return rewrite;
}
Also used : ImportRewrite(org.eclipse.jdt.core.dom.rewrite.ImportRewrite)

Example 32 with ImportRewrite

use of org.eclipse.jdt.core.dom.rewrite.ImportRewrite 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)

Example 33 with ImportRewrite

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

the class ImplementInterfaceProposal 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());
    }
    ImportRewrite imports = createImportRewrite(newRoot);
    if (declNode instanceof TypeDeclaration) {
        AST ast = declNode.getAST();
        ASTRewrite rewrite = ASTRewrite.create(ast);
        ImportRewriteContext importRewriteContext = new ContextSensitiveImportRewriteContext(declNode, imports);
        Type newInterface = imports.addImport(fNewInterface, ast, importRewriteContext);
        ListRewrite listRewrite = rewrite.getListRewrite(declNode, TypeDeclaration.SUPER_INTERFACE_TYPES_PROPERTY);
        listRewrite.insertLast(newInterface, null);
        // set up linked mode
        //$NON-NLS-1$
        final String KEY_TYPE = "type";
        addLinkedPosition(rewrite.track(newInterface), true, KEY_TYPE);
        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) ContextSensitiveImportRewriteContext(org.eclipse.jdt.internal.corext.codemanipulation.ContextSensitiveImportRewriteContext) Type(org.eclipse.jdt.core.dom.Type) ImportRewrite(org.eclipse.jdt.core.dom.rewrite.ImportRewrite) 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) ListRewrite(org.eclipse.jdt.core.dom.rewrite.ListRewrite) TypeDeclaration(org.eclipse.jdt.core.dom.TypeDeclaration)

Example 34 with ImportRewrite

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

the class JavaTypeCompletionProposal method apply.

/* (non-Javadoc)
	 * @see ICompletionProposalExtension#apply(IDocument, char, int)
	 */
@Override
public void apply(IDocument document, char trigger, int offset) {
    try {
        ImportRewrite impRewrite = null;
        if (fCompilationUnit != null && allowAddingImports()) {
            impRewrite = StubUtility.createImportRewrite(fCompilationUnit, true);
        }
        boolean updateCursorPosition = updateReplacementString(document, trigger, offset, impRewrite);
        if (updateCursorPosition)
            setCursorPosition(getReplacementString().length());
        super.apply(document, trigger, offset);
        if (impRewrite != null) {
            int oldLen = document.getLength();
            impRewrite.rewriteImports(new NullProgressMonitor()).apply(document, TextEdit.UPDATE_REGIONS);
            setReplacementOffset(getReplacementOffset() + document.getLength() - oldLen);
        }
    } catch (CoreException e) {
        JavaPlugin.log(e);
    } catch (BadLocationException e) {
        JavaPlugin.log(e);
    }
}
Also used : NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) ImportRewrite(org.eclipse.jdt.core.dom.rewrite.ImportRewrite) CoreException(org.eclipse.core.runtime.CoreException) BadLocationException(org.eclipse.jface.text.BadLocationException)

Example 35 with ImportRewrite

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

the class LazyJavaTypeCompletionProposal method createImportRewrite.

private ImportRewrite createImportRewrite() {
    if (fCompilationUnit != null && allowAddingImports() && !JavaModelUtil.isPackageInfo(fCompilationUnit)) {
        try {
            CompilationUnit cu = getASTRoot(fCompilationUnit);
            if (cu == null) {
                ImportRewrite rewrite = StubUtility.createImportRewrite(fCompilationUnit, true);
                fImportContext = null;
                return rewrite;
            } else {
                ImportRewrite rewrite = StubUtility.createImportRewrite(cu, true);
                fImportContext = new ContextSensitiveImportRewriteContext(cu, fInvocationContext.getInvocationOffset(), rewrite);
                return rewrite;
            }
        } catch (CoreException x) {
            JavaPlugin.log(x);
        }
    }
    return null;
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) ContextSensitiveImportRewriteContext(org.eclipse.jdt.internal.corext.codemanipulation.ContextSensitiveImportRewriteContext) ImportRewrite(org.eclipse.jdt.core.dom.rewrite.ImportRewrite) CoreException(org.eclipse.core.runtime.CoreException)

Aggregations

ImportRewrite (org.eclipse.jdt.core.dom.rewrite.ImportRewrite)62 ASTRewrite (org.eclipse.jdt.core.dom.rewrite.ASTRewrite)34 ContextSensitiveImportRewriteContext (org.eclipse.jdt.internal.corext.codemanipulation.ContextSensitiveImportRewriteContext)32 ImportRewriteContext (org.eclipse.jdt.core.dom.rewrite.ImportRewrite.ImportRewriteContext)28 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)27 AST (org.eclipse.jdt.core.dom.AST)25 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)20 CompilationUnit (org.eclipse.jdt.core.dom.CompilationUnit)18 SimpleName (org.eclipse.jdt.core.dom.SimpleName)18 Image (org.eclipse.swt.graphics.Image)17 ASTNode (org.eclipse.jdt.core.dom.ASTNode)16 Type (org.eclipse.jdt.core.dom.Type)15 ASTRewriteCorrectionProposal (org.eclipse.jdt.ui.text.java.correction.ASTRewriteCorrectionProposal)14 ListRewrite (org.eclipse.jdt.core.dom.rewrite.ListRewrite)12 SingleVariableDeclaration (org.eclipse.jdt.core.dom.SingleVariableDeclaration)10 ArrayList (java.util.ArrayList)9 IType (org.eclipse.jdt.core.IType)9 BodyDeclaration (org.eclipse.jdt.core.dom.BodyDeclaration)9 Expression (org.eclipse.jdt.core.dom.Expression)9 MethodDeclaration (org.eclipse.jdt.core.dom.MethodDeclaration)9