public boolean initialize(Object element) { fgInstance = this; fElements.add(element); fArguments.add(getArguments()); if (element instanceof IJavaElement) fHandles.add(((IJavaElement) element).getHandleIdentifier()); else fHandles.add(((IResource) element).getFullPath().toString()); IJavaElementMapper updating = (IJavaElementMapper) getProcessor().getAdapter(IJavaElementMapper.class); if ((updating != null) && getArguments() instanceof RenameTypeArguments) { RenameTypeArguments arguments = (RenameTypeArguments) getArguments(); if (arguments.getUpdateSimilarDeclarations()) { IJavaElement[] elements = arguments.getSimilarDeclarations(); for (int i = 0; i < elements.length; i++) { IJavaElement updated = updating.getRefactoredJavaElement(elements[i]); if (updated != null) { fSimilarToHandle.put(elements[i].getHandleIdentifier(), getKey(updated)); fSimilarToNewName.put(elements[i].getHandleIdentifier(), updated.getElementName()); } } } } return true; }
private void checkMappingChanged( IJavaElementMapper jm, IResourceMapper rm, Object[][] resOrJeToChangeds) { for (int i = 0; i < resOrJeToChangeds.length; i++) { Object[] resOrJeToChanged = resOrJeToChangeds[i]; Object resOrJE = resOrJeToChanged[0]; Object changed = resOrJeToChanged[1]; if (resOrJE instanceof IJavaElement) { IJavaElement javaElement = (IJavaElement) resOrJE; TestCase.assertEquals(changed, jm.getRefactoredJavaElement(javaElement)); resOrJE = javaElement.getResource(); changed = ((IJavaElement) resOrJeToChanged[1]).getResource(); } if (resOrJE instanceof IResource) { IResource resource = (IResource) resOrJE; TestCase.assertEquals(changed, rm.getRefactoredResource(resource)); } } }
private void checkMappedSimilarElementsExist(Refactoring ref) { RenameTypeProcessor rtp = (RenameTypeProcessor) ((RenameRefactoring) ref).getProcessor(); IJavaElementMapper mapper = (IJavaElementMapper) rtp.getAdapter(IJavaElementMapper.class); IJavaElement[] similarElements = rtp.getSimilarElements(); if (similarElements == null) return; for (int i = 0; i < similarElements.length; i++) { IJavaElement element = similarElements[i]; if (!(element instanceof ILocalVariable)) { IJavaElement newElement = mapper.getRefactoredJavaElement(element); TestCase.assertTrue(newElement.exists()); Assert.assertFalse(element.exists()); } } }
private void checkMappingUnchanged(IJavaElementMapper jm, IResourceMapper rm, Object[] resOrJEs) { for (int i = 0; i < resOrJEs.length; i++) { Object resOrJE = resOrJEs[i]; if (resOrJE instanceof IJavaElement) { IJavaElement javaElement = (IJavaElement) resOrJE; resOrJE = javaElement.getResource(); TestCase.assertEquals(javaElement, jm.getRefactoredJavaElement(javaElement)); } if (resOrJE instanceof IResource) { IResource resource = (IResource) resOrJE; TestCase.assertEquals(resource, rm.getRefactoredResource(resource)); } } }
@Test public void testPackageRenameWithResource1() 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(" org.test.MyClass me;\n"); buf.append("}\n"); fragment.createCompilationUnit("MyClass.java", buf.toString(), true, null); IFile file = ((IFolder) getRoot().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.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(), file}); IFile newFile = ((IContainer) getRoot().getResource()).getFile(new Path("x.properties")); assertEquals("This is about 'org.test2' and more", getContents(newFile)); checkMappingChanged( jm, rm, new Object[][] {{fragment, getRoot().getPackageFragment("org.test2")}}); }
@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}}); }
@Test public void test1() throws Exception { fIsPreDeltaTest = true; RenamePackageProcessor proc = helper2(new String[] {"r"}, new String[][] {{"A"}}, "p1"); IJavaElementMapper jm = (IJavaElementMapper) proc.getAdapter(IJavaElementMapper.class); IResourceMapper rm = (IResourceMapper) proc.getAdapter(IResourceMapper.class); IJavaModel javaModel = JavaCore.create(ResourcesPlugin.getWorkspace().getRoot()); IJavaProject project = getRoot().getJavaProject(); IFile _project = project.getProject().getFile(".project"); checkMappingUnchanged( jm, rm, new Object[] { project, _project, getRoot(), getPackageP(), getRoot().getPackageFragment("inexistent"), getRoot().getPackageFragment("r.inexistent"), getRoot().getPackageFragment("p1.inexistent") }); IPackageFragment r = getRoot().getPackageFragment("p1"); ICompilationUnit r_A = r.getCompilationUnit("A.java"); IType r_A_A = r_A.getType("A"); IField r_A_A_a = r_A_A.getField("a"); IPackageFragment p1 = getRoot().getPackageFragment("p1"); ICompilationUnit p1_A = p1.getCompilationUnit("A.java"); IType p1_A_A = p1_A.getType("A"); IField p1_A_A_a = p1_A_A.getField("a"); checkMappingChanged( jm, rm, new Object[][] { {r, p1}, {r_A, p1_A}, {r_A_A, p1_A_A}, {r_A_A_a, p1_A_A_a}, }); }