diff --git a/jps/model-impl/src/org/jetbrains/jps/model/java/impl/JavaModuleIndexImpl.java b/jps/model-impl/src/org/jetbrains/jps/model/java/impl/JavaModuleIndexImpl.java index 53a32e564a14..80515f6b6072 100644 --- a/jps/model-impl/src/org/jetbrains/jps/model/java/impl/JavaModuleIndexImpl.java +++ b/jps/model-impl/src/org/jetbrains/jps/model/java/impl/JavaModuleIndexImpl.java @@ -1,4 +1,4 @@ -// Copyright 2000-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. +// Copyright 2000-2024 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license. package org.jetbrains.jps.model.java.impl; import org.jetbrains.annotations.NotNull; @@ -9,63 +9,50 @@ import org.jetbrains.jps.model.java.JavaModuleIndex; import org.jetbrains.jps.model.java.JavaSourceRootType; import org.jetbrains.jps.model.java.compiler.JpsCompilerExcludes; import org.jetbrains.jps.model.module.JpsModule; -import org.jetbrains.jps.model.module.JpsModuleSourceRoot; import org.jetbrains.jps.util.JpsPathUtil; import java.io.File; -import java.util.HashMap; import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; public class JavaModuleIndexImpl extends JpsElementBase implements JavaModuleIndex { private static final String SOURCE_SUFFIX = ":S"; private static final String TEST_SUFFIX = ":T"; private static final String MODULE_INFO_FILE = "module-info.java"; + private static final File NULL_FILE = new File("-"); private final Map myMapping; private final JpsCompilerExcludes myExcludes; public JavaModuleIndexImpl(@NotNull JpsCompilerExcludes excludes) { - myMapping = new HashMap<>(); + myMapping = new ConcurrentHashMap<>(); myExcludes = excludes; } @Override - @NotNull - public JavaModuleIndexImpl createCopy() { - JavaModuleIndexImpl copy = new JavaModuleIndexImpl(myExcludes); - copy.myMapping.putAll(myMapping); - return copy; + public @Nullable File getModuleInfoFile(@NotNull JpsModule module, boolean forTests) { + var key = module.getName() + (forTests ? TEST_SUFFIX : SOURCE_SUFFIX); + var file = myMapping.computeIfAbsent(key, __ -> findModuleInfoFile(module, forTests)); + return file == NULL_FILE ? null : file; } - @Nullable - @Override - public File getModuleInfoFile(@NotNull JpsModule module, boolean forTests) { - String key = module.getName() + (forTests ? TEST_SUFFIX : SOURCE_SUFFIX); + private File findModuleInfoFile(JpsModule module, boolean forTests) { + var rootType = forTests ? JavaSourceRootType.TEST_SOURCE : JavaSourceRootType.SOURCE; - if (myMapping.containsKey(key)) { - return myMapping.get(key); - } - - File file = findModuleInfoFile(module, forTests ? JavaSourceRootType.TEST_SOURCE : JavaSourceRootType.SOURCE); - myMapping.put(key, file); - return file; - } - - private File findModuleInfoFile(JpsModule module, JavaSourceRootType rootType) { - for (JpsModuleSourceRoot root : module.getSourceRoots()) { + for (var root : module.getSourceRoots()) { if (rootType.equals(root.getRootType())) { - File file = new File(JpsPathUtil.urlToOsPath(root.getUrl()), MODULE_INFO_FILE); + var file = new File(JpsPathUtil.urlToOsPath(root.getUrl()), MODULE_INFO_FILE); if (file.isFile() && !myExcludes.isExcluded(file)) { return file; } } } - return null; + return NULL_FILE; } @TestOnly public void dropCache() { myMapping.clear(); } -} \ No newline at end of file +}