Search in sources :

Example 86 with ASTRewrite

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

the class InlineTempRefactoring method inlineTemp.

private void inlineTemp(CompilationUnitRewrite cuRewrite) throws JavaModelException {
    SimpleName[] references = getReferences();
    TextEditGroup groupDesc = cuRewrite.createGroupDescription(RefactoringCoreMessages.InlineTempRefactoring_inline_edit_name);
    ASTRewrite rewrite = cuRewrite.getASTRewrite();
    for (int i = 0; i < references.length; i++) {
        SimpleName curr = references[i];
        ASTNode initializerCopy = getInitializerSource(cuRewrite, curr);
        rewrite.replace(curr, initializerCopy, groupDesc);
    }
}
Also used : SimpleName(org.eclipse.jdt.core.dom.SimpleName) ASTNode(org.eclipse.jdt.core.dom.ASTNode) ASTRewrite(org.eclipse.jdt.core.dom.rewrite.ASTRewrite) TextEditGroup(org.eclipse.text.edits.TextEditGroup)

Example 87 with ASTRewrite

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

the class IntroduceFactoryRefactoring method addAllChangesFor.

/**
	 * Add all changes necessary on the <code>ICompilationUnit</code> in the given
	 * <code>SearchResultGroup</code> to implement the refactoring transformation
	 * to the given <code>CompilationUnitChange</code>.
	 * @param rg the <code>SearchResultGroup</code> for which changes should be created
	 * @param unitHandle
	 * @param unitChange the CompilationUnitChange object for the compilation unit in question
	 * @return <code>true</code> iff a change has been added
	 * @throws CoreException
	 */
private boolean addAllChangesFor(SearchResultGroup rg, ICompilationUnit unitHandle, CompilationUnitChange unitChange) throws CoreException {
    //		ICompilationUnit	unitHandle= rg.getCompilationUnit();
    Assert.isTrue(rg == null || rg.getCompilationUnit() == unitHandle);
    CompilationUnit unit = getASTFor(unitHandle);
    ASTRewrite unitRewriter = ASTRewrite.create(unit.getAST());
    MultiTextEdit root = new MultiTextEdit();
    boolean someChange = false;
    unitChange.setEdit(root);
    fImportRewriter = StubUtility.createImportRewrite(unit, true);
    // First create the factory method
    if (unitHandle.equals(fFactoryUnitHandle)) {
        TextEditGroup factoryGD = new TextEditGroup(RefactoringCoreMessages.IntroduceFactory_addFactoryMethod);
        createFactoryChange(unitRewriter, unit, factoryGD);
        unitChange.addTextEditGroup(factoryGD);
        someChange = true;
    }
    // Now rewrite all the constructor calls to use the factory method
    if (rg != null)
        if (replaceConstructorCalls(rg, unit, unitRewriter, unitChange))
            someChange = true;
    // Finally, make the constructor private, if requested.
    if (shouldProtectConstructor() && isConstructorUnit(unitHandle)) {
        TextEditGroup declGD = new TextEditGroup(RefactoringCoreMessages.IntroduceFactory_protectConstructor);
        if (protectConstructor(unit, unitRewriter, declGD)) {
            unitChange.addTextEditGroup(declGD);
            someChange = true;
        }
    }
    if (someChange) {
        root.addChild(unitRewriter.rewriteAST());
        root.addChild(fImportRewriter.rewriteImports(null));
    }
    return someChange;
}
Also used : CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) ASTRewrite(org.eclipse.jdt.core.dom.rewrite.ASTRewrite) TextEditGroup(org.eclipse.text.edits.TextEditGroup) MultiTextEdit(org.eclipse.text.edits.MultiTextEdit)

Example 88 with ASTRewrite

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

the class ExtractTempRefactoring method createTempType.

private Type createTempType() throws CoreException {
    Expression expression = getSelectedExpression().getAssociatedExpression();
    Type resultingType = null;
    ITypeBinding typeBinding = expression.resolveTypeBinding();
    ASTRewrite rewrite = fCURewrite.getASTRewrite();
    AST ast = rewrite.getAST();
    if (expression instanceof ClassInstanceCreation && (typeBinding == null || typeBinding.getTypeArguments().length == 0)) {
        resultingType = (Type) rewrite.createCopyTarget(((ClassInstanceCreation) expression).getType());
    } else if (expression instanceof CastExpression) {
        resultingType = (Type) rewrite.createCopyTarget(((CastExpression) expression).getType());
    } else {
        if (typeBinding == null) {
            typeBinding = ASTResolving.guessBindingForReference(expression);
        }
        if (typeBinding != null) {
            typeBinding = Bindings.normalizeForDeclarationUse(typeBinding, ast);
            ImportRewrite importRewrite = fCURewrite.getImportRewrite();
            ImportRewriteContext context = new ContextSensitiveImportRewriteContext(expression, importRewrite);
            resultingType = importRewrite.addImport(typeBinding, ast, context);
        } else {
            //$NON-NLS-1$
            resultingType = ast.newSimpleType(ast.newSimpleName("Object"));
        }
    }
    if (fLinkedProposalModel != null) {
        LinkedProposalPositionGroup typeGroup = fLinkedProposalModel.getPositionGroup(KEY_TYPE, true);
        typeGroup.addPosition(rewrite.track(resultingType), false);
        if (typeBinding != null) {
            ITypeBinding[] relaxingTypes = ASTResolving.getNarrowingTypes(ast, typeBinding);
            for (int i = 0; i < relaxingTypes.length; i++) {
                typeGroup.addProposal(relaxingTypes[i], fCURewrite.getCu(), relaxingTypes.length - i);
            }
        }
    }
    return resultingType;
}
Also used : ClassInstanceCreation(org.eclipse.jdt.core.dom.ClassInstanceCreation) AST(org.eclipse.jdt.core.dom.AST) ImportRewrite(org.eclipse.jdt.core.dom.rewrite.ImportRewrite) Type(org.eclipse.jdt.core.dom.Type) ContextSensitiveImportRewriteContext(org.eclipse.jdt.internal.corext.codemanipulation.ContextSensitiveImportRewriteContext) ImportRewriteContext(org.eclipse.jdt.core.dom.rewrite.ImportRewrite.ImportRewriteContext) ContextSensitiveImportRewriteContext(org.eclipse.jdt.internal.corext.codemanipulation.ContextSensitiveImportRewriteContext) Expression(org.eclipse.jdt.core.dom.Expression) CastExpression(org.eclipse.jdt.core.dom.CastExpression) VariableDeclarationExpression(org.eclipse.jdt.core.dom.VariableDeclarationExpression) PostfixExpression(org.eclipse.jdt.core.dom.PostfixExpression) PrefixExpression(org.eclipse.jdt.core.dom.PrefixExpression) ParenthesizedExpression(org.eclipse.jdt.core.dom.ParenthesizedExpression) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) ASTRewrite(org.eclipse.jdt.core.dom.rewrite.ASTRewrite) CastExpression(org.eclipse.jdt.core.dom.CastExpression) LinkedProposalPositionGroup(org.eclipse.jdt.internal.corext.fix.LinkedProposalPositionGroup)

Example 89 with ASTRewrite

use of org.eclipse.jdt.core.dom.rewrite.ASTRewrite in project bayou by capergroup.

the class Visitor method postprocessGlobal.

/**
 * Performs global post-processing of synthesized code:
 * - Adds import declarations
 *
 * @param ast      the owner of the document
 * @param env      environment that was used for synthesis
 * @param document draft program document
 * @throws BadLocationException if an error occurred when rewriting document
 */
private void postprocessGlobal(AST ast, Environment env, Document document) throws BadLocationException {
    /* add imports */
    ASTRewrite rewriter = ASTRewrite.create(ast);
    ListRewrite lrw = rewriter.getListRewrite(cu, CompilationUnit.IMPORTS_PROPERTY);
    Set<Class> toImport = new HashSet<>(env.imports);
    // add all catch(...) types to imports
    toImport.addAll(sketch.exceptionsThrown());
    for (Class cls : toImport) {
        while (cls.isArray()) cls = cls.getComponentType();
        if (cls.isPrimitive() || cls.getPackage().getName().equals("java.lang"))
            continue;
        ImportDeclaration impDecl = cu.getAST().newImportDeclaration();
        String className = cls.getName().replaceAll("\\$", "\\.");
        impDecl.setName(cu.getAST().newName(className.split("\\.")));
        lrw.insertLast(impDecl, null);
    }
    rewriter.rewriteAST(document, null).apply(document);
}
Also used : ASTRewrite(org.eclipse.jdt.core.dom.rewrite.ASTRewrite) ListRewrite(org.eclipse.jdt.core.dom.rewrite.ListRewrite)

Example 90 with ASTRewrite

use of org.eclipse.jdt.core.dom.rewrite.ASTRewrite in project eclipse-pmd by acanda.

the class ASTRewriteQuickFix method fixMarker.

@Override
protected boolean fixMarker(final T node, final IDocument document, final Map<?, ?> options) throws JavaModelException {
    final ASTRewrite rewrite = ASTRewrite.create(node.getAST());
    final boolean isSuccessful = rewrite(node, rewrite);
    if (isSuccessful) {
        rootTextEdit.addChild(rewrite.rewriteAST(document, options));
    }
    return isSuccessful;
}
Also used : ASTRewrite(org.eclipse.jdt.core.dom.rewrite.ASTRewrite)

Aggregations

ASTRewrite (org.eclipse.jdt.core.dom.rewrite.ASTRewrite)180 ASTRewriteCorrectionProposal (org.eclipse.jdt.ui.text.java.correction.ASTRewriteCorrectionProposal)93 Image (org.eclipse.swt.graphics.Image)80 AST (org.eclipse.jdt.core.dom.AST)78 ASTNode (org.eclipse.jdt.core.dom.ASTNode)69 Expression (org.eclipse.jdt.core.dom.Expression)54 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)49 ListRewrite (org.eclipse.jdt.core.dom.rewrite.ListRewrite)44 Block (org.eclipse.jdt.core.dom.Block)38 MethodDeclaration (org.eclipse.jdt.core.dom.MethodDeclaration)35 SimpleName (org.eclipse.jdt.core.dom.SimpleName)34 ImportRewrite (org.eclipse.jdt.core.dom.rewrite.ImportRewrite)34 CastExpression (org.eclipse.jdt.core.dom.CastExpression)33 ParenthesizedExpression (org.eclipse.jdt.core.dom.ParenthesizedExpression)32 Type (org.eclipse.jdt.core.dom.Type)30 ContextSensitiveImportRewriteContext (org.eclipse.jdt.internal.corext.codemanipulation.ContextSensitiveImportRewriteContext)30 CompilationUnit (org.eclipse.jdt.core.dom.CompilationUnit)29 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)29 InfixExpression (org.eclipse.jdt.core.dom.InfixExpression)29 ReturnStatement (org.eclipse.jdt.core.dom.ReturnStatement)29