Concrete header classes replaced by kinds

AS a consequence, instanceof checks removed, code simplified
This commit is contained in:
Andrey Breslav
2014-01-07 21:04:05 +04:00
parent ce18d5ad8c
commit 8b37db12b9
12 changed files with 85 additions and 160 deletions
@@ -30,7 +30,6 @@ import org.jetbrains.jet.lang.resolve.java.resolver.ErrorReporter;
import org.jetbrains.jet.lang.resolve.java.resolver.JavaClassResolver;
import org.jetbrains.jet.lang.resolve.java.resolver.JavaPackageFragmentProvider;
import org.jetbrains.jet.lang.resolve.kotlin.header.KotlinClassHeader;
import org.jetbrains.jet.lang.resolve.kotlin.header.SerializedDataHeader;
import org.jetbrains.jet.lang.resolve.name.FqName;
import org.jetbrains.jet.lang.resolve.name.Name;
import org.jetbrains.jet.lang.resolve.scopes.JetScope;
@@ -41,8 +40,8 @@ import java.util.Collection;
import static org.jetbrains.jet.lang.resolve.java.DescriptorSearchRule.INCLUDE_KOTLIN_SOURCES;
import static org.jetbrains.jet.lang.resolve.kotlin.DeserializedResolverUtils.kotlinFqNameToJavaFqName;
import static org.jetbrains.jet.lang.resolve.kotlin.header.SerializedDataHeader.Kind.CLASS;
import static org.jetbrains.jet.lang.resolve.kotlin.header.SerializedDataHeader.Kind.PACKAGE;
import static org.jetbrains.jet.lang.resolve.kotlin.header.KotlinClassHeader.Kind.CLASS;
import static org.jetbrains.jet.lang.resolve.kotlin.header.KotlinClassHeader.Kind.PACKAGE_FACADE;
public final class DeserializedDescriptorResolver {
private AnnotationDescriptorDeserializer annotationDeserializer;
@@ -103,7 +102,7 @@ public final class DeserializedDescriptorResolver {
@Nullable
public JetScope createKotlinPackageScope(@NotNull PackageFragmentDescriptor descriptor, @NotNull KotlinJvmBinaryClass kotlinClass) {
String[] data = readData(kotlinClass, PACKAGE);
String[] data = readData(kotlinClass, PACKAGE_FACADE);
if (data != null) {
return new DeserializedPackageMemberScope(storageManager, descriptor, annotationDeserializer, javaDescriptorFinder,
JavaProtoBufUtil.readPackageDataFrom(data));
@@ -112,17 +111,16 @@ public final class DeserializedDescriptorResolver {
}
@Nullable
private String[] readData(@NotNull KotlinJvmBinaryClass kotlinClass, @NotNull SerializedDataHeader.Kind expectedKind) {
private String[] readData(@NotNull KotlinJvmBinaryClass kotlinClass, @NotNull KotlinClassHeader.Kind expectedKind) {
KotlinClassHeader header = kotlinClass.getClassHeader();
if (header instanceof SerializedDataHeader) {
SerializedDataHeader serializedDataHeader = (SerializedDataHeader) header;
if (serializedDataHeader.getKind() != expectedKind) return null;
return serializedDataHeader.getAnnotationData();
}
if (header == null) return null;
if (header != null) {
if (header.getKind() == KotlinClassHeader.Kind.INCOMPATIBLE_ABI_VERSION) {
errorReporter.reportIncompatibleAbiVersion(kotlinClass, header.getVersion());
}
else if (header.getKind() == expectedKind) {
return header.getAnnotationData();
}
return null;
}
@@ -1,23 +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;
public class IncompatibleAnnotationHeader extends KotlinClassHeader {
protected IncompatibleAnnotationHeader(int version) {
super(version);
}
}
@@ -16,15 +16,65 @@
package org.jetbrains.jet.lang.resolve.kotlin.header;
public abstract class KotlinClassHeader {
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
public class KotlinClassHeader {
@NotNull
public static KotlinClassHeader createClassHeader(int version, @NotNull String[] annotationData) {
return new KotlinClassHeader(Kind.CLASS, version, annotationData);
}
@NotNull
public static KotlinClassHeader createPackageFacadeHeader(int version, @NotNull String[] annotationData) {
return new KotlinClassHeader(Kind.PACKAGE_FACADE, version, annotationData);
}
@NotNull
public static KotlinClassHeader createPackageFragmentHeader(int version) {
return new KotlinClassHeader(Kind.PACKAGE_FRAGMENT, version, null);
}
@NotNull
public static KotlinClassHeader createTraitImplHeader(int version) {
return new KotlinClassHeader(Kind.TRAIT_IMPL, version, null);
}
@NotNull
public static KotlinClassHeader createIncompatibleVersionErrorHeader(int version) {
return new KotlinClassHeader(Kind.INCOMPATIBLE_ABI_VERSION, version, null);
}
public enum Kind {
CLASS,
PACKAGE_FACADE,
PACKAGE_FRAGMENT,
TRAIT_IMPL,
INCOMPATIBLE_ABI_VERSION
}
private final Kind kind;
private final int version;
private final String[] data;
protected KotlinClassHeader(int version) {
private KotlinClassHeader(@NotNull Kind kind, int version, @Nullable String[] annotationData) {
this.kind = kind;
this.version = version;
this.data = annotationData;
}
@NotNull
public Kind getKind() {
return kind;
}
public int getVersion() {
return version;
}
@Nullable
public String[] getAnnotationData() {
return data;
}
}
@@ -1,23 +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;
public class PackageFragmentClassHeader extends KotlinClassHeader {
protected PackageFragmentClassHeader(int version) {
super(version);
}
}
@@ -83,33 +83,27 @@ public class ReadKotlinClassHeaderAnnotationVisitor implements AnnotationVisitor
}
if (!AbiVersionUtil.isAbiVersionCompatible(version)) {
return new IncompatibleAnnotationHeader(version);
return KotlinClassHeader.createIncompatibleVersionErrorHeader(version);
}
switch (foundType) {
case CLASS:
return serializedDataHeader(SerializedDataHeader.Kind.CLASS);
// This means that the annotation is found and its ABI version is compatible, but there's no "data" string array in it.
// We tell the outside world that there's really no annotation at all
if (annotationData == null) return null;
return KotlinClassHeader.createClassHeader(version, annotationData);
case PACKAGE:
return serializedDataHeader(SerializedDataHeader.Kind.PACKAGE);
if (annotationData == null) return null;
return KotlinClassHeader.createPackageFacadeHeader(version, annotationData);
case PACKAGE_FRAGMENT:
return new PackageFragmentClassHeader(version);
return KotlinClassHeader.createPackageFragmentHeader(version);
case TRAIT_IMPL:
return new TraitImplClassHeader(version);
return KotlinClassHeader.createTraitImplHeader(version);
default:
throw new UnsupportedOperationException("Unknown compatible HeaderType: " + foundType);
}
}
@Nullable
private SerializedDataHeader serializedDataHeader(@NotNull SerializedDataHeader.Kind kind) {
if (annotationData == null) {
// This means that the annotation is found and its ABI version is compatible, but there's no "data" string array in it.
// We tell the outside world that there's really no annotation at all
return null;
}
return new SerializedDataHeader(version, annotationData, kind);
}
@Nullable
@Override
public AnnotationArgumentVisitor visitAnnotation(@NotNull JvmClassName annotationClassName) {
@@ -1,45 +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;
public class SerializedDataHeader extends KotlinClassHeader {
public enum Kind {
CLASS,
PACKAGE
}
private final String[] data;
private final Kind kind;
protected SerializedDataHeader(int version, @NotNull String[] annotationData, @NotNull Kind kind) {
super(version);
this.data = annotationData;
this.kind = kind;
}
@NotNull
public String[] getAnnotationData() {
return data;
}
@NotNull
public Kind getKind() {
return kind;
}
}
@@ -1,23 +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;
/* package */ class TraitImplClassHeader extends KotlinClassHeader {
protected TraitImplClassHeader(int version) {
super(version);
}
}
@@ -32,7 +32,6 @@ import org.jetbrains.jet.lang.resolve.java.JavaResolverPsiUtils;
import org.jetbrains.jet.lang.resolve.kotlin.KotlinJvmBinaryClass;
import org.jetbrains.jet.lang.resolve.kotlin.VirtualFileFinder;
import org.jetbrains.jet.lang.resolve.kotlin.header.KotlinClassHeader;
import org.jetbrains.jet.lang.resolve.kotlin.header.SerializedDataHeader;
import org.jetbrains.jet.lang.resolve.name.FqName;
import org.jetbrains.jet.lang.resolve.name.Name;
import org.jetbrains.jet.util.QualifiedNamesUtil;
@@ -120,8 +119,8 @@ public class JetFromJavaDescriptorHelper {
if (virtualFile != null) {
KotlinJvmBinaryClass kotlinClass = VirtualFileFinder.SERVICE.getInstance(psiClass.getProject()).createKotlinClass(virtualFile);
KotlinClassHeader header = kotlinClass.getClassHeader();
if (header instanceof SerializedDataHeader) {
return ((SerializedDataHeader) header).getAnnotationData();
if (header != null) {
return header.getAnnotationData();
}
}
return null;
@@ -31,7 +31,6 @@ import org.jetbrains.jet.lang.resolve.java.JavaDescriptorResolver;
import org.jetbrains.jet.lang.resolve.kotlin.KotlinJvmBinaryClass;
import org.jetbrains.jet.lang.resolve.kotlin.VirtualFileFinder;
import org.jetbrains.jet.lang.resolve.kotlin.header.KotlinClassHeader;
import org.jetbrains.jet.lang.resolve.kotlin.header.SerializedDataHeader;
import org.jetbrains.jet.lang.resolve.name.FqName;
import org.jetbrains.jet.renderer.DescriptorRenderer;
import org.jetbrains.jet.renderer.DescriptorRendererBuilder;
@@ -54,7 +53,7 @@ public final class DecompiledDataFactory {
@NotNull
private final JavaDescriptorResolver javaDescriptorResolver;
@NotNull
private final SerializedDataHeader classFileHeader;
private final KotlinClassHeader classFileHeader;
@NotNull
private final FqName classFqName;
@NotNull
@@ -72,8 +71,8 @@ public final class DecompiledDataFactory {
this.classFqName = kotlinClass.getClassName().getFqNameForClassNameWithoutDollars();
KotlinClassHeader header = kotlinClass.getClassHeader();
assert header instanceof SerializedDataHeader : "Decompiled data factory shouldn't be called on an unsupported file: " + classFile;
this.classFileHeader = (SerializedDataHeader) header;
assert header != null : "Decompiled data factory shouldn't be called on an unsupported file: " + classFile;
this.classFileHeader = header;
}
@NotNull
@@ -84,8 +83,8 @@ public final class DecompiledDataFactory {
private JetDecompiledData build() {
FqName packageFqName = classFqName.parent();
appendDecompiledTextAndPackageName(packageFqName);
SerializedDataHeader.Kind kind = classFileHeader.getKind();
if (kind == SerializedDataHeader.Kind.PACKAGE) {
KotlinClassHeader.Kind kind = classFileHeader.getKind();
if (kind == KotlinClassHeader.Kind.PACKAGE_FACADE) {
PackageFragmentDescriptor pf = javaDescriptorResolver.getPackageFragmentProvider().getPackageFragment(packageFqName);
if (pf != null) {
for (DeclarationDescriptor member : sortDeclarations(pf.getMemberScope().getAllDescriptors())) {
@@ -96,7 +95,7 @@ public final class DecompiledDataFactory {
}
}
}
else if (kind == SerializedDataHeader.Kind.CLASS) {
else if (kind == KotlinClassHeader.Kind.CLASS) {
ClassDescriptor cd = javaDescriptorResolver.resolveClass(classFqName, INCLUDE_KOTLIN_SOURCES);
if (cd != null) {
appendDescriptor(cd, "");
@@ -22,7 +22,7 @@ import com.intellij.openapi.vfs.VirtualFile;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.lang.resolve.kotlin.KotlinJvmBinaryClass;
import org.jetbrains.jet.lang.resolve.kotlin.VirtualFileFinder;
import org.jetbrains.jet.lang.resolve.kotlin.header.SerializedDataHeader;
import org.jetbrains.jet.lang.resolve.kotlin.header.KotlinClassHeader;
public final class DecompiledUtils {
@@ -32,7 +32,8 @@ public final class DecompiledUtils {
}
//TODO: check index
KotlinJvmBinaryClass kotlinClass = VirtualFileFinder.SERVICE.getInstance(project).createKotlinClass(file);
return kotlinClass.getClassHeader() instanceof SerializedDataHeader;
KotlinClassHeader header = kotlinClass.getClassHeader();
return header != null && header.getAnnotationData() != null;
}
private DecompiledUtils() {
@@ -25,9 +25,8 @@ import com.intellij.psi.stubs.PsiFileStub;
import com.intellij.util.cls.ClsFormatException;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.lang.resolve.java.PackageClassUtils;
import org.jetbrains.jet.lang.resolve.kotlin.KotlinJvmBinaryClass;
import org.jetbrains.jet.lang.resolve.kotlin.VirtualFileKotlinClass;
import org.jetbrains.jet.lang.resolve.kotlin.header.PackageFragmentClassHeader;
import org.jetbrains.jet.lang.resolve.kotlin.header.KotlinClassHeader;
import org.jetbrains.jet.storage.LockBasedStorageManager;
/**
@@ -46,8 +45,8 @@ public class EmptyPackageFragmentClsStubBuilderFactory extends ClsStubBuilderFac
public boolean canBeProcessed(VirtualFile file, byte[] bytes) {
if (file.getName().contains(PackageClassUtils.PACKAGE_CLASS_NAME_SUFFIX + "-") &&
StdFileTypes.CLASS.getDefaultExtension().equals(file.getExtension())) {
KotlinJvmBinaryClass kotlinClass = new VirtualFileKotlinClass(LockBasedStorageManager.NO_LOCKS, file);
return kotlinClass.getClassHeader() instanceof PackageFragmentClassHeader;
KotlinClassHeader header = new VirtualFileKotlinClass(LockBasedStorageManager.NO_LOCKS, file).getClassHeader();
return header != null && header.getKind() == KotlinClassHeader.Kind.PACKAGE_FRAGMENT;
}
return false;
}
@@ -24,7 +24,6 @@ import com.intellij.util.io.KeyDescriptor;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.lang.resolve.kotlin.KotlinJvmBinaryClass;
import org.jetbrains.jet.lang.resolve.kotlin.VirtualFileFinder;
import org.jetbrains.jet.lang.resolve.kotlin.header.IncompatibleAnnotationHeader;
import org.jetbrains.jet.lang.resolve.kotlin.header.KotlinClassHeader;
import org.jetbrains.jet.lang.resolve.name.FqName;
@@ -79,7 +78,7 @@ public final class KotlinClassFileIndex extends ScalarIndexExtension<FqName> {
KotlinJvmBinaryClass kotlinClass = VirtualFileFinder.SERVICE.getInstance(inputData.getProject())
.createKotlinClass(inputData.getFile());
KotlinClassHeader header = kotlinClass.getClassHeader();
if (header != null && !(header instanceof IncompatibleAnnotationHeader)) {
if (header != null && header.getKind() != KotlinClassHeader.Kind.INCOMPATIBLE_ABI_VERSION) {
return Collections.singletonMap(kotlinClass.getClassName().getFqNameForClassNameWithoutDollars(), null);
}
}