Search in sources :

Example 16 with ContextSensitiveImportRewriteContext

use of org.eclipse.jdt.internal.corext.codemanipulation.ContextSensitiveImportRewriteContext 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 17 with ContextSensitiveImportRewriteContext

use of org.eclipse.jdt.internal.corext.codemanipulation.ContextSensitiveImportRewriteContext in project che by eclipse.

the class ExtractMethodAnalyzer method initReturnType.

private void initReturnType(ImportRewrite rewriter) {
    AST ast = fEnclosingBodyDeclaration.getAST();
    fReturnType = null;
    fReturnTypeBinding = null;
    switch(fReturnKind) {
        case ACCESS_TO_LOCAL:
            VariableDeclaration declaration = ASTNodes.findVariableDeclaration(fReturnValue, fEnclosingBodyDeclaration);
            fReturnType = ASTNodeFactory.newType(ast, declaration, rewriter, new ContextSensitiveImportRewriteContext(declaration, rewriter));
            if (declaration.resolveBinding() != null) {
                fReturnTypeBinding = declaration.resolveBinding().getType();
            }
            break;
        case EXPRESSION:
            Expression expression = (Expression) getFirstSelectedNode();
            if (expression.getNodeType() == ASTNode.CLASS_INSTANCE_CREATION) {
                fExpressionBinding = ((ClassInstanceCreation) expression).getType().resolveBinding();
            } else {
                fExpressionBinding = expression.resolveTypeBinding();
            }
            if (fExpressionBinding != null) {
                if (fExpressionBinding.isNullType()) {
                    getStatus().addFatalError(RefactoringCoreMessages.ExtractMethodAnalyzer_cannot_extract_null_type, JavaStatusContext.create(fCUnit, expression));
                } else {
                    ITypeBinding normalizedBinding = Bindings.normalizeForDeclarationUse(fExpressionBinding, ast);
                    if (normalizedBinding != null) {
                        ImportRewriteContext context = new ContextSensitiveImportRewriteContext(fEnclosingBodyDeclaration, rewriter);
                        fReturnType = rewriter.addImport(normalizedBinding, ast, context);
                        fReturnTypeBinding = normalizedBinding;
                    }
                }
            } else {
                fReturnType = ast.newPrimitiveType(PrimitiveType.VOID);
                //$NON-NLS-1$
                fReturnTypeBinding = ast.resolveWellKnownType("void");
                getStatus().addError(RefactoringCoreMessages.ExtractMethodAnalyzer_cannot_determine_return_type, JavaStatusContext.create(fCUnit, expression));
            }
            break;
        case RETURN_STATEMENT_VALUE:
            LambdaExpression enclosingLambdaExpr = ASTResolving.findEnclosingLambdaExpression(getFirstSelectedNode());
            if (enclosingLambdaExpr != null) {
                fReturnType = ASTNodeFactory.newReturnType(enclosingLambdaExpr, ast, rewriter, null);
                IMethodBinding methodBinding = enclosingLambdaExpr.resolveMethodBinding();
                fReturnTypeBinding = methodBinding != null ? methodBinding.getReturnType() : null;
            } else if (fEnclosingBodyDeclaration.getNodeType() == ASTNode.METHOD_DECLARATION) {
                fReturnType = ((MethodDeclaration) fEnclosingBodyDeclaration).getReturnType2();
                fReturnTypeBinding = fReturnType != null ? fReturnType.resolveBinding() : null;
            }
            break;
        default:
            fReturnType = ast.newPrimitiveType(PrimitiveType.VOID);
            //$NON-NLS-1$
            fReturnTypeBinding = ast.resolveWellKnownType("void");
    }
    if (fReturnType == null) {
        fReturnType = ast.newPrimitiveType(PrimitiveType.VOID);
        //$NON-NLS-1$
        fReturnTypeBinding = ast.resolveWellKnownType("void");
    }
}
Also used : ClassInstanceCreation(org.eclipse.jdt.core.dom.ClassInstanceCreation) IMethodBinding(org.eclipse.jdt.core.dom.IMethodBinding) AST(org.eclipse.jdt.core.dom.AST) 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) ThisExpression(org.eclipse.jdt.core.dom.ThisExpression) Expression(org.eclipse.jdt.core.dom.Expression) VariableDeclarationExpression(org.eclipse.jdt.core.dom.VariableDeclarationExpression) LambdaExpression(org.eclipse.jdt.core.dom.LambdaExpression) MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) VariableDeclaration(org.eclipse.jdt.core.dom.VariableDeclaration) LambdaExpression(org.eclipse.jdt.core.dom.LambdaExpression)

Example 18 with ContextSensitiveImportRewriteContext

use of org.eclipse.jdt.internal.corext.codemanipulation.ContextSensitiveImportRewriteContext in project che by eclipse.

the class JavaContext method addStaticImport.

/**
	 * Adds a static import for the member with name <code>qualifiedMemberName</code>. The member is
	 * either a static field or a static method or a '*' to import all static members of a type.
	 *
	 * @param qualifiedMemberName the fully qualified name of the member to import or a qualified type
	 * 			name plus a '.*' suffix.
	 * @return returns either the simple member name if the import was successful or else the qualified name.
	 * @since 3.4
	 */
public String addStaticImport(String qualifiedMemberName) {
    if (isReadOnly())
        return qualifiedMemberName;
    ICompilationUnit cu = getCompilationUnit();
    if (cu == null)
        return qualifiedMemberName;
    int memberOffset = qualifiedMemberName.lastIndexOf('.');
    if (memberOffset == -1)
        return qualifiedMemberName;
    String typeName = qualifiedMemberName.substring(0, memberOffset);
    String memberName = qualifiedMemberName.substring(memberOffset + 1, qualifiedMemberName.length());
    try {
        boolean isField;
        if ("*".equals(memberName)) {
            //$NON-NLS-1$
            isField = true;
        } else {
            IJavaProject javaProject = cu.getJavaProject();
            IType type = javaProject.findType(typeName);
            if (type == null)
                return qualifiedMemberName;
            IField field = type.getField(memberName);
            if (field.exists()) {
                isField = true;
            } else if (hasMethod(type, memberName)) {
                isField = false;
            } else {
                return qualifiedMemberName;
            }
        }
        CompilationUnit root = getASTRoot(cu);
        if (fImportRewrite == null) {
            if (root == null) {
                fImportRewrite = StubUtility.createImportRewrite(cu, true);
            } else {
                fImportRewrite = StubUtility.createImportRewrite(root, true);
            }
        }
        ImportRewriteContext context;
        if (root == null)
            context = null;
        else
            context = new ContextSensitiveImportRewriteContext(root, getCompletionOffset(), fImportRewrite);
        return fImportRewrite.addStaticImport(typeName, memberName, isField, context);
    } catch (JavaModelException e) {
        handleException(null, e);
        return typeName;
    }
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) ContextSensitiveImportRewriteContext(org.eclipse.jdt.internal.corext.codemanipulation.ContextSensitiveImportRewriteContext) JavaModelException(org.eclipse.jdt.core.JavaModelException) IJavaProject(org.eclipse.jdt.core.IJavaProject) ImportRewriteContext(org.eclipse.jdt.core.dom.rewrite.ImportRewrite.ImportRewriteContext) ContextSensitiveImportRewriteContext(org.eclipse.jdt.internal.corext.codemanipulation.ContextSensitiveImportRewriteContext) IField(org.eclipse.jdt.core.IField) IType(org.eclipse.jdt.core.IType)

Example 19 with ContextSensitiveImportRewriteContext

use of org.eclipse.jdt.internal.corext.codemanipulation.ContextSensitiveImportRewriteContext in project che by eclipse.

the class SourceProvider method replaceParameterWithExpression.

private void replaceParameterWithExpression(ASTRewrite rewriter, CallContext context, ImportRewrite importRewrite) throws CoreException {
    Expression[] arguments = context.arguments;
    try {
        ITextFileBuffer buffer = RefactoringFileBuffers.acquire(context.compilationUnit);
        for (int i = 0; i < arguments.length; i++) {
            Expression expression = arguments[i];
            String expressionString = null;
            if (expression instanceof SimpleName) {
                expressionString = ((SimpleName) expression).getIdentifier();
            } else {
                try {
                    expressionString = buffer.getDocument().get(expression.getStartPosition(), expression.getLength());
                } catch (BadLocationException exception) {
                    JavaPlugin.log(exception);
                    continue;
                }
            }
            ParameterData parameter = getParameterData(i);
            List<SimpleName> references = parameter.references();
            for (Iterator<SimpleName> iter = references.iterator(); iter.hasNext(); ) {
                ASTNode element = iter.next();
                Expression newExpression = (Expression) rewriter.createStringPlaceholder(expressionString, expression.getNodeType());
                AST ast = rewriter.getAST();
                ITypeBinding explicitCast = ASTNodes.getExplicitCast(expression, (Expression) element);
                if (explicitCast != null) {
                    CastExpression cast = ast.newCastExpression();
                    if (NecessaryParenthesesChecker.needsParentheses(expression, cast, CastExpression.EXPRESSION_PROPERTY)) {
                        newExpression = createParenthesizedExpression(newExpression, ast);
                    }
                    cast.setExpression(newExpression);
                    ImportRewriteContext importRewriteContext = new ContextSensitiveImportRewriteContext(expression, importRewrite);
                    cast.setType(importRewrite.addImport(explicitCast, ast, importRewriteContext));
                    expression = newExpression = cast;
                }
                if (NecessaryParenthesesChecker.needsParentheses(expression, element.getParent(), element.getLocationInParent())) {
                    newExpression = createParenthesizedExpression(newExpression, ast);
                }
                rewriter.replace(element, newExpression, null);
            }
        }
    } finally {
        RefactoringFileBuffers.release(context.compilationUnit);
    }
}
Also used : AST(org.eclipse.jdt.core.dom.AST) SimpleName(org.eclipse.jdt.core.dom.SimpleName) 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) ConditionalExpression(org.eclipse.jdt.core.dom.ConditionalExpression) ThisExpression(org.eclipse.jdt.core.dom.ThisExpression) Expression(org.eclipse.jdt.core.dom.Expression) CastExpression(org.eclipse.jdt.core.dom.CastExpression) ParenthesizedExpression(org.eclipse.jdt.core.dom.ParenthesizedExpression) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) ITextFileBuffer(org.eclipse.core.filebuffers.ITextFileBuffer) ASTNode(org.eclipse.jdt.core.dom.ASTNode) CastExpression(org.eclipse.jdt.core.dom.CastExpression) BadLocationException(org.eclipse.jface.text.BadLocationException)

Example 20 with ContextSensitiveImportRewriteContext

use of org.eclipse.jdt.internal.corext.codemanipulation.ContextSensitiveImportRewriteContext in project che by eclipse.

the class SourceProvider method updateImplicitReceivers.

private void updateImplicitReceivers(ASTRewrite rewriter, CallContext context) {
    if (context.receiver == null)
        return;
    List<Expression> implicitReceivers = fAnalyzer.getImplicitReceivers();
    for (Iterator<Expression> iter = implicitReceivers.iterator(); iter.hasNext(); ) {
        ASTNode node = iter.next();
        ImportRewriteContext importRewriteContext = new ContextSensitiveImportRewriteContext(node, context.importer);
        if (node instanceof MethodInvocation) {
            final MethodInvocation inv = (MethodInvocation) node;
            rewriter.set(inv, MethodInvocation.EXPRESSION_PROPERTY, createReceiver(rewriter, context, (IMethodBinding) inv.getName().resolveBinding(), importRewriteContext), null);
        } else if (node instanceof ClassInstanceCreation) {
            final ClassInstanceCreation inst = (ClassInstanceCreation) node;
            rewriter.set(inst, ClassInstanceCreation.EXPRESSION_PROPERTY, createReceiver(rewriter, context, inst.resolveConstructorBinding(), importRewriteContext), null);
        } else if (node instanceof ThisExpression) {
            rewriter.replace(node, rewriter.createStringPlaceholder(context.receiver, ASTNode.METHOD_INVOCATION), null);
        } else if (node instanceof FieldAccess) {
            final FieldAccess access = (FieldAccess) node;
            rewriter.set(access, FieldAccess.EXPRESSION_PROPERTY, createReceiver(rewriter, context, access.resolveFieldBinding(), importRewriteContext), null);
        } else if (node instanceof SimpleName && ((SimpleName) node).resolveBinding() instanceof IVariableBinding) {
            IVariableBinding vb = (IVariableBinding) ((SimpleName) node).resolveBinding();
            if (vb.isField()) {
                Expression receiver = createReceiver(rewriter, context, vb, importRewriteContext);
                if (receiver != null) {
                    FieldAccess access = node.getAST().newFieldAccess();
                    ASTNode target = rewriter.createMoveTarget(node);
                    access.setName((SimpleName) target);
                    access.setExpression(receiver);
                    rewriter.replace(node, access, null);
                }
            }
        }
    }
}
Also used : IMethodBinding(org.eclipse.jdt.core.dom.IMethodBinding) ClassInstanceCreation(org.eclipse.jdt.core.dom.ClassInstanceCreation) SimpleName(org.eclipse.jdt.core.dom.SimpleName) MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation) IVariableBinding(org.eclipse.jdt.core.dom.IVariableBinding) ThisExpression(org.eclipse.jdt.core.dom.ThisExpression) 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) ConditionalExpression(org.eclipse.jdt.core.dom.ConditionalExpression) ThisExpression(org.eclipse.jdt.core.dom.ThisExpression) Expression(org.eclipse.jdt.core.dom.Expression) CastExpression(org.eclipse.jdt.core.dom.CastExpression) ParenthesizedExpression(org.eclipse.jdt.core.dom.ParenthesizedExpression) ASTNode(org.eclipse.jdt.core.dom.ASTNode) FieldAccess(org.eclipse.jdt.core.dom.FieldAccess)

Aggregations

ContextSensitiveImportRewriteContext (org.eclipse.jdt.internal.corext.codemanipulation.ContextSensitiveImportRewriteContext)57 ImportRewriteContext (org.eclipse.jdt.core.dom.rewrite.ImportRewrite.ImportRewriteContext)47 AST (org.eclipse.jdt.core.dom.AST)33 ImportRewrite (org.eclipse.jdt.core.dom.rewrite.ImportRewrite)32 ASTRewrite (org.eclipse.jdt.core.dom.rewrite.ASTRewrite)30 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)26 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)23 ASTNode (org.eclipse.jdt.core.dom.ASTNode)21 Type (org.eclipse.jdt.core.dom.Type)19 SimpleName (org.eclipse.jdt.core.dom.SimpleName)18 CompilationUnit (org.eclipse.jdt.core.dom.CompilationUnit)17 MethodDeclaration (org.eclipse.jdt.core.dom.MethodDeclaration)15 ListRewrite (org.eclipse.jdt.core.dom.rewrite.ListRewrite)15 Image (org.eclipse.swt.graphics.Image)14 Expression (org.eclipse.jdt.core.dom.Expression)13 SingleVariableDeclaration (org.eclipse.jdt.core.dom.SingleVariableDeclaration)13 VariableDeclarationFragment (org.eclipse.jdt.core.dom.VariableDeclarationFragment)12 ASTRewriteCorrectionProposal (org.eclipse.jdt.ui.text.java.correction.ASTRewriteCorrectionProposal)11 VariableDeclarationExpression (org.eclipse.jdt.core.dom.VariableDeclarationExpression)9 Assignment (org.eclipse.jdt.core.dom.Assignment)8