Minor, refactor FileBasedKotlinClass factory method

This commit is contained in:
Alexander Udalov
2014-09-12 21:15:47 +04:00
parent fa39bf03a0
commit 9648c50ac9
3 changed files with 18 additions and 14 deletions
@@ -16,8 +16,8 @@
package org.jetbrains.jet.lang.resolve.kotlin;
import com.intellij.openapi.util.Pair;
import com.intellij.openapi.util.Ref;
import kotlin.Function2;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.lang.resolve.java.JvmClassName;
@@ -45,7 +45,10 @@ public abstract class FileBasedKotlinClass implements KotlinJvmBinaryClass {
protected abstract byte[] getFileContents();
@Nullable
public static Pair<JvmClassName, KotlinClassHeader> readClassNameAndHeader(@NotNull byte[] fileContents) {
protected static <T extends FileBasedKotlinClass> T create(
@NotNull byte[] fileContents,
@NotNull Function2<JvmClassName, KotlinClassHeader, T> factory
) {
final ReadKotlinClassHeaderAnnotationVisitor readHeaderVisitor = new ReadKotlinClassHeaderAnnotationVisitor();
final Ref<JvmClassName> classNameRef = Ref.create();
new ClassReader(fileContents).accept(new ClassVisitor(ASM5) {
@@ -71,7 +74,7 @@ public abstract class FileBasedKotlinClass implements KotlinJvmBinaryClass {
KotlinClassHeader header = readHeaderVisitor.createHeader();
if (header == null) return null;
return Pair.create(className, header);
return factory.invoke(className, header);
}
@NotNull
@@ -18,8 +18,8 @@ package org.jetbrains.jet.lang.resolve.kotlin;
import com.intellij.ide.highlighter.JavaClassFileType;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.util.Pair;
import com.intellij.openapi.vfs.VirtualFile;
import kotlin.Function2;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.lang.resolve.java.JvmClassName;
@@ -39,14 +39,15 @@ public final class VirtualFileKotlinClass extends FileBasedKotlinClass {
}
@Nullable
/* package */ static VirtualFileKotlinClass create(@NotNull VirtualFile file) {
/* package */ static VirtualFileKotlinClass create(@NotNull final VirtualFile file) {
assert file.getFileType() == JavaClassFileType.INSTANCE : "Trying to read binary data from a non-class file " + file;
try {
byte[] fileContents = file.contentsToByteArray();
Pair<JvmClassName, KotlinClassHeader> nameAndHeader = readClassNameAndHeader(fileContents);
if (nameAndHeader == null) return null;
return new VirtualFileKotlinClass(file, nameAndHeader.first, nameAndHeader.second);
return create(file.contentsToByteArray(), new Function2<JvmClassName, KotlinClassHeader, VirtualFileKotlinClass>() {
@Override
public VirtualFileKotlinClass invoke(JvmClassName name, KotlinClassHeader header) {
return new VirtualFileKotlinClass(file, name, header);
}
});
}
catch (Throwable e) {
LOG.warn(renderFileReadingErrorMessage(file));