Search in sources :

Example 21 with ASTNode

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

the class MissingReturnTypeCorrectionProposal method getRewrite.

/*(non-Javadoc)
	 * @see org.eclipse.jdt.internal.ui.text.correction.ASTRewriteCorrectionProposal#getRewrite()
	 */
@Override
protected ASTRewrite getRewrite() {
    AST ast = getAST();
    ITypeBinding returnBinding = getReturnTypeBinding();
    if (fExistingReturn != null) {
        ASTRewrite rewrite = ASTRewrite.create(ast);
        Expression expression = evaluateReturnExpressions(ast, returnBinding, fExistingReturn.getStartPosition());
        if (expression != null) {
            rewrite.set(fExistingReturn, ReturnStatement.EXPRESSION_PROPERTY, expression, null);
            addLinkedPosition(rewrite.track(expression), true, RETURN_EXPRESSION_KEY);
        }
        return rewrite;
    } else {
        ASTRewrite rewrite = ASTRewrite.create(ast);
        ASTNode body = getBody();
        // For lambda the body can be a block or an expression.
        if (body instanceof Block) {
            Block block = (Block) body;
            List<Statement> statements = block.statements();
            int nStatements = statements.size();
            ASTNode lastStatement = null;
            if (nStatements > 0) {
                lastStatement = statements.get(nStatements - 1);
            }
            if (returnBinding != null && lastStatement instanceof ExpressionStatement && lastStatement.getNodeType() != ASTNode.ASSIGNMENT) {
                Expression expression = ((ExpressionStatement) lastStatement).getExpression();
                ITypeBinding binding = expression.resolveTypeBinding();
                if (binding != null && binding.isAssignmentCompatible(returnBinding)) {
                    Expression placeHolder = (Expression) rewrite.createMoveTarget(expression);
                    ReturnStatement returnStatement = ast.newReturnStatement();
                    returnStatement.setExpression(placeHolder);
                    rewrite.replace(lastStatement, returnStatement, null);
                    return rewrite;
                }
            }
            int offset;
            if (lastStatement == null) {
                offset = block.getStartPosition() + 1;
            } else {
                offset = lastStatement.getStartPosition() + lastStatement.getLength();
            }
            ReturnStatement returnStatement = ast.newReturnStatement();
            Expression expression = evaluateReturnExpressions(ast, returnBinding, offset);
            returnStatement.setExpression(expression);
            rewrite.getListRewrite(block, Block.STATEMENTS_PROPERTY).insertLast(returnStatement, null);
            addLinkedPosition(rewrite.track(returnStatement.getExpression()), true, RETURN_EXPRESSION_KEY);
        }
        return rewrite;
    }
}
Also used : AST(org.eclipse.jdt.core.dom.AST) Expression(org.eclipse.jdt.core.dom.Expression) ExpressionStatement(org.eclipse.jdt.core.dom.ExpressionStatement) Statement(org.eclipse.jdt.core.dom.Statement) ReturnStatement(org.eclipse.jdt.core.dom.ReturnStatement) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) ExpressionStatement(org.eclipse.jdt.core.dom.ExpressionStatement) ASTNode(org.eclipse.jdt.core.dom.ASTNode) ReturnStatement(org.eclipse.jdt.core.dom.ReturnStatement) ASTRewrite(org.eclipse.jdt.core.dom.rewrite.ASTRewrite) Block(org.eclipse.jdt.core.dom.Block)

Example 22 with ASTNode

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

the class NewCUUsingWizardProposal method getTypeName.

private static String getTypeName(int typeKind, Name node) {
    String name = ASTNodes.getSimpleNameIdentifier(node);
    if (typeKind == K_CLASS || typeKind == K_INTERFACE) {
        ASTNode parent = node.getParent();
        if (parent.getLocationInParent() == ParameterizedType.TYPE_PROPERTY) {
            // use 'S' or 'T'
            String typeArgBaseName = name.startsWith(String.valueOf('T')) ? String.valueOf('S') : String.valueOf('T');
            int nTypeArgs = ((ParameterizedType) parent.getParent()).typeArguments().size();
            StringBuffer buf = new StringBuffer(name);
            buf.append('<');
            if (nTypeArgs == 1) {
                buf.append(typeArgBaseName);
            } else {
                for (int i = 0; i < nTypeArgs; i++) {
                    if (i != 0)
                        //$NON-NLS-1$
                        buf.append(", ");
                    buf.append(typeArgBaseName).append(i + 1);
                }
            }
            buf.append('>');
            return buf.toString();
        }
    }
    return name;
}
Also used : ASTNode(org.eclipse.jdt.core.dom.ASTNode)

Example 23 with ASTNode

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

the class NewCUUsingWizardProposal method getPossibleSuperTypeBinding.

//	private NewTypeWizardPage getPage(NewElementWizard wizard) {
//		IWizardPage[] pages= wizard.getPages();
//		Assert.isTrue(pages.length > 0 && pages[0] instanceof NewTypeWizardPage);
//		return (NewTypeWizardPage) pages[0];
//	}
//
//	private NewElementWizard createWizard(StructuredSelection selection) {
//		switch (fTypeKind) {
//			case K_CLASS: {
//				NewClassWizardPage page=  new NewClassWizardPage();
//				page.init(selection);
//				configureWizardPage(page);
//				return new NewClassCreationWizard(page, true);
//			}
//			case K_INTERFACE: {
//				NewInterfaceWizardPage page=  new NewInterfaceWizardPage();
//				page.init(selection);
//				configureWizardPage(page);
//				return new NewInterfaceCreationWizard(page, true);
//			}
//			case K_ENUM: {
//				NewEnumWizardPage page=  new NewEnumWizardPage();
//				page.init(selection);
//				configureWizardPage(page);
//				return new NewEnumCreationWizard(page, true);
//			}
//			case K_ANNOTATION: {
//				NewAnnotationWizardPage page=  new NewAnnotationWizardPage();
//				page.init(selection);
//				configureWizardPage(page);
//				return new NewAnnotationCreationWizard(page, true);
//			}
//		}
//		throw new IllegalArgumentException();
//	}
//
//	private void configureWizardPage(NewTypeWizardPage page) {
//		fillInWizardPageName(page);
//		fillInWizardPageSuperTypes(page);
//	}
//
//	/**
//	 * Fill-in the "Package" and "Name" fields.
//	 * @param page the wizard page.
//	 */
//	private void fillInWizardPageName(NewTypeWizardPage page) {
//		// allow to edit when there are type parameters
//		page.setTypeName(fTypeNameWithParameters, fTypeNameWithParameters.indexOf('<') != -1);
//
//		boolean isInEnclosingType= fTypeContainer instanceof IType;
//		if (isInEnclosingType) {
//			page.setEnclosingType((IType) fTypeContainer, true);
//		} else {
//			page.setPackageFragment((IPackageFragment) fTypeContainer, true);
//		}
//		page.setEnclosingTypeSelection(isInEnclosingType, true);
//	}
//
//	/**
//	 * Fill-in the "Super Class" and "Super Interfaces" fields.
//	 * @param page the wizard page.
//	 */
//	private void fillInWizardPageSuperTypes(NewTypeWizardPage page) {
//		ITypeBinding type= getPossibleSuperTypeBinding(fNode);
//		type= Bindings.normalizeTypeBinding(type);
//		if (type != null) {
//			if (type.isArray()) {
//				type= type.getElementType();
//			}
//			if (type.isTopLevel() || type.isMember()) {
//				if (type.isClass() && (fTypeKind == K_CLASS)) {
//					page.setSuperClass(type.getQualifiedName(), true);
//				} else if (type.isInterface()) {
//					List<String> superInterfaces= new ArrayList<String>();
//					superInterfaces.add(type.getQualifiedName());
//					page.setSuperInterfaces(superInterfaces, true);
//				}
//			}
//		}
//	}
private ITypeBinding getPossibleSuperTypeBinding(ASTNode node) {
    if (fTypeKind == K_ANNOTATION) {
        return null;
    }
    AST ast = node.getAST();
    node = ASTNodes.getNormalizedNode(node);
    ASTNode parent = node.getParent();
    switch(parent.getNodeType()) {
        case ASTNode.METHOD_DECLARATION:
            if (node.getLocationInParent() == MethodDeclaration.THROWN_EXCEPTION_TYPES_PROPERTY) {
                //$NON-NLS-1$
                return ast.resolveWellKnownType("java.lang.Exception");
            }
            break;
        case ASTNode.THROW_STATEMENT:
            //$NON-NLS-1$
            return ast.resolveWellKnownType("java.lang.Exception");
        case ASTNode.SINGLE_VARIABLE_DECLARATION:
            if (parent.getLocationInParent() == CatchClause.EXCEPTION_PROPERTY) {
                //$NON-NLS-1$
                return ast.resolveWellKnownType("java.lang.Exception");
            }
            break;
        case ASTNode.VARIABLE_DECLARATION_STATEMENT:
        case ASTNode.FIELD_DECLARATION:
            // no guessing for LHS types, cannot be a supertype of a known type
            return null;
        case ASTNode.PARAMETERIZED_TYPE:
            // Inheritance doesn't help: A<X> z= new A<String>(); ->
            return null;
    }
    ITypeBinding binding = ASTResolving.guessBindingForTypeReference(node);
    if (binding != null && !binding.isRecovered()) {
        return binding;
    }
    return null;
}
Also used : AST(org.eclipse.jdt.core.dom.AST) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) ASTNode(org.eclipse.jdt.core.dom.ASTNode)

Example 24 with ASTNode

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

the class NewMethodCorrectionProposal method evaluateModifiers.

private int evaluateModifiers(ASTNode targetTypeDecl) {
    if (getSenderBinding().isAnnotation()) {
        return 0;
    }
    if (getSenderBinding().isInterface()) {
        // for interface and annotation members copy the modifiers from an existing field
        MethodDeclaration[] methodDecls = ((TypeDeclaration) targetTypeDecl).getMethods();
        if (methodDecls.length > 0) {
            return methodDecls[0].getModifiers();
        }
        return 0;
    }
    ASTNode invocationNode = getInvocationNode();
    if (invocationNode instanceof MethodInvocation) {
        int modifiers = 0;
        Expression expression = ((MethodInvocation) invocationNode).getExpression();
        if (expression != null) {
            if (expression instanceof Name && ((Name) expression).resolveBinding().getKind() == IBinding.TYPE) {
                modifiers |= Modifier.STATIC;
            }
        } else if (ASTResolving.isInStaticContext(invocationNode)) {
            modifiers |= Modifier.STATIC;
        }
        ASTNode node = ASTResolving.findParentType(invocationNode);
        if (targetTypeDecl.equals(node)) {
            modifiers |= Modifier.PRIVATE;
        } else if (node instanceof AnonymousClassDeclaration && ASTNodes.isParent(node, targetTypeDecl)) {
            modifiers |= Modifier.PROTECTED;
            if (ASTResolving.isInStaticContext(node) && expression == null) {
                modifiers |= Modifier.STATIC;
            }
        } else {
            modifiers |= Modifier.PUBLIC;
        }
        return modifiers;
    }
    return Modifier.PUBLIC;
}
Also used : Expression(org.eclipse.jdt.core.dom.Expression) MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) ASTNode(org.eclipse.jdt.core.dom.ASTNode) AnonymousClassDeclaration(org.eclipse.jdt.core.dom.AnonymousClassDeclaration) MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation) SuperMethodInvocation(org.eclipse.jdt.core.dom.SuperMethodInvocation) TypeDeclaration(org.eclipse.jdt.core.dom.TypeDeclaration) SimpleName(org.eclipse.jdt.core.dom.SimpleName) Name(org.eclipse.jdt.core.dom.Name)

Example 25 with ASTNode

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

ASTNode (org.eclipse.jdt.core.dom.ASTNode)710 SimpleName (org.eclipse.jdt.core.dom.SimpleName)151 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)148 CompilationUnit (org.eclipse.jdt.core.dom.CompilationUnit)147 MethodDeclaration (org.eclipse.jdt.core.dom.MethodDeclaration)134 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)126 Expression (org.eclipse.jdt.core.dom.Expression)125 ArrayList (java.util.ArrayList)111 ASTRewrite (org.eclipse.jdt.core.dom.rewrite.ASTRewrite)108 AST (org.eclipse.jdt.core.dom.AST)106 Type (org.eclipse.jdt.core.dom.Type)92 Block (org.eclipse.jdt.core.dom.Block)89 ParenthesizedExpression (org.eclipse.jdt.core.dom.ParenthesizedExpression)77 IMethodBinding (org.eclipse.jdt.core.dom.IMethodBinding)73 VariableDeclarationFragment (org.eclipse.jdt.core.dom.VariableDeclarationFragment)67 AbstractTypeDeclaration (org.eclipse.jdt.core.dom.AbstractTypeDeclaration)66 ListRewrite (org.eclipse.jdt.core.dom.rewrite.ListRewrite)65 CastExpression (org.eclipse.jdt.core.dom.CastExpression)64 BodyDeclaration (org.eclipse.jdt.core.dom.BodyDeclaration)62 VariableDeclarationStatement (org.eclipse.jdt.core.dom.VariableDeclarationStatement)60