Minor refactorings in KotlinClassFileHeader

This commit is contained in:
Alexander Udalov
2013-09-19 18:16:25 +04:00
parent b9ce7f9289
commit b4d9fb75d8
4 changed files with 20 additions and 33 deletions
@@ -1,5 +1,6 @@
package org.jetbrains.jet.lang.resolve.java.resolver; package org.jetbrains.jet.lang.resolve.java.resolver;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.vfs.VirtualFile; import com.intellij.openapi.vfs.VirtualFile;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
@@ -7,9 +8,6 @@ import org.jetbrains.asm4.AnnotationVisitor;
import org.jetbrains.asm4.ClassReader; import org.jetbrains.asm4.ClassReader;
import org.jetbrains.asm4.ClassVisitor; import org.jetbrains.asm4.ClassVisitor;
import org.jetbrains.asm4.Opcodes; import org.jetbrains.asm4.Opcodes;
import org.jetbrains.jet.descriptors.serialization.ClassData;
import org.jetbrains.jet.descriptors.serialization.JavaProtoBufUtil;
import org.jetbrains.jet.descriptors.serialization.PackageData;
import org.jetbrains.jet.lang.resolve.java.AbiVersionUtil; import org.jetbrains.jet.lang.resolve.java.AbiVersionUtil;
import org.jetbrains.jet.lang.resolve.java.JvmAnnotationNames; import org.jetbrains.jet.lang.resolve.java.JvmAnnotationNames;
import org.jetbrains.jet.lang.resolve.java.JvmClassName; import org.jetbrains.jet.lang.resolve.java.JvmClassName;
@@ -23,6 +21,8 @@ import static org.jetbrains.asm4.ClassReader.*;
import static org.jetbrains.jet.lang.resolve.java.AbiVersionUtil.isAbiVersionCompatible; import static org.jetbrains.jet.lang.resolve.java.AbiVersionUtil.isAbiVersionCompatible;
public final class KotlinClassFileHeader { public final class KotlinClassFileHeader {
private static final Logger LOG = Logger.getInstance(KotlinClassFileHeader.class);
@NotNull @NotNull
public static KotlinClassFileHeader readKotlinHeaderFromClassFile(@NotNull VirtualFile virtualFile) { public static KotlinClassFileHeader readKotlinHeaderFromClassFile(@NotNull VirtualFile virtualFile) {
try { try {
@@ -48,16 +48,16 @@ public final class KotlinClassFileHeader {
@Nullable @Nullable
private final JvmClassName correspondingAnnotation; private final JvmClassName correspondingAnnotation;
HeaderType(@Nullable JvmClassName annotation) { private HeaderType(@Nullable JvmClassName annotation) {
correspondingAnnotation = annotation; correspondingAnnotation = annotation;
} }
boolean isValidAnnotation() { private boolean isValidAnnotation() {
return this == CLASS || this == PACKAGE; return this == CLASS || this == PACKAGE;
} }
@NotNull @NotNull
public static HeaderType byDescriptor(@NotNull String desc) { private static HeaderType byDescriptor(@NotNull String desc) {
for (HeaderType headerType : HeaderType.values()) { for (HeaderType headerType : HeaderType.values()) {
JvmClassName annotation = headerType.correspondingAnnotation; JvmClassName annotation = headerType.correspondingAnnotation;
if (annotation == null) { if (annotation == null) {
@@ -76,9 +76,9 @@ public final class KotlinClassFileHeader {
@Nullable @Nullable
private String[] annotationData = null; private String[] annotationData = null;
@NotNull @NotNull
HeaderType type = HeaderType.NONE; private HeaderType type = HeaderType.NONE;
@Nullable @Nullable
JvmClassName jvmClassName = null; private JvmClassName jvmClassName = null;
public int getVersion() { public int getVersion() {
return version; return version;
@@ -89,10 +89,10 @@ public final class KotlinClassFileHeader {
return type; return type;
} }
/* /**
Checks that this is a header for compiled Kotlin file with correct abi version which can be processed by compiler or the IDE. * @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 isKotlinCompiledFile() { public boolean isCompatibleKotlinCompiledFile() {
return type.isValidAnnotation() && isAbiVersionCompatible(version); return type.isValidAnnotation() && isAbiVersionCompatible(version);
} }
@@ -105,27 +105,12 @@ public final class KotlinClassFileHeader {
return jvmClassName.getFqName(); return jvmClassName.getFqName();
} }
@Nullable
public String[] getAnnotationData() { public String[] getAnnotationData() {
assertDataRead();
return annotationData;
}
private void assertDataRead() {
if (annotationData == null && type != HeaderType.NONE) { if (annotationData == null && type != HeaderType.NONE) {
throw new IllegalStateException("Data for annotations " + type.correspondingAnnotation + " was not read."); LOG.error("Data for annotations " + type.correspondingAnnotation + " was not read.");
} }
} return annotationData;
@NotNull
public ClassData readClassData() {
assert type == HeaderType.CLASS;
return JavaProtoBufUtil.readClassDataFrom(getAnnotationData());
}
@NotNull
public PackageData readPackageData() {
assert type == HeaderType.PACKAGE;
return JavaProtoBufUtil.readPackageDataFrom(getAnnotationData());
} }
private class ReadDataFromAnnotationVisitor extends ClassVisitor { private class ReadDataFromAnnotationVisitor extends ClassVisitor {
@@ -103,14 +103,16 @@ public class JetFromJavaDescriptorHelper {
private static ClassData getClassData(@NotNull PsiClass psiClass) { private static ClassData getClassData(@NotNull PsiClass psiClass) {
VirtualFile virtualFile = getVirtualFileForPsiClass(psiClass); VirtualFile virtualFile = getVirtualFileForPsiClass(psiClass);
if (virtualFile == null) return null; if (virtualFile == null) return null;
return KotlinClassFileHeader.readKotlinHeaderFromClassFile(virtualFile).readClassData(); String[] data = KotlinClassFileHeader.readKotlinHeaderFromClassFile(virtualFile).getAnnotationData();
return data == null ? null : JavaProtoBufUtil.readClassDataFrom(data);
} }
@Nullable @Nullable
private static PackageData getPackageData(@NotNull PsiClass psiClass) { private static PackageData getPackageData(@NotNull PsiClass psiClass) {
VirtualFile virtualFile = getVirtualFileForPsiClass(psiClass); VirtualFile virtualFile = getVirtualFileForPsiClass(psiClass);
if (virtualFile == null) return null; if (virtualFile == null) return null;
return KotlinClassFileHeader.readKotlinHeaderFromClassFile(virtualFile).readPackageData(); String[] data = KotlinClassFileHeader.readKotlinHeaderFromClassFile(virtualFile).getAnnotationData();
return data == null ? null : JavaProtoBufUtil.readPackageDataFrom(data);
} }
//TODO: common utility //TODO: common utility
@@ -28,7 +28,7 @@ public final class DecompiledUtils {
return false; return false;
} }
//TODO: check index //TODO: check index
return KotlinClassFileHeader.readKotlinHeaderFromClassFile(file).isKotlinCompiledFile(); return KotlinClassFileHeader.readKotlinHeaderFromClassFile(file).isCompatibleKotlinCompiledFile();
} }
private DecompiledUtils() { private DecompiledUtils() {
@@ -58,7 +58,7 @@ public final class KotlinClassFileIndex extends ScalarIndexExtension<FqName> {
public Map<FqName, Void> map(FileContent inputData) { public Map<FqName, Void> map(FileContent inputData) {
try { try {
KotlinClassFileHeader header = KotlinClassFileHeader.readKotlinHeaderFromClassFile(inputData.getFile()); KotlinClassFileHeader header = KotlinClassFileHeader.readKotlinHeaderFromClassFile(inputData.getFile());
if (header.isKotlinCompiledFile()) { if (header.isCompatibleKotlinCompiledFile()) {
return Collections.singletonMap(header.getFqName(), null); return Collections.singletonMap(header.getFqName(), null);
} }
} }