Search in sources :

Example 96 with ASTRewrite

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

the class UnresolvedElementsSubProcessor method createTypeRefChangeProposal.

private static CUCorrectionProposal createTypeRefChangeProposal(ICompilationUnit cu, String fullName, Name node, int relevance, int maxProposals) {
    ImportRewrite importRewrite = null;
    String simpleName = fullName;
    String packName = Signature.getQualifier(fullName);
    if (packName.length() > 0) {
        // no imports for primitive types, type variables
        importRewrite = StubUtility.createImportRewrite((CompilationUnit) node.getRoot(), true);
        // can be null in package-info.java
        BodyDeclaration scope = ASTResolving.findParentBodyDeclaration(node);
        ImportRewriteContext context = new ContextSensitiveImportRewriteContext(scope != null ? scope : node, importRewrite);
        simpleName = importRewrite.addImport(fullName, context);
    }
    if (!isLikelyTypeName(simpleName)) {
        relevance -= 2;
    }
    ASTRewriteCorrectionProposal proposal;
    if (importRewrite != null && node.isSimpleName() && simpleName.equals(((SimpleName) node).getIdentifier())) {
        // import only
        // import only
        String[] arg = { BasicElementLabels.getJavaElementName(simpleName), BasicElementLabels.getJavaElementName(packName) };
        String label = Messages.format(CorrectionMessages.UnresolvedElementsSubProcessor_importtype_description, arg);
        Image image = JavaPluginImages.get(JavaPluginImages.IMG_OBJS_IMPDECL);
        int boost = QualifiedTypeNameHistory.getBoost(fullName, 0, maxProposals);
        proposal = new AddImportCorrectionProposal(label, cu, relevance + 100 + boost, image, packName, simpleName, (SimpleName) node);
        proposal.setCommandId(ADD_IMPORT_ID);
    } else {
        String label;
        if (packName.length() == 0) {
            label = Messages.format(CorrectionMessages.UnresolvedElementsSubProcessor_changetype_nopack_description, BasicElementLabels.getJavaElementName(simpleName));
        } else {
            String[] arg = { BasicElementLabels.getJavaElementName(simpleName), BasicElementLabels.getJavaElementName(packName) };
            label = Messages.format(CorrectionMessages.UnresolvedElementsSubProcessor_changetype_description, arg);
        }
        ASTRewrite rewrite = ASTRewrite.create(node.getAST());
        rewrite.replace(node, rewrite.createStringPlaceholder(simpleName, ASTNode.SIMPLE_TYPE), null);
        Image image = JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
        proposal = new ASTRewriteCorrectionProposal(label, cu, rewrite, relevance, image);
    }
    if (importRewrite != null) {
        proposal.setImportRewrite(importRewrite);
    }
    return proposal;
}
Also used : CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) ImportRewrite(org.eclipse.jdt.core.dom.rewrite.ImportRewrite) SimpleName(org.eclipse.jdt.core.dom.SimpleName) Image(org.eclipse.swt.graphics.Image) ASTRewriteCorrectionProposal(org.eclipse.jdt.ui.text.java.correction.ASTRewriteCorrectionProposal) AddImportCorrectionProposal(org.eclipse.jdt.internal.ui.text.correction.proposals.AddImportCorrectionProposal) 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) ASTRewrite(org.eclipse.jdt.core.dom.rewrite.ASTRewrite) BodyDeclaration(org.eclipse.jdt.core.dom.BodyDeclaration)

Example 97 with ASTRewrite

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

the class QuickAssistProcessor method getArrayInitializerToArrayCreation.

private static boolean getArrayInitializerToArrayCreation(IInvocationContext context, ASTNode node, Collection<ICommandAccess> resultingCollections) {
    if (!(node instanceof ArrayInitializer)) {
        return false;
    }
    ArrayInitializer initializer = (ArrayInitializer) node;
    ASTNode parent = initializer.getParent();
    while (parent instanceof ArrayInitializer) {
        initializer = (ArrayInitializer) parent;
        parent = parent.getParent();
    }
    ITypeBinding typeBinding = initializer.resolveTypeBinding();
    if (!(parent instanceof VariableDeclaration) || typeBinding == null || !typeBinding.isArray()) {
        return false;
    }
    if (resultingCollections == null) {
        return true;
    }
    AST ast = node.getAST();
    ASTRewrite rewrite = ASTRewrite.create(ast);
    String label = CorrectionMessages.QuickAssistProcessor_typetoarrayInitializer_description;
    Image image = JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
    LinkedCorrectionProposal proposal = new LinkedCorrectionProposal(label, context.getCompilationUnit(), rewrite, IProposalRelevance.ADD_TYPE_TO_ARRAY_INITIALIZER, image);
    ImportRewrite imports = proposal.createImportRewrite(context.getASTRoot());
    ImportRewriteContext importRewriteContext = new ContextSensitiveImportRewriteContext(node, imports);
    String typeName = imports.addImport(typeBinding, importRewriteContext);
    ArrayCreation creation = ast.newArrayCreation();
    creation.setInitializer((ArrayInitializer) rewrite.createMoveTarget(initializer));
    creation.setType((ArrayType) ASTNodeFactory.newType(ast, typeName));
    rewrite.replace(initializer, creation, null);
    resultingCollections.add(proposal);
    return true;
}
Also used : ImportRewrite(org.eclipse.jdt.core.dom.rewrite.ImportRewrite) Image(org.eclipse.swt.graphics.Image) 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) LinkedCorrectionProposal(org.eclipse.jdt.internal.ui.text.correction.proposals.LinkedCorrectionProposal) ASTRewrite(org.eclipse.jdt.core.dom.rewrite.ASTRewrite)

Example 98 with ASTRewrite

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

the class QuickAssistProcessor method getUnrollMultiCatchProposals.

private static boolean getUnrollMultiCatchProposals(IInvocationContext context, ASTNode covering, Collection<ICommandAccess> resultingCollections) {
    if (!JavaModelUtil.is17OrHigher(context.getCompilationUnit().getJavaProject()))
        return false;
    CatchClause catchClause = (CatchClause) ASTResolving.findAncestor(covering, ASTNode.CATCH_CLAUSE);
    if (catchClause == null) {
        return false;
    }
    Statement statement = ASTResolving.findParentStatement(covering);
    if (statement != catchClause.getParent() && statement != catchClause.getBody()) {
        // selection is in a statement inside the body
        return false;
    }
    Type type1 = catchClause.getException().getType();
    Type selectedMultiCatchType = null;
    if (type1.isUnionType() && covering instanceof Name) {
        Name topMostName = ASTNodes.getTopMostName((Name) covering);
        ASTNode parent = topMostName.getParent();
        if (parent instanceof SimpleType || parent instanceof NameQualifiedType) {
            selectedMultiCatchType = (Type) parent;
        }
    }
    if (selectedMultiCatchType != null)
        return false;
    SingleVariableDeclaration singleVariableDeclaration = catchClause.getException();
    Type type = singleVariableDeclaration.getType();
    if (!(type instanceof UnionType))
        return false;
    if (resultingCollections == null)
        return true;
    AST ast = covering.getAST();
    ASTRewrite rewrite = ASTRewrite.create(ast);
    TryStatement tryStatement = (TryStatement) catchClause.getParent();
    ListRewrite listRewrite = rewrite.getListRewrite(tryStatement, TryStatement.CATCH_CLAUSES_PROPERTY);
    UnionType unionType = (UnionType) type;
    List<Type> types = unionType.types();
    for (int i = types.size() - 1; i >= 0; i--) {
        Type type2 = types.get(i);
        CatchClause newCatchClause = ast.newCatchClause();
        SingleVariableDeclaration newSingleVariableDeclaration = ast.newSingleVariableDeclaration();
        newSingleVariableDeclaration.setType((Type) rewrite.createCopyTarget(type2));
        newSingleVariableDeclaration.setName((SimpleName) rewrite.createCopyTarget(singleVariableDeclaration.getName()));
        newCatchClause.setException(newSingleVariableDeclaration);
        setCatchClauseBody(newCatchClause, rewrite, catchClause);
        listRewrite.insertAfter(newCatchClause, catchClause, null);
    }
    rewrite.remove(catchClause, null);
    Image image = JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
    String label = CorrectionMessages.QuickAssistProcessor_convert_to_multiple_singletype_catch_blocks;
    ASTRewriteCorrectionProposal proposal = new ASTRewriteCorrectionProposal(label, context.getCompilationUnit(), rewrite, IProposalRelevance.USE_SEPARATE_CATCH_BLOCKS, image);
    resultingCollections.add(proposal);
    return true;
}
Also used : ListRewrite(org.eclipse.jdt.core.dom.rewrite.ListRewrite) Image(org.eclipse.swt.graphics.Image) ASTRewriteCorrectionProposal(org.eclipse.jdt.ui.text.java.correction.ASTRewriteCorrectionProposal) IType(org.eclipse.jdt.core.IType) ASTRewrite(org.eclipse.jdt.core.dom.rewrite.ASTRewrite)

Example 99 with ASTRewrite

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

the class ModifierCorrectionSubProcessor method removeOverrideAnnotationProposal.

public static void removeOverrideAnnotationProposal(IInvocationContext context, IProblemLocation problem, Collection<ICommandAccess> proposals) throws CoreException {
    ICompilationUnit cu = context.getCompilationUnit();
    ASTNode selectedNode = problem.getCoveringNode(context.getASTRoot());
    if (!(selectedNode instanceof MethodDeclaration)) {
        return;
    }
    MethodDeclaration methodDecl = (MethodDeclaration) selectedNode;
    //$NON-NLS-1$
    Annotation annot = findAnnotation("java.lang.Override", methodDecl.modifiers());
    if (annot != null) {
        ASTRewrite rewrite = ASTRewrite.create(annot.getAST());
        rewrite.remove(annot, null);
        String label = CorrectionMessages.ModifierCorrectionSubProcessor_remove_override;
        Image image = JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
        ASTRewriteCorrectionProposal proposal = new ASTRewriteCorrectionProposal(label, cu, rewrite, IProposalRelevance.REMOVE_OVERRIDE, image);
        proposals.add(proposal);
        QuickAssistProcessor.getCreateInSuperClassProposals(context, methodDecl.getName(), proposals);
    }
}
Also used : ASTRewriteCorrectionProposal(org.eclipse.jdt.ui.text.java.correction.ASTRewriteCorrectionProposal) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) 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) Annotation(org.eclipse.jdt.core.dom.Annotation)

Example 100 with ASTRewrite

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

the class QuickAssistProcessor method getPickoutTypeFromMulticatchProposals.

private static boolean getPickoutTypeFromMulticatchProposals(IInvocationContext context, ASTNode node, ArrayList<ASTNode> coveredNodes, Collection<ICommandAccess> resultingCollections) {
    CatchClause catchClause = (CatchClause) ASTResolving.findAncestor(node, ASTNode.CATCH_CLAUSE);
    if (catchClause == null) {
        return false;
    }
    Statement statement = ASTResolving.findParentStatement(node);
    if (statement != catchClause.getParent() && statement != catchClause.getBody()) {
        // selection is in a statement inside the body
        return false;
    }
    Type type = catchClause.getException().getType();
    if (!type.isUnionType()) {
        return false;
    }
    Type selectedMultiCatchType = null;
    if (type.isUnionType() && node instanceof Name) {
        Name topMostName = ASTNodes.getTopMostName((Name) node);
        ASTNode parent = topMostName.getParent();
        if (parent instanceof SimpleType || parent instanceof NameQualifiedType) {
            selectedMultiCatchType = (Type) parent;
        }
    }
    boolean multipleExceptions = coveredNodes.size() > 1;
    if ((selectedMultiCatchType == null) && (!(node instanceof UnionType) || !multipleExceptions)) {
        return false;
    }
    if (!multipleExceptions) {
        coveredNodes.add(selectedMultiCatchType);
    }
    BodyDeclaration bodyDeclaration = ASTResolving.findParentBodyDeclaration(catchClause);
    if (!(bodyDeclaration instanceof MethodDeclaration) && !(bodyDeclaration instanceof Initializer)) {
        return false;
    }
    if (resultingCollections == null) {
        return true;
    }
    AST ast = bodyDeclaration.getAST();
    ASTRewrite rewrite = ASTRewrite.create(ast);
    CatchClause newCatchClause = ast.newCatchClause();
    SingleVariableDeclaration newSingleVariableDeclaration = ast.newSingleVariableDeclaration();
    UnionType newUnionType = ast.newUnionType();
    List<Type> types = newUnionType.types();
    for (int i = 0; i < coveredNodes.size(); i++) {
        ASTNode typeNode = coveredNodes.get(i);
        types.add((Type) rewrite.createCopyTarget(typeNode));
        rewrite.remove(typeNode, null);
    }
    newSingleVariableDeclaration.setType(newUnionType);
    newSingleVariableDeclaration.setName((SimpleName) rewrite.createCopyTarget(catchClause.getException().getName()));
    newCatchClause.setException(newSingleVariableDeclaration);
    setCatchClauseBody(newCatchClause, rewrite, catchClause);
    TryStatement tryStatement = (TryStatement) catchClause.getParent();
    ListRewrite listRewrite = rewrite.getListRewrite(tryStatement, TryStatement.CATCH_CLAUSES_PROPERTY);
    listRewrite.insertAfter(newCatchClause, catchClause, null);
    Image image = JavaPluginImages.get(JavaPluginImages.IMG_OBJS_EXCEPTION);
    String label = !multipleExceptions ? CorrectionMessages.QuickAssistProcessor_move_exception_to_separate_catch_block : CorrectionMessages.QuickAssistProcessor_move_exceptions_to_separate_catch_block;
    ASTRewriteCorrectionProposal proposal = new ASTRewriteCorrectionProposal(label, context.getCompilationUnit(), rewrite, IProposalRelevance.MOVE_EXCEPTION_TO_SEPERATE_CATCH_BLOCK, image);
    resultingCollections.add(proposal);
    return true;
}
Also used : ListRewrite(org.eclipse.jdt.core.dom.rewrite.ListRewrite) Image(org.eclipse.swt.graphics.Image) ASTRewriteCorrectionProposal(org.eclipse.jdt.ui.text.java.correction.ASTRewriteCorrectionProposal) IType(org.eclipse.jdt.core.IType) ASTRewrite(org.eclipse.jdt.core.dom.rewrite.ASTRewrite)

Aggregations

ASTRewrite (org.eclipse.jdt.core.dom.rewrite.ASTRewrite)177 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)68 Expression (org.eclipse.jdt.core.dom.Expression)54 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)49 ListRewrite (org.eclipse.jdt.core.dom.rewrite.ListRewrite)43 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 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)29 InfixExpression (org.eclipse.jdt.core.dom.InfixExpression)29 ReturnStatement (org.eclipse.jdt.core.dom.ReturnStatement)29 CompilationUnit (org.eclipse.jdt.core.dom.CompilationUnit)28