use of org.jetbrains.org.objectweb.asm.ClassVisitor in project kotlin by JetBrains.
the class CompilingEvaluatorUtils method changeSuperToMagicAccessor.
public static byte[] changeSuperToMagicAccessor(byte[] bytes) {
ClassWriter classWriter = new ClassWriter(0);
ClassVisitor classVisitor = new ClassVisitor(Opcodes.ASM5, classWriter) {
@Override
public void visit(int version, int access, String name, String signature, String superName, String[] interfaces) {
if ("java/lang/Object".equals(superName)) {
superName = "sun/reflect/MagicAccessorImpl";
}
super.visit(version, access, name, signature, superName, interfaces);
}
};
new ClassReader(bytes).accept(classVisitor, 0);
return classWriter.toByteArray();
}
use of org.jetbrains.org.objectweb.asm.ClassVisitor in project kotlin by JetBrains.
the class FileBasedKotlinClass method loadClassAnnotations.
@Override
public void loadClassAnnotations(@NotNull final AnnotationVisitor annotationVisitor, @Nullable byte[] cachedContents) {
byte[] fileContents = cachedContents != null ? cachedContents : getFileContents();
new ClassReader(fileContents).accept(new ClassVisitor(ASM5) {
@Override
public org.jetbrains.org.objectweb.asm.AnnotationVisitor visitAnnotation(@NotNull String desc, boolean visible) {
return convertAnnotationVisitor(annotationVisitor, desc, innerClasses);
}
@Override
public void visitEnd() {
annotationVisitor.visitEnd();
}
}, SKIP_CODE | SKIP_DEBUG | SKIP_FRAMES);
}
use of org.jetbrains.org.objectweb.asm.ClassVisitor in project kotlin by JetBrains.
the class FileBasedKotlinClass method visitMembers.
@Override
public void visitMembers(@NotNull final MemberVisitor memberVisitor, @Nullable byte[] cachedContents) {
byte[] fileContents = cachedContents != null ? cachedContents : getFileContents();
new ClassReader(fileContents).accept(new ClassVisitor(ASM5) {
@Override
public FieldVisitor visitField(int access, @NotNull String name, @NotNull String desc, String signature, Object value) {
final AnnotationVisitor v = memberVisitor.visitField(Name.identifier(name), desc, value);
if (v == null)
return null;
return new FieldVisitor(ASM5) {
@Override
public org.jetbrains.org.objectweb.asm.AnnotationVisitor visitAnnotation(@NotNull String desc, boolean visible) {
return convertAnnotationVisitor(v, desc, innerClasses);
}
@Override
public void visitEnd() {
v.visitEnd();
}
};
}
@Override
public MethodVisitor visitMethod(int access, @NotNull String name, @NotNull String desc, String signature, String[] exceptions) {
final MethodAnnotationVisitor v = memberVisitor.visitMethod(Name.identifier(name), desc);
if (v == null)
return null;
return new MethodVisitor(ASM5) {
@Override
public org.jetbrains.org.objectweb.asm.AnnotationVisitor visitAnnotation(@NotNull String desc, boolean visible) {
return convertAnnotationVisitor(v, desc, innerClasses);
}
@Override
public org.jetbrains.org.objectweb.asm.AnnotationVisitor visitParameterAnnotation(int parameter, @NotNull String desc, boolean visible) {
AnnotationArgumentVisitor av = v.visitParameterAnnotation(parameter, resolveNameByDesc(desc, innerClasses), SourceElement.NO_SOURCE);
return av == null ? null : convertAnnotationVisitor(av, innerClasses);
}
@Override
public void visitEnd() {
v.visitEnd();
}
};
}
}, SKIP_CODE | SKIP_DEBUG | SKIP_FRAMES);
}
use of org.jetbrains.org.objectweb.asm.ClassVisitor in project kotlin by JetBrains.
the class FileBasedKotlinClass method create.
// TODO public to be accessible in companion object of subclass, workaround for KT-3974
@Nullable
public static <T extends FileBasedKotlinClass> T create(@NotNull byte[] fileContents, @NotNull Function4<ClassId, Integer, KotlinClassHeader, InnerClassesInfo, T> factory) {
final ReadKotlinClassHeaderAnnotationVisitor readHeaderVisitor = new ReadKotlinClassHeaderAnnotationVisitor();
final Ref<String> classNameRef = Ref.create();
final Ref<Integer> classVersion = Ref.create();
final InnerClassesInfo innerClasses = new InnerClassesInfo();
new ClassReader(fileContents).accept(new ClassVisitor(ASM5) {
@Override
public void visit(int version, int access, @NotNull String name, String signature, String superName, String[] interfaces) {
classNameRef.set(name);
classVersion.set(version);
}
@Override
public void visitInnerClass(@NotNull String name, String outerName, String innerName, int access) {
innerClasses.add(name, outerName, innerName);
}
@Override
public org.jetbrains.org.objectweb.asm.AnnotationVisitor visitAnnotation(@NotNull String desc, boolean visible) {
return convertAnnotationVisitor(readHeaderVisitor, desc, innerClasses);
}
@Override
public void visitEnd() {
readHeaderVisitor.visitEnd();
}
}, SKIP_CODE | SKIP_DEBUG | SKIP_FRAMES);
String className = classNameRef.get();
if (className == null)
return null;
KotlinClassHeader header = readHeaderVisitor.createHeader();
if (header == null)
return null;
ClassId id = resolveNameByInternalName(className, innerClasses);
return factory.invoke(id, classVersion.get(), header, innerClasses);
}
use of org.jetbrains.org.objectweb.asm.ClassVisitor in project kotlin by JetBrains.
the class CompileKotlinAgainstCustomBinariesTest method testInlineFunWithoutDebugInfo.
/*test source mapping generation when source info is absent*/
public void testInlineFunWithoutDebugInfo() throws Exception {
compileKotlin("sourceInline.kt", tmpdir);
File inlineFunClass = new File(tmpdir.getAbsolutePath(), "test/A.class");
ClassWriter cw = new ClassWriter(Opcodes.ASM5);
new ClassReader(FilesKt.readBytes(inlineFunClass)).accept(new ClassVisitor(Opcodes.ASM5, cw) {
@Override
public void visitSource(String source, String debug) {
//skip debug info
}
}, 0);
assert inlineFunClass.delete();
assert !inlineFunClass.exists();
FilesKt.writeBytes(inlineFunClass, cw.toByteArray());
compileKotlin("source.kt", tmpdir, tmpdir);
final Ref<String> debugInfo = new Ref<String>();
File resultFile = new File(tmpdir.getAbsolutePath(), "test/B.class");
new ClassReader(FilesKt.readBytes(resultFile)).accept(new ClassVisitor(Opcodes.ASM5) {
@Override
public void visitSource(String source, String debug) {
//skip debug info
debugInfo.set(debug);
}
}, 0);
String expected = "SMAP\n" + "source.kt\n" + "Kotlin\n" + "*S Kotlin\n" + "*F\n" + "+ 1 source.kt\n" + "test/B\n" + "*L\n" + "1#1,13:1\n" + "*E\n";
if (InlineCodegenUtil.GENERATE_SMAP) {
assertEquals(expected, debugInfo.get());
} else {
assertEquals(null, debugInfo.get());
}
}
Aggregations