Minor, refactor FileBasedKotlinClass factory method
This commit is contained in:
+6
-3
@@ -16,8 +16,8 @@
|
|||||||
|
|
||||||
package org.jetbrains.jet.lang.resolve.kotlin;
|
package org.jetbrains.jet.lang.resolve.kotlin;
|
||||||
|
|
||||||
import com.intellij.openapi.util.Pair;
|
|
||||||
import com.intellij.openapi.util.Ref;
|
import com.intellij.openapi.util.Ref;
|
||||||
|
import kotlin.Function2;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
import org.jetbrains.annotations.Nullable;
|
import org.jetbrains.annotations.Nullable;
|
||||||
import org.jetbrains.jet.lang.resolve.java.JvmClassName;
|
import org.jetbrains.jet.lang.resolve.java.JvmClassName;
|
||||||
@@ -45,7 +45,10 @@ public abstract class FileBasedKotlinClass implements KotlinJvmBinaryClass {
|
|||||||
protected abstract byte[] getFileContents();
|
protected abstract byte[] getFileContents();
|
||||||
|
|
||||||
@Nullable
|
@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 ReadKotlinClassHeaderAnnotationVisitor readHeaderVisitor = new ReadKotlinClassHeaderAnnotationVisitor();
|
||||||
final Ref<JvmClassName> classNameRef = Ref.create();
|
final Ref<JvmClassName> classNameRef = Ref.create();
|
||||||
new ClassReader(fileContents).accept(new ClassVisitor(ASM5) {
|
new ClassReader(fileContents).accept(new ClassVisitor(ASM5) {
|
||||||
@@ -71,7 +74,7 @@ public abstract class FileBasedKotlinClass implements KotlinJvmBinaryClass {
|
|||||||
KotlinClassHeader header = readHeaderVisitor.createHeader();
|
KotlinClassHeader header = readHeaderVisitor.createHeader();
|
||||||
if (header == null) return null;
|
if (header == null) return null;
|
||||||
|
|
||||||
return Pair.create(className, header);
|
return factory.invoke(className, header);
|
||||||
}
|
}
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
|
|||||||
+8
-7
@@ -18,8 +18,8 @@ package org.jetbrains.jet.lang.resolve.kotlin;
|
|||||||
|
|
||||||
import com.intellij.ide.highlighter.JavaClassFileType;
|
import com.intellij.ide.highlighter.JavaClassFileType;
|
||||||
import com.intellij.openapi.diagnostic.Logger;
|
import com.intellij.openapi.diagnostic.Logger;
|
||||||
import com.intellij.openapi.util.Pair;
|
|
||||||
import com.intellij.openapi.vfs.VirtualFile;
|
import com.intellij.openapi.vfs.VirtualFile;
|
||||||
|
import kotlin.Function2;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
import org.jetbrains.annotations.Nullable;
|
import org.jetbrains.annotations.Nullable;
|
||||||
import org.jetbrains.jet.lang.resolve.java.JvmClassName;
|
import org.jetbrains.jet.lang.resolve.java.JvmClassName;
|
||||||
@@ -39,14 +39,15 @@ public final class VirtualFileKotlinClass extends FileBasedKotlinClass {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Nullable
|
@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;
|
assert file.getFileType() == JavaClassFileType.INSTANCE : "Trying to read binary data from a non-class file " + file;
|
||||||
try {
|
try {
|
||||||
byte[] fileContents = file.contentsToByteArray();
|
return create(file.contentsToByteArray(), new Function2<JvmClassName, KotlinClassHeader, VirtualFileKotlinClass>() {
|
||||||
Pair<JvmClassName, KotlinClassHeader> nameAndHeader = readClassNameAndHeader(fileContents);
|
@Override
|
||||||
if (nameAndHeader == null) return null;
|
public VirtualFileKotlinClass invoke(JvmClassName name, KotlinClassHeader header) {
|
||||||
|
return new VirtualFileKotlinClass(file, name, header);
|
||||||
return new VirtualFileKotlinClass(file, nameAndHeader.first, nameAndHeader.second);
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
catch (Throwable e) {
|
catch (Throwable e) {
|
||||||
LOG.warn(renderFileReadingErrorMessage(file));
|
LOG.warn(renderFileReadingErrorMessage(file));
|
||||||
|
|||||||
@@ -31,10 +31,10 @@ class LocalFileKotlinClass private(
|
|||||||
class object {
|
class object {
|
||||||
fun create(file: File): LocalFileKotlinClass? {
|
fun create(file: File): LocalFileKotlinClass? {
|
||||||
val fileContents = file.readBytes()
|
val fileContents = file.readBytes()
|
||||||
val nameAndHeader = FileBasedKotlinClass.readClassNameAndHeader(fileContents)
|
return FileBasedKotlinClass.create(fileContents) {
|
||||||
if (nameAndHeader == null) return null
|
className, classHeader ->
|
||||||
|
LocalFileKotlinClass(file, fileContents, className, classHeader)
|
||||||
return LocalFileKotlinClass(file, fileContents, nameAndHeader.first!!, nameAndHeader.second!!)
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user