From bc00216df9049f56687d2f5ed36537daf237014f Mon Sep 17 00:00:00 2001 From: Stepan Koltsov Date: Thu, 12 Apr 2012 20:45:00 +0400 Subject: [PATCH] replace JavaPsiFacade with own --- .../java/JavaPsiFacadeKotlinHacks.java | 105 ++++++++++++++++++ .../resolve/java/PsiClassFinderForJvm.java | 5 +- .../jet/asJava/JavaElementFinder.java | 3 +- 3 files changed, 109 insertions(+), 4 deletions(-) create mode 100644 compiler/frontend.java/src/org/jetbrains/jet/lang/resolve/java/JavaPsiFacadeKotlinHacks.java diff --git a/compiler/frontend.java/src/org/jetbrains/jet/lang/resolve/java/JavaPsiFacadeKotlinHacks.java b/compiler/frontend.java/src/org/jetbrains/jet/lang/resolve/java/JavaPsiFacadeKotlinHacks.java new file mode 100644 index 00000000000..29faad69f95 --- /dev/null +++ b/compiler/frontend.java/src/org/jetbrains/jet/lang/resolve/java/JavaPsiFacadeKotlinHacks.java @@ -0,0 +1,105 @@ +/* + * Copyright 2010-2012 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 org.jetbrains.jet.lang.resolve.java; + +import com.google.common.collect.Lists; +import com.intellij.core.CoreJavaFileManager; +import com.intellij.openapi.progress.ProgressIndicatorProvider; +import com.intellij.openapi.project.Project; +import com.intellij.psi.PsiClass; +import com.intellij.psi.PsiElementFinder; +import com.intellij.psi.PsiPackage; +import com.intellij.psi.impl.JavaPsiFacadeImpl; +import com.intellij.psi.impl.file.impl.JavaFileManager; +import com.intellij.psi.search.GlobalSearchScope; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +import java.util.List; + +/** + * TODO Temporary class until {@link JavaPsiFacadeImpl} hacked. + * + * @author Stepan Koltsov + * + * @see JavaPsiFacadeImpl + */ +public class JavaPsiFacadeKotlinHacks { + public interface KotlinFinderMarker {} + + private final JavaFileManager javaFileManager; + private final List extensionPsiElementFinders; + + public JavaPsiFacadeKotlinHacks(@NotNull Project project) { + this.javaFileManager = findJavaFileManager(project); + this.extensionPsiElementFinders = Lists.newArrayList(); + for (PsiElementFinder finder : project.getExtensions(PsiElementFinder.EP_NAME)) { + if (!(finder instanceof KotlinFinderMarker)) { + this.extensionPsiElementFinders.add(finder); + } + } + } + + @NotNull + private JavaFileManager findJavaFileManager(@NotNull Project project) { + JavaFileManager javaFileManager = project.getComponent(JavaFileManager.class); + if (javaFileManager != null) { + return javaFileManager; + } + javaFileManager = project.getComponent(CoreJavaFileManager.class); + if (javaFileManager != null) { + // TODO: why it is not found by JavaFileManager? + return javaFileManager; + } + throw new IllegalStateException("JavaFileManager component is not found in project"); + } + + @Nullable + public PsiPackage findPackage(@NotNull String qualifiedName) { + PsiPackage psiPackage = javaFileManager.findPackage(qualifiedName); + if (psiPackage != null) { + return psiPackage; + } + + for (PsiElementFinder finder : extensionPsiElementFinders) { + psiPackage = finder.findPackage(qualifiedName); + if (psiPackage != null) { + return psiPackage; + } + } + return psiPackage; + } + + public PsiClass findClass(@NotNull final String qualifiedName, @NotNull GlobalSearchScope scope) { + ProgressIndicatorProvider.checkCanceled(); // We hope this method is being called often enough to cancel daemon processes smoothly + + PsiClass aClass = javaFileManager.findClass(qualifiedName, scope); + if (aClass != null) { + return aClass; + } + + for (PsiElementFinder finder : extensionPsiElementFinders) { + aClass = finder.findClass(qualifiedName, scope); + if (aClass != null) { + return aClass; + } + } + + return null; + } + +} diff --git a/compiler/frontend.java/src/org/jetbrains/jet/lang/resolve/java/PsiClassFinderForJvm.java b/compiler/frontend.java/src/org/jetbrains/jet/lang/resolve/java/PsiClassFinderForJvm.java index 48da447d8a8..82bcea7f4b9 100644 --- a/compiler/frontend.java/src/org/jetbrains/jet/lang/resolve/java/PsiClassFinderForJvm.java +++ b/compiler/frontend.java/src/org/jetbrains/jet/lang/resolve/java/PsiClassFinderForJvm.java @@ -18,7 +18,6 @@ package org.jetbrains.jet.lang.resolve.java; import com.intellij.openapi.project.Project; import com.intellij.openapi.vfs.VirtualFile; -import com.intellij.psi.JavaPsiFacade; import com.intellij.psi.PsiAnnotation; import com.intellij.psi.PsiClass; import com.intellij.psi.PsiPackage; @@ -46,7 +45,7 @@ public class PsiClassFinderForJvm implements PsiClassFinder { private AltClassFinder altClassFinder; private GlobalSearchScope javaSearchScope; - private JavaPsiFacade javaFacade; + private JavaPsiFacadeKotlinHacks javaFacade; @Inject public void setProject(@NotNull Project project) { @@ -67,7 +66,7 @@ public class PsiClassFinderForJvm implements PsiClassFinder { return myBaseScope.contains(file) && file.getFileType() != JetFileType.INSTANCE; } }; - this.javaFacade = JavaPsiFacade.getInstance(project); + this.javaFacade = new JavaPsiFacadeKotlinHacks(project); } diff --git a/compiler/jet.as.java.psi/src/org/jetbrains/jet/asJava/JavaElementFinder.java b/compiler/jet.as.java.psi/src/org/jetbrains/jet/asJava/JavaElementFinder.java index 8a45a0774c9..8f5ef1b4234 100644 --- a/compiler/jet.as.java.psi/src/org/jetbrains/jet/asJava/JavaElementFinder.java +++ b/compiler/jet.as.java.psi/src/org/jetbrains/jet/asJava/JavaElementFinder.java @@ -32,6 +32,7 @@ import org.jetbrains.annotations.Nullable; import org.jetbrains.jet.codegen.JetTypeMapper; import org.jetbrains.jet.lang.psi.*; import org.jetbrains.jet.lang.resolve.FqName; +import org.jetbrains.jet.lang.resolve.java.JavaPsiFacadeKotlinHacks; import org.jetbrains.jet.lang.resolve.java.JetFilesProvider; import org.jetbrains.jet.lang.resolve.java.JvmAbi; import org.jetbrains.jet.util.QualifiedNamesUtil; @@ -41,7 +42,7 @@ import java.util.List; import java.util.Set; import java.util.WeakHashMap; -public class JavaElementFinder extends PsiElementFinder { +public class JavaElementFinder extends PsiElementFinder implements JavaPsiFacadeKotlinHacks.KotlinFinderMarker { private final Project project; private final PsiManager psiManager;