Search in sources :

Example 1 with InlineConstantRefactoring

use of org.eclipse.jdt.internal.corext.refactoring.code.InlineConstantRefactoring in project eclipse.jdt.ls by eclipse.

the class RefactorProcessor method getInlineProposal.

private boolean getInlineProposal(IInvocationContext context, ASTNode node, Collection<ChangeCorrectionProposal> resultingCollections) {
    if (resultingCollections == null) {
        return false;
    }
    if (!(node instanceof SimpleName)) {
        return false;
    }
    SimpleName name = (SimpleName) node;
    IBinding binding = name.resolveBinding();
    try {
        if (binding instanceof IVariableBinding) {
            IVariableBinding varBinding = (IVariableBinding) binding;
            if (varBinding.isParameter()) {
                return false;
            }
            if (varBinding.isField()) {
                // Inline Constant (static final field)
                if (RefactoringAvailabilityTesterCore.isInlineConstantAvailable((IField) varBinding.getJavaElement())) {
                    InlineConstantRefactoring refactoring = new InlineConstantRefactoring(context.getCompilationUnit(), context.getASTRoot(), context.getSelectionOffset(), context.getSelectionLength());
                    if (refactoring != null && refactoring.checkInitialConditions(new NullProgressMonitor()).isOK() && refactoring.getReferences(new NullProgressMonitor(), new RefactoringStatus()).length > 0) {
                        refactoring.setRemoveDeclaration(refactoring.isDeclarationSelected());
                        refactoring.setReplaceAllReferences(refactoring.isDeclarationSelected());
                        CheckConditionsOperation check = new CheckConditionsOperation(refactoring, CheckConditionsOperation.FINAL_CONDITIONS);
                        final CreateChangeOperation create = new CreateChangeOperation(check, RefactoringStatus.FATAL);
                        create.run(new NullProgressMonitor());
                        String label = ActionMessages.InlineConstantRefactoringAction_label;
                        int relevance = IProposalRelevance.INLINE_LOCAL;
                        ChangeCorrectionProposal proposal = new ChangeCorrectionProposal(label, CodeActionKind.RefactorInline, create.getChange(), relevance);
                        resultingCollections.add(proposal);
                        return true;
                    }
                }
                return false;
            }
            ASTNode decl = context.getASTRoot().findDeclaringNode(varBinding);
            if (!(decl instanceof VariableDeclarationFragment) || decl.getLocationInParent() != VariableDeclarationStatement.FRAGMENTS_PROPERTY) {
                return false;
            }
            // Inline Local Variable
            if (binding.getJavaElement() instanceof ILocalVariable && RefactoringAvailabilityTesterCore.isInlineTempAvailable((ILocalVariable) binding.getJavaElement())) {
                InlineTempRefactoring refactoring = new InlineTempRefactoring((VariableDeclaration) decl);
                boolean status;
                try {
                    status = refactoring.checkAllConditions(new NullProgressMonitor()).isOK();
                } catch (Exception e) {
                    // ignore
                    status = false;
                }
                if (status && refactoring.getReferences().length > 0) {
                    String label = CorrectionMessages.QuickAssistProcessor_inline_local_description;
                    int relevance = IProposalRelevance.INLINE_LOCAL;
                    RefactoringCorrectionProposal proposal = new RefactoringCorrectionProposal(label, CodeActionKind.RefactorInline, context.getCompilationUnit(), refactoring, relevance);
                    resultingCollections.add(proposal);
                    return true;
                }
            }
        } else if (binding instanceof IMethodBinding) {
            // Inline Method
            if (RefactoringAvailabilityTesterCore.isInlineMethodAvailable((IMethod) binding.getJavaElement())) {
                InlineMethodRefactoring refactoring = InlineMethodRefactoring.create(context.getCompilationUnit(), context.getASTRoot(), context.getSelectionOffset(), context.getSelectionLength());
                if (refactoring != null && refactoring.checkInitialConditions(new NullProgressMonitor()).isOK()) {
                    CheckConditionsOperation check = new CheckConditionsOperation(refactoring, CheckConditionsOperation.FINAL_CONDITIONS);
                    final CreateChangeOperation create = new CreateChangeOperation(check, RefactoringStatus.FATAL);
                    create.run(new NullProgressMonitor());
                    String label = ActionMessages.InlineMethodRefactoringAction_label;
                    int relevance = IProposalRelevance.INLINE_LOCAL;
                    ChangeCorrectionProposal proposal = new ChangeCorrectionProposal(label, CodeActionKind.RefactorInline, create.getChange(), relevance);
                    resultingCollections.add(proposal);
                    return true;
                }
            }
        }
    } catch (CoreException e) {
        JavaLanguageServerPlugin.log(e);
    }
    return false;
}
Also used : IMethodBinding(org.eclipse.jdt.core.dom.IMethodBinding) NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) InlineMethodRefactoring(org.eclipse.jdt.internal.corext.refactoring.code.InlineMethodRefactoring) InlineConstantRefactoring(org.eclipse.jdt.internal.corext.refactoring.code.InlineConstantRefactoring) SimpleName(org.eclipse.jdt.core.dom.SimpleName) IBinding(org.eclipse.jdt.core.dom.IBinding) RefactoringStatus(org.eclipse.ltk.core.refactoring.RefactoringStatus) IVariableBinding(org.eclipse.jdt.core.dom.IVariableBinding) RefactoringCorrectionProposal(org.eclipse.jdt.ls.core.internal.corrections.proposals.RefactoringCorrectionProposal) TypeChangeCorrectionProposal(org.eclipse.jdt.ls.core.internal.corrections.proposals.TypeChangeCorrectionProposal) ChangeCorrectionProposal(org.eclipse.jdt.ls.core.internal.corrections.proposals.ChangeCorrectionProposal) CoreException(org.eclipse.core.runtime.CoreException) JavaModelException(org.eclipse.jdt.core.JavaModelException) ILocalVariable(org.eclipse.jdt.core.ILocalVariable) CreateChangeOperation(org.eclipse.ltk.core.refactoring.CreateChangeOperation) CoreException(org.eclipse.core.runtime.CoreException) VariableDeclarationFragment(org.eclipse.jdt.core.dom.VariableDeclarationFragment) CheckConditionsOperation(org.eclipse.ltk.core.refactoring.CheckConditionsOperation) ASTNode(org.eclipse.jdt.core.dom.ASTNode) InlineTempRefactoring(org.eclipse.jdt.internal.corext.refactoring.code.InlineTempRefactoring) IMethod(org.eclipse.jdt.core.IMethod)

Aggregations

CoreException (org.eclipse.core.runtime.CoreException)1 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)1 ILocalVariable (org.eclipse.jdt.core.ILocalVariable)1 IMethod (org.eclipse.jdt.core.IMethod)1 JavaModelException (org.eclipse.jdt.core.JavaModelException)1 ASTNode (org.eclipse.jdt.core.dom.ASTNode)1 IBinding (org.eclipse.jdt.core.dom.IBinding)1 IMethodBinding (org.eclipse.jdt.core.dom.IMethodBinding)1 IVariableBinding (org.eclipse.jdt.core.dom.IVariableBinding)1 SimpleName (org.eclipse.jdt.core.dom.SimpleName)1 VariableDeclarationFragment (org.eclipse.jdt.core.dom.VariableDeclarationFragment)1 InlineConstantRefactoring (org.eclipse.jdt.internal.corext.refactoring.code.InlineConstantRefactoring)1 InlineMethodRefactoring (org.eclipse.jdt.internal.corext.refactoring.code.InlineMethodRefactoring)1 InlineTempRefactoring (org.eclipse.jdt.internal.corext.refactoring.code.InlineTempRefactoring)1 ChangeCorrectionProposal (org.eclipse.jdt.ls.core.internal.corrections.proposals.ChangeCorrectionProposal)1 RefactoringCorrectionProposal (org.eclipse.jdt.ls.core.internal.corrections.proposals.RefactoringCorrectionProposal)1 TypeChangeCorrectionProposal (org.eclipse.jdt.ls.core.internal.corrections.proposals.TypeChangeCorrectionProposal)1 CheckConditionsOperation (org.eclipse.ltk.core.refactoring.CheckConditionsOperation)1 CreateChangeOperation (org.eclipse.ltk.core.refactoring.CreateChangeOperation)1 RefactoringStatus (org.eclipse.ltk.core.refactoring.RefactoringStatus)1