private static boolean changePackagePrefixes( PsiPackage psiPackage, final String oldQualifiedName, final String newQualifiedName) { final Module[] modules = ModuleManager.getInstance(psiPackage.getProject()).getModules(); List<ModifiableRootModel> modelsToCommit = new ArrayList<ModifiableRootModel>(); for (final Module module : modules) { boolean anyChange = false; final ModifiableRootModel rootModel = ModuleRootManager.getInstance(module).getModifiableModel(); for (final ContentEntry contentEntry : rootModel.getContentEntries()) { for (final SourceFolder sourceFolder : contentEntry.getSourceFolders(JavaModuleSourceRootTypes.SOURCES)) { final String packagePrefix = sourceFolder.getPackagePrefix(); if (packagePrefix.startsWith(oldQualifiedName)) { sourceFolder.setPackagePrefix( newQualifiedName + packagePrefix.substring(oldQualifiedName.length())); anyChange = true; } } } if (anyChange) { modelsToCommit.add(rootModel); } else { rootModel.dispose(); } } if (!modelsToCommit.isEmpty()) { ModifiableRootModel[] rootModels = modelsToCommit.toArray(new ModifiableRootModel[modelsToCommit.size()]); if (rootModels.length > 0) { ModifiableModelCommitter.multiCommit( rootModels, ModuleManager.getInstance(rootModels[0].getProject()).getModifiableModel()); } return true; } else { return false; } }
@Override public VirtualFile[] occursInPackagePrefixes(PsiPackage psiPackage) { List<VirtualFile> result = new ArrayList<VirtualFile>(); final Module[] modules = ModuleManager.getInstance(psiPackage.getProject()).getModules(); for (final Module module : modules) { for (final ContentEntry contentEntry : ModuleRootManager.getInstance(module).getContentEntries()) { final List<SourceFolder> sourceFolders = contentEntry.getSourceFolders(JavaModuleSourceRootTypes.SOURCES); for (final SourceFolder sourceFolder : sourceFolders) { final String packagePrefix = sourceFolder.getPackagePrefix(); if (packagePrefix.startsWith(psiPackage.getQualifiedName())) { final VirtualFile file = sourceFolder.getFile(); if (file != null) { result.add(file); } } } } } return VfsUtil.toVirtualFileArray(result); }