Search in sources :

Example 6 with ClassVisitor

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();
}
Also used : ClassReader(org.jetbrains.org.objectweb.asm.ClassReader) ClassVisitor(org.jetbrains.org.objectweb.asm.ClassVisitor) ClassWriter(org.jetbrains.org.objectweb.asm.ClassWriter)

Example 7 with ClassVisitor

use of org.jetbrains.org.objectweb.asm.ClassVisitor in project kotlin by JetBrains.

the class GenerateNotNullAssertionsTest method assertNoIntrinsicsMethodIsCalled.

private void assertNoIntrinsicsMethodIsCalled(String className, boolean noClassFileIsAnError) {
    OutputFileCollection classes = generateClassesInFile();
    OutputFile file = classes.get(className + ".class");
    if (noClassFileIsAnError) {
        assertNotNull("File for " + className + " is absent", file);
    } else if (file == null) {
        return;
    }
    ClassReader reader = new ClassReader(file.asByteArray());
    reader.accept(new ClassVisitor(Opcodes.ASM5) {

        @Override
        public MethodVisitor visitMethod(int access, @NotNull final String callerName, @NotNull final String callerDesc, String signature, String[] exceptions) {
            return new MethodVisitor(Opcodes.ASM5) {

                @Override
                public void visitMethodInsn(int opcode, String owner, String name, String desc, boolean itf) {
                    assertFalse("Intrinsics method is called: " + name + desc + "  Caller: " + callerName + callerDesc, "kotlin/jvm/internal/Intrinsics".equals(owner));
                }
            };
        }
    }, 0);
}
Also used : OutputFile(org.jetbrains.kotlin.backend.common.output.OutputFile) OutputFileCollection(org.jetbrains.kotlin.backend.common.output.OutputFileCollection) ClassReader(org.jetbrains.org.objectweb.asm.ClassReader) ClassVisitor(org.jetbrains.org.objectweb.asm.ClassVisitor) MethodVisitor(org.jetbrains.org.objectweb.asm.MethodVisitor)

Example 8 with ClassVisitor

use of org.jetbrains.org.objectweb.asm.ClassVisitor in project intellij-community by JetBrains.

the class ClassProcessingBuilder method getClassFileVersion.

public static int getClassFileVersion(ClassReader reader) {
    final Ref<Integer> result = new Ref<>(0);
    reader.accept(new ClassVisitor(Opcodes.API_VERSION) {

        public void visit(int version, int access, String name, String signature, String superName, String[] interfaces) {
            result.set(version);
        }
    }, ClassReader.SKIP_CODE | ClassReader.SKIP_DEBUG | ClassReader.SKIP_FRAMES);
    return result.get();
}
Also used : Ref(com.intellij.openapi.util.Ref) ClassVisitor(org.jetbrains.org.objectweb.asm.ClassVisitor)

Example 9 with ClassVisitor

use of org.jetbrains.org.objectweb.asm.ClassVisitor in project intellij-community by JetBrains.

the class CompilingEvaluator method changeSuperToMagicAccessor.

private static byte[] changeSuperToMagicAccessor(byte[] bytes) {
    ClassWriter classWriter = new ClassWriter(0);
    ClassVisitor classVisitor = new ClassVisitor(Opcodes.API_VERSION, 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();
}
Also used : ClassReader(org.jetbrains.org.objectweb.asm.ClassReader) ClassVisitor(org.jetbrains.org.objectweb.asm.ClassVisitor) ClassWriter(org.jetbrains.org.objectweb.asm.ClassWriter)

Example 10 with ClassVisitor

use of org.jetbrains.org.objectweb.asm.ClassVisitor in project intellij-community by JetBrains.

the class ClassFileViewProvider method detectInnerClass.

private static boolean detectInnerClass(VirtualFile file, @Nullable byte[] content) {
    String name = file.getNameWithoutExtension();
    int p = name.lastIndexOf('$', name.length() - 2);
    if (p <= 0)
        return false;
    Boolean isInner = IS_INNER_CLASS.get(file);
    if (isInner != null)
        return isInner;
    if (content == null) {
        try {
            content = file.contentsToByteArray(false);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
    ClassReader reader = new ClassReader(content);
    final Ref<Boolean> ref = Ref.create(Boolean.FALSE);
    final String className = reader.getClassName();
    reader.accept(new ClassVisitor(Opcodes.API_VERSION) {

        @Override
        public void visitOuterClass(String owner, String name, String desc) {
            ref.set(Boolean.TRUE);
        }

        @Override
        public void visitInnerClass(String name, String outer, String inner, int access) {
            if (className.equals(name)) {
                ref.set(Boolean.TRUE);
            }
        }
    }, EMPTY_ATTRIBUTES, ClassReader.SKIP_DEBUG | ClassReader.SKIP_CODE | ClassReader.SKIP_FRAMES);
    isInner = ref.get();
    IS_INNER_CLASS.set(file, isInner);
    return isInner;
}
Also used : ClassReader(org.jetbrains.org.objectweb.asm.ClassReader) IOException(java.io.IOException) ClassVisitor(org.jetbrains.org.objectweb.asm.ClassVisitor)

Aggregations

ClassVisitor (org.jetbrains.org.objectweb.asm.ClassVisitor)14 ClassReader (org.jetbrains.org.objectweb.asm.ClassReader)12 OutputFile (org.jetbrains.kotlin.backend.common.output.OutputFile)3 ReadKotlinClassHeaderAnnotationVisitor (org.jetbrains.kotlin.load.kotlin.header.ReadKotlinClassHeaderAnnotationVisitor)3 ClassWriter (org.jetbrains.org.objectweb.asm.ClassWriter)3 Ref (com.intellij.openapi.util.Ref)2 File (java.io.File)2 IOException (java.io.IOException)2 Nullable (org.jetbrains.annotations.Nullable)2 MethodVisitor (org.jetbrains.org.objectweb.asm.MethodVisitor)2 ArrayList (java.util.ArrayList)1 JarFile (java.util.jar.JarFile)1 NotNull (org.jetbrains.annotations.NotNull)1 OutputFileCollection (org.jetbrains.kotlin.backend.common.output.OutputFileCollection)1 KotlinClassHeader (org.jetbrains.kotlin.load.kotlin.header.KotlinClassHeader)1 ClassId (org.jetbrains.kotlin.name.ClassId)1 RecursiveDescriptorComparator.validateAndCompareDescriptorWithFile (org.jetbrains.kotlin.test.util.RecursiveDescriptorComparator.validateAndCompareDescriptorWithFile)1 FieldVisitor (org.jetbrains.org.objectweb.asm.FieldVisitor)1