Search in sources :

Example 56 with MethodDeclaration

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

the class IntroduceFactoryRefactoring method checkSelection.

/**
	 * Determines what kind of AST node was selected, and returns an error status
	 * if the kind of node is inappropriate for this refactoring.
	 * @param pm
	 * @return a RefactoringStatus indicating whether the selection is valid
	 * @throws JavaModelException
	 */
private RefactoringStatus checkSelection(IProgressMonitor pm) throws JavaModelException {
    try {
        pm.beginTask(RefactoringCoreMessages.IntroduceFactory_examiningSelection, 2);
        fSelectedNode = getTargetNode(fCUHandle, fSelectionStart, fSelectionLength);
        if (fSelectedNode == null)
            return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.IntroduceFactory_notAConstructorInvocation);
        // constructor MethodDeclaration; nothing else.
        if (fSelectedNode instanceof ClassInstanceCreation) {
            ClassInstanceCreation classInstanceCreation = (ClassInstanceCreation) fSelectedNode;
            fCtorBinding = classInstanceCreation.resolveConstructorBinding();
        } else if (fSelectedNode instanceof MethodDeclaration) {
            MethodDeclaration methodDeclaration = (MethodDeclaration) fSelectedNode;
            fCtorBinding = methodDeclaration.resolveBinding();
        }
        if (fCtorBinding == null)
            return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.IntroduceFactory_unableToResolveConstructorBinding);
        // If this constructor is of a generic type, get the generic version,
        // not some instantiation thereof.
        fCtorBinding = fCtorBinding.getMethodDeclaration();
        pm.worked(1);
        // We don't handle constructors of nested types at the moment
        if (fCtorBinding.getDeclaringClass().isNested())
            return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.IntroduceFactory_unsupportedNestedTypes);
        ITypeBinding ctorType = fCtorBinding.getDeclaringClass();
        IType ctorOwningType = (IType) ctorType.getJavaElement();
        if (ctorOwningType.isBinary())
            // Can't modify binary CU; don't know what CU to put factory method
            return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.IntroduceFactory_constructorInBinaryClass);
        if (ctorOwningType.isEnum())
            // Doesn't make sense to encapsulate enum constructors
            return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.IntroduceFactory_constructorInEnum);
        // Put the generated factory method inside the type that owns the constructor
        fFactoryUnitHandle = ctorOwningType.getCompilationUnit();
        fFactoryCU = getASTFor(fFactoryUnitHandle);
        Name ctorOwnerName = (Name) NodeFinder.perform(fFactoryCU, ctorOwningType.getNameRange());
        fCtorOwningClass = (AbstractTypeDeclaration) ASTNodes.getParent(ctorOwnerName, AbstractTypeDeclaration.class);
        fFactoryOwningClass = fCtorOwningClass;
        pm.worked(1);
        if (fNewMethodName == null)
            //$NON-NLS-1$
            return setNewMethodName("create" + fCtorBinding.getName());
        else
            return new RefactoringStatus();
    } finally {
        pm.done();
    }
}
Also used : ClassInstanceCreation(org.eclipse.jdt.core.dom.ClassInstanceCreation) MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) RefactoringStatus(org.eclipse.ltk.core.refactoring.RefactoringStatus) IType(org.eclipse.jdt.core.IType) SimpleName(org.eclipse.jdt.core.dom.SimpleName) Name(org.eclipse.jdt.core.dom.Name)

Example 57 with MethodDeclaration

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

the class ExtractMethodRefactoring method getSignature.

/**
	 * Returns the signature of the new method.
	 *
	 * @param methodName the method name used for the new method
	 * @return the signature of the extracted method
	 */
public String getSignature(String methodName) {
    MethodDeclaration methodDecl = createNewMethodDeclaration();
    methodDecl.setBody(null);
    String str = ASTNodes.asString(methodDecl);
    return str.substring(0, str.indexOf(';'));
}
Also used : MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration)

Example 58 with MethodDeclaration

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

the class ExtractMethodAnalyzer method computeLastStatementSelected.

private void computeLastStatementSelected() {
    ASTNode[] nodes = getSelectedNodes();
    if (nodes.length == 0) {
        fIsLastStatementSelected = false;
    } else {
        Block body = null;
        LambdaExpression enclosingLambdaExpr = ASTResolving.findEnclosingLambdaExpression(getFirstSelectedNode());
        if (enclosingLambdaExpr != null) {
            ASTNode lambdaBody = enclosingLambdaExpr.getBody();
            if (lambdaBody instanceof Block) {
                body = (Block) lambdaBody;
            } else {
                fIsLastStatementSelected = true;
                return;
            }
        } else {
            if (fEnclosingBodyDeclaration instanceof MethodDeclaration) {
                body = ((MethodDeclaration) fEnclosingBodyDeclaration).getBody();
            } else if (fEnclosingBodyDeclaration instanceof Initializer) {
                body = ((Initializer) fEnclosingBodyDeclaration).getBody();
            }
        }
        if (body != null) {
            List<Statement> statements = body.statements();
            if (statements.size() > 0) {
                fIsLastStatementSelected = nodes[nodes.length - 1] == statements.get(statements.size() - 1);
            } else {
                fIsLastStatementSelected = true;
            }
        }
    }
}
Also used : Initializer(org.eclipse.jdt.core.dom.Initializer) MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) DoStatement(org.eclipse.jdt.core.dom.DoStatement) Statement(org.eclipse.jdt.core.dom.Statement) ContinueStatement(org.eclipse.jdt.core.dom.ContinueStatement) EnhancedForStatement(org.eclipse.jdt.core.dom.EnhancedForStatement) TryStatement(org.eclipse.jdt.core.dom.TryStatement) SwitchStatement(org.eclipse.jdt.core.dom.SwitchStatement) WhileStatement(org.eclipse.jdt.core.dom.WhileStatement) BreakStatement(org.eclipse.jdt.core.dom.BreakStatement) LabeledStatement(org.eclipse.jdt.core.dom.LabeledStatement) ForStatement(org.eclipse.jdt.core.dom.ForStatement) VariableDeclarationStatement(org.eclipse.jdt.core.dom.VariableDeclarationStatement) ASTNode(org.eclipse.jdt.core.dom.ASTNode) Block(org.eclipse.jdt.core.dom.Block) LambdaExpression(org.eclipse.jdt.core.dom.LambdaExpression)

Example 59 with MethodDeclaration

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

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

the class InlineMethodRefactoring method checkOverridden.

private void checkOverridden(RefactoringStatus status, IProgressMonitor pm) throws JavaModelException {
    //$NON-NLS-1$
    pm.beginTask("", 9);
    pm.setTaskName(RefactoringCoreMessages.InlineMethodRefactoring_checking_overridden);
    MethodDeclaration decl = fSourceProvider.getDeclaration();
    IMethod method = (IMethod) decl.resolveBinding().getJavaElement();
    if (method == null || Flags.isPrivate(method.getFlags())) {
        pm.worked(8);
        return;
    }
    IType type = method.getDeclaringType();
    ITypeHierarchy hierarchy = type.newTypeHierarchy(new SubProgressMonitor(pm, 6));
    checkSubTypes(status, method, hierarchy.getAllSubtypes(type), new SubProgressMonitor(pm, 1));
    checkSuperClasses(status, method, hierarchy.getAllSuperclasses(type), new SubProgressMonitor(pm, 1));
    checkSuperInterfaces(status, method, hierarchy.getAllSuperInterfaces(type), new SubProgressMonitor(pm, 1));
    //$NON-NLS-1$
    pm.setTaskName("");
}
Also used : ITypeHierarchy(org.eclipse.jdt.core.ITypeHierarchy) MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) IMethod(org.eclipse.jdt.core.IMethod) SubProgressMonitor(org.eclipse.core.runtime.SubProgressMonitor) IType(org.eclipse.jdt.core.IType)

Aggregations

MethodDeclaration (org.eclipse.jdt.core.dom.MethodDeclaration)139 ASTNode (org.eclipse.jdt.core.dom.ASTNode)83 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)48 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)40 AST (org.eclipse.jdt.core.dom.AST)39 Type (org.eclipse.jdt.core.dom.Type)37 Block (org.eclipse.jdt.core.dom.Block)35 CompilationUnit (org.eclipse.jdt.core.dom.CompilationUnit)35 ASTRewrite (org.eclipse.jdt.core.dom.rewrite.ASTRewrite)35 Expression (org.eclipse.jdt.core.dom.Expression)34 IMethodBinding (org.eclipse.jdt.core.dom.IMethodBinding)33 BodyDeclaration (org.eclipse.jdt.core.dom.BodyDeclaration)29 SimpleName (org.eclipse.jdt.core.dom.SimpleName)29 AbstractTypeDeclaration (org.eclipse.jdt.core.dom.AbstractTypeDeclaration)28 ReturnStatement (org.eclipse.jdt.core.dom.ReturnStatement)24 SingleVariableDeclaration (org.eclipse.jdt.core.dom.SingleVariableDeclaration)24 TypeDeclaration (org.eclipse.jdt.core.dom.TypeDeclaration)22 Javadoc (org.eclipse.jdt.core.dom.Javadoc)20 PrimitiveType (org.eclipse.jdt.core.dom.PrimitiveType)19 FieldDeclaration (org.eclipse.jdt.core.dom.FieldDeclaration)18