Search in sources :

Example 6 with Refactoring

use of org.eclipse.ltk.core.refactoring.Refactoring in project che by eclipse.

the class RenameTypeTest method testSimilarElements24.

@Test
public void testSimilarElements24() throws Exception {
    // Test transplanter for ICompilationUnit and IFile
    ParticipantTesting.reset();
    ICompilationUnit cu = createCUfromTestFile(getPackageP(), "SomeClass");
    IType someClass = getType(cu, "SomeClass");
    IJavaElement[] someClassMembers = someClass.getChildren();
    RenameJavaElementDescriptor descriptor = createRefactoringDescriptor(someClass, "SomeNewClass");
    setTheOptions(descriptor, true, false, true, null, RenamingNameSuggestor.STRATEGY_EMBEDDED);
    Refactoring ref = createRefactoring(descriptor);
    RefactoringStatus status = performRefactoring(ref);
    Assert.assertNull("was supposed to pass", status);
    checkResultInClass("SomeNewClass");
    checkMappers(ref, someClass, "SomeNewClass.java", someClassMembers);
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) IJavaElement(org.eclipse.jdt.core.IJavaElement) RefactoringStatus(org.eclipse.ltk.core.refactoring.RefactoringStatus) RenameJavaElementDescriptor(org.eclipse.jdt.core.refactoring.descriptors.RenameJavaElementDescriptor) Refactoring(org.eclipse.ltk.core.refactoring.Refactoring) RenameRefactoring(org.eclipse.ltk.core.refactoring.participants.RenameRefactoring) IType(org.eclipse.jdt.core.IType) Test(org.junit.Test) BaseTest(org.eclipse.che.plugin.java.server.che.BaseTest)

Example 7 with Refactoring

use of org.eclipse.ltk.core.refactoring.Refactoring in project che by eclipse.

the class JavaRefactoringDescriptor method createRefactoring.

/**
	 * {@inheritDoc}
	 */
public Refactoring createRefactoring(final RefactoringStatus status) throws CoreException {
    Refactoring refactoring = null;
    final String id = getID();
    final RefactoringContribution contribution = RefactoringCore.getRefactoringContribution(id);
    if (contribution != null) {
        if (contribution instanceof JavaRefactoringContribution) {
            JavaRefactoringContribution javaContribution = (JavaRefactoringContribution) contribution;
            refactoring = javaContribution.createRefactoring(this, status);
        } else
            JavaPlugin.log(new Status(IStatus.ERROR, JavaPlugin.getPluginId(), 0, MessageFormat.format(DescriptorMessages.JavaRefactoringDescriptor_no_resulting_descriptor, new Object[] { id }), null));
    }
    return refactoring;
}
Also used : Status(org.eclipse.core.runtime.Status) IStatus(org.eclipse.core.runtime.IStatus) RefactoringStatus(org.eclipse.ltk.core.refactoring.RefactoringStatus) RefactoringContribution(org.eclipse.ltk.core.refactoring.RefactoringContribution) Refactoring(org.eclipse.ltk.core.refactoring.Refactoring)

Example 8 with Refactoring

use of org.eclipse.ltk.core.refactoring.Refactoring in project che by eclipse.

the class RefactoringManager method createMoveRefactoringSession.

/**
     * Create move refactoring session.
     *
     * @param javaElements
     *         the java elements
     * @return the ID of the refactoring session
     */
public String createMoveRefactoringSession(IJavaElement[] javaElements) throws JavaModelException, RefactoringException {
    IReorgPolicy.IMovePolicy policy = ReorgPolicyFactory.createMovePolicy(new IResource[0], javaElements);
    if (policy.canEnable()) {
        JavaMoveProcessor processor = new JavaMoveProcessor(policy);
        //TODO this may overwrite existing sources.
        processor.setReorgQueries(new NullReorgQueries());
        processor.setCreateTargetQueries(() -> null);
        Refactoring refactoring = new MoveRefactoring(processor);
        MoveRefactoringSession session = new MoveRefactoringSession(refactoring, processor);
        final String id = String.format("move-%s", sessionId.getAndIncrement());
        sessions.put(id, session);
        return id;
    } else {
        throw new RefactoringException("Can't create move refactoring session.");
    }
}
Also used : IReorgPolicy(org.eclipse.jdt.internal.corext.refactoring.reorg.IReorgPolicy) NullReorgQueries(org.eclipse.jdt.internal.corext.refactoring.reorg.NullReorgQueries) Refactoring(org.eclipse.ltk.core.refactoring.Refactoring) RenameRefactoring(org.eclipse.ltk.core.refactoring.participants.RenameRefactoring) MoveRefactoring(org.eclipse.ltk.core.refactoring.participants.MoveRefactoring) MoveRefactoring(org.eclipse.ltk.core.refactoring.participants.MoveRefactoring) MoveRefactoringSession(org.eclipse.che.plugin.java.server.refactoring.session.MoveRefactoringSession) JavaMoveProcessor(org.eclipse.jdt.internal.corext.refactoring.reorg.JavaMoveProcessor)

Example 9 with Refactoring

use of org.eclipse.ltk.core.refactoring.Refactoring in project che by eclipse.

the class QuickAssistProcessor method getExtractVariableProposal.

private static boolean getExtractVariableProposal(IInvocationContext context, boolean problemsAtLocation, Collection<ICommandAccess> proposals) throws CoreException {
    ASTNode node = context.getCoveredNode();
    if (!(node instanceof Expression)) {
        if (context.getSelectionLength() != 0) {
            return false;
        }
        node = context.getCoveringNode();
        if (!(node instanceof Expression)) {
            return false;
        }
    }
    final Expression expression = (Expression) node;
    ITypeBinding binding = expression.resolveTypeBinding();
    if (binding == null || Bindings.isVoidType(binding)) {
        return false;
    }
    if (proposals == null) {
        return true;
    }
    final ICompilationUnit cu = context.getCompilationUnit();
    ExtractTempRefactoring extractTempRefactoring = new ExtractTempRefactoring(context.getASTRoot(), context.getSelectionOffset(), context.getSelectionLength());
    if (extractTempRefactoring.checkInitialConditions(new NullProgressMonitor()).isOK()) {
        LinkedProposalModel linkedProposalModel = new LinkedProposalModel();
        extractTempRefactoring.setLinkedProposalModel(linkedProposalModel);
        extractTempRefactoring.setCheckResultForCompileProblems(false);
        String label = CorrectionMessages.QuickAssistProcessor_extract_to_local_all_description;
        Image image = JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_LOCAL);
        int relevance;
        if (context.getSelectionLength() == 0) {
            relevance = IProposalRelevance.EXTRACT_LOCAL_ALL_ZERO_SELECTION;
        } else if (problemsAtLocation) {
            relevance = IProposalRelevance.EXTRACT_LOCAL_ALL_ERROR;
        } else {
            relevance = IProposalRelevance.EXTRACT_LOCAL_ALL;
        }
        RefactoringCorrectionProposal proposal = new RefactoringCorrectionProposal(label, cu, extractTempRefactoring, relevance, image) {

            @Override
            protected void init(Refactoring refactoring) throws CoreException {
                ExtractTempRefactoring etr = (ExtractTempRefactoring) refactoring;
                // expensive
                etr.setTempName(etr.guessTempName());
            }
        };
        proposal.setCommandId(EXTRACT_LOCAL_ID);
        proposal.setLinkedProposalModel(linkedProposalModel);
        proposals.add(proposal);
    }
    ExtractTempRefactoring extractTempRefactoringSelectedOnly = new ExtractTempRefactoring(context.getASTRoot(), context.getSelectionOffset(), context.getSelectionLength());
    extractTempRefactoringSelectedOnly.setReplaceAllOccurrences(false);
    if (extractTempRefactoringSelectedOnly.checkInitialConditions(new NullProgressMonitor()).isOK()) {
        LinkedProposalModel linkedProposalModel = new LinkedProposalModel();
        extractTempRefactoringSelectedOnly.setLinkedProposalModel(linkedProposalModel);
        extractTempRefactoringSelectedOnly.setCheckResultForCompileProblems(false);
        String label = CorrectionMessages.QuickAssistProcessor_extract_to_local_description;
        Image image = JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_LOCAL);
        int relevance;
        if (context.getSelectionLength() == 0) {
            relevance = IProposalRelevance.EXTRACT_LOCAL_ZERO_SELECTION;
        } else if (problemsAtLocation) {
            relevance = IProposalRelevance.EXTRACT_LOCAL_ERROR;
        } else {
            relevance = IProposalRelevance.EXTRACT_LOCAL;
        }
        RefactoringCorrectionProposal proposal = new RefactoringCorrectionProposal(label, cu, extractTempRefactoringSelectedOnly, relevance, image) {

            @Override
            protected void init(Refactoring refactoring) throws CoreException {
                ExtractTempRefactoring etr = (ExtractTempRefactoring) refactoring;
                // expensive
                etr.setTempName(etr.guessTempName());
            }
        };
        proposal.setCommandId(EXTRACT_LOCAL_NOT_REPLACE_ID);
        proposal.setLinkedProposalModel(linkedProposalModel);
        proposals.add(proposal);
    }
    ExtractConstantRefactoring extractConstRefactoring = new ExtractConstantRefactoring(context.getASTRoot(), context.getSelectionOffset(), context.getSelectionLength());
    if (extractConstRefactoring.checkInitialConditions(new NullProgressMonitor()).isOK()) {
        LinkedProposalModel linkedProposalModel = new LinkedProposalModel();
        extractConstRefactoring.setLinkedProposalModel(linkedProposalModel);
        extractConstRefactoring.setCheckResultForCompileProblems(false);
        String label = CorrectionMessages.QuickAssistProcessor_extract_to_constant_description;
        Image image = JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_LOCAL);
        int relevance;
        if (context.getSelectionLength() == 0) {
            relevance = IProposalRelevance.EXTRACT_CONSTANT_ZERO_SELECTION;
        } else if (problemsAtLocation) {
            relevance = IProposalRelevance.EXTRACT_CONSTANT_ERROR;
        } else {
            relevance = IProposalRelevance.EXTRACT_CONSTANT;
        }
        RefactoringCorrectionProposal proposal = new RefactoringCorrectionProposal(label, cu, extractConstRefactoring, relevance, image) {

            @Override
            protected void init(Refactoring refactoring) throws CoreException {
                ExtractConstantRefactoring etr = (ExtractConstantRefactoring) refactoring;
                // expensive
                etr.setConstantName(etr.guessConstantName());
            }
        };
        proposal.setCommandId(EXTRACT_CONSTANT_ID);
        proposal.setLinkedProposalModel(linkedProposalModel);
        proposals.add(proposal);
    }
    return false;
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) LinkedProposalModel(org.eclipse.jdt.internal.corext.fix.LinkedProposalModel) ExtractTempRefactoring(org.eclipse.jdt.internal.corext.refactoring.code.ExtractTempRefactoring) Image(org.eclipse.swt.graphics.Image) RefactoringCorrectionProposal(org.eclipse.jdt.internal.ui.text.correction.proposals.RefactoringCorrectionProposal) Refactoring(org.eclipse.ltk.core.refactoring.Refactoring) InlineTempRefactoring(org.eclipse.jdt.internal.corext.refactoring.code.InlineTempRefactoring) ExtractTempRefactoring(org.eclipse.jdt.internal.corext.refactoring.code.ExtractTempRefactoring) PromoteTempToFieldRefactoring(org.eclipse.jdt.internal.corext.refactoring.code.PromoteTempToFieldRefactoring) ExtractMethodRefactoring(org.eclipse.jdt.internal.corext.refactoring.code.ExtractMethodRefactoring) ConvertAnonymousToNestedRefactoring(org.eclipse.jdt.internal.corext.refactoring.code.ConvertAnonymousToNestedRefactoring) ExtractConstantRefactoring(org.eclipse.jdt.internal.corext.refactoring.code.ExtractConstantRefactoring) ExtractConstantRefactoring(org.eclipse.jdt.internal.corext.refactoring.code.ExtractConstantRefactoring)

Example 10 with Refactoring

use of org.eclipse.ltk.core.refactoring.Refactoring in project che by eclipse.

the class RenameParametersTest method helper1.

private void helper1(String[] newNames, String[] signature) throws Exception {
    ICompilationUnit cu = createCUfromTestFile(getPackageP(), true, true);
    IType classA = getType(cu, "A");
    IMethod method = classA.getMethod("m", signature);
    Assert.assertTrue("refactoring not available", RefactoringAvailabilityTester.isChangeSignatureAvailable(method));
    ChangeSignatureProcessor processor = new ChangeSignatureProcessor(method);
    Refactoring ref = new ProcessorBasedRefactoring(processor);
    //ref.setUpdateReferences(updateReferences);
    //ref.setNewParameterNames(newNames);
    //ref.setNewNames(createRenamings(method, newNames));
    modifyInfos(processor.getParameterInfos(), newNames);
    RefactoringStatus result = performRefactoring(ref);
    Assert.assertEquals("precondition was supposed to pass", null, result);
    IPackageFragment pack = (IPackageFragment) cu.getParent();
    String newCuName = getSimpleTestFileName(true, true);
    ICompilationUnit newcu = pack.getCompilationUnit(newCuName);
    Assert.assertTrue(newCuName + " does not exist", newcu.exists());
    Assert.assertEquals("invalid renaming", getFileContents(getTestFileName(true, false)).length(), newcu.getSource().length());
    assertEqualLines("invalid renaming", getFileContents(getTestFileName(true, false)), newcu.getSource());
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) RefactoringStatus(org.eclipse.ltk.core.refactoring.RefactoringStatus) IMethod(org.eclipse.jdt.core.IMethod) ProcessorBasedRefactoring(org.eclipse.ltk.core.refactoring.participants.ProcessorBasedRefactoring) Refactoring(org.eclipse.ltk.core.refactoring.Refactoring) ProcessorBasedRefactoring(org.eclipse.ltk.core.refactoring.participants.ProcessorBasedRefactoring) IType(org.eclipse.jdt.core.IType) ChangeSignatureProcessor(org.eclipse.jdt.internal.corext.refactoring.structure.ChangeSignatureProcessor)

Aggregations

Refactoring (org.eclipse.ltk.core.refactoring.Refactoring)17 RefactoringStatus (org.eclipse.ltk.core.refactoring.RefactoringStatus)13 RenameRefactoring (org.eclipse.ltk.core.refactoring.participants.RenameRefactoring)11 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)8 RenameJavaElementDescriptor (org.eclipse.jdt.core.refactoring.descriptors.RenameJavaElementDescriptor)8 IType (org.eclipse.jdt.core.IType)7 IMethod (org.eclipse.jdt.core.IMethod)5 IPackageFragment (org.eclipse.jdt.core.IPackageFragment)5 ProcessorBasedRefactoring (org.eclipse.ltk.core.refactoring.participants.ProcessorBasedRefactoring)5 Test (org.junit.Test)5 IContainer (org.eclipse.core.resources.IContainer)4 IFolder (org.eclipse.core.resources.IFolder)3 IPath (org.eclipse.core.runtime.IPath)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 IFile (org.eclipse.core.resources.IFile)2 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)2 Path (org.eclipse.core.runtime.Path)2 IJavaElement (org.eclipse.jdt.core.IJavaElement)2 IJavaElementMapper (org.eclipse.jdt.core.refactoring.IJavaElementMapper)2 ChangeSignatureProcessor (org.eclipse.jdt.internal.corext.refactoring.structure.ChangeSignatureProcessor)2