Refactor KotlinClassFileHeader
Split into two subclasses: KotlinClass/KotlinPackage-annotated classes and JetClass/JetPackage-annotated classes
This commit is contained in:
+44
-51
@@ -36,63 +36,45 @@ import java.util.List;
|
||||
import static org.jetbrains.asm4.ClassReader.*;
|
||||
import static org.jetbrains.jet.lang.resolve.java.AbiVersionUtil.isAbiVersionCompatible;
|
||||
|
||||
public final class KotlinClassFileHeader {
|
||||
public abstract class KotlinClassFileHeader {
|
||||
private static final Logger LOG = Logger.getInstance(KotlinClassFileHeader.class);
|
||||
|
||||
@Nullable
|
||||
public static KotlinClassFileHeader readKotlinHeaderFromClassFile(@NotNull VirtualFile virtualFile) {
|
||||
try {
|
||||
ClassReader reader = new ClassReader(virtualFile.contentsToByteArray());
|
||||
ReadDataFromAnnotationVisitor visitor = new ReadDataFromAnnotationVisitor();
|
||||
reader.accept(visitor, SKIP_CODE | SKIP_FRAMES | SKIP_DEBUG);
|
||||
if (visitor.foundType == null) {
|
||||
ReadDataFromAnnotationVisitor v = new ReadDataFromAnnotationVisitor();
|
||||
reader.accept(v, SKIP_CODE | SKIP_FRAMES | SKIP_DEBUG);
|
||||
if (v.foundType == null) {
|
||||
return null;
|
||||
}
|
||||
if (visitor.fqName == null) {
|
||||
if (v.fqName == null) {
|
||||
LOG.error("File doesn't have a class name: " + virtualFile);
|
||||
return null;
|
||||
}
|
||||
return new KotlinClassFileHeader(visitor.version, visitor.annotationData, visitor.foundType, visitor.fqName);
|
||||
|
||||
switch (v.foundType) {
|
||||
case CLASS:
|
||||
return SerializedDataHeader.create(v.version, v.annotationData, SerializedDataHeader.Kind.CLASS, v.fqName);
|
||||
case PACKAGE:
|
||||
return SerializedDataHeader.create(v.version, v.annotationData, SerializedDataHeader.Kind.PACKAGE, v.fqName);
|
||||
case OLD_CLASS:
|
||||
case OLD_PACKAGE:
|
||||
return new OldAnnotationHeader(v.fqName);
|
||||
default:
|
||||
throw new UnsupportedOperationException("Unknown HeaderType: " + v.foundType);
|
||||
}
|
||||
}
|
||||
catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings("deprecation")
|
||||
public enum HeaderType {
|
||||
CLASS(JvmAnnotationNames.KOTLIN_CLASS),
|
||||
PACKAGE(JvmAnnotationNames.KOTLIN_PACKAGE),
|
||||
OLD_CLASS(JvmAnnotationNames.OLD_JET_CLASS_ANNOTATION),
|
||||
OLD_PACKAGE(JvmAnnotationNames.OLD_JET_PACKAGE_CLASS_ANNOTATION);
|
||||
|
||||
@NotNull
|
||||
private final JvmClassName annotation;
|
||||
|
||||
private HeaderType(@NotNull JvmClassName annotation) {
|
||||
this.annotation = annotation;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
private static HeaderType byDescriptor(@NotNull String desc) {
|
||||
for (HeaderType headerType : HeaderType.values()) {
|
||||
if (desc.equals(headerType.annotation.getDescriptor())) {
|
||||
return headerType;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private final int version;
|
||||
private final String[] annotationData;
|
||||
private final HeaderType type;
|
||||
private final FqName fqName;
|
||||
|
||||
private KotlinClassFileHeader(int version, @Nullable String[] annotationData, @NotNull HeaderType type, @NotNull FqName fqName) {
|
||||
protected KotlinClassFileHeader(int version, @NotNull FqName fqName) {
|
||||
this.version = version;
|
||||
this.annotationData = annotationData;
|
||||
this.type = type;
|
||||
this.fqName = fqName;
|
||||
}
|
||||
|
||||
@@ -100,20 +82,6 @@ public final class KotlinClassFileHeader {
|
||||
return version;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public String[] getAnnotationData() {
|
||||
if (isCompatibleKotlinCompiledFile() && annotationData == null) {
|
||||
LOG.error("Kotlin annotation " + type + " is incorrect for class: " + fqName);
|
||||
return null;
|
||||
}
|
||||
return annotationData;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public HeaderType getType() {
|
||||
return type;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return FQ name for class header or package class FQ name for package header (e.g. <code>test.TestPackage</code>)
|
||||
*/
|
||||
@@ -126,10 +94,35 @@ public final class KotlinClassFileHeader {
|
||||
* @return true if this is a header for compiled Kotlin file with correct abi version which can be processed by compiler or the IDE
|
||||
*/
|
||||
public boolean isCompatibleKotlinCompiledFile() {
|
||||
return (type == HeaderType.CLASS || type == HeaderType.PACKAGE) && isAbiVersionCompatible(version);
|
||||
return isAbiVersionCompatible(version);
|
||||
}
|
||||
|
||||
private static class ReadDataFromAnnotationVisitor extends ClassVisitor {
|
||||
@SuppressWarnings("deprecation")
|
||||
private enum HeaderType {
|
||||
CLASS(JvmAnnotationNames.KOTLIN_CLASS),
|
||||
PACKAGE(JvmAnnotationNames.KOTLIN_PACKAGE),
|
||||
OLD_CLASS(JvmAnnotationNames.OLD_JET_CLASS_ANNOTATION),
|
||||
OLD_PACKAGE(JvmAnnotationNames.OLD_JET_PACKAGE_CLASS_ANNOTATION);
|
||||
|
||||
@NotNull
|
||||
private final JvmClassName annotation;
|
||||
|
||||
private HeaderType(@NotNull JvmClassName annotation) {
|
||||
this.annotation = annotation;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
private static HeaderType byDescriptor(@NotNull String desc) {
|
||||
for (HeaderType headerType : HeaderType.values()) {
|
||||
if (desc.equals(headerType.annotation.getDescriptor())) {
|
||||
return headerType;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private int version = AbiVersionUtil.INVALID_VERSION;
|
||||
@Nullable
|
||||
private String[] annotationData = null;
|
||||
|
||||
+27
@@ -0,0 +1,27 @@
|
||||
/*
|
||||
* 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.java.header;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.jet.lang.resolve.java.AbiVersionUtil;
|
||||
import org.jetbrains.jet.lang.resolve.name.FqName;
|
||||
|
||||
public class OldAnnotationHeader extends KotlinClassFileHeader {
|
||||
protected OldAnnotationHeader(@NotNull FqName fqName) {
|
||||
super(AbiVersionUtil.INVALID_VERSION, fqName);
|
||||
}
|
||||
}
|
||||
+61
@@ -0,0 +1,61 @@
|
||||
/*
|
||||
* 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.java.header;
|
||||
|
||||
import com.intellij.openapi.diagnostic.Logger;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.jetbrains.jet.lang.resolve.name.FqName;
|
||||
|
||||
import static org.jetbrains.jet.lang.resolve.java.AbiVersionUtil.isAbiVersionCompatible;
|
||||
|
||||
public class SerializedDataHeader extends KotlinClassFileHeader {
|
||||
private static final Logger LOG = Logger.getInstance(SerializedDataHeader.class);
|
||||
|
||||
public enum Kind {
|
||||
CLASS,
|
||||
PACKAGE
|
||||
}
|
||||
|
||||
private final String[] data;
|
||||
private final Kind kind;
|
||||
|
||||
private SerializedDataHeader(int version, @Nullable String[] annotationData, @NotNull Kind kind, @NotNull FqName fqName) {
|
||||
super(version, fqName);
|
||||
this.data = annotationData;
|
||||
this.kind = kind;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public static SerializedDataHeader create(int version, @Nullable String[] annotationData, @NotNull Kind kind, @NotNull FqName fqName) {
|
||||
if (isAbiVersionCompatible(version) && annotationData == null) {
|
||||
LOG.error("Kotlin annotation " + kind + " is incorrect for class: " + fqName);
|
||||
return null;
|
||||
}
|
||||
return new SerializedDataHeader(version, annotationData, kind, fqName);
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public String[] getAnnotationData() {
|
||||
return data;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public Kind getKind() {
|
||||
return kind;
|
||||
}
|
||||
}
|
||||
+7
-8
@@ -26,6 +26,7 @@ import org.jetbrains.jet.lang.descriptors.ClassDescriptor;
|
||||
import org.jetbrains.jet.lang.descriptors.DeclarationDescriptor;
|
||||
import org.jetbrains.jet.lang.descriptors.NamespaceDescriptor;
|
||||
import org.jetbrains.jet.lang.resolve.java.header.KotlinClassFileHeader;
|
||||
import org.jetbrains.jet.lang.resolve.java.header.SerializedDataHeader;
|
||||
import org.jetbrains.jet.lang.resolve.lazy.storage.LockBasedStorageManager;
|
||||
import org.jetbrains.jet.lang.resolve.name.FqName;
|
||||
import org.jetbrains.jet.lang.resolve.name.Name;
|
||||
@@ -34,7 +35,6 @@ import org.jetbrains.jet.lang.resolve.scopes.JetScope;
|
||||
import javax.inject.Inject;
|
||||
import java.util.Collection;
|
||||
|
||||
import static org.jetbrains.jet.lang.resolve.java.AbiVersionUtil.isAbiVersionCompatible;
|
||||
import static org.jetbrains.jet.lang.resolve.java.DescriptorSearchRule.IGNORE_KOTLIN_SOURCES;
|
||||
import static org.jetbrains.jet.lang.resolve.java.DescriptorSearchRule.INCLUDE_KOTLIN_SOURCES;
|
||||
import static org.jetbrains.jet.lang.resolve.java.resolver.DeserializedResolverUtils.kotlinFqNameToJavaFqName;
|
||||
@@ -127,14 +127,13 @@ public final class DeserializedDescriptorResolver {
|
||||
@Nullable
|
||||
private String[] readData(@NotNull VirtualFile virtualFile) {
|
||||
KotlinClassFileHeader header = KotlinClassFileHeader.readKotlinHeaderFromClassFile(virtualFile);
|
||||
if (header == null) {
|
||||
return null;
|
||||
if (header instanceof SerializedDataHeader && header.isCompatibleKotlinCompiledFile()) {
|
||||
return ((SerializedDataHeader) header).getAnnotationData();
|
||||
}
|
||||
int version = header.getVersion();
|
||||
if (!isAbiVersionCompatible(version)) {
|
||||
errorReporter.reportIncompatibleAbiVersion(header.getFqName(), virtualFile, version);
|
||||
return null;
|
||||
|
||||
if (header != null) {
|
||||
errorReporter.reportIncompatibleAbiVersion(header.getFqName(), virtualFile, header.getVersion());
|
||||
}
|
||||
return header.getAnnotationData();
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user