Search in sources :

Example 71 with RenameJavaElementDescriptor

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

the class RenameTypeTest method helperQualifiedName.

private void helperQualifiedName(String oldName, String newName, String textFileName, String filePatterns) throws Exception {
    ICompilationUnit cu = createCUfromTestFile(getPackageP(), oldName);
    IType classA = getType(cu, oldName);
    String content = getFileContents(getTestPath() + name.getMethodName() + TEST_INPUT_INFIX + textFileName);
    IProject project = classA.getJavaProject().getProject();
    IFile file = project.getFile(textFileName);
    file.create(new ByteArrayInputStream(content.getBytes()), true, null);
    ResourceChangedEvent event = new ResourceChangedEvent(new File(BaseTest.class.getResource("/projects").getFile()), new ProjectItemModifiedEvent(ProjectItemModifiedEvent.EventType.CREATED, "projects", fProject.getElementName(), file.getFullPath().toOSString(), false));
    JavaModelManager.getJavaModelManager().deltaState.resourceChanged(event);
    RenameJavaElementDescriptor descriptor = createRefactoringDescriptor(classA, newName);
    descriptor.setUpdateQualifiedNames(true);
    descriptor.setFileNamePatterns(filePatterns);
    assertEquals("was supposed to pass", null, performRefactoring(descriptor));
    ICompilationUnit newcu = getPackageP().getCompilationUnit(newName + ".java");
    assertEqualLines("invalid renaming", getFileContents(getOutputTestFileName(newName)), newcu.getSource());
    InputStreamReader reader = new InputStreamReader(file.getContents(true));
    StringBuffer newContent = new StringBuffer();
    try {
        int ch;
        while ((ch = reader.read()) != -1) newContent.append((char) ch);
    } finally {
        reader.close();
    }
    String definedContent = getFileContents(getTestPath() + name.getMethodName() + TEST_OUTPUT_INFIX + textFileName);
    assertEqualLines("invalid updating", definedContent, newContent.toString());
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) IFile(org.eclipse.core.resources.IFile) ProjectItemModifiedEvent(org.eclipse.che.api.project.server.notification.ProjectItemModifiedEvent) InputStreamReader(java.io.InputStreamReader) IProject(org.eclipse.core.resources.IProject) IType(org.eclipse.jdt.core.IType) ByteArrayInputStream(java.io.ByteArrayInputStream) ResourceChangedEvent(org.eclipse.che.jdt.core.resources.ResourceChangedEvent) RenameJavaElementDescriptor(org.eclipse.jdt.core.refactoring.descriptors.RenameJavaElementDescriptor) IFile(org.eclipse.core.resources.IFile) File(java.io.File) BaseTest(org.eclipse.che.plugin.java.server.che.BaseTest)

Example 72 with RenameJavaElementDescriptor

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

the class RenameNonPrivateFieldTest method helper1_0.

private void helper1_0(String fieldName, String newFieldName) throws Exception {
    IType classA = getType(createCUfromTestFile(getPackageP(), "A"), "A");
    IField field = classA.getField(fieldName);
    RenameJavaElementDescriptor descriptor = RefactoringSignatureDescriptorFactory.createRenameJavaElementDescriptor(IJavaRefactorings.RENAME_FIELD);
    descriptor.setJavaElement(field);
    descriptor.setUpdateReferences(true);
    descriptor.setNewName(newFieldName);
    RefactoringStatus result = performRefactoring(descriptor);
    assertNotNull("precondition was supposed to fail", result);
}
Also used : RefactoringStatus(org.eclipse.ltk.core.refactoring.RefactoringStatus) IField(org.eclipse.jdt.core.IField) RenameJavaElementDescriptor(org.eclipse.jdt.core.refactoring.descriptors.RenameJavaElementDescriptor) IType(org.eclipse.jdt.core.IType)

Example 73 with RenameJavaElementDescriptor

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

the class RenamePackageTest method testPackageRenameWithResource2.

// ---------- tests -------------
@Test
public void testPackageRenameWithResource2() throws Exception {
    IPackageFragment fragment = getRoot().createPackageFragment("org.test", true, null);
    StringBuffer buf = new StringBuffer();
    buf.append("package org.test;\n");
    buf.append("public class MyClass {\n");
    buf.append("}\n");
    fragment.createCompilationUnit("MyClass.java", buf.toString(), true, null);
    IFile file = ((IFolder) fragment.getResource()).getFile("x.properties");
    byte[] content = "This is about 'org.test' and more".getBytes();
    file.create(new ByteArrayInputStream(content), true, null);
    file.refreshLocal(IResource.DEPTH_ONE, null);
    RenameJavaElementDescriptor descriptor = RefactoringSignatureDescriptorFactory.createRenameJavaElementDescriptor(IJavaRefactorings.RENAME_PACKAGE);
    descriptor.setJavaElement(fragment);
    descriptor.setNewName("org.test2");
    descriptor.setUpdateReferences(true);
    descriptor.setUpdateHierarchy(true);
    descriptor.setUpdateQualifiedNames(true);
    descriptor.setFileNamePatterns("*.properties");
    Refactoring refactoring = createRefactoring(descriptor);
    RefactoringStatus status = performRefactoring(refactoring);
    if (status != null)
        assertTrue(status.toString(), status.isOK());
    RefactoringProcessor processor = ((RenameRefactoring) refactoring).getProcessor();
    IResourceMapper rm = (IResourceMapper) processor.getAdapter(IResourceMapper.class);
    IJavaElementMapper jm = (IJavaElementMapper) processor.getAdapter(IJavaElementMapper.class);
    checkMappingUnchanged(jm, rm, new Object[] { getRoot().getJavaProject(), getRoot() });
    IPackageFragment newFragment = getRoot().getPackageFragment("org.test2");
    IFile newFile = ((IContainer) newFragment.getResource()).getFile(new Path("x.properties"));
    assertEquals("This is about 'org.test2' and more", getContents(newFile));
    checkMappingChanged(jm, rm, new Object[][] { { file, newFile } });
}
Also used : IPath(org.eclipse.core.runtime.IPath) Path(org.eclipse.core.runtime.Path) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) IFile(org.eclipse.core.resources.IFile) RenameRefactoring(org.eclipse.ltk.core.refactoring.participants.RenameRefactoring) RefactoringStatus(org.eclipse.ltk.core.refactoring.RefactoringStatus) IResourceMapper(org.eclipse.ltk.core.refactoring.IResourceMapper) ByteArrayInputStream(java.io.ByteArrayInputStream) RefactoringProcessor(org.eclipse.ltk.core.refactoring.participants.RefactoringProcessor) IJavaElementMapper(org.eclipse.jdt.core.refactoring.IJavaElementMapper) RenameJavaElementDescriptor(org.eclipse.jdt.core.refactoring.descriptors.RenameJavaElementDescriptor) IContainer(org.eclipse.core.resources.IContainer) Refactoring(org.eclipse.ltk.core.refactoring.Refactoring) RenameRefactoring(org.eclipse.ltk.core.refactoring.participants.RenameRefactoring) ProcessorBasedRefactoring(org.eclipse.ltk.core.refactoring.participants.ProcessorBasedRefactoring) IFolder(org.eclipse.core.resources.IFolder) Test(org.junit.Test)

Example 74 with RenameJavaElementDescriptor

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

the class RenameStaticMethodTest method helper2_0.

private void helper2_0(String methodName, String newMethodName, String[] signatures, boolean updateReferences, boolean createDelegate) throws Exception {
    ICompilationUnit cu = createCUfromTestFile(getPackageP(), "A");
    IType classA = getType(cu, "A");
    IMethod method = classA.getMethod(methodName, signatures);
    RenameJavaElementDescriptor descriptor = RefactoringSignatureDescriptorFactory.createRenameJavaElementDescriptor(IJavaRefactorings.RENAME_METHOD);
    descriptor.setUpdateReferences(updateReferences);
    descriptor.setJavaElement(method);
    descriptor.setNewName(newMethodName);
    descriptor.setKeepOriginal(createDelegate);
    descriptor.setDeprecateDelegate(true);
    assertEquals("was supposed to pass", null, performRefactoring(descriptor));
    assertEqualLines("invalid renaming", getFileContents(getOutputTestFileName("A")), cu.getSource());
    assertTrue("anythingToUndo", RefactoringCore.getUndoManager().anythingToUndo());
    assertTrue("! anythingToRedo", !RefactoringCore.getUndoManager().anythingToRedo());
    //assertEquals("1 to undo", 1, Refactoring.getUndoManager().getRefactoringLog().size());
    RefactoringCore.getUndoManager().performUndo(null, new NullProgressMonitor());
    assertEqualLines("invalid undo", getFileContents(getInputTestFileName("A")), cu.getSource());
    assertTrue("! anythingToUndo", !RefactoringCore.getUndoManager().anythingToUndo());
    assertTrue("anythingToRedo", RefactoringCore.getUndoManager().anythingToRedo());
    //assertEquals("1 to redo", 1, Refactoring.getUndoManager().getRedoStack().size());
    RefactoringCore.getUndoManager().performRedo(null, new NullProgressMonitor());
    assertEqualLines("invalid redo", getFileContents(getOutputTestFileName("A")), cu.getSource());
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) IMethod(org.eclipse.jdt.core.IMethod) RenameJavaElementDescriptor(org.eclipse.jdt.core.refactoring.descriptors.RenameJavaElementDescriptor) IType(org.eclipse.jdt.core.IType)

Example 75 with RenameJavaElementDescriptor

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

the class RenameStaticMethodTest method test11.

@Test
public void test11() throws Exception {
    //		printTestDisabledMessage("bug 40452");
    //		if (true)	return;
    IPackageFragment packageA = getRoot().createPackageFragment("a", false, new NullProgressMonitor());
    IPackageFragment packageB = getRoot().createPackageFragment("b", false, new NullProgressMonitor());
    ICompilationUnit cuA = createCUfromTestFile(packageA, "A");
    ICompilationUnit cuB = createCUfromTestFile(packageB, "B");
    IType classA = getType(cuA, "A");
    IMethod method = classA.getMethod("method2", new String[0]);
    RenameJavaElementDescriptor descriptor = RefactoringSignatureDescriptorFactory.createRenameJavaElementDescriptor(IJavaRefactorings.RENAME_METHOD);
    descriptor.setUpdateReferences(true);
    descriptor.setJavaElement(method);
    descriptor.setNewName("fred");
    assertEquals("was supposed to pass", null, performRefactoring(descriptor));
    assertEqualLines("invalid renaming in A", getFileContents(getOutputTestFileName("A")), cuA.getSource());
    assertEqualLines("invalid renaming in B", getFileContents(getOutputTestFileName("B")), cuB.getSource());
}
Also used : NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) IMethod(org.eclipse.jdt.core.IMethod) RenameJavaElementDescriptor(org.eclipse.jdt.core.refactoring.descriptors.RenameJavaElementDescriptor) IType(org.eclipse.jdt.core.IType) Test(org.junit.Test)

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