ICompilationUnit cu = ... // an ICompilationUnit to rename RefactoringContribution contribution = RefactoringCore.getRefactoringContribution(IJavaRefactorings .RENAME_COMPILATION_UNIT); RenameJavaElementDescriptor descriptor = (RenameJavaElementDescriptor) contribution.createDescriptor(); descriptor.setProject(cu.getResource().getProject().getName( )); descriptor.setNewName("NewClass"); // new name for a Class descriptor.setJavaElement(cu); RefactoringStatus status = new RefactoringStatus(); try { Refactoring refactoring = descriptor.createRefactoring(status); IProgressMonitor monitor = new NullProgressMonitor(); refactoring.checkInitialConditions(monitor); refactoring.checkFinalConditions(monitor); Change change = refactoring.createChange(monitor); change.perform(monitor); } catch (CoreException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); }
private int getFormattingKind(ICompilationUnit cu, boolean includeComments) { int kind = includeComments ? CodeFormatter.F_INCLUDE_COMMENTS : 0; if (cu.getResource() != null && cu.getResource().getName().equals(IModule.MODULE_INFO_JAVA)) { kind |= CodeFormatter.K_MODULE_INFO; } else { kind |= CodeFormatter.K_COMPILATION_UNIT; } return kind; }
/** * Returns uri for a compilation unit * * @param cu * @return */ public static String toURI(ICompilationUnit cu) { return getFileURI(cu.getResource()); }
public static IFile getFile(ICompilationUnit cu) { IResource resource = cu.getResource(); if (resource != null && resource.getType() == IResource.FILE) { return (IFile) resource; } else { return null; } }
@Override public IBuffer createBuffer(ICompilationUnit workingCopy) { ICompilationUnit original = workingCopy.getPrimary(); IResource resource = original.getResource(); if (resource instanceof IFile) { return new DocumentAdapter(workingCopy, (IFile) resource); } return DocumentAdapter.Null; }
@Override public IBuffer createBuffer(ICompilationUnit workingCopy) { ICompilationUnit original= workingCopy.getPrimary(); IResource resource= original.getResource(); if (resource instanceof IFile) return new DocumentAdapter(workingCopy, (IFile) resource); return DocumentAdapter.NULL; } });
private IFile getFile(ICompilationUnit unit) { unit= unit.getPrimary(); IResource resource= unit.getResource(); if (resource != null && resource.getType() == IResource.FILE) return (IFile)resource; return null; }
@Override public String getText(Object element) { NonNLSElement nlsel= (NonNLSElement)element; String elementName= BasicElementLabels.getPathLabel(nlsel.cu.getResource().getFullPath(), false); return Messages.format( ActionMessages.FindStringsToExternalizeAction_foundStrings, new Object[] {Integer.valueOf(nlsel.count), elementName} ); } @Override
public void copy(ICompilationUnit unit, CopyArguments javaArgs, CopyArguments resourceArgs) { add(unit, javaArgs, null); ResourceMapping mapping= JavaElementResourceMapping.create(unit); if (mapping != null) { add(mapping, resourceArgs, null); } if (unit.getResource() != null) { getResourceModifications().addCopyDelta(unit.getResource(), resourceArgs); } }
private void isValid(RefactoringStatus result, IPackageFragment pack, IProgressMonitor pm) throws JavaModelException { ICompilationUnit[] units= pack.getCompilationUnits(); pm.beginTask("", units.length); //$NON-NLS-1$ for (int i= 0; i < units.length; i++) { pm.subTask(Messages.format(RefactoringCoreMessages.RenamePackageChange_checking_change, JavaElementLabels.getElementLabel(pack, JavaElementLabels.ALL_DEFAULT))); checkIfModifiable(result, units[i].getResource(), VALIDATE_NOT_READ_ONLY | VALIDATE_NOT_DIRTY); pm.worked(1); } pm.done(); }
/** * Returns if a CU can be edited. */ public static boolean isEditable(ICompilationUnit cu) { Assert.isNotNull(cu); IResource resource= cu.getPrimary().getResource(); return (resource.exists() && !resource.getResourceAttributes().isReadOnly()); }
private static void convertCUResourceChange(WorkspaceEdit edit, RenameCompilationUnitChange cuChange) { ICompilationUnit modifiedCU = (ICompilationUnit) cuChange.getModifiedElement(); RenameFile rf = new RenameFile(); String newCUName = cuChange.getNewName(); IPath currentPath = modifiedCU.getResource().getLocation(); rf.setOldUri(ResourceUtils.fixURI(modifiedCU.getResource().getRawLocationURI())); IPath newPath = currentPath.removeLastSegments(1).append(newCUName); rf.setNewUri(ResourceUtils.fixURI(newPath.toFile().toURI())); edit.getDocumentChanges().add(Either.forRight(rf)); }
protected ISchedulingRule getSchedulingRule() { IResource resource = getCompilationUnit().getResource(); IWorkspace workspace = resource.getWorkspace(); return workspace.getRuleFactory().modifyRule(resource); } /**
@Override public IResource getRefactoredResource(IResource element) { if (element instanceof IFile) { if (Checks.isTopLevel(fType) && element.equals(fType.getResource())) return getNewCompilationUnit().getResource(); } return element; }
@Override protected TextChange createTextChange() throws CoreException { init(fRefactoring); fRefactoringStatus = fRefactoring.checkFinalConditions(new NullProgressMonitor()); if (fRefactoringStatus.hasFatalError()) { TextFileChange dummyChange = new TextFileChange("fatal error", (IFile) getCompilationUnit().getResource()); //$NON-NLS-1$ dummyChange.setEdit(new InsertEdit(0, "")); //$NON-NLS-1$ return dummyChange; } return (TextChange) fRefactoring.createChange(new NullProgressMonitor()); }
@Override protected TextChange createTextChange() throws CoreException { init(fRefactoring); fRefactoringStatus= fRefactoring.checkFinalConditions(new NullProgressMonitor()); if (fRefactoringStatus.hasFatalError()) { TextFileChange dummyChange= new TextFileChange("fatal error", (IFile) getCompilationUnit().getResource()); //$NON-NLS-1$ dummyChange.setEdit(new InsertEdit(0, "")); //$NON-NLS-1$ return dummyChange; } return (TextChange) fRefactoring.createChange(new NullProgressMonitor()); }
private IAnnotationModel isInJavaAnnotationModel(ICompilationUnit original) { if (original.isWorkingCopy()) { FileEditorInput editorInput= new FileEditorInput((IFile) original.getResource()); return JavaPlugin.getDefault().getCompilationUnitDocumentProvider().getAnnotationModel(editorInput); } return null; }
private IAnnotationModel isInJavaAnnotationModel(ICompilationUnit original) { if (original.isWorkingCopy()) { FileEditorInput editorInput= new FileEditorInput((IFile) original.getResource()); return JavaPlugin.getDefault().getCompilationUnitDocumentProvider().getAnnotationModel(editorInput); } return null; }
private RefactoringStatus checkNewPathValidity() { IContainer c= fType.getCompilationUnit().getResource().getParent(); String notRename= RefactoringCoreMessages.RenameTypeRefactoring_will_not_rename; IStatus status= c.getWorkspace().validateName(getNewElementName(), IResource.FILE); if (status.getSeverity() == IStatus.ERROR) return RefactoringStatus.createWarningStatus(status.getMessage() + ". " + notRename); //$NON-NLS-1$ status= c.getWorkspace().validatePath(createNewPath(getNewElementName()), IResource.FILE); if (status.getSeverity() == IStatus.ERROR) return RefactoringStatus.createWarningStatus(status.getMessage() + ". " + notRename); //$NON-NLS-1$ return new RefactoringStatus(); }
private RefactoringStatus checkTypesImportedInCu() throws CoreException { IImportDeclaration imp = getImportedType(fType.getCompilationUnit(), getNewElementName()); if (imp == null) { return null; } String msg = Messages.format(RefactoringCoreMessages.RenameTypeRefactoring_imported, new Object[] { getNewElementLabel(), BasicElementLabels.getPathLabel(fType.getCompilationUnit().getResource().getFullPath(), false) }); IJavaElement grandParent = imp.getParent().getParent(); if (grandParent instanceof ICompilationUnit) { return RefactoringStatus.createErrorStatus(msg, JavaStatusContext.create(imp)); } return null; }