Search in sources :

Example 66 with RenameJavaElementDescriptor

use of org.eclipse.jdt.core.refactoring.descriptors.RenameJavaElementDescriptor in project che by eclipse.

the class RenamePackageTest method helperMultiProjects.

/**
     * Custom project and source folder structure.
     *
     * @param roots source folders
     * @param packageNames package names per root
     * @param newPackageName the new package name for packageNames[0][0]
     * @param cuNames cu names per package
     * @throws Exception if one of the resources cannot be created
     */
private void helperMultiProjects(IPackageFragmentRoot[] roots, String[][] packageNames, String newPackageName, String[][][] cuNames) throws Exception {
    ICompilationUnit[][][] cus = new ICompilationUnit[roots.length][][];
    IPackageFragment thisPackage = null;
    for (int r = 0; r < roots.length; r++) {
        IPackageFragment[] packages = new IPackageFragment[packageNames[r].length];
        cus[r] = new ICompilationUnit[packageNames[r].length][];
        for (int pa = 0; pa < packageNames[r].length; pa++) {
            packages[pa] = roots[r].createPackageFragment(packageNames[r][pa], true, null);
            cus[r][pa] = new ICompilationUnit[cuNames[r][pa].length];
            if (r == 0 && pa == 0)
                thisPackage = packages[pa];
            for (int typ = 0; typ < cuNames[r][pa].length; typ++) {
                cus[r][pa][typ] = createCUfromTestFile(packages[pa], cuNames[r][pa][typ], roots[r].getElementName() + "/" + packageNames[r][pa].replace('.', '/') + "/");
            }
        }
    }
    RenameJavaElementDescriptor descriptor = createRefactoringDescriptor(thisPackage, newPackageName);
    descriptor.setUpdateReferences(fUpdateReferences);
    descriptor.setUpdateTextualOccurrences(fUpdateTextualMatches);
    setFilePatterns(descriptor);
    descriptor.setUpdateHierarchy(fRenameSubpackages);
    RefactoringStatus result = performRefactoring(descriptor);
    TestCase.assertEquals("preconditions were supposed to pass", null, result);
    assertTrue("package not renamed", !roots[0].getPackageFragment(packageNames[0][0]).exists());
    IPackageFragment newPackage = roots[0].getPackageFragment(newPackageName);
    assertTrue("new package does not exist", newPackage.exists());
    for (int r = 0; r < cuNames.length; r++) {
        for (int pa = 0; pa < cuNames[r].length; pa++) {
            String packageName = roots[r].getElementName() + "/" + ((r == 0 && pa == 0) ? newPackageName : packageNames[r][pa]).replace('.', '/') + "/";
            for (int typ = 0; typ < cuNames[r][pa].length; typ++) {
                String s1 = getFileContents(getOutputTestFileName(cuNames[r][pa][typ], packageName));
                ICompilationUnit cu = (r == 0 && pa == 0) ? newPackage.getCompilationUnit(cuNames[r][pa][typ] + ".java") : cus[r][pa][typ];
                //DebugUtils.dump("cu:" + cu.getElementName());
                String s2 = cu.getSource();
                //DebugUtils.dump("expected:" + s1);
                //DebugUtils.dump("was:" + s2);
                assertEqualLines("invalid update in file " + cu.toString(), s1, s2);
            }
        }
    }
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) RefactoringStatus(org.eclipse.ltk.core.refactoring.RefactoringStatus) RenameJavaElementDescriptor(org.eclipse.jdt.core.refactoring.descriptors.RenameJavaElementDescriptor)

Example 67 with RenameJavaElementDescriptor

use of org.eclipse.jdt.core.refactoring.descriptors.RenameJavaElementDescriptor in project che by eclipse.

the class RenameVirtualMethodInClassTest method test18.

@Test
public void test18() throws Exception {
    ICompilationUnit cu = createCUfromTestFile(getPackageP(), "A");
    ICompilationUnit cuC = createCUfromTestFile(getPackageP(), "C");
    IType classB = getType(cu, "B");
    IMethod method = classB.getMethod("m", new String[] { "I" });
    final RenameJavaElementDescriptor descriptor = RefactoringSignatureDescriptorFactory.createRenameJavaElementDescriptor(IJavaRefactorings.RENAME_METHOD);
    descriptor.setJavaElement(method);
    descriptor.setNewName("kk");
    descriptor.setUpdateReferences(true);
    final RefactoringStatus status = new RefactoringStatus();
    final Refactoring refactoring = descriptor.createRefactoring(status);
    assertNotNull("Refactoring should not be null", refactoring);
    assertTrue("status should be ok", status.isOK());
    assertEquals("was supposed to pass", null, performRefactoring(refactoring));
    assertEqualLines("invalid renaming A", getFileContents(getOutputTestFileName("A")), cu.getSource());
    assertEqualLines("invalid renaming C", getFileContents(getOutputTestFileName("C")), cuC.getSource());
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) RefactoringStatus(org.eclipse.ltk.core.refactoring.RefactoringStatus) IMethod(org.eclipse.jdt.core.IMethod) 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)

Example 68 with RenameJavaElementDescriptor

use of org.eclipse.jdt.core.refactoring.descriptors.RenameJavaElementDescriptor in project che by eclipse.

the class RenameVirtualMethodInClassTest method test40.

@Test
public void test40() throws Exception {
    // test for bug 68592
    ICompilationUnit cu = createCUfromTestFile(getPackageP(), "A");
    IType localClass = cu.getType("A").getMethod("doit", new String[0]).getType("LocalClass", 1);
    IMethod method = localClass.getMethod("method", new String[] { "I" });
    final RenameJavaElementDescriptor descriptor = RefactoringSignatureDescriptorFactory.createRenameJavaElementDescriptor(IJavaRefactorings.RENAME_METHOD);
    descriptor.setJavaElement(method);
    descriptor.setNewName("method2");
    descriptor.setUpdateReferences(true);
    final RefactoringStatus status = new RefactoringStatus();
    final Refactoring refactoring = descriptor.createRefactoring(status);
    assertNotNull("Refactoring should not be null", refactoring);
    assertTrue("status should be ok", status.isOK());
    assertEquals("was supposed to pass", null, performRefactoring(refactoring));
    assertEqualLines("invalid renaming A", getFileContents(getOutputTestFileName("A")), cu.getSource());
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) RefactoringStatus(org.eclipse.ltk.core.refactoring.RefactoringStatus) IMethod(org.eclipse.jdt.core.IMethod) 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)

Example 69 with RenameJavaElementDescriptor

use of org.eclipse.jdt.core.refactoring.descriptors.RenameJavaElementDescriptor in project che by eclipse.

the class RenameTypeTest method createRefactoringDescriptor.

private RenameJavaElementDescriptor createRefactoringDescriptor(IType type, String newName) {
    RenameJavaElementDescriptor descriptor = RefactoringSignatureDescriptorFactory.createRenameJavaElementDescriptor(IJavaRefactorings.RENAME_TYPE);
    descriptor.setJavaElement(type);
    descriptor.setNewName(newName);
    descriptor.setUpdateReferences(true);
    return descriptor;
}
Also used : RenameJavaElementDescriptor(org.eclipse.jdt.core.refactoring.descriptors.RenameJavaElementDescriptor)

Example 70 with RenameJavaElementDescriptor

use of org.eclipse.jdt.core.refactoring.descriptors.RenameJavaElementDescriptor in project che by eclipse.

the class RenameTypeTest method testSimilarElements11.

@Test
public void testSimilarElements11() throws Exception {
    //Assure participants get notified of normal stuff (type rename
    //and resource changes) AND similarly named elements.
    ParticipantTesting.reset();
    ICompilationUnit cu = createCUfromTestFile(getPackageP(), "SomeClass");
    IType someClass = getType(cu, "SomeClass");
    IType other = getClassFromTestFile(getPackageP(), "SomeOtherClass");
    List handleList = new ArrayList();
    List argumentList = new ArrayList();
    List similarOldHandleList = new ArrayList();
    List similarNewNameList = new ArrayList();
    List similarNewHandleList = new ArrayList();
    final String newName = "SomeNewClass";
    // f-Field + getters/setters
    IField f3 = other.getField("fSomeClass");
    similarOldHandleList.add(f3.getHandleIdentifier());
    similarNewHandleList.add("Lp/SomeOtherClass;.fSomeNewClass");
    similarNewNameList.add("fSomeNewClass");
    IMethod m3 = other.getMethod("getSomeClass", new String[0]);
    similarOldHandleList.add(m3.getHandleIdentifier());
    similarNewNameList.add("getSomeNewClass");
    similarNewHandleList.add("Lp/SomeOtherClass;.getSomeNewClass()V");
    IMethod m4 = other.getMethod("setSomeClass", new String[] { "QSomeClass;" });
    similarOldHandleList.add(m4.getHandleIdentifier());
    similarNewNameList.add("setSomeNewClass");
    similarNewHandleList.add("Lp/SomeOtherClass;.setSomeNewClass(QSomeNewClass;)V");
    // non-f-field + getter/setters
    IField f1 = someClass.getField("someClass");
    similarOldHandleList.add(f1.getHandleIdentifier());
    similarNewNameList.add("someNewClass");
    similarNewHandleList.add("Lp/SomeNewClass;.someNewClass");
    IMethod m1 = someClass.getMethod("getSomeClass", new String[0]);
    similarOldHandleList.add(m1.getHandleIdentifier());
    similarNewNameList.add("getSomeNewClass");
    similarNewHandleList.add("Lp/SomeNewClass;.getSomeNewClass()V");
    IMethod m2 = someClass.getMethod("setSomeClass", new String[] { "QSomeClass;" });
    similarOldHandleList.add(m2.getHandleIdentifier());
    similarNewNameList.add("setSomeNewClass");
    similarNewHandleList.add("Lp/SomeNewClass;.setSomeNewClass(QSomeNewClass;)V");
    // fs-field
    IField f2 = someClass.getField("fsSomeClass");
    similarOldHandleList.add(f2.getHandleIdentifier());
    similarNewNameList.add("fsSomeNewClass");
    similarNewHandleList.add("Lp/SomeNewClass;.fsSomeNewClass");
    // Type Stuff
    handleList.add(someClass);
    argumentList.add(new RenameArguments(newName, true));
    handleList.add(cu);
    argumentList.add(new RenameArguments(newName + ".java", true));
    handleList.add(cu.getResource());
    argumentList.add(new RenameArguments(newName + ".java", true));
    String[] handles = ParticipantTesting.createHandles(handleList.toArray());
    RenameArguments[] arguments = (RenameArguments[]) argumentList.toArray(new RenameArguments[0]);
    RenameJavaElementDescriptor descriptor = createRefactoringDescriptor(someClass, newName);
    setTheOptions(descriptor, true, false, true, null, RenamingNameSuggestor.STRATEGY_EMBEDDED);
    RefactoringStatus status = performRefactoring(descriptor);
    Assert.assertNull("was supposed to pass", status);
    checkResultInClass(newName);
    checkResultInClass("SomeOtherClass");
    ParticipantTesting.testRename(handles, arguments);
    ParticipantTesting.testSimilarElements(similarOldHandleList, similarNewNameList, similarNewHandleList);
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) RenameArguments(org.eclipse.ltk.core.refactoring.participants.RenameArguments) ArrayList(java.util.ArrayList) RefactoringStatus(org.eclipse.ltk.core.refactoring.RefactoringStatus) ArrayList(java.util.ArrayList) List(java.util.List) IMethod(org.eclipse.jdt.core.IMethod) IField(org.eclipse.jdt.core.IField) RenameJavaElementDescriptor(org.eclipse.jdt.core.refactoring.descriptors.RenameJavaElementDescriptor) IType(org.eclipse.jdt.core.IType) Test(org.junit.Test) BaseTest(org.eclipse.che.plugin.java.server.che.BaseTest)

Aggregations

RenameJavaElementDescriptor (org.eclipse.jdt.core.refactoring.descriptors.RenameJavaElementDescriptor)120 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)56 IType (org.eclipse.jdt.core.IType)55 RefactoringStatus (org.eclipse.ltk.core.refactoring.RefactoringStatus)55 Test (org.junit.Test)33 RenameRefactoring (org.eclipse.ltk.core.refactoring.participants.RenameRefactoring)29 IMethod (org.eclipse.jdt.core.IMethod)28 JDTRefactoringDescriptorComment (org.eclipse.jdt.internal.corext.refactoring.JDTRefactoringDescriptorComment)26 IJavaProject (org.eclipse.jdt.core.IJavaProject)25 IPackageFragment (org.eclipse.jdt.core.IPackageFragment)25 IField (org.eclipse.jdt.core.IField)24 Refactoring (org.eclipse.ltk.core.refactoring.Refactoring)23 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)22 DynamicValidationRefactoringChange (org.eclipse.jdt.internal.corext.refactoring.changes.DynamicValidationRefactoringChange)21 ArrayList (java.util.ArrayList)19 RenameArguments (org.eclipse.ltk.core.refactoring.participants.RenameArguments)19 JavaModelException (org.eclipse.jdt.core.JavaModelException)15 IPath (org.eclipse.core.runtime.IPath)12 TextChange (org.eclipse.ltk.core.refactoring.TextChange)10 IFile (org.eclipse.core.resources.IFile)9