Search in sources :

Example 21 with CompilationUnitRewrite

use of org.eclipse.jdt.internal.corext.refactoring.structure.CompilationUnitRewrite in project che by eclipse.

the class IntroduceIndirectionRefactoring method checkInitialConditions.

// ********** CONDITION CHECKING **********
@Override
public RefactoringStatus checkInitialConditions(IProgressMonitor pm) throws CoreException, OperationCanceledException {
    try {
        pm.beginTask(RefactoringCoreMessages.IntroduceIndirectionRefactoring_checking_activation, 1);
        fRewrites = new HashMap<ICompilationUnit, CompilationUnitRewrite>();
        if (fTargetMethod == null) {
            if (fSelectionStart == 0)
                return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.IntroduceIndirectionRefactoring_not_available_on_this_selection);
            // if a text selection exists, source is available.
            CompilationUnit selectionCURoot;
            ASTNode selectionNode;
            if (fSelectionCompilationUnit != null) {
                // compilation unit - could use CuRewrite later on
                selectionCURoot = getCachedCURewrite(fSelectionCompilationUnit).getRoot();
                selectionNode = getSelectedNode(fSelectionCompilationUnit, selectionCURoot, fSelectionStart, fSelectionLength);
            } else {
                // binary class file - no cu rewrite
                ASTParser parser = ASTParser.newParser(ASTProvider.SHARED_AST_LEVEL);
                parser.setResolveBindings(true);
                parser.setSource(fSelectionClassFile);
                selectionCURoot = (CompilationUnit) parser.createAST(null);
                selectionNode = getSelectedNode(null, selectionCURoot, fSelectionStart, fSelectionLength);
            }
            if (selectionNode == null)
                return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.IntroduceIndirectionRefactoring_not_available_on_this_selection);
            IMethodBinding targetMethodBinding = null;
            if (selectionNode.getNodeType() == ASTNode.METHOD_INVOCATION) {
                targetMethodBinding = ((MethodInvocation) selectionNode).resolveMethodBinding();
            } else if (selectionNode.getNodeType() == ASTNode.METHOD_DECLARATION) {
                targetMethodBinding = ((MethodDeclaration) selectionNode).resolveBinding();
            } else if (selectionNode.getNodeType() == ASTNode.SUPER_METHOD_INVOCATION) {
                // Allow invocation on super methods calls. makes sense as other
                // calls or even only the declaration can be updated.
                targetMethodBinding = ((SuperMethodInvocation) selectionNode).resolveMethodBinding();
            } else {
                return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.IntroduceIndirectionRefactoring_not_available_on_this_selection);
            }
            // resolve generics
            fTargetMethodBinding = targetMethodBinding.getMethodDeclaration();
            fTargetMethod = (IMethod) fTargetMethodBinding.getJavaElement();
            //allow single updating mode if an invocation was selected and the invocation can be updated
            if (selectionNode instanceof MethodInvocation && fSelectionCompilationUnit != null)
                fSelectionMethodInvocation = (MethodInvocation) selectionNode;
        } else {
            if (fTargetMethod.getDeclaringType().isAnnotation())
                return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.IntroduceIndirectionRefactoring_not_available_on_annotation);
            if (fTargetMethod.getCompilationUnit() != null) {
                // source method
                CompilationUnit selectionCURoot = getCachedCURewrite(fTargetMethod.getCompilationUnit()).getRoot();
                MethodDeclaration declaration = ASTNodeSearchUtil.getMethodDeclarationNode(fTargetMethod, selectionCURoot);
                fTargetMethodBinding = declaration.resolveBinding().getMethodDeclaration();
            } else {
                // binary method - no CURewrite available (and none needed as we cannot update the method anyway)
                ASTParser parser = ASTParser.newParser(ASTProvider.SHARED_AST_LEVEL);
                parser.setProject(fTargetMethod.getJavaProject());
                IBinding[] bindings = parser.createBindings(new IJavaElement[] { fTargetMethod }, null);
                fTargetMethodBinding = ((IMethodBinding) bindings[0]).getMethodDeclaration();
            }
        }
        if (fTargetMethod == null || fTargetMethodBinding == null || (!RefactoringAvailabilityTester.isIntroduceIndirectionAvailable(fTargetMethod)))
            return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.IntroduceIndirectionRefactoring_not_available_on_this_selection);
        if (fTargetMethod.getDeclaringType().isLocal() || fTargetMethod.getDeclaringType().isAnonymous())
            return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.IntroduceIndirectionRefactoring_not_available_for_local_or_anonymous_types);
        if (fTargetMethod.isConstructor())
            return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.IntroduceIndirectionRefactoring_not_available_for_constructors);
        if (fIntermediaryMethodName == null)
            fIntermediaryMethodName = fTargetMethod.getElementName();
        if (fIntermediaryType == null) {
            if (fSelectionCompilationUnit != null && !fSelectionCompilationUnit.isReadOnly())
                fIntermediaryType = getEnclosingInitialSelectionMember().getDeclaringType();
            else if (!fTargetMethod.isBinary() && !fTargetMethod.isReadOnly())
                fIntermediaryType = fTargetMethod.getDeclaringType();
        }
        return new RefactoringStatus();
    } finally {
        pm.done();
    }
}
Also used : CompilationUnitRewrite(org.eclipse.jdt.internal.corext.refactoring.structure.CompilationUnitRewrite) CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) IMethodBinding(org.eclipse.jdt.core.dom.IMethodBinding) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) IBinding(org.eclipse.jdt.core.dom.IBinding) ASTNode(org.eclipse.jdt.core.dom.ASTNode) RefactoringStatus(org.eclipse.ltk.core.refactoring.RefactoringStatus) MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation) SuperMethodInvocation(org.eclipse.jdt.core.dom.SuperMethodInvocation) ASTParser(org.eclipse.jdt.core.dom.ASTParser)

Example 22 with CompilationUnitRewrite

use of org.eclipse.jdt.internal.corext.refactoring.structure.CompilationUnitRewrite in project che by eclipse.

the class DelegateCreator method setSourceRewrite.

/**
	 * Sets the compilation unit rewrite of the declaration to create a delegate
	 * for. Must always be called prior to prepareDelegate(). Bindings need not
	 * be resolved.
	 *
	 * @param rewrite the CompilationUnitRewrite.
	 */
public void setSourceRewrite(CompilationUnitRewrite rewrite) {
    fOriginalRewrite = rewrite;
    fPreferences = JavaPreferencesSettings.getCodeGenerationSettings(rewrite.getCu().getJavaProject());
    fDelegateRewrite = new CompilationUnitRewrite(rewrite.getCu(), rewrite.getRoot());
    fDelegateRewrite.getASTRewrite().setTargetSourceRangeComputer(rewrite.getASTRewrite().getExtendedSourceRangeComputer());
}
Also used : CompilationUnitRewrite(org.eclipse.jdt.internal.corext.refactoring.structure.CompilationUnitRewrite)

Aggregations

CompilationUnitRewrite (org.eclipse.jdt.internal.corext.refactoring.structure.CompilationUnitRewrite)22 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)10 RefactoringStatus (org.eclipse.ltk.core.refactoring.RefactoringStatus)9 CompilationUnitChange (org.eclipse.jdt.core.refactoring.CompilationUnitChange)6 OperationCanceledException (org.eclipse.core.runtime.OperationCanceledException)5 CompilationUnit (org.eclipse.jdt.core.dom.CompilationUnit)5 SubProgressMonitor (org.eclipse.core.runtime.SubProgressMonitor)4 IMember (org.eclipse.jdt.core.IMember)4 ASTNode (org.eclipse.jdt.core.dom.ASTNode)4 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)4 MethodDeclaration (org.eclipse.jdt.core.dom.MethodDeclaration)4 IMethod (org.eclipse.jdt.core.IMethod)3 IType (org.eclipse.jdt.core.IType)3 MethodInvocation (org.eclipse.jdt.core.dom.MethodInvocation)3 SuperMethodInvocation (org.eclipse.jdt.core.dom.SuperMethodInvocation)3 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 IMethodBinding (org.eclipse.jdt.core.dom.IMethodBinding)2 VariableDeclarationFragment (org.eclipse.jdt.core.dom.VariableDeclarationFragment)2 SearchMatch (org.eclipse.jdt.core.search.SearchMatch)2