Extracted util method reading class header from bytes.
This commit is contained in:
@@ -0,0 +1,31 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2010-2014 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;
|
||||||
|
|
||||||
|
import com.intellij.openapi.util.Pair;
|
||||||
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
import org.jetbrains.annotations.Nullable;
|
||||||
|
import org.jetbrains.jet.lang.resolve.java.JvmClassName;
|
||||||
|
import org.jetbrains.jet.lang.resolve.kotlin.header.KotlinClassHeader;
|
||||||
|
|
||||||
|
public class ReadClassHeader {
|
||||||
|
@Nullable
|
||||||
|
public static KotlinClassHeader readClassHeader(@NotNull byte[] fileContents) {
|
||||||
|
Pair<JvmClassName, KotlinClassHeader> pair = VirtualFileKotlinClass.readClassNameAndHeader(fileContents);
|
||||||
|
return pair == null ? null : pair.second;
|
||||||
|
}
|
||||||
|
}
|
||||||
+37
-25
@@ -17,6 +17,7 @@
|
|||||||
package org.jetbrains.jet.lang.resolve.kotlin;
|
package org.jetbrains.jet.lang.resolve.kotlin;
|
||||||
|
|
||||||
import com.intellij.openapi.diagnostic.Logger;
|
import com.intellij.openapi.diagnostic.Logger;
|
||||||
|
import com.intellij.openapi.util.Pair;
|
||||||
import com.intellij.openapi.util.Ref;
|
import com.intellij.openapi.util.Ref;
|
||||||
import com.intellij.openapi.vfs.VirtualFile;
|
import com.intellij.openapi.vfs.VirtualFile;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
@@ -47,35 +48,46 @@ public class VirtualFileKotlinClass implements KotlinJvmBinaryClass {
|
|||||||
this.classHeader = classHeader;
|
this.classHeader = classHeader;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Nullable
|
||||||
|
/* package */ static Pair<JvmClassName, KotlinClassHeader> readClassNameAndHeader(@NotNull byte[] fileContents) {
|
||||||
|
final ReadKotlinClassHeaderAnnotationVisitor readHeaderVisitor = new ReadKotlinClassHeaderAnnotationVisitor();
|
||||||
|
final Ref<JvmClassName> classNameRef = Ref.create();
|
||||||
|
new ClassReader(fileContents).accept(new ClassVisitor(ASM4) {
|
||||||
|
@Override
|
||||||
|
public void visit(int version, int access, String name, String signature, String superName, String[] interfaces) {
|
||||||
|
classNameRef.set(JvmClassName.byInternalName(name));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public org.jetbrains.asm4.AnnotationVisitor visitAnnotation(String desc, boolean visible) {
|
||||||
|
return convertAnnotationVisitor(readHeaderVisitor, desc);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void visitEnd() {
|
||||||
|
readHeaderVisitor.visitEnd();
|
||||||
|
}
|
||||||
|
}, SKIP_CODE | SKIP_DEBUG | SKIP_FRAMES);
|
||||||
|
|
||||||
|
JvmClassName className = classNameRef.get();
|
||||||
|
if (className == null) return null;
|
||||||
|
|
||||||
|
KotlinClassHeader header = readHeaderVisitor.createHeader();
|
||||||
|
if (header == null) return null;
|
||||||
|
|
||||||
|
return Pair.create(className, header);
|
||||||
|
}
|
||||||
|
|
||||||
@Nullable
|
@Nullable
|
||||||
/* package */ static VirtualFileKotlinClass create(@NotNull VirtualFile file) {
|
/* package */ static VirtualFileKotlinClass create(@NotNull VirtualFile file) {
|
||||||
try {
|
try {
|
||||||
final ReadKotlinClassHeaderAnnotationVisitor readHeaderVisitor = new ReadKotlinClassHeaderAnnotationVisitor();
|
byte[] fileContents = file.contentsToByteArray();
|
||||||
final Ref<JvmClassName> classNameRef = Ref.create();
|
Pair<JvmClassName, KotlinClassHeader> nameAndHeader = readClassNameAndHeader(fileContents);
|
||||||
new ClassReader(file.contentsToByteArray()).accept(new ClassVisitor(ASM4) {
|
if (nameAndHeader == null) {
|
||||||
@Override
|
return null;
|
||||||
public void visit(int version, int access, String name, String signature, String superName, String[] interfaces) {
|
}
|
||||||
classNameRef.set(JvmClassName.byInternalName(name));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
return new VirtualFileKotlinClass(file, nameAndHeader.first, nameAndHeader.second);
|
||||||
public org.jetbrains.asm4.AnnotationVisitor visitAnnotation(String desc, boolean visible) {
|
|
||||||
return convertAnnotationVisitor(readHeaderVisitor, desc);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void visitEnd() {
|
|
||||||
readHeaderVisitor.visitEnd();
|
|
||||||
}
|
|
||||||
}, SKIP_CODE | SKIP_DEBUG | SKIP_FRAMES);
|
|
||||||
|
|
||||||
JvmClassName className = classNameRef.get();
|
|
||||||
if (className == null) return null;
|
|
||||||
|
|
||||||
KotlinClassHeader header = readHeaderVisitor.createHeader();
|
|
||||||
if (header == null) return null;
|
|
||||||
|
|
||||||
return new VirtualFileKotlinClass(file, className, header);
|
|
||||||
}
|
}
|
||||||
catch (Throwable e) {
|
catch (Throwable e) {
|
||||||
LOG.warn(renderFileReadingErrorMessage(file), e);
|
LOG.warn(renderFileReadingErrorMessage(file), e);
|
||||||
|
|||||||
Reference in New Issue
Block a user