diff --git a/platform/core-api/src/com/intellij/openapi/project/Project.java b/platform/core-api/src/com/intellij/openapi/project/Project.java index 5b26daecad15..168d5d62d03c 100644 --- a/platform/core-api/src/com/intellij/openapi/project/Project.java +++ b/platform/core-api/src/com/intellij/openapi/project/Project.java @@ -18,6 +18,8 @@ package com.intellij.openapi.project; import com.intellij.openapi.components.ComponentManager; import com.intellij.openapi.extensions.AreaInstance; import com.intellij.openapi.vfs.VirtualFile; +import com.intellij.util.SystemDependent; +import com.intellij.util.SystemIndependent; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -52,12 +54,13 @@ public interface Project extends ComponentManager, AreaInstance { VirtualFile getBaseDir(); /** - * Returns a system-independent path to a project base directory (see {@linkplain #getBaseDir()}).
+ * Returns a path to a project base directory (see {@linkplain #getBaseDir()}).
* Returns {@code null} for default project. * * @return a path to a project base directory, or {@code null} for default project */ @Nullable + @SystemIndependent String getBasePath(); /** @@ -74,9 +77,10 @@ public interface Project extends ComponentManager, AreaInstance { VirtualFile getProjectFile(); /** - * @return a system-independent path to project file (see {@linkplain #getProjectFile()}) or {@code null} for default project. + * @return a path to project file (see {@linkplain #getProjectFile()}) or {@code null} for default project. */ @Nullable + @SystemIndependent String getProjectFilePath(); /** @@ -88,6 +92,7 @@ public interface Project extends ComponentManager, AreaInstance { * @return presentable project path */ @Nullable + @SystemDependent default String getPresentableUrl() { return null; } diff --git a/platform/core-api/src/com/intellij/util/PathUtil.java b/platform/core-api/src/com/intellij/util/PathUtil.java index c2d1c2750a3a..4e1ac15d203b 100644 --- a/platform/core-api/src/com/intellij/util/PathUtil.java +++ b/platform/core-api/src/com/intellij/util/PathUtil.java @@ -125,6 +125,21 @@ public class PathUtil { return path == null ? null : FileUtilRt.toSystemDependentName(path); } + /** + * Makes sure that the given path is system-independent (with / separators). + * + * @param path Path + * @throws IllegalArgumentException + * @see SystemDependent + * @see SystemIndependent + */ + public static void assertSystemIndependentName(@Nullable String path) { + // A simplified check that test only for a drive letter (to improve the performance and avoid possible false-negatives). + if (path != null && path.length() > 3 && path.substring(1).startsWith(":\\")) { + throw new IllegalArgumentException("System-dependent path: " + path); + } + } + @NotNull public static String driveLetterToLowerCase(@NotNull String path) { if (SystemInfo.isWindows && path.length() >= 2 && Character.isUpperCase(path.charAt(0)) && path.charAt(1) == ':') { diff --git a/platform/core-api/src/com/intellij/util/SystemDependent.java b/platform/core-api/src/com/intellij/util/SystemDependent.java new file mode 100644 index 000000000000..17c60c6b32fb --- /dev/null +++ b/platform/core-api/src/com/intellij/util/SystemDependent.java @@ -0,0 +1,32 @@ +/* + * Copyright 2000-2017 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.intellij.util; + +import java.lang.annotation.*; + +/** + * Designates a system-dependent path, which can contain \ separators. + *

+ * This annotation doesn't add any bytecode instrumentation. + * + * @see PathUtil#toSystemIndependentName(String) + * @see PathUtil#toSystemDependentName(String) + */ +@Documented +@Retention(RetentionPolicy.CLASS) +@Target(ElementType.TYPE_USE) +public @interface SystemDependent { +} diff --git a/platform/core-api/src/com/intellij/util/SystemIndependent.java b/platform/core-api/src/com/intellij/util/SystemIndependent.java new file mode 100644 index 000000000000..f51e62a1afbe --- /dev/null +++ b/platform/core-api/src/com/intellij/util/SystemIndependent.java @@ -0,0 +1,33 @@ +/* + * Copyright 2000-2017 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.intellij.util; + +import java.lang.annotation.*; + +/** + * Designates a system-independent path, with / separators. + *

+ * This annotation doesn't add any bytecode instrumentation.

+ * + * @see PathUtil#toSystemIndependentName(String) + * @see PathUtil#toSystemDependentName(String) + * @see PathUtil#assertSystemIndependentName(String) + */ +@Documented +@Retention(RetentionPolicy.CLASS) +@Target(ElementType.TYPE_USE) +public @interface SystemIndependent { +} diff --git a/platform/core-impl/src/com/intellij/mock/MockProject.java b/platform/core-impl/src/com/intellij/mock/MockProject.java index 9d3309999ebf..1a51cdb2f450 100644 --- a/platform/core-impl/src/com/intellij/mock/MockProject.java +++ b/platform/core-impl/src/com/intellij/mock/MockProject.java @@ -25,6 +25,8 @@ import com.intellij.openapi.project.Project; import com.intellij.openapi.util.Condition; import com.intellij.openapi.util.Disposer; import com.intellij.openapi.vfs.VirtualFile; +import com.intellij.util.SystemDependent; +import com.intellij.util.SystemIndependent; import org.jetbrains.annotations.NonNls; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -89,6 +91,7 @@ public class MockProject extends MockComponentManager implements Project { @Override @Nullable + @SystemIndependent public String getProjectFilePath() { return null; } @@ -109,6 +112,7 @@ public class MockProject extends MockComponentManager implements Project { } @Nullable + @SystemIndependent @Override public String getBasePath() { return null; diff --git a/platform/platform-impl/src/com/intellij/openapi/command/impl/DummyProject.java b/platform/platform-impl/src/com/intellij/openapi/command/impl/DummyProject.java index bdf54d9c3f07..961644597d61 100644 --- a/platform/platform-impl/src/com/intellij/openapi/command/impl/DummyProject.java +++ b/platform/platform-impl/src/com/intellij/openapi/command/impl/DummyProject.java @@ -22,6 +22,7 @@ import com.intellij.openapi.util.Condition; import com.intellij.openapi.util.UserDataHolderBase; import com.intellij.openapi.vfs.VirtualFile; import com.intellij.util.ArrayUtil; +import com.intellij.util.SystemIndependent; import com.intellij.util.messages.MessageBus; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -61,6 +62,7 @@ public class DummyProject extends UserDataHolderBase implements Project { @Override @Nullable + @SystemIndependent public String getProjectFilePath() { return null; } @@ -77,6 +79,7 @@ public class DummyProject extends UserDataHolderBase implements Project { } @Nullable + @SystemIndependent @Override public String getBasePath() { return null; diff --git a/platform/platform-impl/src/com/intellij/openapi/project/impl/ProjectImpl.java b/platform/platform-impl/src/com/intellij/openapi/project/impl/ProjectImpl.java index 64b1e2cc792a..7fcfd45feba0 100644 --- a/platform/platform-impl/src/com/intellij/openapi/project/impl/ProjectImpl.java +++ b/platform/platform-impl/src/com/intellij/openapi/project/impl/ProjectImpl.java @@ -52,6 +52,9 @@ import com.intellij.openapi.wm.WindowManager; import com.intellij.openapi.wm.impl.FrameTitleBuilder; import com.intellij.project.ProjectKt; import com.intellij.psi.impl.DebugUtil; +import com.intellij.util.PathUtil; +import com.intellij.util.SystemDependent; +import com.intellij.util.SystemIndependent; import com.intellij.util.TimedReference; import com.intellij.util.io.storage.HeavyProcessLatch; import com.intellij.util.pico.CachingConstructorInjectionComponentAdapter; @@ -211,6 +214,7 @@ public class ProjectImpl extends PlatformComponentManagerImpl implements Project @Override @Nullable + @SystemIndependent public String getProjectFilePath() { return isDefault() ? null : getStateStore().getProjectFilePath(); } @@ -227,6 +231,7 @@ public class ProjectImpl extends PlatformComponentManagerImpl implements Project @Nullable @Override + @SystemIndependent public String getBasePath() { return isDefault() ? null : getStateStore().getProjectBasePath(); } @@ -240,6 +245,7 @@ public class ProjectImpl extends PlatformComponentManagerImpl implements Project return myName; } + @SystemDependent @Override public String getPresentableUrl() { if (isDefault()) { @@ -247,7 +253,7 @@ public class ProjectImpl extends PlatformComponentManagerImpl implements Project } IProjectStore store = getStateStore(); - return FileUtil.toSystemDependentName(store.getStorageScheme() == StorageScheme.DIRECTORY_BASED ? store.getProjectBasePath() : store.getProjectFilePath()); + return PathUtil.toSystemDependentName(store.getStorageScheme() == StorageScheme.DIRECTORY_BASED ? store.getProjectBasePath() : store.getProjectFilePath()); } @NotNull