001 /*
002 * Copyright 2010-2014 JetBrains s.r.o.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017 package org.jetbrains.jet.lang.resolve.java;
018
019 import com.google.common.base.Predicate;
020 import com.google.common.collect.ImmutableList;
021 import com.intellij.openapi.project.Project;
022 import com.intellij.psi.PsiFile;
023 import org.jetbrains.annotations.NotNull;
024 import org.jetbrains.annotations.Nullable;
025 import org.jetbrains.jet.analyzer.AnalyzeExhaust;
026 import org.jetbrains.jet.context.ContextPackage;
027 import org.jetbrains.jet.context.GlobalContext;
028 import org.jetbrains.jet.di.InjectorForTopDownAnalyzerForJvm;
029 import org.jetbrains.jet.lang.descriptors.PackageFragmentProvider;
030 import org.jetbrains.jet.lang.descriptors.impl.ModuleDescriptorImpl;
031 import org.jetbrains.jet.lang.psi.JetFile;
032 import org.jetbrains.jet.lang.resolve.BindingTrace;
033 import org.jetbrains.jet.lang.resolve.ImportPath;
034 import org.jetbrains.jet.lang.resolve.TopDownAnalysisParameters;
035 import org.jetbrains.jet.lang.resolve.java.mapping.JavaToKotlinClassMap;
036 import org.jetbrains.jet.lang.resolve.kotlin.incremental.IncrementalPackageFragmentProvider;
037 import org.jetbrains.jet.lang.resolve.kotlin.incremental.cache.IncrementalCache;
038 import org.jetbrains.jet.lang.resolve.kotlin.incremental.cache.IncrementalCacheProvider;
039 import org.jetbrains.jet.lang.resolve.name.Name;
040
041 import java.util.ArrayList;
042 import java.util.Collection;
043 import java.util.List;
044
045 public enum TopDownAnalyzerFacadeForJVM {
046
047 INSTANCE;
048
049 public static final List<ImportPath> DEFAULT_IMPORTS = ImmutableList.of(
050 new ImportPath("java.lang.*"),
051 new ImportPath("kotlin.*"),
052 new ImportPath("kotlin.jvm.*"),
053 new ImportPath("kotlin.io.*")
054 );
055
056 private TopDownAnalyzerFacadeForJVM() {
057 }
058
059 @NotNull
060 public static AnalyzeExhaust analyzeFilesWithJavaIntegration(
061 Project project,
062 Collection<JetFile> files,
063 BindingTrace trace,
064 Predicate<PsiFile> filesToAnalyzeCompletely,
065 ModuleDescriptorImpl module,
066 List<String> moduleIds,
067 @Nullable IncrementalCacheProvider incrementalCacheProvider
068 ) {
069 GlobalContext globalContext = ContextPackage.GlobalContext();
070 TopDownAnalysisParameters topDownAnalysisParameters = TopDownAnalysisParameters.create(
071 globalContext.getStorageManager(),
072 globalContext.getExceptionTracker(),
073 filesToAnalyzeCompletely,
074 false,
075 false
076 );
077
078 InjectorForTopDownAnalyzerForJvm injector = new InjectorForTopDownAnalyzerForJvm(project, topDownAnalysisParameters, trace, module);
079 try {
080 List<PackageFragmentProvider> additionalProviders = new ArrayList<PackageFragmentProvider>();
081
082 if (moduleIds != null && incrementalCacheProvider != null) {
083 for (String moduleId : moduleIds) {
084 IncrementalCache incrementalCache = incrementalCacheProvider.getIncrementalCache(moduleId);
085
086 additionalProviders.add(
087 new IncrementalPackageFragmentProvider(
088 files, module, globalContext.getStorageManager(), injector.getDeserializationGlobalContextForJava(),
089 incrementalCache, moduleId, injector.getJavaDescriptorResolver()
090 )
091 );
092 }
093 }
094 additionalProviders.add(injector.getJavaDescriptorResolver().getPackageFragmentProvider());
095
096 injector.getTopDownAnalyzer().analyzeFiles(topDownAnalysisParameters, files, additionalProviders);
097 return AnalyzeExhaust.success(trace.getBindingContext(), module);
098 }
099 finally {
100 injector.destroy();
101 }
102 }
103
104 @NotNull
105 public static ModuleDescriptorImpl createJavaModule(@NotNull String name) {
106 return new ModuleDescriptorImpl(Name.special(name), DEFAULT_IMPORTS, JavaToKotlinClassMap.getInstance());
107 }
108 }