diff --git a/compiler/frontend/src/org/jetbrains/kotlin/resolve/source/PsiSourceElement.kt b/compiler/frontend/src/org/jetbrains/kotlin/resolve/source/PsiSourceElement.kt index f211e5c19ce..9cf854f8706 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/resolve/source/PsiSourceElement.kt +++ b/compiler/frontend/src/org/jetbrains/kotlin/resolve/source/PsiSourceElement.kt @@ -17,8 +17,17 @@ package org.jetbrains.kotlin.resolve.source import com.intellij.psi.PsiElement +import com.intellij.psi.PsiFile import org.jetbrains.kotlin.descriptors.SourceElement +import org.jetbrains.kotlin.descriptors.SourceFile public interface PsiSourceElement : SourceElement { public val psi: PsiElement? + + override fun getContainingFile(): SourceFile = psi?.containingFile?.let { PsiSourceFile(it) } ?: SourceFile.NO_SOURCE_FILE +} + +public class PsiSourceFile(private val psiFile: PsiFile): SourceFile { + override fun equals(other: Any?): Boolean = other is PsiSourceFile && psiFile == other.psiFile + override fun hashCode(): Int = psiFile.hashCode() } diff --git a/core/descriptor.loader.java/src/org/jetbrains/kotlin/load/kotlin/KotlinJvmBinarySourceElement.kt b/core/descriptor.loader.java/src/org/jetbrains/kotlin/load/kotlin/KotlinJvmBinarySourceElement.kt index 249f2501b59..e8fdc9db233 100644 --- a/core/descriptor.loader.java/src/org/jetbrains/kotlin/load/kotlin/KotlinJvmBinarySourceElement.kt +++ b/core/descriptor.loader.java/src/org/jetbrains/kotlin/load/kotlin/KotlinJvmBinarySourceElement.kt @@ -17,7 +17,9 @@ package org.jetbrains.kotlin.load.kotlin import org.jetbrains.kotlin.descriptors.SourceElement +import org.jetbrains.kotlin.descriptors.SourceFile public class KotlinJvmBinarySourceElement(public val binaryClass: KotlinJvmBinaryClass) : SourceElement { override fun toString() = javaClass.name + ": " + binaryClass.toString() + override fun getContainingFile(): SourceFile = SourceFile.NO_SOURCE_FILE } diff --git a/core/descriptors.runtime/src/org/jetbrains/kotlin/load/java/components/RuntimeSourceElementFactory.kt b/core/descriptors.runtime/src/org/jetbrains/kotlin/load/java/components/RuntimeSourceElementFactory.kt index 4282a6e544e..33e21b8f80b 100644 --- a/core/descriptors.runtime/src/org/jetbrains/kotlin/load/java/components/RuntimeSourceElementFactory.kt +++ b/core/descriptors.runtime/src/org/jetbrains/kotlin/load/java/components/RuntimeSourceElementFactory.kt @@ -16,6 +16,7 @@ package org.jetbrains.kotlin.load.java.components +import org.jetbrains.kotlin.descriptors.SourceFile import org.jetbrains.kotlin.load.java.sources.JavaSourceElement import org.jetbrains.kotlin.load.java.sources.JavaSourceElementFactory import org.jetbrains.kotlin.load.java.structure.JavaElement @@ -24,6 +25,7 @@ import org.jetbrains.kotlin.load.java.structure.reflect.ReflectJavaElement public object RuntimeSourceElementFactory : JavaSourceElementFactory { private class RuntimeSourceElement(override val javaElement: ReflectJavaElement) : JavaSourceElement { override fun toString() = javaClass.name + ": " + javaElement.toString() + override fun getContainingFile(): SourceFile = SourceFile.NO_SOURCE_FILE } override fun source(javaElement: JavaElement) = RuntimeSourceElement(javaElement as ReflectJavaElement) diff --git a/core/descriptors.runtime/src/org/jetbrains/kotlin/load/kotlin/reflect/ReflectAnnotationSource.kt b/core/descriptors.runtime/src/org/jetbrains/kotlin/load/kotlin/reflect/ReflectAnnotationSource.kt index ad57626d904..a4d2960bda0 100644 --- a/core/descriptors.runtime/src/org/jetbrains/kotlin/load/kotlin/reflect/ReflectAnnotationSource.kt +++ b/core/descriptors.runtime/src/org/jetbrains/kotlin/load/kotlin/reflect/ReflectAnnotationSource.kt @@ -17,5 +17,8 @@ package org.jetbrains.kotlin.load.kotlin.reflect import org.jetbrains.kotlin.descriptors.SourceElement +import org.jetbrains.kotlin.descriptors.SourceFile -public class ReflectAnnotationSource(public val annotation: Annotation) : SourceElement +public class ReflectAnnotationSource(public val annotation: Annotation) : SourceElement { + override fun getContainingFile(): SourceFile = SourceFile.NO_SOURCE_FILE +} diff --git a/core/descriptors/src/org/jetbrains/kotlin/descriptors/SourceElement.java b/core/descriptors/src/org/jetbrains/kotlin/descriptors/SourceElement.java index 6fe7ba61f1c..c27ea07cc5c 100644 --- a/core/descriptors/src/org/jetbrains/kotlin/descriptors/SourceElement.java +++ b/core/descriptors/src/org/jetbrains/kotlin/descriptors/SourceElement.java @@ -16,11 +16,22 @@ package org.jetbrains.kotlin.descriptors; +import org.jetbrains.annotations.NotNull; + public interface SourceElement { SourceElement NO_SOURCE = new SourceElement() { @Override public String toString() { return "NO_SOURCE"; } + + @NotNull + @Override + public SourceFile getContainingFile() { + return SourceFile.NO_SOURCE_FILE; + } }; + + @NotNull + SourceFile getContainingFile(); } diff --git a/core/descriptors/src/org/jetbrains/kotlin/descriptors/SourceFile.kt b/core/descriptors/src/org/jetbrains/kotlin/descriptors/SourceFile.kt new file mode 100644 index 00000000000..ff696ca8fa2 --- /dev/null +++ b/core/descriptors/src/org/jetbrains/kotlin/descriptors/SourceFile.kt @@ -0,0 +1,23 @@ +/* + * Copyright 2010-2015 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.kotlin.descriptors + +public interface SourceFile { + companion object { + val NO_SOURCE_FILE = object : SourceFile {} + } +} diff --git a/core/descriptors/src/org/jetbrains/kotlin/resolve/DescriptorUtils.java b/core/descriptors/src/org/jetbrains/kotlin/resolve/DescriptorUtils.java index 09d650b2d48..f0e1cfac231 100644 --- a/core/descriptors/src/org/jetbrains/kotlin/resolve/DescriptorUtils.java +++ b/core/descriptors/src/org/jetbrains/kotlin/resolve/DescriptorUtils.java @@ -589,6 +589,19 @@ public class DescriptorUtils { return getJvmNameAnnotation(annotated.getAnnotations()); } + @NotNull + public static SourceFile getContainingSourceFile(@NotNull DeclarationDescriptor descriptor) { + if (descriptor instanceof PropertySetterDescriptor) { + descriptor = ((PropertySetterDescriptor) descriptor).getCorrespondingProperty(); + } + + if (descriptor instanceof DeclarationDescriptorWithSource) { + return ((DeclarationDescriptorWithSource) descriptor).getSource().getContainingFile(); + } + + return SourceFile.NO_SOURCE_FILE; + } + private static void getSubPackagesFqNames(PackageViewDescriptor packageView, Set result) { FqName fqName = packageView.getFqName(); if (!fqName.isRoot()) {