Extract isCompatibleAbiVersion from kinds enum and store read kinds even for incompatible version

This commit is contained in:
Nikolay Krasko
2014-11-06 19:03:48 +03:00
parent 95a2dce627
commit c242665cd3
9 changed files with 70 additions and 37 deletions
@@ -89,15 +89,15 @@ private fun LazyJavaResolverContext.resolveBinaryClass(kotlinClass: KotlinJvmBin
if (kotlinClass == null) return null
val header = kotlinClass.getClassHeader()
if (header.kind == KotlinClassHeader.Kind.CLASS) {
if (!header.isCompatibleAbiVersion) {
errorReporter.reportIncompatibleAbiVersion(kotlinClass, header.version)
}
else if (header.kind == KotlinClassHeader.Kind.CLASS) {
val descriptor = packageFragmentProvider.resolveKotlinBinaryClass(kotlinClass)
if (descriptor != null) {
return JavaClassLookupResult(kClass = descriptor)
}
}
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
return JavaClassLookupResult()
@@ -78,7 +78,7 @@ public final class DeserializedDescriptorResolver {
@Nullable
public String[] readData(@NotNull KotlinJvmBinaryClass kotlinClass, @NotNull KotlinClassHeader.Kind expectedKind) {
KotlinClassHeader header = kotlinClass.getClassHeader();
if (header.getKind() == KotlinClassHeader.Kind.INCOMPATIBLE_ABI_VERSION) {
if (!header.getIsCompatibleAbiVersion()) {
errorReporter.reportIncompatibleAbiVersion(kotlinClass, header.getVersion());
}
else if (header.getKind() == expectedKind) {
@@ -17,6 +17,7 @@
package org.jetbrains.jet.lang.resolve.kotlin.header
import org.jetbrains.jet.lang.resolve.java.JvmAnnotationNames.KotlinSyntheticClass
import org.jetbrains.jet.lang.resolve.java.AbiVersionUtil
public class KotlinClassHeader(
public val kind: KotlinClassHeader.Kind,
@@ -24,19 +25,24 @@ public class KotlinClassHeader(
public val annotationData: Array<String>?,
public val syntheticClassKind: KotlinSyntheticClass.Kind?
) {
public val isCompatibleAbiVersion: Boolean get() = AbiVersionUtil.isAbiVersionCompatible(version);
{
assert((annotationData == null) == (kind != Kind.CLASS && kind != Kind.PACKAGE_FACADE)) {
assert(!isCompatibleAbiVersion || (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)) {
assert(!isCompatibleAbiVersion || (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
}
}
}
public fun KotlinClassHeader.isCompatibleClassKind(): Boolean = isCompatibleAbiVersion && kind == KotlinClassHeader.Kind.CLASS
public fun KotlinClassHeader.isCompatiblePackageFacadeKind(): Boolean = isCompatibleAbiVersion && kind == KotlinClassHeader.Kind.PACKAGE_FACADE
public fun KotlinClassHeader.isCompatibleSyntheticClassKind(): Boolean = isCompatibleAbiVersion && kind == KotlinClassHeader.Kind.SYNTHETIC_CLASS
@@ -21,7 +21,6 @@ import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.lang.resolve.java.AbiVersionUtil;
import org.jetbrains.jet.lang.resolve.java.JvmClassName;
import org.jetbrains.jet.lang.resolve.name.ClassId;
import org.jetbrains.jet.lang.resolve.name.FqName;
import org.jetbrains.jet.lang.resolve.name.Name;
import java.util.*;
@@ -33,21 +32,28 @@ import static org.jetbrains.jet.lang.resolve.kotlin.header.KotlinClassHeader.Kin
public class ReadKotlinClassHeaderAnnotationVisitor implements AnnotationVisitor {
private static final Map<JvmClassName, KotlinClassHeader.Kind> HEADER_KINDS = new HashMap<JvmClassName, KotlinClassHeader.Kind>();
private static final Map<JvmClassName, KotlinClassHeader.Kind> OLD_DEPRECATED_ANNOTATIONS_KINDS = new HashMap<JvmClassName, KotlinClassHeader.Kind>();
private int version = AbiVersionUtil.INVALID_VERSION;
static {
HEADER_KINDS.put(JvmClassName.byFqNameWithoutInnerClasses(KOTLIN_CLASS), CLASS);
HEADER_KINDS.put(JvmClassName.byFqNameWithoutInnerClasses(KOTLIN_PACKAGE), PACKAGE_FACADE);
HEADER_KINDS.put(KotlinSyntheticClass.CLASS_NAME, SYNTHETIC_CLASS);
@SuppressWarnings("deprecation")
List<FqName> incompatible = Arrays.asList(OLD_JET_CLASS_ANNOTATION, OLD_JET_PACKAGE_CLASS_ANNOTATION, OLD_KOTLIN_CLASS,
OLD_KOTLIN_PACKAGE, OLD_KOTLIN_PACKAGE_FRAGMENT, OLD_KOTLIN_TRAIT_IMPL);
for (FqName fqName : incompatible) {
HEADER_KINDS.put(JvmClassName.byFqNameWithoutInnerClasses(fqName), INCOMPATIBLE_ABI_VERSION);
}
initOldAnnotations();
}
@SuppressWarnings("deprecation")
private static void initOldAnnotations() {
OLD_DEPRECATED_ANNOTATIONS_KINDS.put(JvmClassName.byFqNameWithoutInnerClasses(OLD_JET_CLASS_ANNOTATION), CLASS);
OLD_DEPRECATED_ANNOTATIONS_KINDS.put(JvmClassName.byFqNameWithoutInnerClasses(OLD_JET_PACKAGE_CLASS_ANNOTATION),
KotlinClassHeader.Kind.PACKAGE_FACADE);
OLD_DEPRECATED_ANNOTATIONS_KINDS.put(JvmClassName.byFqNameWithoutInnerClasses(OLD_KOTLIN_CLASS), CLASS);
OLD_DEPRECATED_ANNOTATIONS_KINDS.put(JvmClassName.byFqNameWithoutInnerClasses(OLD_KOTLIN_PACKAGE), PACKAGE_FACADE);
OLD_DEPRECATED_ANNOTATIONS_KINDS.put(JvmClassName.byFqNameWithoutInnerClasses(OLD_KOTLIN_PACKAGE_FRAGMENT), SYNTHETIC_CLASS);
OLD_DEPRECATED_ANNOTATIONS_KINDS.put(JvmClassName.byFqNameWithoutInnerClasses(OLD_KOTLIN_TRAIT_IMPL), SYNTHETIC_CLASS);
}
private int version = AbiVersionUtil.INVALID_VERSION;
private String[] annotationData = null;
private KotlinClassHeader.Kind headerKind = null;
private KotlinSyntheticClass.Kind syntheticClassKind = null;
@@ -59,7 +65,7 @@ public class ReadKotlinClassHeaderAnnotationVisitor implements AnnotationVisitor
}
if (!AbiVersionUtil.isAbiVersionCompatible(version)) {
return new KotlinClassHeader(INCOMPATIBLE_ABI_VERSION, version, null, null);
return new KotlinClassHeader(headerKind, version, null, syntheticClassKind);
}
if ((headerKind == CLASS || headerKind == PACKAGE_FACADE) && annotationData == null) {
@@ -74,22 +80,32 @@ public class ReadKotlinClassHeaderAnnotationVisitor implements AnnotationVisitor
@Nullable
@Override
public AnnotationArgumentVisitor visitAnnotation(@NotNull ClassId classId) {
JvmClassName annotation = JvmClassName.byClassId(classId);
KotlinClassHeader.Kind newKind = HEADER_KINDS.get(annotation);
if (newKind == null) return null;
if (headerKind != null) {
// Ignore all Kotlin annotations except the first found
return null;
}
headerKind = newKind;
JvmClassName annotation = JvmClassName.byClassId(classId);
if (newKind == CLASS || newKind == PACKAGE_FACADE) {
return kotlinClassOrPackageVisitor(annotation);
KotlinClassHeader.Kind newKind = HEADER_KINDS.get(annotation);
if (newKind != null) {
headerKind = newKind;
switch (newKind) {
case CLASS:
return kotlinClassOrPackageVisitor(annotation);
case PACKAGE_FACADE:
return kotlinClassOrPackageVisitor(annotation);
case SYNTHETIC_CLASS:
return syntheticClassAnnotationVisitor();
default:
throw new IllegalStateException("Unknown kind: " + newKind);
}
}
else if (newKind == SYNTHETIC_CLASS) {
return syntheticClassAnnotationVisitor();
KotlinClassHeader.Kind oldAnnotationKind = OLD_DEPRECATED_ANNOTATIONS_KINDS.get(annotation);
if (oldAnnotationKind != null) {
headerKind = oldAnnotationKind;
}
return null;
@@ -145,6 +161,7 @@ public class ReadKotlinClassHeaderAnnotationVisitor implements AnnotationVisitor
@Override
public void visitEnd() {
//noinspection SSBasedInspection
annotationData = strings.toArray(new String[strings.size()]);
}
};