Search in sources :

Example 1 with ClassWriter

use of org.jetbrains.org.objectweb.asm.ClassWriter 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 2 with ClassWriter

use of org.jetbrains.org.objectweb.asm.ClassWriter 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());
    }
}
Also used : Ref(com.intellij.openapi.util.Ref) ClassReader(org.jetbrains.org.objectweb.asm.ClassReader) ClassVisitor(org.jetbrains.org.objectweb.asm.ClassVisitor) JarFile(java.util.jar.JarFile) RecursiveDescriptorComparator.validateAndCompareDescriptorWithFile(org.jetbrains.kotlin.test.util.RecursiveDescriptorComparator.validateAndCompareDescriptorWithFile) File(java.io.File) ClassWriter(org.jetbrains.org.objectweb.asm.ClassWriter)

Example 3 with ClassWriter

use of org.jetbrains.org.objectweb.asm.ClassWriter 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 4 with ClassWriter

use of org.jetbrains.org.objectweb.asm.ClassWriter in project android by JetBrains.

the class ResourceClassGenerator method generate.

/**
   * @param fqcn Fully qualified class name (as accepted by ClassLoader, or as returned by Class.getName())
   */
@Nullable
public byte[] generate(String fqcn) {
    String className = fqcn.replace('.', '/');
    if (LOG.isDebugEnabled()) {
        LOG.debug(String.format("generate(%s)", anonymizeClassName(className)));
    }
    // Don't compute MAXS and FRAMES.
    ClassWriter cw = new ClassWriter(0);
    cw.visit(V1_6, ACC_PUBLIC + ACC_FINAL + ACC_SUPER, className, null, Type.getInternalName(Object.class), null);
    int index = className.lastIndexOf('$');
    if (index != -1) {
        String typeName = className.substring(index + 1);
        ResourceType type = ResourceType.getEnum(typeName);
        if (type == null) {
            if (LOG.isDebugEnabled()) {
                LOG.debug(String.format("  type '%s' doesn't exist", typeName));
            }
            return null;
        }
        cw.visitInnerClass(className, className.substring(0, index), typeName, ACC_PUBLIC + ACC_FINAL + ACC_STATIC);
        if (myCache == null) {
            myCache = Maps.newHashMap();
        }
        if (type == ResourceType.STYLEABLE) {
            if (myStyleableCache == null) {
                TObjectIntHashMap<String> styleableIntCache = new TObjectIntHashMap<String>();
                myCache.put(type, styleableIntCache);
                myStyleableCache = Maps.newHashMap();
                generateStyleable(cw, styleableIntCache, className);
            } else {
                TObjectIntHashMap<String> styleableIntCache = myCache.get(type);
                assert styleableIntCache != null;
                generateFields(cw, styleableIntCache);
                generateIntArrayFromCache(cw, className, myStyleableCache);
            }
        } else {
            TObjectIntHashMap<String> typeCache = myCache.get(type);
            if (typeCache == null) {
                typeCache = new TObjectIntHashMap<String>();
                myCache.put(type, typeCache);
                generateValuesForType(cw, type, typeCache);
            } else {
                generateFields(cw, typeCache);
            }
        }
    } else {
        // Default R class.
        boolean styleableAdded = false;
        for (ResourceType t : myAppResources.getAvailableResourceTypes()) {
            // getAvailableResourceTypes() sometimes returns both styleable and declare styleable. Make sure that we only create one subclass.
            if (t == ResourceType.DECLARE_STYLEABLE) {
                t = ResourceType.STYLEABLE;
            }
            if (t == ResourceType.STYLEABLE) {
                if (styleableAdded) {
                    continue;
                } else {
                    styleableAdded = true;
                }
            }
            cw.visitInnerClass(className + "$" + t.getName(), className, t.getName(), ACC_PUBLIC + ACC_FINAL + ACC_STATIC);
        }
    }
    generateConstructor(cw);
    cw.visitEnd();
    return cw.toByteArray();
}
Also used : TObjectIntHashMap(gnu.trove.TObjectIntHashMap) ResourceType(com.android.resources.ResourceType) ClassWriter(org.jetbrains.org.objectweb.asm.ClassWriter) Nullable(org.jetbrains.annotations.Nullable)

Example 5 with ClassWriter

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

the class BaseInstrumentingBuilder method performBuild.

@Override
protected final ExitCode performBuild(CompileContext context, ModuleChunk chunk, InstrumentationClassFinder finder, OutputConsumer outputConsumer) {
    ExitCode exitCode = ExitCode.NOTHING_DONE;
    for (CompiledClass compiledClass : outputConsumer.getCompiledClasses().values()) {
        if (Utils.IS_TEST_MODE || LOG.isDebugEnabled()) {
            LOG.info("checking " + compiledClass + " by " + getClass());
        }
        final BinaryContent originalContent = compiledClass.getContent();
        final ClassReader reader = new FailSafeClassReader(originalContent.getBuffer(), originalContent.getOffset(), originalContent.getLength());
        final int version = getClassFileVersion(reader);
        if (IS_INSTRUMENTED_KEY.get(compiledClass, Boolean.FALSE) || !canInstrument(compiledClass, version)) {
            // do not instrument the same content twice
            continue;
        }
        final ClassWriter writer = new InstrumenterClassWriter(reader, getAsmClassWriterFlags(version), finder);
        try {
            if (Utils.IS_TEST_MODE || LOG.isDebugEnabled()) {
                LOG.info("instrumenting " + compiledClass + " by " + getClass());
            }
            final BinaryContent instrumented = instrument(context, compiledClass, reader, writer, finder);
            if (instrumented != null) {
                compiledClass.setContent(instrumented);
                finder.cleanCachedData(compiledClass.getClassName());
                IS_INSTRUMENTED_KEY.set(compiledClass, Boolean.TRUE);
                exitCode = ExitCode.OK;
            }
        } catch (Throwable e) {
            LOG.info(e);
            final String message = e.getMessage();
            if (message != null) {
                context.processMessage(new CompilerMessage(getPresentableName(), BuildMessage.Kind.ERROR, message, ContainerUtil.getFirstItem(compiledClass.getSourceFilesPaths())));
            } else {
                context.processMessage(new CompilerMessage(getPresentableName(), e));
            }
        }
    }
    return exitCode;
}
Also used : CompilerMessage(org.jetbrains.jps.incremental.messages.CompilerMessage) FailSafeClassReader(com.intellij.compiler.instrumentation.FailSafeClassReader) ClassReader(org.jetbrains.org.objectweb.asm.ClassReader) InstrumenterClassWriter(com.intellij.compiler.instrumentation.InstrumenterClassWriter) FailSafeClassReader(com.intellij.compiler.instrumentation.FailSafeClassReader) ClassWriter(org.jetbrains.org.objectweb.asm.ClassWriter) InstrumenterClassWriter(com.intellij.compiler.instrumentation.InstrumenterClassWriter)

Aggregations

ClassWriter (org.jetbrains.org.objectweb.asm.ClassWriter)8 ClassReader (org.jetbrains.org.objectweb.asm.ClassReader)4 File (java.io.File)3 ClassVisitor (org.jetbrains.org.objectweb.asm.ClassVisitor)3 FailSafeClassReader (com.intellij.compiler.instrumentation.FailSafeClassReader)2 AsmCodeGenerator (com.intellij.uiDesigner.compiler.AsmCodeGenerator)2 ResourceType (com.android.resources.ResourceType)1 PsiClassWriter (com.intellij.compiler.PsiClassWriter)1 InstrumenterClassWriter (com.intellij.compiler.instrumentation.InstrumenterClassWriter)1 Ref (com.intellij.openapi.util.Ref)1 CodeGenerationException (com.intellij.uiDesigner.compiler.CodeGenerationException)1 FormErrorInfo (com.intellij.uiDesigner.compiler.FormErrorInfo)1 LwRootContainer (com.intellij.uiDesigner.lw.LwRootContainer)1 TObjectIntHashMap (gnu.trove.TObjectIntHashMap)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 JarFile (java.util.jar.JarFile)1 NonNls (org.jetbrains.annotations.NonNls)1 Nullable (org.jetbrains.annotations.Nullable)1 CompilerMessage (org.jetbrains.jps.incremental.messages.CompilerMessage)1 RecursiveDescriptorComparator.validateAndCompareDescriptorWithFile (org.jetbrains.kotlin.test.util.RecursiveDescriptorComparator.validateAndCompareDescriptorWithFile)1