Search in sources :

Example 6 with Type

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

the class ExtractToNullCheckedLocalProposal method getRewrite.

@Override
protected ASTRewrite getRewrite() throws CoreException {
    // infrastructure:
    AST ast = this.compilationUnit.getAST();
    ASTRewrite rewrite = ASTRewrite.create(ast);
    ImportRewrite imports = ImportRewrite.create(this.compilationUnit, true);
    TextEditGroup group = new TextEditGroup(FixMessages.ExtractToNullCheckedLocalProposal_extractCheckedLocal_editName);
    LinkedProposalPositionGroup localNameGroup = new LinkedProposalPositionGroup(LOCAL_NAME_POSITION_GROUP);
    getLinkedProposalModel().addPositionGroup(localNameGroup);
    // AST context:
    Statement origStmt = (Statement) ASTNodes.getParent(this.fieldReference, Statement.class);
    // determine suitable strategy for rearranging elements towards a new code structure:
    RearrangeStrategy rearrangeStrategy = RearrangeStrategy.create(origStmt, rewrite, group);
    Expression toReplace;
    ASTNode directParent = this.fieldReference.getParent();
    if (directParent instanceof FieldAccess) {
        toReplace = (Expression) directParent;
    } else if (directParent instanceof QualifiedName && this.fieldReference.getLocationInParent() == QualifiedName.NAME_PROPERTY) {
        toReplace = (Expression) directParent;
    } else {
        toReplace = this.fieldReference;
    }
    // new local declaration initialized from the field reference
    VariableDeclarationFragment localFrag = ast.newVariableDeclarationFragment();
    VariableDeclarationStatement localDecl = ast.newVariableDeclarationStatement(localFrag);
    // ... type
    localDecl.setType(newType(toReplace.resolveTypeBinding(), ast, imports));
    localDecl.modifiers().add(ast.newModifier(Modifier.ModifierKeyword.FINAL_KEYWORD));
    // ... name
    String localName = proposeLocalName(this.fieldReference, this.compilationUnit, getCompilationUnit().getJavaProject());
    localFrag.setName(ast.newSimpleName(localName));
    // ... initialization
    localFrag.setInitializer((Expression) ASTNode.copySubtree(ast, toReplace));
    rearrangeStrategy.insertLocalDecl(localDecl);
    // if statement:
    IfStatement ifStmt = ast.newIfStatement();
    // condition:
    InfixExpression nullCheck = ast.newInfixExpression();
    nullCheck.setLeftOperand(ast.newSimpleName(localName));
    nullCheck.setRightOperand(ast.newNullLiteral());
    nullCheck.setOperator(InfixExpression.Operator.NOT_EQUALS);
    ifStmt.setExpression(nullCheck);
    // then block: the original statement
    Block thenBlock = ast.newBlock();
    thenBlock.statements().add(rearrangeStrategy.createMoveTargetForOrigStmt());
    ifStmt.setThenStatement(thenBlock);
    // ... but with the field reference replaced by the new local:
    SimpleName dereferencedName = ast.newSimpleName(localName);
    rewrite.replace(toReplace, dereferencedName, group);
    // else block: a Todo comment
    Block elseBlock = ast.newBlock();
    //$NON-NLS-1$
    String elseStatement = "// TODO " + FixMessages.ExtractToNullCheckedLocalProposal_todoHandleNullDescription;
    if (origStmt instanceof ReturnStatement) {
        Type returnType = newType(((ReturnStatement) origStmt).getExpression().resolveTypeBinding(), ast, imports);
        ReturnStatement returnStatement = ast.newReturnStatement();
        returnStatement.setExpression(ASTNodeFactory.newDefaultExpression(ast, returnType, 0));
        elseStatement += '\n' + ASTNodes.asFormattedString(returnStatement, 0, String.valueOf('\n'), getCompilationUnit().getJavaProject().getOptions(true));
    }
    EmptyStatement todoNode = (EmptyStatement) rewrite.createStringPlaceholder(elseStatement, ASTNode.EMPTY_STATEMENT);
    elseBlock.statements().add(todoNode);
    ifStmt.setElseStatement(elseBlock);
    // link all three occurrences of the new local variable:
    addLinkedPosition(rewrite.track(localFrag.getName()), true, /*first*/
    LOCAL_NAME_POSITION_GROUP);
    addLinkedPosition(rewrite.track(nullCheck.getLeftOperand()), false, LOCAL_NAME_POSITION_GROUP);
    addLinkedPosition(rewrite.track(dereferencedName), false, LOCAL_NAME_POSITION_GROUP);
    rearrangeStrategy.insertIfStatement(ifStmt, thenBlock);
    return rewrite;
}
Also used : AST(org.eclipse.jdt.core.dom.AST) ImportRewrite(org.eclipse.jdt.core.dom.rewrite.ImportRewrite) Statement(org.eclipse.jdt.core.dom.Statement) IfStatement(org.eclipse.jdt.core.dom.IfStatement) ReturnStatement(org.eclipse.jdt.core.dom.ReturnStatement) EmptyStatement(org.eclipse.jdt.core.dom.EmptyStatement) VariableDeclarationStatement(org.eclipse.jdt.core.dom.VariableDeclarationStatement) QualifiedName(org.eclipse.jdt.core.dom.QualifiedName) SimpleName(org.eclipse.jdt.core.dom.SimpleName) EmptyStatement(org.eclipse.jdt.core.dom.EmptyStatement) IfStatement(org.eclipse.jdt.core.dom.IfStatement) Type(org.eclipse.jdt.core.dom.Type) PrimitiveType(org.eclipse.jdt.core.dom.PrimitiveType) ParameterizedType(org.eclipse.jdt.core.dom.ParameterizedType) Expression(org.eclipse.jdt.core.dom.Expression) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) VariableDeclarationFragment(org.eclipse.jdt.core.dom.VariableDeclarationFragment) ASTNode(org.eclipse.jdt.core.dom.ASTNode) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) ReturnStatement(org.eclipse.jdt.core.dom.ReturnStatement) ASTRewrite(org.eclipse.jdt.core.dom.rewrite.ASTRewrite) VariableDeclarationStatement(org.eclipse.jdt.core.dom.VariableDeclarationStatement) Block(org.eclipse.jdt.core.dom.Block) FieldAccess(org.eclipse.jdt.core.dom.FieldAccess) TextEditGroup(org.eclipse.text.edits.TextEditGroup) LinkedProposalPositionGroup(org.eclipse.jdt.internal.corext.fix.LinkedProposalPositionGroup)

Example 7 with Type

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

the class NewAnnotationMemberProposal method getStub.

private AnnotationTypeMemberDeclaration getStub(ASTRewrite rewrite, AnnotationTypeDeclaration targetTypeDecl) {
    AST ast = targetTypeDecl.getAST();
    AnnotationTypeMemberDeclaration decl = ast.newAnnotationTypeMemberDeclaration();
    SimpleName newNameNode = getNewName(rewrite);
    decl.modifiers().addAll(ASTNodeFactory.newModifiers(ast, evaluateModifiers(targetTypeDecl)));
    ModifierCorrectionSubProcessor.installLinkedVisibilityProposals(getLinkedProposalModel(), rewrite, decl.modifiers(), true);
    decl.setName(newNameNode);
    Type returnType = getNewType(rewrite);
    decl.setType(returnType);
    return decl;
}
Also used : AST(org.eclipse.jdt.core.dom.AST) Type(org.eclipse.jdt.core.dom.Type) AnnotationTypeMemberDeclaration(org.eclipse.jdt.core.dom.AnnotationTypeMemberDeclaration) SimpleName(org.eclipse.jdt.core.dom.SimpleName)

Example 8 with Type

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

the class NewAnnotationMemberProposal method getNewType.

private Type getNewType(ASTRewrite rewrite) {
    AST ast = rewrite.getAST();
    Type newTypeNode = null;
    ITypeBinding binding = null;
    if (fInvocationNode.getLocationInParent() == MemberValuePair.NAME_PROPERTY) {
        Expression value = ((MemberValuePair) fInvocationNode.getParent()).getValue();
        binding = value.resolveTypeBinding();
    } else if (fInvocationNode instanceof Expression) {
        binding = ((Expression) fInvocationNode).resolveTypeBinding();
    }
    if (binding != null) {
        ImportRewriteContext importRewriteContext = new ContextSensitiveImportRewriteContext(fInvocationNode, getImportRewrite());
        newTypeNode = getImportRewrite().addImport(binding, ast, importRewriteContext);
    }
    if (newTypeNode == null) {
        //$NON-NLS-1$
        newTypeNode = ast.newSimpleType(ast.newSimpleName("String"));
    }
    addLinkedPosition(rewrite.track(newTypeNode), false, KEY_TYPE);
    return newTypeNode;
}
Also used : AST(org.eclipse.jdt.core.dom.AST) Type(org.eclipse.jdt.core.dom.Type) ContextSensitiveImportRewriteContext(org.eclipse.jdt.internal.corext.codemanipulation.ContextSensitiveImportRewriteContext) MemberValuePair(org.eclipse.jdt.core.dom.MemberValuePair) ImportRewriteContext(org.eclipse.jdt.core.dom.rewrite.ImportRewrite.ImportRewriteContext) ContextSensitiveImportRewriteContext(org.eclipse.jdt.internal.corext.codemanipulation.ContextSensitiveImportRewriteContext) Expression(org.eclipse.jdt.core.dom.Expression) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding)

Example 9 with Type

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

the class NewDefiningMethodProposal method addNewExceptions.

/* (non-Javadoc)
	 * @see org.eclipse.jdt.internal.ui.text.correction.proposals.AbstractMethodCorrectionProposal#addNewExceptions(org.eclipse.jdt.core.dom.rewrite.ASTRewrite, java.util.List)
	 */
@Override
protected void addNewExceptions(ASTRewrite rewrite, List<Type> exceptions) throws CoreException {
    AST ast = rewrite.getAST();
    ImportRewrite importRewrite = getImportRewrite();
    ITypeBinding[] bindings = fMethod.getExceptionTypes();
    for (int i = 0; i < bindings.length; i++) {
        Type newType = importRewrite.addImport(bindings[i], ast);
        exceptions.add(newType);
        //$NON-NLS-1$
        addLinkedPosition(rewrite.track(newType), false, "exc_type_" + i);
    }
}
Also used : AST(org.eclipse.jdt.core.dom.AST) Type(org.eclipse.jdt.core.dom.Type) ImportRewrite(org.eclipse.jdt.core.dom.rewrite.ImportRewrite) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding)

Example 10 with Type

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

the class NewMethodCorrectionProposal method getNewMethodType.

/* (non-Javadoc)
	 * @see org.eclipse.jdt.internal.ui.text.correction.proposals.AbstractMethodCorrectionProposal#getNewMethodType(org.eclipse.jdt.core.dom.rewrite.ASTRewrite)
	 */
@Override
protected Type getNewMethodType(ASTRewrite rewrite) throws CoreException {
    ASTNode node = getInvocationNode();
    AST ast = rewrite.getAST();
    Type newTypeNode = null;
    ITypeBinding[] otherProposals = null;
    ImportRewriteContext importRewriteContext = new ContextSensitiveImportRewriteContext(node, getImportRewrite());
    if (node.getParent() instanceof MethodInvocation) {
        MethodInvocation parent = (MethodInvocation) node.getParent();
        if (parent.getExpression() == node) {
            ITypeBinding[] bindings = ASTResolving.getQualifierGuess(node.getRoot(), parent.getName().getIdentifier(), parent.arguments(), getSenderBinding());
            if (bindings.length > 0) {
                newTypeNode = getImportRewrite().addImport(bindings[0], ast, importRewriteContext);
                otherProposals = bindings;
            }
        }
    }
    if (newTypeNode == null) {
        ITypeBinding binding = ASTResolving.guessBindingForReference(node);
        if (binding != null && binding.isWildcardType()) {
            binding = ASTResolving.normalizeWildcardType(binding, false, ast);
        }
        if (binding != null) {
            newTypeNode = getImportRewrite().addImport(binding, ast, importRewriteContext);
        } else {
            ASTNode parent = node.getParent();
            if (parent instanceof ExpressionStatement) {
                newTypeNode = ast.newPrimitiveType(PrimitiveType.VOID);
            } else {
                newTypeNode = ASTResolving.guessTypeForReference(ast, node);
                if (newTypeNode == null) {
                    //$NON-NLS-1$
                    newTypeNode = ast.newSimpleType(ast.newSimpleName("Object"));
                }
            }
        }
    }
    addLinkedPosition(rewrite.track(newTypeNode), false, KEY_TYPE);
    if (otherProposals != null) {
        for (int i = 0; i < otherProposals.length; i++) {
            addLinkedPositionProposal(KEY_TYPE, otherProposals[i]);
        }
    }
    return newTypeNode;
}
Also used : AST(org.eclipse.jdt.core.dom.AST) Type(org.eclipse.jdt.core.dom.Type) PrimitiveType(org.eclipse.jdt.core.dom.PrimitiveType) ParameterizedType(org.eclipse.jdt.core.dom.ParameterizedType) 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) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) ExpressionStatement(org.eclipse.jdt.core.dom.ExpressionStatement) ASTNode(org.eclipse.jdt.core.dom.ASTNode) MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation) SuperMethodInvocation(org.eclipse.jdt.core.dom.SuperMethodInvocation)

Aggregations

Type (org.eclipse.jdt.core.dom.Type)131 ASTNode (org.eclipse.jdt.core.dom.ASTNode)55 PrimitiveType (org.eclipse.jdt.core.dom.PrimitiveType)54 ArrayType (org.eclipse.jdt.core.dom.ArrayType)51 AST (org.eclipse.jdt.core.dom.AST)49 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)49 ParameterizedType (org.eclipse.jdt.core.dom.ParameterizedType)48 Expression (org.eclipse.jdt.core.dom.Expression)41 MethodDeclaration (org.eclipse.jdt.core.dom.MethodDeclaration)35 NameQualifiedType (org.eclipse.jdt.core.dom.NameQualifiedType)34 SimpleType (org.eclipse.jdt.core.dom.SimpleType)34 IType (org.eclipse.jdt.core.IType)33 ASTRewrite (org.eclipse.jdt.core.dom.rewrite.ASTRewrite)30 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)27 SimpleName (org.eclipse.jdt.core.dom.SimpleName)27 CompilationUnit (org.eclipse.jdt.core.dom.CompilationUnit)26 Block (org.eclipse.jdt.core.dom.Block)24 ReturnStatement (org.eclipse.jdt.core.dom.ReturnStatement)23 VariableDeclarationFragment (org.eclipse.jdt.core.dom.VariableDeclarationFragment)22 SingleVariableDeclaration (org.eclipse.jdt.core.dom.SingleVariableDeclaration)20