From 493781241480c8807c72b3936f9ab0dd4c4a9af5 Mon Sep 17 00:00:00 2001 From: Alexander Udalov Date: Fri, 7 Mar 2014 03:39:55 +0400 Subject: [PATCH] Rewrite KotlinClassHeader to Kotlin --- .../jet/lang/resolve/java/lazy/resolvers.kt | 7 +- .../kotlin/header/KotlinClassHeader.java | 72 ------------------- .../kotlin/header/KotlinClassHeader.kt | 42 +++++++++++ .../plugin/libraries/DecompiledTextFactory.kt | 2 +- .../jet/plugin/libraries/DecompiledUtils.kt | 6 +- .../libraries/DeserializerForDecompiler.kt | 5 +- .../libraries/InternalCompiledClassesTest.kt | 7 +- 7 files changed, 52 insertions(+), 89 deletions(-) delete mode 100644 core/descriptor.loader.java/src/org/jetbrains/jet/lang/resolve/kotlin/header/KotlinClassHeader.java create mode 100644 core/descriptor.loader.java/src/org/jetbrains/jet/lang/resolve/kotlin/header/KotlinClassHeader.kt diff --git a/core/descriptor.loader.java/src/org/jetbrains/jet/lang/resolve/java/lazy/resolvers.kt b/core/descriptor.loader.java/src/org/jetbrains/jet/lang/resolve/java/lazy/resolvers.kt index 14f226f710e..d4a9f076c4b 100644 --- a/core/descriptor.loader.java/src/org/jetbrains/jet/lang/resolve/java/lazy/resolvers.kt +++ b/core/descriptor.loader.java/src/org/jetbrains/jet/lang/resolve/java/lazy/resolvers.kt @@ -24,7 +24,6 @@ import org.jetbrains.jet.lang.resolve.java.lazy.descriptors.LazyJavaTypeParamete import org.jetbrains.jet.lang.descriptors.DeclarationDescriptor import org.jetbrains.jet.lang.resolve.name.FqName import org.jetbrains.jet.lang.resolve.kotlin.header.KotlinClassHeader -import org.jetbrains.jet.lang.resolve.kotlin.header.KotlinClassHeader.Kind import org.jetbrains.jet.lang.resolve.java.resolver.DescriptorResolverUtils trait LazyJavaClassResolver { @@ -78,14 +77,14 @@ fun LazyJavaResolverContext.findClassInJava(fqName: FqName): JavaClassLookupResu val kotlinClass = kotlinClassFinder.findKotlinClass(fqName) val header = kotlinClass?.getClassHeader() if (kotlinClass != null && header != null) { - if (header.getKind() == KotlinClassHeader.Kind.CLASS) { + if (header.kind == KotlinClassHeader.Kind.CLASS) { val descriptor = packageFragmentProvider.resolveKotlinBinaryClass(kotlinClass) if (descriptor != null) { return JavaClassLookupResult(kClass = descriptor) } } - else if (header.getKind() == KotlinClassHeader.Kind.INCOMPATIBLE_ABI_VERSION) { - errorReporter.reportIncompatibleAbiVersion(kotlinClass, header.getVersion()) + else if (header.kind == KotlinClassHeader.Kind.INCOMPATIBLE_ABI_VERSION) { + errorReporter.reportIncompatibleAbiVersion(kotlinClass, header.version) } else { // This is a package or trait-impl or something like that diff --git a/core/descriptor.loader.java/src/org/jetbrains/jet/lang/resolve/kotlin/header/KotlinClassHeader.java b/core/descriptor.loader.java/src/org/jetbrains/jet/lang/resolve/kotlin/header/KotlinClassHeader.java deleted file mode 100644 index b045c42c886..00000000000 --- a/core/descriptor.loader.java/src/org/jetbrains/jet/lang/resolve/kotlin/header/KotlinClassHeader.java +++ /dev/null @@ -1,72 +0,0 @@ -/* - * Copyright 2010-2013 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.kotlin.header; - -import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; - -import static org.jetbrains.jet.lang.resolve.java.JvmAnnotationNames.KotlinSyntheticClass; - -public class KotlinClassHeader { - - public enum Kind { - INCOMPATIBLE_ABI_VERSION, - CLASS, - PACKAGE_FACADE, - SYNTHETIC_CLASS, - } - - private final Kind kind; - private final int version; - private final String[] data; - private final KotlinSyntheticClass.Kind syntheticClassKind; - - public KotlinClassHeader( - @NotNull Kind kind, - int version, - @Nullable String[] annotationData, - @Nullable KotlinSyntheticClass.Kind syntheticClassKind - ) { - assert (annotationData == null) == (kind != Kind.CLASS && kind != Kind.PACKAGE_FACADE) - : "Annotation data should be not null only for CLASS and PACKAGE_FACADE (kind=" + kind + ")"; - assert (syntheticClassKind == null) == (kind != Kind.SYNTHETIC_CLASS) - : "Synthetic class kind should be present for SYNTHETIC_CLASS (kind=" + kind + ")"; - this.kind = kind; - this.version = version; - this.data = annotationData; - this.syntheticClassKind = syntheticClassKind; - } - - @NotNull - public Kind getKind() { - return kind; - } - - public int getVersion() { - return version; - } - - @Nullable - public String[] getAnnotationData() { - return data; - } - - @Nullable - public KotlinSyntheticClass.Kind getSyntheticClassKind() { - return syntheticClassKind; - } -} diff --git a/core/descriptor.loader.java/src/org/jetbrains/jet/lang/resolve/kotlin/header/KotlinClassHeader.kt b/core/descriptor.loader.java/src/org/jetbrains/jet/lang/resolve/kotlin/header/KotlinClassHeader.kt new file mode 100644 index 00000000000..cd8e81eebe2 --- /dev/null +++ b/core/descriptor.loader.java/src/org/jetbrains/jet/lang/resolve/kotlin/header/KotlinClassHeader.kt @@ -0,0 +1,42 @@ +/* + * Copyright 2010-2014 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.kotlin.header + +import org.jetbrains.jet.lang.resolve.java.JvmAnnotationNames.KotlinSyntheticClass + +public class KotlinClassHeader( + public val kind: KotlinClassHeader.Kind, + public val version: Int, + public val annotationData: Array?, + public val syntheticClassKind: KotlinSyntheticClass.Kind? +) { + { + assert((annotationData == null) == (kind != Kind.CLASS && kind != Kind.PACKAGE_FACADE)) { + "Annotation data should be not null only for CLASS and PACKAGE_FACADE (kind=" + kind + ")" + } + assert((syntheticClassKind == null) == (kind != Kind.SYNTHETIC_CLASS)) { + "Synthetic class kind should be present for SYNTHETIC_CLASS (kind=" + kind + ")" + } + } + + public enum class Kind { + INCOMPATIBLE_ABI_VERSION + CLASS + PACKAGE_FACADE + SYNTHETIC_CLASS + } +} \ No newline at end of file diff --git a/idea/src/org/jetbrains/jet/plugin/libraries/DecompiledTextFactory.kt b/idea/src/org/jetbrains/jet/plugin/libraries/DecompiledTextFactory.kt index fe3a80a7625..5fdab8b7c12 100644 --- a/idea/src/org/jetbrains/jet/plugin/libraries/DecompiledTextFactory.kt +++ b/idea/src/org/jetbrains/jet/plugin/libraries/DecompiledTextFactory.kt @@ -38,7 +38,7 @@ public fun buildDecompiledText( val classFqName = kotlinClass.getClassName().getFqNameForClassNameWithoutDollars() val classFileHeader = kotlinClass.getClassHeader() assert(classFileHeader != null) { "Decompiled data factory shouldn't be called on an unsupported file: " + classFile } - val kind = classFileHeader!!.getKind() + val kind = classFileHeader!!.kind val packageFqName = classFqName.parent() return if (kind == KotlinClassHeader.Kind.PACKAGE_FACADE) { diff --git a/idea/src/org/jetbrains/jet/plugin/libraries/DecompiledUtils.kt b/idea/src/org/jetbrains/jet/plugin/libraries/DecompiledUtils.kt index fb4dd548cfb..1126410aa9a 100644 --- a/idea/src/org/jetbrains/jet/plugin/libraries/DecompiledUtils.kt +++ b/idea/src/org/jetbrains/jet/plugin/libraries/DecompiledUtils.kt @@ -31,7 +31,7 @@ public fun isKotlinCompiledFile(file: VirtualFile): Boolean { return false } val header = KotlinBinaryClassCache.getKotlinBinaryClass(file).getClassHeader() - return header != null && header.getSyntheticClassKind() != KotlinSyntheticClass.Kind.TRAIT_IMPL + return header != null && header.syntheticClassKind != KotlinSyntheticClass.Kind.TRAIT_IMPL } public fun isKotlinCompiledFileWithIncompatibleAbiVersion(file: VirtualFile): Boolean { @@ -39,7 +39,7 @@ public fun isKotlinCompiledFileWithIncompatibleAbiVersion(file: VirtualFile): Bo return false } val header = KotlinBinaryClassCache.getKotlinBinaryClass(file).getClassHeader() - return header?.getKind() == KotlinClassHeader.Kind.INCOMPATIBLE_ABI_VERSION + return header?.kind == KotlinClassHeader.Kind.INCOMPATIBLE_ABI_VERSION } public fun isKotlinInternalCompiledFile(file: VirtualFile): Boolean { @@ -50,5 +50,5 @@ public fun isKotlinInternalCompiledFile(file: VirtualFile): Boolean { return true } val header = KotlinBinaryClassCache.getKotlinBinaryClass(file).getClassHeader() - return header?.getKind() == KotlinClassHeader.Kind.SYNTHETIC_CLASS + return header?.kind == KotlinClassHeader.Kind.SYNTHETIC_CLASS } diff --git a/idea/src/org/jetbrains/jet/plugin/libraries/DeserializerForDecompiler.kt b/idea/src/org/jetbrains/jet/plugin/libraries/DeserializerForDecompiler.kt index f6a632c19ad..a6fbccc3931 100644 --- a/idea/src/org/jetbrains/jet/plugin/libraries/DeserializerForDecompiler.kt +++ b/idea/src/org/jetbrains/jet/plugin/libraries/DeserializerForDecompiler.kt @@ -35,7 +35,6 @@ import org.jetbrains.jet.lang.resolve.kotlin.DeserializedResolverUtils import org.jetbrains.jet.descriptors.serialization.descriptors.DeserializedPackageMemberScope import org.jetbrains.jet.lang.resolve.kotlin.AnnotationDescriptorDeserializer import org.jetbrains.jet.lang.resolve.java.resolver.ErrorReporter -import org.jetbrains.jet.lang.types.ErrorUtils.getErrorModule import org.jetbrains.jet.lang.types.error.MissingDependencyErrorClassDescriptor import org.jetbrains.jet.lang.resolve.java.PackageClassUtils import com.intellij.openapi.diagnostic.Logger @@ -57,7 +56,7 @@ public class DeserializerForDecompiler(val packageDirectory: VirtualFile, val di assert(packageFqName == directoryPackageFqName, "Was called for $packageFqName but only $directoryPackageFqName is expected.") val packageClassFqName = PackageClassUtils.getPackageClassFqName(packageFqName) val binaryClassForPackageClass = localClassFinder.findKotlinClass(packageClassFqName) - val annotationData = binaryClassForPackageClass?.getClassHeader()?.getAnnotationData() + val annotationData = binaryClassForPackageClass?.getClassHeader()?.annotationData if (annotationData == null) { LOG.error("Could not read annotation data for $packageFqName from ${binaryClassForPackageClass?.getClassName()}") return Collections.emptyList() @@ -145,7 +144,7 @@ public class DeserializerForDecompiler(val packageDirectory: VirtualFile, val di } private fun deserializeBinaryClass(kotlinClass: KotlinJvmBinaryClass): ClassDescriptor { - val data = kotlinClass.getClassHeader()?.getAnnotationData() + val data = kotlinClass.getClassHeader()?.annotationData if (data == null) { LOG.error("Annotation data missing for ${kotlinClass.getClassName()}") } diff --git a/idea/tests/org/jetbrains/jet/plugin/libraries/InternalCompiledClassesTest.kt b/idea/tests/org/jetbrains/jet/plugin/libraries/InternalCompiledClassesTest.kt index 03e557ee89d..d8e29ce3013 100644 --- a/idea/tests/org/jetbrains/jet/plugin/libraries/InternalCompiledClassesTest.kt +++ b/idea/tests/org/jetbrains/jet/plugin/libraries/InternalCompiledClassesTest.kt @@ -24,18 +24,13 @@ import com.intellij.openapi.vfs.VirtualFile import org.jetbrains.jet.lang.resolve.java.JvmAbi import com.intellij.psi.PsiManager import junit.framework.Assert -import org.jetbrains.jet.lang.resolve.java.PackageClassUtils import com.intellij.psi.ClassFileViewProvider import com.intellij.psi.impl.compiled.ClsFileImpl import com.intellij.psi.PsiCompiledFile -import com.intellij.psi.PsiClass import com.intellij.psi.PsiJavaFile -import org.jetbrains.jet.lang.resolve.kotlin.header.KotlinClassHeader -import org.jetbrains.jet.storage.LockBasedStorageManager import org.jetbrains.jet.lang.resolve.java.JvmAnnotationNames.KotlinSyntheticClass import org.jetbrains.jet.lang.resolve.java.JvmAnnotationNames.KotlinSyntheticClass.Kind.* import org.jetbrains.jet.lang.resolve.kotlin.KotlinBinaryClassCache -import com.intellij.openapi.fileTypes.StdFileTypes public class InternalCompiledClassesTest : JetLightCodeInsightFixtureTestCase() { @@ -85,7 +80,7 @@ public class InternalCompiledClassesTest : JetLightCodeInsightFixtureTestCase() private fun isSyntheticClassOfKind(kind: KotlinSyntheticClass.Kind) : VirtualFile.() -> Boolean = { val header = KotlinBinaryClassCache.getKotlinBinaryClass(this).getClassHeader() - header?.getSyntheticClassKind() == kind + header?.syntheticClassKind == kind } private fun doTestNoPsiFilesAreBuiltForSyntheticClass(kind: KotlinSyntheticClass.Kind) =