修复获取路径可能存在空指针问题

This commit is contained in:
makejava 2020-08-03 17:34:00 +08:00
parent 32b7f77d94
commit 16b3802e18
2 changed files with 18 additions and 9 deletions

View File

@ -34,7 +34,12 @@ public final class ModuleUtils {
public static VirtualFile getSourcePath(@NotNull Module module) { public static VirtualFile getSourcePath(@NotNull Module module) {
List<VirtualFile> virtualFileList = ModuleRootManager.getInstance(module).getSourceRoots(JavaSourceRootType.SOURCE); List<VirtualFile> virtualFileList = ModuleRootManager.getInstance(module).getSourceRoots(JavaSourceRootType.SOURCE);
if (CollectionUtil.isEmpty(virtualFileList)) { if (CollectionUtil.isEmpty(virtualFileList)) {
return VirtualFileManager.getInstance().findFileByUrl(String.format("file://%s", ModuleUtil.getModuleDirPath(module))); String modulePath = ModuleUtil.getModuleDirPath(module);
int index = modulePath.indexOf(".idea");
if (index > 0) {
modulePath = modulePath.substring(0, index - 1);
}
return VirtualFileManager.getInstance().findFileByUrl(String.format("file://%s", modulePath));
} }
return virtualFileList.get(0); return virtualFileList.get(0);
} }

View File

@ -7,7 +7,6 @@ import com.intellij.openapi.module.Module;
import com.intellij.openapi.module.ModuleManager; import com.intellij.openapi.module.ModuleManager;
import com.intellij.openapi.project.Project; import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.Messages; import com.intellij.openapi.ui.Messages;
import com.intellij.openapi.vfs.LocalFileSystem;
import com.intellij.openapi.vfs.VirtualFile; import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.PsiPackage; import com.intellij.psi.PsiPackage;
import com.sjhy.plugin.config.Settings; import com.sjhy.plugin.config.Settings;
@ -18,10 +17,7 @@ import com.sjhy.plugin.entity.Template;
import com.sjhy.plugin.entity.TemplateGroup; import com.sjhy.plugin.entity.TemplateGroup;
import com.sjhy.plugin.service.CodeGenerateService; import com.sjhy.plugin.service.CodeGenerateService;
import com.sjhy.plugin.service.TableInfoService; import com.sjhy.plugin.service.TableInfoService;
import com.sjhy.plugin.tool.CacheDataUtils; import com.sjhy.plugin.tool.*;
import com.sjhy.plugin.tool.CurrGroupUtils;
import com.sjhy.plugin.tool.ModuleUtils;
import com.sjhy.plugin.tool.StringUtils;
import javax.swing.*; import javax.swing.*;
import java.awt.*; import java.awt.*;
@ -313,7 +309,7 @@ public class SelectSavePath extends JDialog {
//选择路径 //选择路径
pathChooseButton.addActionListener(e -> { pathChooseButton.addActionListener(e -> {
//将当前选中的model设置为基础路径 //将当前选中的model设置为基础路径
VirtualFile path = LocalFileSystem.getInstance().findFileByPath(project.getBasePath()); VirtualFile path = ProjectUtils.getBaseDir(project);
Module module = getSelectModule(); Module module = getSelectModule();
if (module != null) { if (module != null) {
path = ModuleUtils.getSourcePath(module); path = ModuleUtils.getSourcePath(module);
@ -391,9 +387,17 @@ public class SelectSavePath extends JDialog {
*/ */
private String getBasePath() { private String getBasePath() {
Module module = getSelectModule(); Module module = getSelectModule();
String baseDir = project.getBasePath(); VirtualFile baseVirtualFile = ProjectUtils.getBaseDir(project);
if (baseVirtualFile == null) {
Messages.showWarningDialog("无法获取到项目基本路径!", MsgValue.TITLE_INFO);
return "";
}
String baseDir = baseVirtualFile.getPath();
if (module != null) { if (module != null) {
baseDir = ModuleUtils.getSourcePath(module).getPath(); VirtualFile virtualFile = ModuleUtils.getSourcePath(module);
if (virtualFile != null) {
baseDir = virtualFile.getPath();
}
} }
return baseDir; return baseDir;
} }