a7ccfde32ec4df0d2d9fa2433aff7dd400d8c1c5
[idea/community.git] / java / java-psi-impl / src / com / intellij / core / JavaCoreProjectEnvironment.java
1 // 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.
2 package com.intellij.core;
3
4 import com.intellij.lang.jvm.facade.JvmFacade;
5 import com.intellij.lang.jvm.facade.JvmFacadeImpl;
6 import com.intellij.mock.MockFileIndexFacade;
7 import com.intellij.openapi.Disposable;
8 import com.intellij.openapi.roots.LanguageLevelProjectExtension;
9 import com.intellij.openapi.roots.PackageIndex;
10 import com.intellij.openapi.vfs.VirtualFile;
11 import com.intellij.psi.JavaPsiFacade;
12 import com.intellij.psi.JvmPsiConversionHelper;
13 import com.intellij.psi.PsiElementFactory;
14 import com.intellij.psi.PsiResolveHelper;
15 import com.intellij.psi.codeStyle.JavaCodeStyleManager;
16 import com.intellij.psi.codeStyle.JavaCodeStyleSettingsFacade;
17 import com.intellij.psi.controlFlow.ControlFlowFactory;
18 import com.intellij.psi.impl.JavaPsiFacadeImpl;
19 import com.intellij.psi.impl.JavaPsiImplementationHelper;
20 import com.intellij.psi.impl.JvmPsiConversionHelperImpl;
21 import com.intellij.psi.impl.PsiElementFactoryImpl;
22 import com.intellij.psi.impl.file.impl.JavaFileManager;
23 import com.intellij.psi.impl.source.resolve.JavaResolveCache;
24 import com.intellij.psi.impl.source.resolve.PsiResolveHelperImpl;
25 import org.jetbrains.annotations.NotNull;
26
27 import java.io.File;
28
29 /**
30  * Used in Kotlin.
31  */
32 public class JavaCoreProjectEnvironment extends CoreProjectEnvironment {
33   private final JavaFileManager myFileManager;
34   private final PackageIndex myPackageIndex;
35
36   public JavaCoreProjectEnvironment(@NotNull Disposable parentDisposable, @NotNull CoreApplicationEnvironment applicationEnvironment) {
37     super(parentDisposable, applicationEnvironment);
38
39     myProject.registerService(PsiElementFactory.class, new PsiElementFactoryImpl(myPsiManager));
40     myProject.registerService(JavaPsiImplementationHelper.class, createJavaPsiImplementationHelper());
41     myProject.registerService(PsiResolveHelper.class, new PsiResolveHelperImpl(myPsiManager));
42     myProject.registerService(LanguageLevelProjectExtension.class, new CoreLanguageLevelProjectExtension());
43     myProject.registerService(JavaResolveCache.class, new JavaResolveCache(myMessageBus));
44     myProject.registerService(JavaCodeStyleSettingsFacade.class, new CoreJavaCodeStyleSettingsFacade());
45     myProject.registerService(JavaCodeStyleManager.class, new CoreJavaCodeStyleManager());
46     myProject.registerService(ControlFlowFactory.class, new ControlFlowFactory(myPsiManager));
47
48     myPackageIndex = createCorePackageIndex();
49     myProject.registerService(PackageIndex.class, myPackageIndex);
50
51     myFileManager = createCoreFileManager();
52     myProject.registerService(JavaFileManager.class, myFileManager);
53
54     myProject.registerService(JvmPsiConversionHelper.class, new JvmPsiConversionHelperImpl());
55     registerJavaPsiFacade();
56     myProject.registerService(JvmFacade.class, new JvmFacadeImpl(myProject, myMessageBus));
57   }
58
59   protected void registerJavaPsiFacade() {
60     JavaPsiFacadeImpl javaPsiFacade = new JavaPsiFacadeImpl(myProject, myPsiManager, myFileManager, myMessageBus);
61     myProject.registerService(JavaPsiFacade.class, javaPsiFacade);
62   }
63
64   protected CoreJavaPsiImplementationHelper createJavaPsiImplementationHelper() {
65     return new CoreJavaPsiImplementationHelper(myProject);
66   }
67
68   protected JavaFileManager createCoreFileManager() {
69     return new CoreJavaFileManager(myPsiManager);
70   }
71
72   protected PackageIndex createCorePackageIndex() {
73     return new CorePackageIndex();
74   }
75
76   public void addJarToClassPath(File path) {
77     assert path.isFile();
78
79     final VirtualFile root = getEnvironment().getJarFileSystem().findFileByPath(path + "!/");
80     if (root == null) {
81       throw new IllegalArgumentException("trying to add non-existing file to classpath: " + path);
82     }
83
84     addSourcesToClasspath(root);
85   }
86
87   public void addSourcesToClasspath(@NotNull VirtualFile root) {
88     assert root.isDirectory();
89     ((CoreJavaFileManager)myFileManager).addToClasspath(root);
90     ((CorePackageIndex)myPackageIndex).addToClasspath(root);
91     ((MockFileIndexFacade)myFileIndexFacade).addLibraryRoot(root);
92   }
93 }