diff --git a/src/main/java/io/openliberty/tools/intellij/LibertyExplorer.java b/src/main/java/io/openliberty/tools/intellij/LibertyExplorer.java index a831ee6a3..30631e326 100644 --- a/src/main/java/io/openliberty/tools/intellij/LibertyExplorer.java +++ b/src/main/java/io/openliberty/tools/intellij/LibertyExplorer.java @@ -130,11 +130,10 @@ public static Tree buildTree(Project project, Color backgroundColor) { for (BuildFile buildFile : mavenBuildFiles) { // create a new Liberty project - PsiFile psiFile = buildFile.getBuildFile(); + VirtualFile virtualFile = buildFile.getBuildFile(); String projectName = null; - VirtualFile virtualFile = psiFile.getVirtualFile(); if (virtualFile == null) { - LOGGER.error(String.format("Could not resolve current Maven project %s", psiFile)); + LOGGER.error(String.format("Could not resolve current Maven project %s", virtualFile)); break; } LibertyModuleNode node; @@ -152,7 +151,7 @@ public static Tree buildTree(Project project, Color backgroundColor) { } boolean validContainerVersion = buildFile.isValidContainerVersion(); - LibertyModule module = libertyModules.addLibertyModule(new LibertyModule(project, psiFile.getVirtualFile(), projectName, Constants.LIBERTY_MAVEN_PROJECT, validContainerVersion)); + LibertyModule module = libertyModules.addLibertyModule(new LibertyModule(project, virtualFile, projectName, Constants.LIBERTY_MAVEN_PROJECT, validContainerVersion)); node = new LibertyModuleNode(module); top.add(node); @@ -176,9 +175,8 @@ public static Tree buildTree(Project project, Color backgroundColor) { } for (BuildFile buildFile : gradleBuildFiles) { - PsiFile psiFile = buildFile.getBuildFile(); + VirtualFile virtualFile = buildFile.getBuildFile(); String projectName = null; - VirtualFile virtualFile = psiFile.getVirtualFile(); if (virtualFile == null) { LOGGER.error(String.format("Could not resolve current Gradle project %s", buildFile)); break; diff --git a/src/main/java/io/openliberty/tools/intellij/LibertyModule.java b/src/main/java/io/openliberty/tools/intellij/LibertyModule.java index a9eb9fb0f..d51ff1217 100644 --- a/src/main/java/io/openliberty/tools/intellij/LibertyModule.java +++ b/src/main/java/io/openliberty/tools/intellij/LibertyModule.java @@ -51,7 +51,7 @@ public LibertyModule(Project project, VirtualFile buildFile, String name, String public LibertyModule(Project project, BuildFile buildFile) { this(project); - this.buildFile = buildFile.getBuildFile().getVirtualFile(); + this.buildFile = buildFile.getBuildFile(); this.name = buildFile.getProjectName(); this.projectType = buildFile.getProjectType(); this.validContainerVersion = buildFile.isValidContainerVersion(); diff --git a/src/main/java/io/openliberty/tools/intellij/actions/LibertyProjectAction.java b/src/main/java/io/openliberty/tools/intellij/actions/LibertyProjectAction.java index 2bc7cce7d..91d647e0c 100644 --- a/src/main/java/io/openliberty/tools/intellij/actions/LibertyProjectAction.java +++ b/src/main/java/io/openliberty/tools/intellij/actions/LibertyProjectAction.java @@ -88,7 +88,7 @@ protected final List getBuildFileList(Project project) { // Resolve project names and add to list mavenBuildFiles.forEach(mavenBuildFile -> { // resolve project name - VirtualFile virtualFile = mavenBuildFile.getBuildFile().getVirtualFile(); + VirtualFile virtualFile = mavenBuildFile.getBuildFile(); if (virtualFile == null) { LOGGER.error(String.format("Could not resolve Maven project for build file: %s", mavenBuildFile.getBuildFile())); } else { @@ -103,7 +103,7 @@ protected final List getBuildFileList(Project project) { }); gradleBuildFiles.forEach(gradleBuildFile -> { - VirtualFile virtualFile = gradleBuildFile.getBuildFile().getVirtualFile(); + VirtualFile virtualFile = gradleBuildFile.getBuildFile(); if (virtualFile == null) { LOGGER.error(String.format("Could not resolve Gradle project for build file: %s", gradleBuildFile.getBuildFile())); } else { diff --git a/src/main/java/io/openliberty/tools/intellij/util/BuildFile.java b/src/main/java/io/openliberty/tools/intellij/util/BuildFile.java index 13542c311..5fe28a584 100644 --- a/src/main/java/io/openliberty/tools/intellij/util/BuildFile.java +++ b/src/main/java/io/openliberty/tools/intellij/util/BuildFile.java @@ -9,13 +9,14 @@ *******************************************************************************/ package io.openliberty.tools.intellij.util; +import com.intellij.openapi.vfs.VirtualFile; import com.intellij.psi.PsiFile; /** * Defines a BuildFile object */ public class BuildFile { - public PsiFile buildFile; + public VirtualFile buildFile; public boolean validBuildFile; public boolean validContainerVersion; @@ -37,9 +38,9 @@ public BuildFile(boolean validBuildFile, boolean validContainerVersion) { this.buildFile = null; } - public PsiFile getBuildFile() { return this.buildFile; } + public VirtualFile getBuildFile() { return this.buildFile; } - public void setBuildFile(PsiFile buildFile) { + public void setBuildFile(VirtualFile buildFile) { this.buildFile = buildFile; } diff --git a/src/main/java/io/openliberty/tools/intellij/util/LibertyGradleUtil.java b/src/main/java/io/openliberty/tools/intellij/util/LibertyGradleUtil.java index 10129f361..08518f8b8 100644 --- a/src/main/java/io/openliberty/tools/intellij/util/LibertyGradleUtil.java +++ b/src/main/java/io/openliberty/tools/intellij/util/LibertyGradleUtil.java @@ -91,8 +91,8 @@ public static String fileToString(String path) throws IOException { * validContainerVersion true if plugin version is valid for dev mode in containers * @throws IOException */ - public static BuildFile validBuildGradle(PsiFile file) throws IOException { - String buildFile = fileToString(file.getVirtualFile().getPath()); + public static BuildFile validBuildGradle(VirtualFile file) throws IOException { + String buildFile = fileToString(file.getPath()); if (buildFile.isEmpty()) { return (new BuildFile(false, false)); } // instead of iterating over capture groups in a plugin{}, search directly diff --git a/src/main/java/io/openliberty/tools/intellij/util/LibertyMavenUtil.java b/src/main/java/io/openliberty/tools/intellij/util/LibertyMavenUtil.java index 8dfc5eadf..f51d37c14 100644 --- a/src/main/java/io/openliberty/tools/intellij/util/LibertyMavenUtil.java +++ b/src/main/java/io/openliberty/tools/intellij/util/LibertyMavenUtil.java @@ -84,12 +84,12 @@ public static String getProjectNameFromPom(VirtualFile file) throws ParserConfig * @throws IOException * @throws SAXException */ - public static BuildFile validPom(PsiFile file) throws ParserConfigurationException, IOException, SAXException { + public static BuildFile validPom(VirtualFile file) throws ParserConfigurationException, IOException, SAXException { BuildFile buildFile = new BuildFile(false, false); DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); - File inputFile = new File(file.getVirtualFile().getPath()); + File inputFile = new File(file.getPath()); Document doc = builder.parse(inputFile); doc.getDocumentElement().normalize(); diff --git a/src/main/java/io/openliberty/tools/intellij/util/LibertyProjectUtil.java b/src/main/java/io/openliberty/tools/intellij/util/LibertyProjectUtil.java index a75fbf51e..ec1e60c9d 100644 --- a/src/main/java/io/openliberty/tools/intellij/util/LibertyProjectUtil.java +++ b/src/main/java/io/openliberty/tools/intellij/util/LibertyProjectUtil.java @@ -13,8 +13,8 @@ import com.intellij.openapi.actionSystem.DataContext; import com.intellij.openapi.diagnostic.Logger; import com.intellij.openapi.project.Project; +import com.intellij.openapi.vfs.VirtualFile; import com.intellij.openapi.wm.ToolWindow; -import com.intellij.psi.PsiFile; import com.intellij.psi.search.FilenameIndex; import com.intellij.psi.search.GlobalSearchScope; import com.intellij.terminal.JBTerminalWidget; @@ -32,31 +32,28 @@ import javax.xml.parsers.ParserConfigurationException; import java.io.File; import java.io.IOException; -import java.util.ArrayList; -import java.util.Set; -import java.util.Collections; -import java.util.HashSet; +import java.util.*; public class LibertyProjectUtil { private static Logger LOGGER = Logger.getInstance(LibertyProjectUtil.class); enum BuildFileFilter { ADDABLE { - public boolean matches(Project project, BuildFile buildFile, PsiFile psiFile) { - return !LIST.matches(project, buildFile, psiFile); + public boolean matches(Project project, BuildFile buildFile, VirtualFile virtualFile) { + return !LIST.matches(project, buildFile, virtualFile); } }, REMOVABLE { - public boolean matches(Project project, BuildFile buildFile, PsiFile psiFile) { - return isCustomLibertyProject(project, psiFile) && !(buildFile.isValidBuildFile() || isLibertyProject(psiFile)); + public boolean matches(Project project, BuildFile buildFile, VirtualFile virtualFile) { + return isCustomLibertyProject(project, virtualFile) && !(buildFile.isValidBuildFile() || isLibertyProject(virtualFile)); } }, LIST { - public boolean matches(Project project, BuildFile buildFile, PsiFile psiFile) { - return buildFile.isValidBuildFile() || isLibertyProject(psiFile) || isCustomLibertyProject(project, psiFile); + public boolean matches(Project project, BuildFile buildFile, VirtualFile virtualFile) { + return buildFile.isValidBuildFile() || isLibertyProject(virtualFile) || isCustomLibertyProject(project, virtualFile); } }; - public abstract boolean matches(Project project, BuildFile buildFile, PsiFile psiFile); + public abstract boolean matches(Project project, BuildFile buildFile, VirtualFile virtualFile); } /** REVISIT: In memory collection of Liberty projects but need to persist. **/ @@ -82,9 +79,9 @@ public static void removeCustomLibertyProject(LibertyModule libertyModule) { LibertyModules.getInstance().removeLibertyModule(libertyModule); } - public static boolean isCustomLibertyProject(Project project, PsiFile buildFile) { + public static boolean isCustomLibertyProject(Project project, VirtualFile buildFile) { final LibertyProjectSettings state = LibertyProjectSettings.getInstance(project); - return state.getCustomLibertyProjects().contains(buildFile.getVirtualFile().getPath()); + return state.getCustomLibertyProjects().contains(buildFile.getPath()); } /** @@ -187,8 +184,8 @@ private static ArrayList getBuildFiles(Project project, String buildF ArrayList buildFiles = new ArrayList(); if (buildFileType.equals(Constants.LIBERTY_MAVEN_PROJECT)) { - PsiFile[] mavenFiles = FilenameIndex.getFilesByName(project, "pom.xml", GlobalSearchScope.projectScope(project)); - for (PsiFile mavenFile : mavenFiles) { + Collection mavenFiles = FilenameIndex.getVirtualFilesByName("pom.xml", GlobalSearchScope.projectScope(project)); + for (VirtualFile mavenFile : mavenFiles) { BuildFile buildFile = LibertyMavenUtil.validPom(mavenFile); // check if valid pom.xml, or if part of Liberty project if (filter.matches(project, buildFile, mavenFile)) { @@ -197,8 +194,8 @@ private static ArrayList getBuildFiles(Project project, String buildF } } } else if (buildFileType.equals(Constants.LIBERTY_GRADLE_PROJECT)) { - PsiFile[] gradleFiles = FilenameIndex.getFilesByName(project, "build.gradle", GlobalSearchScope.projectScope(project)); - for (PsiFile gradleFile : gradleFiles) { + Collection gradleFiles = FilenameIndex.getVirtualFilesByName("build.gradle", GlobalSearchScope.projectScope(project)); + for (VirtualFile gradleFile : gradleFiles) { try { BuildFile buildFile = LibertyGradleUtil.validBuildGradle(gradleFile); // check if valid build.gradle, or if part of Liberty project @@ -219,8 +216,8 @@ private static ArrayList getBuildFiles(Project project, String buildF * @param buildFile maven or gradle build file in the form of PsiFile * @return true if the project contains src/main/liberty/config/server.xml relative to the build file; false otherwise */ - private static boolean isLibertyProject(PsiFile buildFile) { - String rootDir = buildFile.getVirtualFile().getParent().getPath(); + private static boolean isLibertyProject(VirtualFile buildFile) { + String rootDir = buildFile.getParent().getPath(); return new File(rootDir, "src/main/liberty/config/server.xml").exists(); }