diff --git a/plugins/maven/src/main/java/org/jetbrains/idea/maven/dom/MavenDomUtil.java b/plugins/maven/src/main/java/org/jetbrains/idea/maven/dom/MavenDomUtil.java index 143136dd6233..e2360f823332 100644 --- a/plugins/maven/src/main/java/org/jetbrains/idea/maven/dom/MavenDomUtil.java +++ b/plugins/maven/src/main/java/org/jetbrains/idea/maven/dom/MavenDomUtil.java @@ -298,13 +298,6 @@ public class MavenDomUtil { return children[index]; } - @Nullable - public static PropertiesFile getPropertiesFile(@NotNull Project project, @NotNull String fileName) { - VirtualFile file = MavenPropertiesVirtualFileSystem.getInstance().findFileByPath(fileName); - if (file == null) return null; - return getPropertiesFile(project, file); - } - @Nullable public static PropertiesFile getPropertiesFile(@NotNull Project project, @NotNull VirtualFile file) { PsiFile psiFile = PsiManager.getInstance(project).findFile(file); @@ -312,12 +305,6 @@ public class MavenDomUtil { return (PropertiesFile)psiFile; } - @Nullable - public static IProperty findProperty(@NotNull Project project, @NotNull String fileName, @NotNull String propName) { - PropertiesFile propertiesFile = getPropertiesFile(project, fileName); - return propertiesFile == null ? null : propertiesFile.findPropertyByKey(propName); - } - @Nullable public static IProperty findProperty(@NotNull Project project, @NotNull VirtualFile file, @NotNull String propName) { PropertiesFile propertiesFile = getPropertiesFile(project, file); diff --git a/plugins/maven/src/main/java/org/jetbrains/idea/maven/dom/references/MavenPropertyPsiReference.java b/plugins/maven/src/main/java/org/jetbrains/idea/maven/dom/references/MavenPropertyPsiReference.java index d6f9888c1954..43d555c9dcf8 100644 --- a/plugins/maven/src/main/java/org/jetbrains/idea/maven/dom/references/MavenPropertyPsiReference.java +++ b/plugins/maven/src/main/java/org/jetbrains/idea/maven/dom/references/MavenPropertyPsiReference.java @@ -185,21 +185,22 @@ public class MavenPropertyPsiReference extends MavenPsiReference { if (result != null) return result; } - IProperty property = MavenDomUtil.findProperty(myProject, MavenPropertiesVirtualFileSystem.SYSTEM_PROPERTIES_FILE, myText); + MavenPropertiesVirtualFileSystem mavenPropertiesVirtualFileSystem = MavenPropertiesVirtualFileSystem.getInstance(); + + IProperty property = mavenPropertiesVirtualFileSystem.findSystemProperty(myProject, myText); if (property != null) return property.getPsiElement(); if (myText.startsWith("env.")) { - property = MavenDomUtil.findProperty(myProject, MavenPropertiesVirtualFileSystem.ENV_PROPERTIES_FILE, - myText.substring("env.".length())); + property = mavenPropertiesVirtualFileSystem.findEnvProperty(myProject, myText.substring("env.".length())); if (property != null) return property.getPsiElement(); } String textWithEnv = "env." + myText; - property = MavenDomUtil.findProperty(myProject, MavenPropertiesVirtualFileSystem.SYSTEM_PROPERTIES_FILE, textWithEnv); + property = mavenPropertiesVirtualFileSystem.findSystemProperty(myProject, textWithEnv); if (property != null) return property.getPsiElement(); - property = MavenDomUtil.findProperty(myProject, MavenPropertiesVirtualFileSystem.ENV_PROPERTIES_FILE, textWithEnv); + property = mavenPropertiesVirtualFileSystem.findEnvProperty(myProject, textWithEnv); if (property != null) return property.getPsiElement(); if (!hasPrefix) { @@ -390,7 +391,8 @@ public class MavenPropertyPsiReference extends MavenPsiReference { } private void collectSystemEnvProperties(String propertiesFileName, @Nullable String prefix, List result) { - PropertiesFile file = MavenDomUtil.getPropertiesFile(myProject, propertiesFileName); + VirtualFile virtualFile = MavenPropertiesVirtualFileSystem.getInstance().findFileByPath(propertiesFileName); + PropertiesFile file = MavenDomUtil.getPropertiesFile(myProject, virtualFile); collectPropertiesFileVariants(file, prefix, result); } diff --git a/plugins/maven/src/main/java/org/jetbrains/idea/maven/utils/MavenUtil.java b/plugins/maven/src/main/java/org/jetbrains/idea/maven/utils/MavenUtil.java index 64976f06bce4..3b661033de7d 100644 --- a/plugins/maven/src/main/java/org/jetbrains/idea/maven/utils/MavenUtil.java +++ b/plugins/maven/src/main/java/org/jetbrains/idea/maven/utils/MavenUtil.java @@ -203,29 +203,6 @@ public class MavenUtil { Notifications.Bus.notify(new Notification(MAVEN_NOTIFICATION_GROUP, title, e.getMessage(), NotificationType.ERROR), project); } - public static Properties getSystemProperties() { - Properties result = (Properties)System.getProperties().clone(); - for (String each : new THashSet((Set)result.keySet())) { - if (each.startsWith("idea.")) { - result.remove(each); - } - } - return result; - } - - public static Properties getEnvProperties() { - Properties reuslt = new Properties(); - for (Map.Entry each : System.getenv().entrySet()) { - if (isMagicalProperty(each.getKey())) continue; - reuslt.put(each.getKey(), each.getValue()); - } - return reuslt; - } - - private static boolean isMagicalProperty(String key) { - return key.startsWith("="); - } - public static File getPluginSystemDir(String folder) { // PathManager.getSystemPath() may return relative path return new File(PathManager.getSystemPath(), "Maven" + "/" + folder).getAbsoluteFile(); diff --git a/plugins/maven/src/main/java/org/jetbrains/idea/maven/vfs/MavenPropertiesVirtualFileSystem.java b/plugins/maven/src/main/java/org/jetbrains/idea/maven/vfs/MavenPropertiesVirtualFileSystem.java index 83d0da5856a2..a1089fcfcf02 100644 --- a/plugins/maven/src/main/java/org/jetbrains/idea/maven/vfs/MavenPropertiesVirtualFileSystem.java +++ b/plugins/maven/src/main/java/org/jetbrains/idea/maven/vfs/MavenPropertiesVirtualFileSystem.java @@ -15,16 +15,19 @@ */ package org.jetbrains.idea.maven.vfs; +import com.intellij.lang.properties.IProperty; +import com.intellij.openapi.project.Project; +import com.intellij.openapi.util.SystemInfo; import com.intellij.openapi.vfs.VirtualFile; import com.intellij.openapi.vfs.VirtualFileManager; import com.intellij.openapi.vfs.ex.dummy.DummyFileSystem; -import gnu.trove.THashMap; import org.jetbrains.annotations.NonNls; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; -import org.jetbrains.idea.maven.utils.MavenUtil; +import org.jetbrains.idea.maven.dom.MavenDomUtil; import java.util.Map; +import java.util.Properties; public class MavenPropertiesVirtualFileSystem extends DummyFileSystem { @NonNls public static final String PROTOCOL = "maven-properties"; @@ -34,7 +37,8 @@ public class MavenPropertiesVirtualFileSystem extends DummyFileSystem { public static final String[] PROPERTIES_FILES = new String[]{SYSTEM_PROPERTIES_FILE, ENV_PROPERTIES_FILE}; - private final Map myFiles = new THashMap(); + private VirtualFile mySystemPropertiesFile; + private VirtualFile myEnvPropertiesFile; public static MavenPropertiesVirtualFileSystem getInstance() { return (MavenPropertiesVirtualFileSystem)VirtualFileManager.getInstance().getFileSystem(PROTOCOL); @@ -45,27 +49,65 @@ public class MavenPropertiesVirtualFileSystem extends DummyFileSystem { return PROTOCOL; } + public VirtualFile getSystemPropertiesFile() { + if (mySystemPropertiesFile == null) { + Properties systemProperties = new Properties(); + + for (Map.Entry entry : System.getProperties().entrySet()) { + if (entry.getKey() instanceof String && entry.getValue() instanceof String) { + String key = (String)entry.getKey(); + if (!key.startsWith("idea.")) { + systemProperties.setProperty(key, (String)entry.getValue()); + } + } + } + + mySystemPropertiesFile = new MavenPropertiesVirtualFile(SYSTEM_PROPERTIES_FILE, systemProperties, this); + } + + return mySystemPropertiesFile; + } + + public VirtualFile getEnvPropertiesFile() { + if (myEnvPropertiesFile == null) { + Properties envProperties = new Properties(); + + for (Map.Entry each : System.getenv().entrySet()) { + if (each.getKey().startsWith("=")) continue; + envProperties.setProperty(each.getKey(), SystemInfo.isWindows ? each.getValue().toUpperCase() : each.getValue()); + } + + myEnvPropertiesFile = new MavenPropertiesVirtualFile(ENV_PROPERTIES_FILE, envProperties, this); + } + + return myEnvPropertiesFile; + } + //@Override //public boolean isPhysical() { // return false; //} public synchronized VirtualFile findFileByPath(@NotNull @NonNls String path) { - VirtualFile result = myFiles.get(path); - if (result != null) return result; - - result = createFile(path); - if (result != null) { - myFiles.put(path, result); + if (path.equals(SYSTEM_PROPERTIES_FILE)) { + return getSystemPropertiesFile(); } - return result; + + if (path.equals(ENV_PROPERTIES_FILE)) { + return getEnvPropertiesFile(); + } + + return null; } @Nullable - private VirtualFile createFile(String path) { - if (SYSTEM_PROPERTIES_FILE.equals(path)) return new MavenPropertiesVirtualFile(path, MavenUtil.getSystemProperties(), this); - if (ENV_PROPERTIES_FILE.equals(path)) return new MavenPropertiesVirtualFile(path, MavenUtil.getEnvProperties(), this); - return null; + public IProperty findSystemProperty(Project project, @NotNull String propertyName) { + return MavenDomUtil.findProperty(project, getSystemPropertiesFile(), propertyName); + } + + @Nullable + public IProperty findEnvProperty(Project project, @NotNull String propertyName) { + return MavenDomUtil.findProperty(project, getEnvPropertiesFile(), propertyName); } //protected void deleteFile(Object requestor, VirtualFile vFile) throws IOException { diff --git a/plugins/maven/src/test/java/org/jetbrains/idea/maven/dom/MavenPropertyCompletionAndResolutionTest.java b/plugins/maven/src/test/java/org/jetbrains/idea/maven/dom/MavenPropertyCompletionAndResolutionTest.java index c04b7c4379c4..d615d908c5ed 100644 --- a/plugins/maven/src/test/java/org/jetbrains/idea/maven/dom/MavenPropertyCompletionAndResolutionTest.java +++ b/plugins/maven/src/test/java/org/jetbrains/idea/maven/dom/MavenPropertyCompletionAndResolutionTest.java @@ -15,9 +15,13 @@ */ package org.jetbrains.idea.maven.dom; +import com.intellij.lang.properties.IProperty; +import com.intellij.openapi.util.SystemInfo; import com.intellij.openapi.vfs.VirtualFile; import com.intellij.psi.PsiDirectory; +import com.intellij.psi.PsiElement; import com.intellij.psi.PsiManager; +import com.intellij.psi.PsiReference; import org.jetbrains.idea.maven.dom.model.MavenDomProfiles; import org.jetbrains.idea.maven.dom.model.MavenDomProfilesModel; import org.jetbrains.idea.maven.dom.model.MavenDomSettingsModel; @@ -689,9 +693,7 @@ public class MavenPropertyCompletionAndResolutionTest extends MavenDomTestCase { "${user.home}"); - assertResolved(myProjectPom, MavenDomUtil.findProperty(myProject, - MavenPropertiesVirtualFileSystem.SYSTEM_PROPERTIES_FILE, - "user.home").getPsiElement()); + assertResolved(myProjectPom, MavenPropertiesVirtualFileSystem.getInstance().findSystemProperty(myProject, "user.home").getPsiElement()); } public void testEnvProperties() throws Exception { @@ -701,9 +703,47 @@ public class MavenPropertyCompletionAndResolutionTest extends MavenDomTestCase { "${env." + getEnvVar() + "}"); - assertResolved(myProjectPom, MavenDomUtil.findProperty(myProject, - MavenPropertiesVirtualFileSystem.ENV_PROPERTIES_FILE, - getEnvVar()).getPsiElement()); + assertResolved(myProjectPom, MavenPropertiesVirtualFileSystem.getInstance().findEnvProperty(myProject, getEnvVar()).getPsiElement()); + } + + public void testUpperCaseEnvPropertiesOnWindows() throws Exception { + if (!SystemInfo.isWindows) return; + + createProjectPom("test" + + "project" + + "1" + + + "${env.PATH}"); + + PsiReference ref = getReferenceAtCaret(myProjectPom); + assertNotNull(ref); + + PsiElement resolved = ref.resolve(); + assertEquals(System.getenv("Path"), ((IProperty)resolved).getValue()); + } + + public void testCaseInsencitiveOnWindows() throws Exception { + if (!SystemInfo.isWindows) return; + + createProjectPom("test" + + "project" + + "1" + + + "${env.PaTH}"); + + assertUnresolved(myProjectPom); + } + + public void testNotUpperCaseEnvPropertiesOnWindows() throws Exception { + if (!SystemInfo.isWindows) return; + + createProjectPom("test" + + "project" + + "1" + + + "${env.Path}"); + + assertUnresolved(myProjectPom); } public void testHighlightUnresolvedProperties() throws Exception { diff --git a/plugins/maven/src/test/java/org/jetbrains/idea/maven/project/MavenProjectReaderTest.java b/plugins/maven/src/test/java/org/jetbrains/idea/maven/project/MavenProjectReaderTest.java index 006275f7646d..d102aecd4d5f 100644 --- a/plugins/maven/src/test/java/org/jetbrains/idea/maven/project/MavenProjectReaderTest.java +++ b/plugins/maven/src/test/java/org/jetbrains/idea/maven/project/MavenProjectReaderTest.java @@ -1294,7 +1294,7 @@ public class MavenProjectReaderTest extends MavenTestCase { } public void testActivatingProfilesByEnvProperty() throws Exception { - String value = MavenUtil.getEnvProperties().getProperty(getEnvVar()); + String value = System.getenv(getEnvVar()); createProjectPom("" + " " +